Skip to content

Commit 0674e5f

Browse files
committed
Apply prettier to all hook code
1 parent 7ef2c27 commit 0674e5f

15 files changed

Lines changed: 541 additions & 474 deletions

File tree

hooks/cascading-scans/hook/hook.test.js

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

hooks/cascading-scans/hook/hook.ts

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
forEach,
1313
isArray,
1414
} from "lodash";
15-
import {isMatch as wildcardIsMatch} from "matcher";
15+
import { isMatch as wildcardIsMatch } from "matcher";
1616
import Mustache from "mustache";
1717

1818
import {
@@ -30,14 +30,14 @@ import {
3030
mergeInheritedArray,
3131
mergeInheritedSelector,
3232
} from "./scan-helpers";
33-
import {isInScope, scopeDomain} from "./scope-limiter";
33+
import { isInScope, scopeDomain } from "./scope-limiter";
3434

3535
interface HandleArgs {
3636
scan: Scan;
3737
getFindings: () => Array<Finding>;
3838
}
3939

40-
export async function handle({scan, getFindings}: HandleArgs) {
40+
export async function handle({ scan, getFindings }: HandleArgs) {
4141
const findings = await getFindings();
4242
const cascadingRules = await getCascadingRules(scan);
4343
const cascadedRuleUsedForParentScan = await getCascadedRuleForScan(scan);
@@ -48,7 +48,7 @@ export async function handle({scan, getFindings}: HandleArgs) {
4848
findings,
4949
cascadingRules,
5050
cascadedRuleUsedForParentScan,
51-
parseDefinition
51+
parseDefinition,
5252
);
5353

5454
for (const cascadingScan of cascadingScans) {
@@ -75,22 +75,22 @@ export function getCascadingScans(
7575
findings: Array<Finding>,
7676
cascadingRules: Array<CascadingRule>,
7777
cascadedRuleUsedForParentScan: CascadingRule,
78-
parseDefinition: ParseDefinition
78+
parseDefinition: ParseDefinition,
7979
): Array<Scan> {
8080
let cascadingScans: Array<Scan> = [];
8181
const cascadingRuleChain = getScanChain(parentScan);
8282

8383
parentScan = purgeCascadedRuleFromScan(
8484
parentScan,
85-
cascadedRuleUsedForParentScan
85+
cascadedRuleUsedForParentScan,
8686
);
8787

8888
for (const cascadingRule of cascadingRules) {
8989
// Check if the Same CascadingRule was already applied in the Cascading Chain
9090
// If it has already been used skip this rule as it could potentially lead to loops
9191
if (cascadingRuleChain.includes(cascadingRule.metadata.name)) {
9292
console.log(
93-
`Skipping Rule "${cascadingRule.metadata.name}" as it was already applied in this chain.`
93+
`Skipping Rule "${cascadingRule.metadata.name}" as it was already applied in this chain.`,
9494
);
9595
continue;
9696
}
@@ -99,13 +99,18 @@ export function getCascadingScans(
9999
forEach(cascadingRule.spec.scanAnnotations, (value, key) => {
100100
if (key.startsWith(scopeDomain)) {
101101
throw new Error(
102-
`may not add scope annotation '${key}':'${value}' in Cascading Rule spec`
102+
`may not add scope annotation '${key}':'${value}' in Cascading Rule spec`,
103103
);
104104
}
105105
});
106106

107107
cascadingScans = cascadingScans.concat(
108-
getScansMatchingRule(parentScan, findings, cascadingRule, parseDefinition)
108+
getScansMatchingRule(
109+
parentScan,
110+
findings,
111+
cascadingRule,
112+
parseDefinition,
113+
),
109114
);
110115
}
111116

@@ -129,7 +134,7 @@ function getScansMatchingRule(
129134
parentScan: Scan,
130135
findings: Array<Finding>,
131136
cascadingRule: CascadingRule,
132-
parseDefinition: ParseDefinition
137+
parseDefinition: ParseDefinition,
133138
) {
134139
const cascadingScans: Array<Scan> = [];
135140
for (const finding of findings) {
@@ -138,23 +143,23 @@ function getScansMatchingRule(
138143
parentScan.spec.cascades.scopeLimiter,
139144
parentScan.metadata.annotations,
140145
finding,
141-
parseDefinition.spec.scopeLimiterAliases
146+
parseDefinition.spec.scopeLimiterAliases,
142147
);
143148

144149
if (!inScope) {
145150
console.log(
146-
`Cascading Rule ${cascadingRule.metadata.name} not triggered as scope limiter did not pass`
151+
`Cascading Rule ${cascadingRule.metadata.name} not triggered as scope limiter did not pass`,
147152
);
148153
console.log(
149-
`Scan annotations ${JSON.stringify(parentScan.metadata.annotations)}`
154+
`Scan annotations ${JSON.stringify(parentScan.metadata.annotations)}`,
150155
);
151156
console.log(
152-
`Scope limiter ${JSON.stringify(parentScan.spec.cascades.scopeLimiter)}`
157+
`Scope limiter ${JSON.stringify(parentScan.spec.cascades.scopeLimiter)}`,
153158
);
154159
console.log(
155160
`Scope limiter aliases ${JSON.stringify(
156-
parseDefinition.spec.scopeLimiterAliases
157-
)}`
161+
parseDefinition.spec.scopeLimiterAliases,
162+
)}`,
158163
);
159164
console.log(`Finding ${JSON.stringify(finding)}`);
160165
continue;
@@ -164,7 +169,7 @@ function getScansMatchingRule(
164169
const matches = cascadingRule.spec.matches.anyOf.some(
165170
(matchesRule) =>
166171
isMatch(finding, matchesRule) ||
167-
isMatchWith(finding, matchesRule, wildcardMatcher)
172+
isMatchWith(finding, matchesRule, wildcardMatcher),
168173
);
169174

170175
if (matches) {
@@ -177,16 +182,16 @@ function getScansMatchingRule(
177182
function getCascadingScan(
178183
parentScan: Scan,
179184
finding: Finding,
180-
cascadingRule: CascadingRule
185+
cascadingRule: CascadingRule,
181186
) {
182187
// Make a deep copy of the original cascading rule so that we can template it again with different findings.
183188
cascadingRule = templateCascadingRule(
184189
parentScan,
185190
finding,
186-
cloneDeep(cascadingRule)
191+
cloneDeep(cascadingRule),
187192
);
188193

189-
let {scanType, parameters} = cascadingRule.spec.scanSpec;
194+
let { scanType, parameters } = cascadingRule.spec.scanSpec;
190195

191196
let {
192197
annotations,
@@ -218,7 +223,7 @@ function getCascadingScan(
218223
cascadingRule.metadata.name,
219224
].join(","),
220225
...pickBy(parentScan.metadata.annotations, (value, key) =>
221-
key.startsWith(scopeDomain)
226+
key.startsWith(scopeDomain),
222227
),
223228
},
224229
ownerReferences: [
@@ -249,7 +254,7 @@ function getCascadingScan(
249254
}
250255

251256
function mergeCascadingRuleWithScan(scan: Scan, cascadingRule: CascadingRule) {
252-
const {scanAnnotations, scanLabels} = cascadingRule.spec;
257+
const { scanAnnotations, scanLabels } = cascadingRule.spec;
253258
let {
254259
env = [],
255260
volumes = [],
@@ -280,7 +285,7 @@ function mergeCascadingRuleWithScan(scan: Scan, cascadingRule: CascadingRule) {
280285
selectedTolerations = mergeInheritedArray(
281286
scan.spec.tolerations,
282287
tolerations,
283-
inheritTolerations
288+
inheritTolerations,
284289
);
285290
} else if (inheritTolerations) {
286291
selectedTolerations = scan.spec.tolerations;
@@ -297,25 +302,25 @@ function mergeCascadingRuleWithScan(scan: Scan, cascadingRule: CascadingRule) {
297302
annotations: mergeInheritedMap(
298303
scan.metadata.annotations,
299304
scanAnnotations,
300-
inheritAnnotations
305+
inheritAnnotations,
301306
),
302307
labels: mergeInheritedMap(scan.metadata.labels, scanLabels, inheritLabels),
303308
env: mergeInheritedArray(scan.spec.env, env, inheritEnv),
304309
volumes: mergeInheritedArray(scan.spec.volumes, volumes, inheritVolumes),
305310
volumeMounts: mergeInheritedArray(
306311
scan.spec.volumeMounts,
307312
volumeMounts,
308-
inheritVolumes
313+
inheritVolumes,
309314
),
310315
initContainers: mergeInheritedArray(
311316
scan.spec.initContainers,
312317
initContainers,
313-
inheritInitContainers
318+
inheritInitContainers,
314319
),
315320
hookSelector: mergeInheritedSelector(
316321
scan.spec.hookSelector,
317322
hookSelector,
318-
inheritHookSelector
323+
inheritHookSelector,
319324
),
320325
affinity: selectedAffinity,
321326
tolerations: selectedTolerations,
@@ -339,7 +344,7 @@ function hostOrIP(finding: Finding): string {
339344
function templateCascadingRule(
340345
parentScan: Scan,
341346
finding: Finding,
342-
cascadingRule: CascadingRule
347+
cascadingRule: CascadingRule,
343348
): CascadingRule {
344349
const templateArgs = {
345350
...finding,
@@ -350,17 +355,17 @@ function templateCascadingRule(
350355
},
351356
};
352357

353-
const {scanSpec, scanAnnotations, scanLabels} = cascadingRule.spec;
354-
const {scanType, parameters, initContainers} = scanSpec;
358+
const { scanSpec, scanAnnotations, scanLabels } = cascadingRule.spec;
359+
const { scanType, parameters, initContainers } = scanSpec;
355360

356361
// Templating for scanType
357362
cascadingRule.spec.scanSpec.scanType = Mustache.render(
358363
scanType,
359-
templateArgs
364+
templateArgs,
360365
);
361366
// Templating for scan parameters
362367
cascadingRule.spec.scanSpec.parameters = parameters.map((parameter) =>
363-
Mustache.render(parameter, templateArgs)
368+
Mustache.render(parameter, templateArgs),
364369
);
365370
// Templating for environmental variables
366371
if (cascadingRule.spec.scanSpec.env !== undefined) {
@@ -379,7 +384,7 @@ function templateCascadingRule(
379384
cascadingRule.spec.scanSpec.initContainers.forEach((container) => {
380385
// Templating for the command
381386
container.command = container.command.map((parameter) =>
382-
Mustache.render(parameter, templateArgs)
387+
Mustache.render(parameter, templateArgs),
383388
);
384389
// Templating for env variables, similar to above.
385390
if (container.env !== undefined) {
@@ -396,7 +401,7 @@ function templateCascadingRule(
396401
scanAnnotations === undefined
397402
? {}
398403
: mapValues(scanAnnotations, (value) =>
399-
Mustache.render(value, templateArgs)
404+
Mustache.render(value, templateArgs),
400405
);
401406
// Templating for scan labels
402407
cascadingRule.spec.scanLabels =
@@ -409,7 +414,7 @@ function templateCascadingRule(
409414

410415
function generateCascadingScanName(
411416
parentScan: Scan,
412-
cascadingRule: CascadingRule
417+
cascadingRule: CascadingRule,
413418
): string {
414419
let namePrefix = parentScan.metadata.name;
415420

@@ -418,7 +423,7 @@ function generateCascadingScanName(
418423
if (namePrefix.startsWith(parentScan.spec.scanType)) {
419424
namePrefix = namePrefix.replace(
420425
parentScan.spec.scanType,
421-
cascadingRule.spec.scanSpec.scanType
426+
cascadingRule.spec.scanSpec.scanType,
422427
);
423428
}
424429
return `${namePrefix}-${cascadingRule.metadata.name}`;
@@ -430,7 +435,7 @@ function wildcardMatcher(findingValue: any, matchesRuleValue: any): boolean {
430435
return wildcardIsMatch(
431436
findingValue.toString(),
432437
matchesRuleValue.toString(),
433-
{caseSensitive: true}
438+
{ caseSensitive: true },
434439
);
435440
// return new RegExp('^' + new String(matchesRuleValue).replace(/\*/g, '.*') + '$').test(findingValue);
436441
} catch (error) {

hooks/cascading-scans/hook/kubernetes-label-selector.test.js

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

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

77
test("should generate a empty string if passed an empty object", () => {
88
expect(generateSelectorString({})).toBe("");
@@ -11,14 +11,14 @@ test("should generate a empty string if passed an empty object", () => {
1111
test("should generate basic label string for key values selector", () => {
1212
expect(
1313
generateSelectorString({
14-
matchLabels: {environment: "production"},
15-
})
14+
matchLabels: { environment: "production" },
15+
}),
1616
).toBe("environment=production");
1717

1818
expect(
1919
generateSelectorString({
20-
matchLabels: {environment: "testing"},
21-
})
20+
matchLabels: { environment: "testing" },
21+
}),
2222
).toBe("environment=testing");
2323
});
2424

@@ -29,7 +29,7 @@ test("should generate basic label string for multiple key values selector", () =
2929
environment: "production",
3030
team: "search",
3131
},
32-
})
32+
}),
3333
).toBe("environment=production,team=search");
3434

3535
expect(
@@ -38,7 +38,7 @@ test("should generate basic label string for multiple key values selector", () =
3838
environment: "testing",
3939
team: "payment",
4040
},
41-
})
41+
}),
4242
).toBe("environment=testing,team=payment");
4343
});
4444

@@ -52,7 +52,7 @@ test("should generate label string for set based expressions", () => {
5252
values: ["testing", "development"],
5353
},
5454
],
55-
})
55+
}),
5656
).toBe("environment in (testing,development)");
5757

5858
expect(
@@ -64,7 +64,7 @@ test("should generate label string for set based expressions", () => {
6464
values: ["development"],
6565
},
6666
],
67-
})
67+
}),
6868
).toBe("environment in (development)");
6969
});
7070

@@ -83,7 +83,7 @@ test("should generate label string for set based expressions with multiple entri
8383
values: ["search", "payment"],
8484
},
8585
],
86-
})
86+
}),
8787
).toBe("environment notin (production),team in (search,payment)");
8888
});
8989

@@ -100,7 +100,7 @@ test("should generate label string for set based Exists and DoesNotExist operato
100100
operator: "DoesNotExist",
101101
},
102102
],
103-
})
103+
}),
104104
).toBe("environment,!team");
105105
});
106106

@@ -130,9 +130,9 @@ test("should generate selectors with both expression and labelMatching", () => {
130130
matchLabels: {
131131
critical: "true",
132132
},
133-
})
133+
}),
134134
).toBe(
135-
"critical=true,environment notin (production),team in (search,payment),foobar,!barfoo"
135+
"critical=true,environment notin (production),team in (search,payment),foobar,!barfoo",
136136
);
137137
});
138138

@@ -146,8 +146,8 @@ test("should throw a exception when passed a unknown operator", () => {
146146
values: ["production"],
147147
},
148148
],
149-
})
149+
}),
150150
).toThrowErrorMatchingInlineSnapshot(
151-
`"Unknown LabelSelector Operator "FooBar". Supported are (In, NotIn, Exists, DoesNotExist). If this is an official label selector operator in kubernetes please open up a issue in the secureCodeBox Repo."`
151+
`"Unknown LabelSelector Operator "FooBar". Supported are (In, NotIn, Exists, DoesNotExist). If this is an official label selector operator in kubernetes please open up a issue in the secureCodeBox Repo."`,
152152
);
153153
});

0 commit comments

Comments
 (0)