Skip to content

Commit a59caad

Browse files
Merge pull request #733 from controlplaneio/scan-specific-rules
feat: Add support for scanning specific rules
2 parents cfa37b0 + 5f37ab2 commit a59caad

7 files changed

Lines changed: 489 additions & 314 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ kubesec scan ./deployment.yaml --format table
155155
kubesec scan ./deployment.yaml --format template --template report-template.tmpl
156156
```
157157

158+
#### Scan specific rules
159+
160+
```bash
161+
# One rule
162+
kubesec scan --rules CapSysAdmin kubesec-test.yaml
163+
164+
# Multiple rules
165+
kubesec scan --rules RunAsNonRoot,SeccompAny,ApparmorAny kubesec-test.yaml
166+
```
167+
158168
##### Example JSON Output
159169

160170
```json
@@ -294,6 +304,9 @@ curl -sSX POST --data-binary @"deployment.yaml" https://v2.kubesec.io/scan
294304

295305
# Parse the API output using jq to return a non-zero exit code if the score is <= 10
296306
curl -sSX POST --data-binary @"deployment.yaml" https://v2.kubesec.io/scan | jq --exit-status '.score > 10'
307+
308+
# Use the "rule" query parameter to scan only specific rules (multiple supported)
309+
curl -sSX POST --data-binary @test/asset/score-0-cap-sys-admin.yml "http://localhost:8080/scan?rule=SeccompAny&rule=ApparmorAny"
297310
```
298311

299312
You may also define a Bash function, e.g.:

cmd/print-rules.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ func init() {
2727
rootCmd.SilenceErrors = true
2828
rootCmd.SilenceUsage = true
2929

30-
ruleSet := ruler.NewRuleset(logger)
30+
ruleSet, err := ruler.NewRuleset(logger)
31+
if err != nil {
32+
return err
33+
}
3134

3235
// Sort by rule ID
3336
sort.Slice(ruleSet.Rules, func(i, j int) bool {

cmd/scan.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
schemaLocations = []string{}
3232
outputLocation string
3333
exitCode int
34+
rulesIDs []string
3435
)
3536

3637
func init() {
@@ -40,6 +41,7 @@ func init() {
4041
scanCmd.Flags().StringVar(&k8sVersion, "kubernetes-version", "", "Kubernetes version to validate manifets")
4142
scanCmd.Flags().StringSliceVar(&schemaLocations, "schema-location", []string{}, "Override schema location search path, local or http (can be specified multiple times)")
4243
scanCmd.Flags().StringVarP(&template, "template", "t", "", "Set output template, it will check for a file or read input as the template")
44+
scanCmd.Flags().StringSliceVarP(&rulesIDs, "rules", "r", []string{}, "Comma-separated list of rule IDs to scan (empty scans all rules). Run 'kubesec print-rules' to see all rules")
4345
scanCmd.Flags().StringVarP(&outputLocation, "output", "o", "", "Set output location")
4446
scanCmd.Flags().IntVar(&exitCode, "exit-code", 2, "Set the exit-code to use on failure")
4547
rootCmd.AddCommand(scanCmd)
@@ -126,7 +128,11 @@ var scanCmd = &cobra.Command{
126128
schemaConfig.Locations = schemaLocations
127129
schemaConfig.ValidatorOpts.KubernetesVersion = k8sVersion
128130

129-
reports, err := ruler.NewRuleset(logger).Run(file.fileName, file.fileBytes, schemaConfig)
131+
ruleset, err := ruler.NewRuleset(logger, rulesIDs...)
132+
if err != nil {
133+
return err
134+
}
135+
reports, err := ruleset.Run(file.fileName, file.fileBytes, schemaConfig)
130136
if err != nil {
131137
return err
132138
}

0 commit comments

Comments
 (0)