Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[dssh] Add templating to CascadingRule.spec.scanLabels and CascadingR…
…ule.spec.scanLabels.
  • Loading branch information
EndPositive committed May 18, 2021
commit b84dcdd70f76f5c91e57144528110bf95c7db727
47 changes: 47 additions & 0 deletions hooks/declarative-subsequent-scans/hook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,50 @@ test("should copy scanAnnotations from CascadingRule to cascading scan", () => {
cascadingScanDefinition.metadata.annotations[label] === value
)).toBe(true)
});

test("should properly parse template values in scanLabels and scanAnnotations", () => {
sslyzeCascadingRules[0].spec.scanAnnotations = {
k_one: "{{metadata.name}}",
k_two: "{{metadata.unknown_property}}",
k_three: "{{$.hostOrIP}}"
}

sslyzeCascadingRules[0].spec.scanLabels = {
k_one: "{{metadata.name}}",
k_two: "{{metadata.unknown_property}}",
k_three: "{{$.hostOrIP}}"
}

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 { scanLabels, scanAnnotations } = cascadedScans[0]

// No snapshots as scanLabels/scanAnnotations can be in any order
const result = {
"k_one": "nmap-foobar.com",
"k_two": "",
"k_three": "foobar.com",
}

expect(scanLabels).toEqual(result)

expect(scanAnnotations).toEqual(result)
})

9 changes: 6 additions & 3 deletions hooks/declarative-subsequent-scans/hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMatch, isMatchWith, isString } from "lodash";
import { isMatch, isMatchWith, isString, mapValues } from "lodash";
import { isMatch as wildcardIsMatch } from "matcher";
import * as Mustache from "mustache";

Expand Down Expand Up @@ -92,6 +92,7 @@ export function getCascadingScans(

const templateArgs = {
...finding,
...parentScan,
// Attribute "$" hold special non finding helper attributes
$: {
hostOrIP:
Expand All @@ -108,8 +109,10 @@ export function getCascadingScans(
cascades: null,
generatedBy: cascadingRule.metadata.name,
env,
scanLabels: cascadingRule.spec.scanLabels === undefined ? {} : cascadingRule.spec.scanLabels,
scanAnnotations: cascadingRule.spec.scanAnnotations === undefined ? {} : cascadingRule.spec.scanAnnotations,
scanLabels: cascadingRule.spec.scanLabels === undefined ? {} :
mapValues(cascadingRule.spec.scanLabels, value => Mustache.render(value, templateArgs)),
scanAnnotations: cascadingRule.spec.scanAnnotations === undefined ? {} :
mapValues(cascadingRule.spec.scanAnnotations, value => Mustache.render(value, templateArgs)),
});
}
}
Expand Down