|
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | const { getCascadingScans } = require("./hook"); |
| 6 | +const {LabelSelectorRequirementOperator} = require("./kubernetes-label-selector"); |
6 | 7 |
|
7 | 8 | let parentScan = undefined; |
8 | 9 | let sslyzeCascadingRules = undefined; |
@@ -104,6 +105,7 @@ test("Should create subsequent scans for open HTTPS ports (NMAP findings)", () = |
104 | 105 | "spec": Object { |
105 | 106 | "cascades": Object {}, |
106 | 107 | "env": Array [], |
| 108 | + "hookSelector": Object {}, |
107 | 109 | "initContainers": Array [], |
108 | 110 | "parameters": Array [ |
109 | 111 | "--regular", |
@@ -241,6 +243,7 @@ test("Should not crash when the annotations are not set", () => { |
241 | 243 | "spec": Object { |
242 | 244 | "cascades": Object {}, |
243 | 245 | "env": Array [], |
| 246 | + "hookSelector": Object {}, |
244 | 247 | "initContainers": Array [], |
245 | 248 | "parameters": Array [ |
246 | 249 | "--regular", |
@@ -372,6 +375,7 @@ test("Should allow wildcards in cascadingRules", () => { |
372 | 375 | "spec": Object { |
373 | 376 | "cascades": Object {}, |
374 | 377 | "env": Array [], |
| 378 | + "hookSelector": Object {}, |
375 | 379 | "initContainers": Array [], |
376 | 380 | "parameters": Array [ |
377 | 381 | "--regular", |
@@ -1128,6 +1132,7 @@ test("Templating should also apply to initContainer commands", () => { |
1128 | 1132 | "spec": Object { |
1129 | 1133 | "cascades": Object {}, |
1130 | 1134 | "env": Array [], |
| 1135 | + "hookSelector": Object {}, |
1131 | 1136 | "initContainers": Array [ |
1132 | 1137 | Object { |
1133 | 1138 | "command": Array [ |
@@ -1260,6 +1265,7 @@ test("Templating should not break special encoding (http://...) when using tripl |
1260 | 1265 | "spec": Object { |
1261 | 1266 | "cascades": Object {}, |
1262 | 1267 | "env": Array [], |
| 1268 | + "hookSelector": Object {}, |
1263 | 1269 | "initContainers": Array [ |
1264 | 1270 | Object { |
1265 | 1271 | "command": Array [ |
@@ -1301,6 +1307,145 @@ test("Templating should not break special encoding (http://...) when using tripl |
1301 | 1307 | `); |
1302 | 1308 | }); |
1303 | 1309 |
|
| 1310 | +test("should merge hookSelector into cascaded scan", () => { |
| 1311 | + parentScan.spec.cascades.inheritHookSelector = true |
| 1312 | + const findings = [ |
| 1313 | + { |
| 1314 | + name: "Port 443 is open", |
| 1315 | + category: "Open Port", |
| 1316 | + attributes: { |
| 1317 | + state: "open", |
| 1318 | + hostname: "foobar.com", |
| 1319 | + port: 443, |
| 1320 | + service: "https" |
| 1321 | + } |
| 1322 | + } |
| 1323 | + ]; |
| 1324 | + |
| 1325 | + parentScan.spec.hookSelector = {} |
| 1326 | + parentScan.spec.hookSelector.matchLabels = { |
| 1327 | + "securecodebox.io/internal": "true", |
| 1328 | + } |
| 1329 | + parentScan.spec.hookSelector.matchExpressions = [ |
| 1330 | + { |
| 1331 | + key: "securecodebox.io/name", |
| 1332 | + operator: LabelSelectorRequirementOperator.In, |
| 1333 | + values: ["cascading-scans"] |
| 1334 | + } |
| 1335 | + ] |
| 1336 | + |
| 1337 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector = {}; |
| 1338 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector.matchExpressions = [ |
| 1339 | + { |
| 1340 | + key: "securecodebox.io/name", |
| 1341 | + operator: LabelSelectorRequirementOperator.NotIn, |
| 1342 | + values: ["cascading-scans"] |
| 1343 | + } |
| 1344 | + ] |
| 1345 | + |
| 1346 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector.matchLabels = { |
| 1347 | + "securecodebox.io/internal": "false", |
| 1348 | + } |
| 1349 | + |
| 1350 | + const cascadedScans = getCascadingScans( |
| 1351 | + parentScan, |
| 1352 | + findings, |
| 1353 | + sslyzeCascadingRules |
| 1354 | + ); |
| 1355 | + |
| 1356 | + const cascadedScan = cascadedScans[0]; |
| 1357 | + |
| 1358 | + expect(cascadedScan.spec.hookSelector).toMatchInlineSnapshot(` |
| 1359 | + Object { |
| 1360 | + "matchExpressions": Array [ |
| 1361 | + Object { |
| 1362 | + "key": "securecodebox.io/name", |
| 1363 | + "operator": "In", |
| 1364 | + "values": Array [ |
| 1365 | + "cascading-scans", |
| 1366 | + ], |
| 1367 | + }, |
| 1368 | + Object { |
| 1369 | + "key": "securecodebox.io/name", |
| 1370 | + "operator": "NotIn", |
| 1371 | + "values": Array [ |
| 1372 | + "cascading-scans", |
| 1373 | + ], |
| 1374 | + }, |
| 1375 | + ], |
| 1376 | + "matchLabels": Object { |
| 1377 | + "securecodebox.io/internal": "false", |
| 1378 | + }, |
| 1379 | + } |
| 1380 | + `); |
| 1381 | +}); |
| 1382 | + |
| 1383 | + |
| 1384 | +test("should not merge hookSelector into cascaded scan if inheritHookSelector is disabled", () => { |
| 1385 | + parentScan.spec.cascades.inheritHookSelector = false |
| 1386 | + const findings = [ |
| 1387 | + { |
| 1388 | + name: "Port 443 is open", |
| 1389 | + category: "Open Port", |
| 1390 | + attributes: { |
| 1391 | + state: "open", |
| 1392 | + hostname: "foobar.com", |
| 1393 | + port: 443, |
| 1394 | + service: "https" |
| 1395 | + } |
| 1396 | + } |
| 1397 | + ]; |
| 1398 | + |
| 1399 | + parentScan.spec.hookSelector = {} |
| 1400 | + parentScan.spec.hookSelector.matchLabels = { |
| 1401 | + "securecodebox.io/internal": "true", |
| 1402 | + } |
| 1403 | + parentScan.spec.hookSelector.matchExpressions = [ |
| 1404 | + { |
| 1405 | + key: "securecodebox.io/name", |
| 1406 | + operator: LabelSelectorRequirementOperator.In, |
| 1407 | + values: ["cascading-scans"] |
| 1408 | + } |
| 1409 | + ] |
| 1410 | + |
| 1411 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector = {}; |
| 1412 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector.matchExpressions = [ |
| 1413 | + { |
| 1414 | + key: "securecodebox.io/name", |
| 1415 | + operator: LabelSelectorRequirementOperator.NotIn, |
| 1416 | + values: ["cascading-scans"] |
| 1417 | + } |
| 1418 | + ] |
| 1419 | + |
| 1420 | + sslyzeCascadingRules[0].spec.scanSpec.hookSelector.matchLabels = { |
| 1421 | + "securecodebox.io/internal": "false", |
| 1422 | + } |
| 1423 | + |
| 1424 | + const cascadedScans = getCascadingScans( |
| 1425 | + parentScan, |
| 1426 | + findings, |
| 1427 | + sslyzeCascadingRules |
| 1428 | + ); |
| 1429 | + |
| 1430 | + const cascadedScan = cascadedScans[0]; |
| 1431 | + |
| 1432 | + expect(cascadedScan.spec.hookSelector).toMatchInlineSnapshot(` |
| 1433 | + Object { |
| 1434 | + "matchExpressions": Array [ |
| 1435 | + Object { |
| 1436 | + "key": "securecodebox.io/name", |
| 1437 | + "operator": "NotIn", |
| 1438 | + "values": Array [ |
| 1439 | + "cascading-scans", |
| 1440 | + ], |
| 1441 | + }, |
| 1442 | + ], |
| 1443 | + "matchLabels": Object { |
| 1444 | + "securecodebox.io/internal": "false", |
| 1445 | + }, |
| 1446 | + } |
| 1447 | + `); |
| 1448 | +}); |
1304 | 1449 |
|
1305 | 1450 | test("should purge cascaded scan spec from parent scan", () => { |
1306 | 1451 | parentScan.spec.cascades.inheritEnv = true |
|
0 commit comments