Skip to content

Commit 05cb861

Browse files
authored
Merge pull request #72 from prachidamle/fix_custom
CIS v2 Alerting and Custom Benchmark fixes
2 parents 20309ac + 4f14958 commit 05cb861

File tree

13 files changed

+126
-45
lines changed

13 files changed

+126
-45
lines changed

crds/clusterscan.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828
- JSONPath: .status.lastRunTimestamp
2929
name: LastRunTimestamp
3030
type: string
31-
- JSONPath: .spec.cronSchedule
31+
- JSONPath: .spec.scheduledScanConfig.cronSchedule
3232
name: CronSchedule
3333
type: string
3434
group: cis.cattle.io

crds/clusterscanbenchmark.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ spec:
1313
- JSONPath: .spec.maxKubernetesVersion
1414
name: MaxKubernetesVersion
1515
type: string
16+
- JSONPath: .spec.customBenchmarkConfigMapName
17+
name: customBenchmarkConfigMapName
18+
type: string
19+
- JSONPath: .spec.customBenchmarkConfigMapNamespace
20+
name: customBenchmarkConfigMapNamespace
21+
type: string
1622
group: cis.cattle.io
1723
names:
1824
kind: ClusterScanBenchmark
@@ -31,7 +37,7 @@ spec:
3137
customBenchmarkConfigMapName:
3238
nullable: true
3339
type: string
34-
customBenchmarkConfigMapNameSpace:
40+
customBenchmarkConfigMapNamespace:
3541
nullable: true
3642
type: string
3743
maxKubernetesVersion:

examples/clusterscanrke.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
apiVersion: cis.cattle.io/v1
33
kind: ClusterScan
44
metadata:
5-
name: rke-cis
5+
name: can-you-alert
66
spec:
77
scanProfileName: rke-profile-hardened
8+
scheduledScanConfig:
9+
cronSchedule: "*/2 * * * *"
10+
scanAlertRule:
11+
alertOnComplete: true
12+
alertOnFailure: true

examples/custombenchmarkscan.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
apiVersion: cis.cattle.io/v1
3+
kind: ClusterScan
4+
metadata:
5+
name: rke-cis-1.6-perm-custom
6+
spec:
7+
scanProfileName: "rke-cis-1.6-permissive"

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
securityScanImageTag = "v0.2.1"
3939
sonobuoyImage = "rancher/sonobuoy-sonobuoy"
4040
sonobuoyImageTag = "v0.16.3"
41+
clusterName string
4142
)
4243

4344
func main() {
@@ -104,6 +105,12 @@ func main() {
104105
Value: "warning",
105106
Destination: &alertSeverity,
106107
},
108+
cli.StringFlag{
109+
Name: "clusterName",
110+
EnvVar: "CLUSTER_NAME",
111+
Value: "",
112+
Destination: &clusterName,
113+
},
107114
}
108115
app.Action = run
109116

@@ -137,6 +144,7 @@ func run(c *cli.Context) {
137144
SonobuoyImage: sonobuoyImage,
138145
SonobuoyImageTag: sonobuoyImageTag,
139146
AlertSeverity: alertSeverity,
147+
ClusterName: clusterName,
140148
}
141149

142150
if err := validateConfig(imgConfig); err != nil {

pkg/apis/cis.cattle.io/v1/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type ClusterScanBenchmarkSpec struct {
117117
MaxKubernetesVersion string `json:"maxKubernetesVersion,omitempty"`
118118

119119
CustomBenchmarkConfigMapName string `json:"customBenchmarkConfigMapName,omitempty"`
120-
CustomBenchmarkConfigMapNameSpace string `json:"customBenchmarkConfigMapNameSpace,omitempty"`
120+
CustomBenchmarkConfigMapNamespace string `json:"customBenchmarkConfigMapNamespace,omitempty"`
121121
}
122122

123123
// +genclient
@@ -159,4 +159,5 @@ type ScanImageConfig struct {
159159
SonobuoyImage string
160160
SonobuoyImageTag string
161161
AlertSeverity string
162+
ClusterName string
162163
}

pkg/crds/crd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func List() []crd.CRD {
4949
WithColumn("Warn", ".status.summary.warn").
5050
WithColumn("Not Applicable", ".status.summary.notApplicable").
5151
WithColumn("LastRunTimestamp", ".status.lastRunTimestamp").
52-
WithColumn("CronSchedule", ".spec.cronSchedule")
52+
WithColumn("CronSchedule", ".spec.scheduledScanConfig.cronSchedule")
5353
}),
5454
newCRD(&cisoperator.ClusterScanProfile{}, func(c crd.CRD) crd.CRD {
5555
return c.
@@ -64,7 +64,9 @@ func List() []crd.CRD {
6464
return c.
6565
WithColumn("ClusterProvider", ".spec.clusterProvider").
6666
WithColumn("MinKubernetesVersion", ".spec.minKubernetesVersion").
67-
WithColumn("MaxKubernetesVersion", ".spec.maxKubernetesVersion")
67+
WithColumn("MaxKubernetesVersion", ".spec.maxKubernetesVersion").
68+
WithColumn("customBenchmarkConfigMapName", ".spec.customBenchmarkConfigMapName").
69+
WithColumn("customBenchmarkConfigMapNamespace", ".spec.customBenchmarkConfigMapNamespace")
6870
}),
6971
}
7072
}

