Skip to content

Commit 75c9727

Browse files
committed
Merge branch 'v3' into bugfix/nmap-release-name
Signed-off-by: Jop Zitman <jop-zitman@hotmail.com>
2 parents 4ec547e + 165749b commit 75c9727

File tree

83 files changed

+1064
-1061
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1064
-1061
lines changed

.github/workflows/ci.yaml

Lines changed: 71 additions & 70 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2020 iteratec GmbH
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
all: help
6+
7+
.PHONY:
8+
npm-ci-all: ## Runs npm ci in all node module subfolders.
9+
# This find construct is basedon https://stackoverflow.com/questions/4210042/how-to-exclude-a-directory-in-find-command/4210072#4210072
10+
find . \( \
11+
-name '.git' -o \
12+
-name '.github' -o \
13+
-name '.idea' -o \
14+
-name '.reuse' -o \
15+
-name '.vagrant' -o \
16+
-name '.vscode' -o \
17+
-name 'bin' -o \
18+
-name 'docs' -o \
19+
-name 'LICENSES' -o \
20+
-name 'coverage' -o \
21+
-name 'dist' -o \
22+
-name 'node_modules' -o \
23+
-name target \) \
24+
-prune \
25+
-false \
26+
-o -type d \
27+
-exec test -e '{}'/package.json \; \
28+
-execdir npm ci \;
29+
30+
.PHONY:
31+
npm-test-all: ## Runs all Jest basedtest suites.
32+
npm test
33+
34+
.PHONY:
35+
help: ## Display this help screen.
36+
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
37+
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

bin/install-npm-test-dependnecies.sh

Lines changed: 0 additions & 47 deletions
This file was deleted.

