Skip to content

Commit

Permalink
internal/config: merge the includes while the graph is discovered
Browse files Browse the repository at this point in the history
  • Loading branch information
seilagamo committed Sep 19, 2024
1 parent 3a1d9cf commit ceb518e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
28 changes: 22 additions & 6 deletions internal/config/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (

// include represents every include reference in a config file.
type include struct {
url string
resolved bool
resolver *resolver
cfg *Config
parent *include
includes []include
url string
resolved bool
resolver *resolver
cfg *Config
resolvedCfg *Config
parent *include
includes []include
}

// Resolve parses the configuration of an include recursively.
Expand All @@ -39,6 +40,21 @@ func (in *include) resolve() error {
in.resolved = false
return fmt.Errorf("set includes: %w", err)
}

if in.parent != nil {
if in.resolvedCfg == nil {
// This is the most common ancestor included.
in.resolvedCfg = in.cfg
}
resolvedConfig, err := merge(*in.resolvedCfg, *in.parent.cfg)
if err != nil {
in.resolved = false
return fmt.Errorf("merge include: %w", err)
}
in.parent.resolvedCfg = &resolvedConfig
} else if in.resolvedCfg == nil {
in.resolvedCfg = in.cfg
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func (r *resolver) resolve() (cfg Config, err error) {
if !r.root.resolved {
return Config{}, ErrRootIncludeNotResolved
}
return *r.root.cfg, nil
return *r.root.resolvedCfg, nil
}
16 changes: 8 additions & 8 deletions internal/config/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ func TestResolver_Resolve(t *testing.T) {
want: Config{
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
},
Targets: []Target{
{
Identifier: "example.com",
AssetType: types.DomainName,
},
"commonchecktypes.json",
},
},
wantErr: false,
Expand All @@ -40,6 +34,7 @@ func TestResolver_Resolve(t *testing.T) {
Includes: []string{"testdata/includes/without_includes.yaml"},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes.json",
},
Targets: []Target{
Expand All @@ -66,6 +61,7 @@ func TestResolver_Resolve(t *testing.T) {
},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes.json",
},
Targets: []Target{
Expand All @@ -82,19 +78,23 @@ func TestResolver_Resolve(t *testing.T) {
URL: "testdata/includes/include_two_with_common_include.yaml",
want: Config{
Includes: []string{
"testdata/includes/without_includes.yaml",
"testdata/includes/include_a.yaml",
"testdata/includes/include_b.yaml",
},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
"commonchecktypes.json",
},
Targets: []Target{
{
Identifier: "example.com",
AssetType: types.DomainName,
},
},
ReportConfig: ReportConfig{
Severity: ptr(SeverityCritical),
},
},
wantErr: false,
},
Expand Down
7 changes: 2 additions & 5 deletions internal/config/testdata/includes/include_a.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
lava: v1.0.0
include:
- testdata/includes/without_includes.yaml
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
report:
severity: medium
7 changes: 2 additions & 5 deletions internal/config/testdata/includes/include_b.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
lava: v1.0.0
include:
- testdata/includes/without_includes.yaml
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
report:
severity: high
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ lava: v1.0.0
include:
- testdata/includes/include_a.yaml
- testdata/includes/include_b.yaml
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
report:
severity: critical
5 changes: 1 addition & 4 deletions internal/config/testdata/includes/without_includes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
lava: v1.0.0
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
- commonchecktypes.json

0 comments on commit ceb518e

Please sign in to comment.