pkg/securityscan/alert/prometheusrule.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func NewPrometheusRule(clusterscan *cisoperatorapiv1.ClusterScan, clusterscanpro
2626
"scanProfileName": clusterscanprofile.Name,
2727
"alertOnFailure": clusterscan.Spec.ScheduledScanConfig.ScanAlertRule.AlertOnFailure,
2828
"alertOnComplete": clusterscan.Spec.ScheduledScanConfig.ScanAlertRule.AlertOnComplete,
29+
"failOnWarn": clusterscan.Spec.ScoreWarning == cisoperatorapiv1.ClusterScanFailOnWarning,
2930
}
3031
scanAlertRule, err := generatePrometheusRule(clusterscan, templateName, templatePath, configdata)
3132
if err != nil {

pkg/securityscan/alert/templates/prometheusrule.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ spec:
1212
{{- if .alertOnFailure }}
1313
- alert: CISScanHasFailures
1414
annotations:
15-
description: CIS ClusterScan "{{ .scanName }}" has {{ "{{ $value }}" }} test failures
15+
description: CIS ClusterScan "{{ .scanName }}" has {{ "{{ $value }}" }} test failures or warnings
1616
summary: CIS ClusterScan has tests failures
17+
{{- if .failOnWarn }}
18+
expr: cis_scan_num_tests_fail{scan_name="{{ .scanName }}"} > 0 or ON(scan_name) cis_scan_num_tests_warn{scan_name="{{ .scanName }}"} > 0
19+
{{- else }}
1720
expr: cis_scan_num_tests_fail{scan_name="{{ .scanName }}"} > 0
21+
{{- end }}
1822
for: 1m
1923
labels:
2024
severity: {{ .severity }}

pkg/securityscan/controller.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Controller struct {
5050
numTestsTotal *prometheus.GaugeVec
5151
numTestsNA *prometheus.GaugeVec
5252
numTestsPassed *prometheus.GaugeVec
53+
numTestsWarn *prometheus.GaugeVec
5354
}
5455

5556
func NewController(ctx context.Context, cfg *rest.Config, namespace, name string, imgConfig *cisoperatorapiv1.ScanImageConfig) (ctl *Controller, err error) {
@@ -186,6 +187,7 @@ func initializeMetrics(ctl *Controller) error {
186187
"scan_name",
187188
// name of the clusterScanProfile used for scanning
188189
"scan_profile_name",
190+
"cluster_name",
189191
},
190192
)
191193
if err := prometheus.Register(ctl.numTestsFailed); err != nil {
@@ -202,6 +204,7 @@ func initializeMetrics(ctl *Controller) error {
202204
"scan_name",
203205
// name of the clusterScanProfile used for scanning
204206
"scan_profile_name",
207+
"cluster_name",
205208
},
206209
)
207210
if err := prometheus.Register(ctl.numScansComplete); err != nil {
@@ -218,6 +221,7 @@ func initializeMetrics(ctl *Controller) error {
218221
"scan_name",
219222
// name of the clusterScanProfile used for scanning
220223
"scan_profile_name",
224+
"cluster_name",
221225
},
222226
)
223227
if err := prometheus.Register(ctl.numTestsTotal); err != nil {
@@ -234,6 +238,7 @@ func initializeMetrics(ctl *Controller) error {
234238
"scan_name",
235239
// name of the clusterScanProfile used for scanning
236240
"scan_profile_name",
241+
"cluster_name",
237242
},
238243
)
239244
if err := prometheus.Register(ctl.numTestsPassed); err != nil {
@@ -250,6 +255,7 @@ func initializeMetrics(ctl *Controller) error {
250255
"scan_name",
251256
// name of the clusterScanProfile used for scanning
252257
"scan_profile_name",
258+
"cluster_name",
253259
},
254260
)
255261
if err := prometheus.Register(ctl.numTestsSkipped); err != nil {
@@ -266,11 +272,29 @@ func initializeMetrics(ctl *Controller) error {
266272
"scan_name",
267273
// name of the clusterScanProfile used for scanning
268274
"scan_profile_name",
275+
"cluster_name",
269276
},
270277
)
271278
if err := prometheus.Register(ctl.numTestsNA); err != nil {
272279
return err
273280
}
274281

282+
ctl.numTestsWarn = prometheus.NewGaugeVec(
283+
prometheus.GaugeOpts{
284+
Name: "cis_scan_num_tests_warn",
285+
Help: "Number of tests having warn status in the CIS scans, partioned by scan_name, scan_profile_name",
286+
},
287+
[]string{
288+
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
289+
"scan_name",
290+
// name of the clusterScanProfile used for scanning
291+
"scan_profile_name",
292+
"cluster_name",
293+
},
294+
)
295+
if err := prometheus.Register(ctl.numTestsWarn); err != nil {
296+
return err
297+
}
298+
275299
return nil
276300
}

0 commit comments

Comments
 (0)