Skip to content

Commit cb66108

Browse files
authored
fix(config): change selector type (fanal#189)
* fix(config): change selector type * test(policy): fix test data
1 parent ac56d1c commit cb66108

25 files changed

+123
-108
lines changed

cmd/fanal/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func run() (err error) {
6060
Aliases: []string{"fs"},
6161
Usage: "inspect a local directory",
6262
Flags: []cli.Flag{
63+
&cli.StringSliceFlag{
64+
Name: "namespace",
65+
Usage: "namespaces",
66+
Value: cli.NewStringSlice("appshield"),
67+
},
6368
&cli.StringSliceFlag{
6469
Name: "policy",
6570
Usage: "policy paths",
@@ -141,6 +146,7 @@ func archiveAction(c *cli.Context, fsCache cache.Cache) error {
141146

142147
func fsAction(c *cli.Context, fsCache cache.Cache) error {
143148
art, err := local.NewArtifact(c.Args().First(), fsCache, nil, config.ScannerOption{
149+
Namespaces: []string{"appshield"},
144150
PolicyPaths: c.StringSlice("policy"),
145151
})
146152
if err != nil {
@@ -189,6 +195,9 @@ func inspect(ctx context.Context, art artifact.Artifact, c cache.LocalArtifactCa
189195
}
190196
for _, misconf := range mergedLayer.Misconfigurations {
191197
fmt.Printf(" %s: failures %d, warnings %d\n", misconf.FilePath, len(misconf.Failures), len(misconf.Warnings))
198+
for _, failure := range misconf.Failures {
199+
fmt.Printf(" %s: %s\n", failure.ID, failure.Message)
200+
}
192201
}
193202
return nil
194203
}

policy/engine.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ func (e *Engine) Check(ctx context.Context, configs []types.Config, namespaces [
196196
}
197197

198198
var selectedConfigs []types.Config
199-
if len(inputOption.Selector.Types) > 0 {
199+
if len(inputOption.Selectors) > 0 {
200200
// Pass only the config files that match the selector types
201-
for _, t := range inputOption.Selector.Types {
201+
for _, t := range uniqueSelectorTypes(inputOption.Selectors) {
202202
selectedConfigs = append(selectedConfigs, typedConfigs[t]...)
203203
}
204204
} else {
205-
// When the 'types' is not specified, it means '*'.
205+
// When the 'selector' is not specified, it means '*'.
206206
selectedConfigs = configs
207207
}
208208

@@ -666,6 +666,14 @@ func removeRulePrefix(rule string) string {
666666
return rule
667667
}
668668

669+
func uniqueSelectorTypes(selectors []types.PolicyInputSelector) []string {
670+
selectorTypes := map[string]struct{}{}
671+
for _, s := range selectors {
672+
selectorTypes[s.Type] = struct{}{}
673+
}
674+
return utils.Keys(selectorTypes)
675+
}
676+
669677
func uniqueResults(results []types.MisconfResult) []types.MisconfResult {
670678
uniq := map[string]types.MisconfResult{}
671679
for _, result := range results {

policy/testdata/combine/combined_deployment.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["kubernetes"]},
1514
"combine": true,
15+
"selector": [{"type": "kubernetes"}],
1616
}
1717

1818
deny[res] {

policy/testdata/combine/docker.rego renamed to policy/testdata/combine/combined_docker.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["dockerfile"]},
1514
"combine": true,
15+
"selector": [{"type": "dockerfile"}],
1616
}
1717

1818
deny[res] {

policy/testdata/combine/combined_pod.rego

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package testdata.xyz_400
22

33
__rego_metadata__ := {
4-
"id": "XYZ-400",
5-
"title": "Bad Combined Pod",
6-
"version": "v1.0.0",
7-
"severity": "LOW",
8-
"type": "Kubernetes Security Check",
4+
"id": "XYZ-400",
5+
"title": "Bad Combined Pod",
6+
"version": "v1.0.0",
7+
"severity": "LOW",
8+
"type": "Kubernetes Security Check",
99
}
1010

1111
__rego_input__ := {
12-
"selector": {
13-
"types": ["kubernetes"]
14-
},
15-
"combine": true,
12+
"combine": true,
13+
"selector": [{"type": "kubernetes"}],
1614
}
1715

1816
deny[res] {
19-
input[i].contents.kind == "Pod"
17+
input[i].contents.kind == "Pod"
2018
res := {
2119
"filepath": input[i].path,
2220
"msg": sprintf("deny combined %s", [input[i].contents.metadata.name]),

policy/testdata/combine/deployment.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["kubernetes"]},
1514
"combine": false,
15+
"selector": [{"type": "kubernetes"}],
1616
}
1717

1818
warn[msg] {

policy/testdata/combine_exception/combined_deployment.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["kubernetes"]},
1514
"combine": true,
15+
"selector": [{"type": "kubernetes"}],
1616
}
1717

1818
warn[res] {

policy/testdata/combine_exception/deployment.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["kubernetes"]},
1514
"combine": false,
15+
"selector": [{"type": "kubernetes"}],
1616
}
1717

1818
warn[msg] {

policy/testdata/combine_exception/exceptions.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package namespace.exceptions
33
import data.namespaces
44

55
exception[ns] {
6-
ns := data.namespaces[_]
7-
ns == "testdata.xyz_300"
6+
ns := data.namespaces[_]
7+
ns == "testdata.xyz_300"
88
}

policy/testdata/combine_exception/fail.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ __rego_metadata__ := {
1111
}
1212

1313
__rego_input__ := {
14-
"selector": {"types": ["kubernetes"]},
1514
"combine": true,
15+
"selector": [{"type": "kubernetes"}],
1616
}
1717

1818
deny[res] {

0 commit comments

Comments
 (0)