Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions hooks/cascading-scans/hook/hook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ test("should properly parse template values in scanLabels and scanAnnotations",
}

const findings = [
{
name: "Port 8443 is open",
category: "Open Port",
attributes: {
state: "open",
hostname: "foobar.com",
port: 8443,
service: "https"
}
},
{
name: "Port 443 is open",
category: "Open Port",
Expand All @@ -678,6 +688,8 @@ test("should properly parse template values in scanLabels and scanAnnotations",
sslyzeCascadingRules[0]
);

expect(sslyzeCascadingRules[0].spec.scanSpec.parameters).toEqual(["--regular", "{{$.hostOrIP}}:{{attributes.port}}"])

const { labels, annotations } = cascadedScans[0].metadata;

// No snapshots as scanLabels/scanAnnotations can be in any order
Expand Down Expand Up @@ -1181,3 +1193,71 @@ test("should not copy cascaded scan spec from parent scan if inheritance is unde
expect(secondCascadedScan.spec.volumeMounts).toMatchInlineSnapshot(`Array []`)

});


test("should append cascading rule to further cascading scan chains", () => {
const findings = [
{
name: "Port 443 is open",
category: "Open Port",
attributes: {
state: "open",
hostname: "foobar.com",
port: 443,
service: "https"
}
}
];

const cascadedScans = getCascadingScans(
parentScan,
findings,
sslyzeCascadingRules
);

const cascadedScan = cascadedScans[0]
// Create a second cascading rule
sslyzeCascadingRules[1] = {
apiVersion: "cascading.securecodebox.io/v1",
kind: "CascadingRule",
metadata: {
name: "tls-scans-second"
},
spec: {
matches: {
anyOf: [
{
category: "Open Port",
attributes: {
port: 443,
service: "https"
}
},
{
category: "Open Port",
attributes: {
service: "https"
}
}
]
},
scanSpec: {
scanType: "sslyze",
parameters: ["--regular", "{{$.hostOrIP}}:{{attributes.port}}"]
}
}
}

cascadedScan.metadata.name = cascadedScan.metadata.generateName

const secondCascadedScans = getCascadingScans(
cascadedScan,
findings,
sslyzeCascadingRules,
sslyzeCascadingRules[0] // cascaded rule on parent
);

const secondCascadedScan = secondCascadedScans[0];

expect(secondCascadedScan.metadata.annotations["cascading.securecodebox.io/chain"]).toEqual("tls-scans,tls-scans-second")
});
14 changes: 5 additions & 9 deletions hooks/cascading-scans/hook/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

import { isMatch, isMatchWith, isString, mapValues } from "lodash";
import { isMatch, isMatchWith, isString, mapValues, cloneDeep } from "lodash";
import { isMatch as wildcardIsMatch } from "matcher";
import * as Mustache from "mustache";

Expand Down Expand Up @@ -105,18 +105,14 @@ function getCascadingScan(
finding: Finding,
cascadingRule: CascadingRule
) {
cascadingRule = templateCascadingRule(parentScan, finding, cascadingRule);
// Make a deep copy of the original cascading rule so that we can template it again with different findings.
cascadingRule = templateCascadingRule(parentScan, finding, cloneDeep(cascadingRule));

let { scanType, parameters } = cascadingRule.spec.scanSpec;

let { annotations, labels, env, volumes, volumeMounts } = mergeCascadingRuleWithScan(parentScan, cascadingRule);

let cascadingChain: Array<string> = [];
if (parentScan.metadata.annotations && parentScan.metadata.annotations["cascading.securecodebox.io/chain"]) {
cascadingChain = parentScan.metadata.annotations[
"cascading.securecodebox.io/chain"
].split(",");
}
let cascadingChain = getScanChain(parentScan);

return {
apiVersion: "execution.securecodebox.io/v1",
Expand All @@ -125,14 +121,14 @@ function getCascadingScan(
generateName: `${generateCascadingScanName(parentScan, cascadingRule)}-`,
labels,
annotations: {
...annotations,
"securecodebox.io/hook": "cascading-scans",
"cascading.securecodebox.io/parent-scan": parentScan.metadata.name,
"cascading.securecodebox.io/matched-finding": finding.id,
"cascading.securecodebox.io/chain": [
...cascadingChain,
cascadingRule.metadata.name
].join(","),
...annotations,
},
ownerReferences: [
{
Expand Down