|
| 1 | +import * as tap from 'tap'; |
| 2 | +import { parse } from 'yaml'; |
| 3 | +import { readFileSync } from 'fs'; |
| 4 | +import { V1Deployment } from '@kubernetes/client-node'; |
| 5 | + |
| 6 | +tap.test('ensure the security properties of the deployment files are unchanged', async (t) => { |
| 7 | + const deploymentFiles = ['./snyk-monitor/templates/deployment.yaml', './snyk-monitor-deployment.yaml']; |
| 8 | + |
| 9 | + for (const filePath of deploymentFiles) { |
| 10 | + const fileContent = readFileSync(filePath, 'utf8'); |
| 11 | + const deployment: V1Deployment = parse(fileContent); |
| 12 | + |
| 13 | + if ( |
| 14 | + !deployment.spec || |
| 15 | + !deployment.spec.template.spec || |
| 16 | + !deployment.spec.template.spec.containers || |
| 17 | + deployment.spec.template.spec.containers.length === 0 || |
| 18 | + !deployment.spec.template.spec.containers[0].securityContext |
| 19 | + ) { |
| 20 | + tap.fail('bad container spec or missing securityContext'); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const securityContext = |
| 25 | + deployment.spec.template.spec.containers[0].securityContext; |
| 26 | + |
| 27 | + if (!securityContext.capabilities) { |
| 28 | + tap.fail('missing capabilities section in pod securityContext'); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + tap.same(securityContext.capabilities, { drop: ['ALL'] }, 'all capabilities are dropped and none are added'); |
| 33 | + tap.ok(securityContext.allowPrivilegeEscalation === false, 'must explicitly set allowPrivilegeEscalation to false'); |
| 34 | + tap.ok(securityContext.privileged === false, 'must explicitly set privileged to false'); |
| 35 | + tap.ok(securityContext.runAsNonRoot === true, 'must explicitly set runAsNonRoot to true'); |
| 36 | + tap.ok(securityContext.runAsUser === 10001, 'must explicitly set runAsUser to 10001'); |
| 37 | + tap.ok(securityContext.runAsGroup === 10001, 'must explicitly set runAsGroup to 10001'); |
| 38 | + |
| 39 | + // TODO: currently we do not set this to true because skopeo pulls |
| 40 | + // temporary files to /var/tmp and this behaviour is not configurable |
| 41 | + // To be secure, this value MUST be set to "true"! |
| 42 | + tap.ok(securityContext.readOnlyRootFilesystem === false, 'readOnlyRootFilesystem is not set ON PURPOSE'); |
| 43 | + } |
| 44 | +}); |
0 commit comments