Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 0ca30a4

Browse files
committed
Automatically update targets findings stats from all its child scans
1 parent a7c4fe2 commit 0ca30a4

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

operator/controllers/targets/host_controller.go

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"reflect"
2223
"time"
2324

2425
"github.com/go-logr/logr"
@@ -111,6 +112,50 @@ func (r *HostReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
111112
log.Info("Created ScheduledScan for Target", "ScheduledScan", scanName)
112113
}
113114

115+
// Update Targets Findings Status
116+
var childScans executionv1.ScheduledScanList
117+
if err := r.List(ctx, &childScans, client.InNamespace(req.Namespace), client.MatchingFields{ownerKey: req.Name}); err != nil {
118+
log.Error(err, "unable to list child ScheduledScans")
119+
return ctrl.Result{}, err
120+
}
121+
122+
totalStats := executionv1.FindingStats{
123+
Count: 0,
124+
FindingSeverities: executionv1.FindingSeverities{
125+
Informational: 0,
126+
Low: 0,
127+
Medium: 0,
128+
High: 0,
129+
},
130+
FindingCategories: map[string]uint64{},
131+
}
132+
for _, scan := range childScans.Items {
133+
stats := scan.Status.Findings
134+
135+
totalStats.Count += stats.Count
136+
totalStats.FindingSeverities.Informational += stats.FindingSeverities.Informational
137+
totalStats.FindingSeverities.Low += stats.FindingSeverities.Low
138+
totalStats.FindingSeverities.Medium += stats.FindingSeverities.Medium
139+
totalStats.FindingSeverities.High += stats.FindingSeverities.High
140+
141+
for key, value := range stats.FindingCategories {
142+
if _, ok := totalStats.FindingCategories[key]; ok {
143+
totalStats.FindingCategories[key] += value
144+
} else {
145+
totalStats.FindingCategories[key] = value
146+
}
147+
}
148+
}
149+
150+
if !reflect.DeepEqual(host.Status.Findings, totalStats) {
151+
log.V(0).Info("Updating ScheduledScans Findings as they appear to have changed")
152+
host.Status.Findings = *totalStats.DeepCopy()
153+
if err := r.Status().Update(ctx, &host); err != nil {
154+
log.Error(err, "unable to update Host status")
155+
return ctrl.Result{}, err
156+
}
157+
}
158+
114159
return ctrl.Result{}, nil
115160
}
116161

@@ -129,16 +174,16 @@ func CreateScanTemplatesForHost(host targetsv1.Host) []ScanTemplates {
129174
},
130175
})
131176
}
132-
// if port.Type == "http" || port.Type == "https" {
133-
// scanTemplates = append(scanTemplates, ScanTemplates{
134-
// Port: port.Port,
135-
// Type: port.Type,
136-
// ScanSpec: executionv1.ScanSpec{
137-
// ScanType: "zap-baseline",
138-
// Parameters: []string{"-t", fmt.Sprintf("%s://%s:%d", port.Type, host.Spec.Hostname, port.Port)},
139-
// },
140-
// })
141-
// }
177+
if port.Type == "http" || port.Type == "https" {
178+
scanTemplates = append(scanTemplates, ScanTemplates{
179+
Port: port.Port,
180+
Type: port.Type,
181+
ScanSpec: executionv1.ScanSpec{
182+
ScanType: "zap-baseline",
183+
Parameters: []string{"-t", fmt.Sprintf("%s://%s:%d", port.Type, host.Spec.Hostname, port.Port)},
184+
},
185+
})
186+
}
142187
}
143188

144189
return scanTemplates

0 commit comments

Comments
 (0)