hooks/declarative-subsequent-scans/hook.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,50 @@ export function getCascadingScans(
5757
findings: Array<Finding>,
5858
cascadingRules: Array<CascadingRule>
5959
): Array<ExtendedScanSpec> {
60-
const cascadingScans: Array<ExtendedScanSpec> = [];
60+
let cascadingScans: Array<ExtendedScanSpec> = [];
61+
const cascadingRuleChain = getScanChain(parentScan);
6162

62-
const cascadingRuleChain = new Set<string>();
63+
for (const cascadingRule of cascadingRules) {
64+
// Check if the Same CascadingRule was already applied in the Cascading Chain
65+
// If it has already been used skip this rule as it could potentially lead to loops
66+
if (cascadingRuleChain.includes(cascadingRule.metadata.name)) {
67+
console.log(
68+
`Skipping Rule "${cascadingRule.metadata.name}" as it was already applied in this chain.`
69+
);
70+
continue;
71+
}
72+
73+
cascadingScans = cascadingScans.concat(getScansMatchingRule(parentScan, findings, cascadingRule))
74+
}
6375

76+
return cascadingScans;
77+
}
78+
79+
function getScanChain(parentScan: Scan) {
6480
// Get the current Scan Chain (meaning which CascadingRules were used to start this scan and its parents) and convert it to a set, which makes it easier to query.
6581
if (
6682
parentScan.metadata.annotations &&
6783
parentScan.metadata.annotations["cascading.securecodebox.io/chain"]
6884
) {
69-
const chainElements = parentScan.metadata.annotations[
85+
return parentScan.metadata.annotations[
7086
"cascading.securecodebox.io/chain"
7187
].split(",");
72-
73-
for (const element of chainElements) {
74-
cascadingRuleChain.add(element);
75-
}
7688
}
89+
return []
90+
}
7791

78-
for (const cascadingRule of cascadingRules) {
79-
// Check if the Same CascadingRule was already applied in the Cascading Chain
80-
// If it has already been used skip this rule as it could potentially lead to loops
81-
if (cascadingRuleChain.has(cascadingRule.metadata.name)) {
82-
console.log(
83-
`Skipping Rule "${cascadingRule.metadata.name}" as it was already applied in this chain.`
84-
);
85-
continue;
86-
}
87-
88-
for (const finding of findings) {
89-
// Check if one (ore more) of the CascadingRule matchers apply to the finding
90-
const matches = cascadingRule.spec.matches.anyOf.some(matchesRule =>
91-
isMatch(finding, matchesRule) || isMatchWith(finding, matchesRule, wildcardMatcher)
92-
);
92+
function getScansMatchingRule(parentScan: Scan, findings: Array<Finding>, cascadingRule: CascadingRule) {
93+
const cascadingScans: Array<ExtendedScanSpec> = [];
94+
for (const finding of findings) {
95+
// Check if one (ore more) of the CascadingRule matchers apply to the finding
96+
const matches = cascadingRule.spec.matches.anyOf.some(matchesRule =>
97+
isMatch(finding, matchesRule) || isMatchWith(finding, matchesRule, wildcardMatcher)
98+
);
9399

94-
if (matches) {
95-
cascadingScans.push(getCascadingScan(parentScan, finding, cascadingRule))
96-
}
100+
if (matches) {
101+
cascadingScans.push(getCascadingScan(parentScan, finding, cascadingRule))
97102
}
98103
}
99-
100104
return cascadingScans;
101105
}
102106

hooks/declarative-subsequent-scans/kubernetes-label-selector.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
const { generateLabelSelectorString } = require("./kubernetes-label-selector");
5+
const { generateSelectorString } = require("./kubernetes-label-selector");
66

77
test("should generate a empty string if passed an empty object", () => {
8-
expect(generateLabelSelectorString({})).toBe("");
8+
expect(generateSelectorString({})).toBe("");
99
});
1010

1111
test("should generate basic label string for key values selector", () => {
1212
expect(
13-
generateLabelSelectorString({
13+
generateSelectorString({
1414
matchLabels: { environment: "production" }
1515
})
1616
).toBe("environment=production");
1717

1818
expect(
19-
generateLabelSelectorString({
19+
generateSelectorString({
2020
matchLabels: { environment: "testing" }
2121
})
2222
).toBe("environment=testing");
2323
});
2424

2525
test("should generate basic label string for multiple key values selector", () => {
2626
expect(
27-
generateLabelSelectorString({
27+
generateSelectorString({
2828
matchLabels: {
2929
environment: "production",
3030
team: "search"
@@ -33,7 +33,7 @@ test("should generate basic label string for multiple key values selector", () =
3333
).toBe("environment=production,team=search");
3434

3535
expect(
36-
generateLabelSelectorString({
36+
generateSelectorString({
3737
matchLabels: {
3838
environment: "testing",
3939
team: "payment"
@@ -44,7 +44,7 @@ test("should generate basic label string for multiple key values selector", () =
4444

4545
test("should generate label string for set based expressions", () => {
4646
expect(
47-
generateLabelSelectorString({
47+
generateSelectorString({
4848
matchExpressions: [
4949
{
5050
key: "environment",
@@ -56,7 +56,7 @@ test("should generate label string for set based expressions", () => {
5656
).toBe("environment in (testing,development)");
5757

5858
expect(
59-
generateLabelSelectorString({
59+
generateSelectorString({
6060
matchExpressions: [
6161
{
6262
key: "environment",
@@ -70,7 +70,7 @@ test("should generate label string for set based expressions", () => {
7070

7171
test("should generate label string for set based expressions with multiple entries", () => {
7272
expect(
73-
generateLabelSelectorString({
73+
generateSelectorString({
7474
matchExpressions: [
7575
{
7676
key: "environment",
@@ -89,7 +89,7 @@ test("should generate label string for set based expressions with multiple entri
8989

9090
test("should generate label string for set based Exists and DoesNotExist operators", () => {
9191
expect(
92-
generateLabelSelectorString({
92+
generateSelectorString({
9393
matchExpressions: [
9494
{
9595
key: "environment",
@@ -106,7 +106,7 @@ test("should generate label string for set based Exists and DoesNotExist operato
106106

107107
test("should generate selectors with both expression and labelMatching", () => {
108108
expect(
109-
generateLabelSelectorString({
109+
generateSelectorString({
110110
matchExpressions: [
111111
{
112112
key: "environment",
@@ -138,7 +138,7 @@ test("should generate selectors with both expression and labelMatching", () => {
138138

139139
test("should throw a exception when passed a unknown operator", () => {
140140
expect(() =>
141-
generateLabelSelectorString({
141+
generateSelectorString({
142142
matchExpressions: [
143143
{
144144
key: "environment",

hooks/declarative-subsequent-scans/kubernetes-label-selector.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,41 @@ export interface LabelSelector {
2323
matchLabels: Map<string, string>;
2424
}
2525

26-
// generateLabelSelectorString transforms a kubernetes labelSelector object in to the string representation
27-
export function generateLabelSelectorString({
26+
// generateSelectorString transforms a kubernetes labelSelector object in to the string representation
27+
export function generateSelectorString({
2828
matchExpressions = [],
2929
matchLabels = new Map()
3030
}: LabelSelector): string {
31-
const matchLabelsSelector = Array.from(Object.entries(matchLabels)).map(
32-
([key, values]) => `${key}=${values}`
33-
);
34-
35-
const matchExpressionsSelector = matchExpressions.map(
36-
({ key, values, operator }) => {
37-
if (
38-
operator === LabelSelectorRequirementOperator.In ||
39-
operator === LabelSelectorRequirementOperator.NotIn
40-
) {
41-
return `${key} ${operator.toLowerCase()} (${values.join(",")})`;
42-
}
43-
44-
if (operator === LabelSelectorRequirementOperator.Exists) {
45-
return key;
46-
}
47-
if (operator === LabelSelectorRequirementOperator.DoesNotExist) {
48-
return `!${key}`;
49-
}
31+
const matchLabelsSelector = Array.from(Object.entries(matchLabels)).map(generateLabelsSelectorString);
5032

33+
const matchExpressionsSelector = matchExpressions.map(generateExpressionsSelectorString);
34+
35+
return [...matchLabelsSelector, ...matchExpressionsSelector].join(",");
36+
}
37+
38+
function generateLabelsSelectorString([key, values]) {
39+
return `${key}=${values}`
40+
}
41+
42+
function generateExpressionsSelectorString({key, values, operator}: LabelSelectorRequirement) {
43+
switch (operator) {
44+
case LabelSelectorRequirementOperator.In:
45+
case LabelSelectorRequirementOperator.NotIn:
46+
return `${key} ${operator.toLowerCase()} (${values.join(",")})`;
47+
48+
case LabelSelectorRequirementOperator.Exists:
49+
return key;
50+
51+
case LabelSelectorRequirementOperator.DoesNotExist:
52+
return `!${key}`;
53+
54+
default:
5155
const supportedOperators = Object.values(
5256
LabelSelectorRequirementOperator
5357
).join(", ");
5458

5559
throw new Error(
5660
`Unknown LabelSelector Operator "${operator}". Supported are (${supportedOperators}). If this is an official label selector operator in kubernetes please open up a issue in the secureCodeBox Repo.`
5761
);
58-
}
59-
);
60-
61-
return [...matchLabelsSelector, ...matchExpressionsSelector].join(",");
62+
}
6263
}

hooks/declarative-subsequent-scans/scan-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as k8s from "@kubernetes/client-node";
66

77
import {
8-
generateLabelSelectorString,
8+
generateSelectorString,
99
LabelSelector
1010
} from "./kubernetes-label-selector";
1111

@@ -178,7 +178,7 @@ export async function getCascadingRulesForScan(scan: Scan) {
178178
}
179179

180180
try {
181-
const labelSelector = generateLabelSelectorString(scan.spec.cascades);
181+
const labelSelector = generateSelectorString(scan.spec.cascades);
182182

183183
console.log(
184184
`Fetching CascadingScans using LabelSelector: "${labelSelector}"`

hooks/persistence-elastic/package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hooks/persistence-elastic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@elastic/elasticsearch": "^7.12.0",
4242
"lodash.chunk": "^4.2.0",
4343
"lodash.flatmap": "^4.5.0",
44-
"luxon": "^1.26.0"
44+
"luxon": "^1.27.0"
4545
},
4646
"devDependencies": {
4747
"jest": "^27.0.3"

0 commit comments

Comments
 (0)