|
| 1 | +import { parse } from 'yaml'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +import { V1Deployment } from '@kubernetes/client-node'; |
| 4 | +import { config } from '../../src/common/config'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Note that these checks are also performed at runtime on the deployed snyk-monitor, see the integration tests. |
| 8 | + */ |
| 9 | + |
| 10 | +test('ensure the security properties of the deployment files are unchanged', async () => { |
| 11 | + expect(config.IMAGE_STORAGE_ROOT).toEqual('/var/tmp'); |
| 12 | + |
| 13 | + const deploymentFiles = ['./snyk-monitor-deployment.yaml']; |
| 14 | + |
| 15 | + for (const filePath of deploymentFiles) { |
| 16 | + const fileContent = readFileSync(filePath, 'utf8'); |
| 17 | + const deployment: V1Deployment = parse(fileContent); |
| 18 | + |
| 19 | + validateSecureConfiguration(deployment); |
| 20 | + validateVolumeMounts(deployment); |
| 21 | + validateEnvironmentVariables(deployment); |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +export function validateEnvironmentVariables(deployment: V1Deployment) { |
| 26 | + if ( |
| 27 | + !deployment.spec || |
| 28 | + !deployment.spec.template.spec || |
| 29 | + !deployment.spec.template.spec.containers || |
| 30 | + deployment.spec.template.spec.containers.length === 0 || |
| 31 | + !deployment.spec.template.spec.containers[0].env |
| 32 | + ) { |
| 33 | + fail('bad container spec or missing env'); |
| 34 | + } |
| 35 | + |
| 36 | + const env = deployment.spec.template.spec.containers[0].env; |
| 37 | + |
| 38 | + const envHasHomeEntry = env.some( |
| 39 | + (entry) => entry.name === 'HOME' && entry.value === '/srv/app', |
| 40 | + ); |
| 41 | + expect(envHasHomeEntry).toBeTruthy(); |
| 42 | +} |
| 43 | + |
| 44 | +export function validateSecureConfiguration(deployment: V1Deployment) { |
| 45 | + if ( |
| 46 | + !deployment.spec || |
| 47 | + !deployment.spec.template.spec || |
| 48 | + !deployment.spec.template.spec.containers || |
| 49 | + deployment.spec.template.spec.containers.length === 0 || |
| 50 | + !deployment.spec.template.spec.containers[0].securityContext |
| 51 | + ) { |
| 52 | + fail('bad container spec or missing securityContext'); |
| 53 | + } |
| 54 | + |
| 55 | + const securityContext = |
| 56 | + deployment.spec.template.spec.containers[0].securityContext; |
| 57 | + |
| 58 | + if (!securityContext.capabilities) { |
| 59 | + fail('missing capabilities section in pod securityContext'); |
| 60 | + } |
| 61 | + |
| 62 | + expect(securityContext.capabilities.drop).toEqual(['ALL']); |
| 63 | + |
| 64 | + if (securityContext.capabilities.add) { |
| 65 | + expect(securityContext.capabilities.add.includes('SYS_ADMIN')).toEqual( |
| 66 | + false, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + expect(securityContext.readOnlyRootFilesystem).toEqual(true); |
| 71 | + |
| 72 | + expect(securityContext.allowPrivilegeEscalation).toEqual(false); |
| 73 | + expect(securityContext.privileged).toEqual(false); |
| 74 | + expect(securityContext.runAsNonRoot).toEqual(true); |
| 75 | +} |
| 76 | + |
| 77 | +export function validateVolumeMounts(deployment: V1Deployment) { |
| 78 | + if ( |
| 79 | + !deployment.spec || |
| 80 | + !deployment.spec.template.spec || |
| 81 | + !deployment.spec.template.spec.containers || |
| 82 | + deployment.spec.template.spec.containers.length === 0 || |
| 83 | + !deployment.spec.template.spec.containers[0].volumeMounts |
| 84 | + ) { |
| 85 | + fail('bad container spec or missing volumeMounts'); |
| 86 | + } |
| 87 | + |
| 88 | + const volumeMounts = deployment.spec.template.spec.containers[0].volumeMounts; |
| 89 | + |
| 90 | + const temporaryStorageMount = volumeMounts.find( |
| 91 | + (mount) => mount.name === 'temporary-storage', |
| 92 | + ); |
| 93 | + if (!temporaryStorageMount) { |
| 94 | + fail('missing deployment mount "temporary-storage"'); |
| 95 | + } |
| 96 | + |
| 97 | + expect(temporaryStorageMount.mountPath).toEqual('/var/tmp'); |
| 98 | + |
| 99 | + const dockerConfigMount = volumeMounts.find( |
| 100 | + (mount) => mount.name === 'docker-config', |
| 101 | + ); |
| 102 | + if (!dockerConfigMount) { |
| 103 | + fail('missing deployment mount "docker-config"'); |
| 104 | + } |
| 105 | + |
| 106 | + expect(dockerConfigMount.readOnly).toEqual(true); |
| 107 | + expect(dockerConfigMount.mountPath).toEqual('/srv/app/.docker'); |
| 108 | +} |
0 commit comments