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

Commit 0c89b89

Browse files
jorgestiganigthknightJ12934
committed
Update findings stats after ReadWrite hooks
Co-authored-by: Yannik Fuhrmeister <12710254+fuhrmeistery@users.noreply.github.com> Co-authored-by: Jannik Hollenbach <jannik@hollenbach.de>
1 parent 9142e20 commit 0c89b89

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

hook-sdk/nodejs/hook-wrapper.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ const axios = require("axios");
22
const { handle } = require("./hook/hook");
33
const k8s = require("@kubernetes/client-node");
44

5+
const scanName = process.env["SCAN_NAME"];
6+
const namespace = process.env["NAMESPACE"];
7+
console.log(`Starting hook for Scan "${scanName}"`);
8+
9+
const kc = new k8s.KubeConfig();
10+
kc.loadFromCluster();
11+
12+
const k8sApi = kc.makeApiClient(k8s.CustomObjectsApi);
13+
514
function downloadFile(url) {
615
return axios.get(url);
716
}
@@ -62,7 +71,14 @@ function updateRawResults(fileContents) {
6271
return uploadFile(rawResultUploadUrl, fileContents);
6372
}
6473

65-
function updateFindings(findings) {
74+
function severityCount(findings, severity) {
75+
return findings.filter(
76+
({ severity: findingSeverity }) =>
77+
findingSeverity.toUpperCase() === severity
78+
).length;
79+
}
80+
81+
async function updateFindings(findings) {
6682
const findingsUploadUrl = process.argv[5];
6783
if (findingsUploadUrl === undefined) {
6884
console.error(
@@ -73,19 +89,55 @@ function updateFindings(findings) {
7389
"If you want to change Findings you'll need to use a ReadAndWrite Hook."
7490
);
7591
}
76-
return uploadFile(findingsUploadUrl, JSON.stringify(findings));
77-
}
92+
await uploadFile(findingsUploadUrl, JSON.stringify(findings));
7893

79-
async function main() {
80-
const scanName = process.env["SCAN_NAME"];
81-
const namespace = process.env["NAMESPACE"];
82-
console.log(`Starting hook for Scan "${scanName}"`);
94+
// Update the scans findingStats (severities, categories, or the count) of the scan results
95+
const findingCategories = new Map();
96+
for (const { category } of findings) {
97+
if (findingCategories.has(category)) {
98+
findingCategories.set(category, findingCategories.get(category) + 1);
99+
} else {
100+
findingCategories.set(category, 1);
101+
}
102+
}
83103

84-
const kc = new k8s.KubeConfig();
85-
kc.loadFromCluster();
104+
const findingStats = {
105+
count: findings.length,
106+
severities: {
107+
informational: severityCount(findings, "INFORMATIONAL"),
108+
low: severityCount(findings, "LOW"),
109+
medium: severityCount(findings, "MEDIUM"),
110+
high: severityCount(findings, "HIGH"),
111+
},
112+
categories: Object.fromEntries(findingCategories.entries()),
113+
};
86114

87-
const k8sApi = kc.makeApiClient(k8s.CustomObjectsApi);
115+
await k8sApi.patchNamespacedCustomObjectStatus(
116+
"execution.experimental.securecodebox.io",
117+
"v1",
118+
namespace,
119+
"scans",
120+
scanName,
121+
{
122+
status: {
123+
findings: {
124+
count: findings.length,
125+
severities: {
126+
informational: severityCount(findings, "INFORMATIONAL"),
127+
low: severityCount(findings, "LOW"),
128+
medium: severityCount(findings, "MEDIUM"),
129+
high: severityCount(findings, "HIGH"),
130+
},
131+
categories: Object.fromEntries(findingCategories.entries()),
132+
},
133+
},
134+
},
135+
{ headers: { "content-type": "application/merge-patch+json" } }
136+
);
137+
console.log("Updated status successfully");
138+
}
88139

140+
async function main() {
89141
let scan;
90142
try {
91143
const { body } = await k8sApi.getNamespacedCustomObject(

operator/controllers/execution/scan_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,11 @@ func (r *ScanReconciler) createJobForHook(hook *executionv1.ScanCompletionHook,
979979
Resources: []string{"scans"},
980980
Verbs: []string{"get"},
981981
},
982+
{
983+
APIGroups: []string{"execution.experimental.securecodebox.io"},
984+
Resources: []string{"scans/status"},
985+
Verbs: []string{"get", "patch"},
986+
},
982987
}
983988
serviceAccountName := "scan-completion-hook"
984989
r.ensureServiceAccountExists(

0 commit comments

Comments
 (0)