|
| 1 | +import * as tap from 'tap'; |
| 2 | +import { V1Deployment } from '@kubernetes/client-node'; |
| 3 | + |
| 4 | +export function validateSecureConfiguration(test: tap, deployment: V1Deployment) { |
| 5 | + if ( |
| 6 | + !deployment.spec || |
| 7 | + !deployment.spec.template.spec || |
| 8 | + !deployment.spec.template.spec.containers || |
| 9 | + deployment.spec.template.spec.containers.length === 0 || |
| 10 | + !deployment.spec.template.spec.containers[0].securityContext |
| 11 | + ) { |
| 12 | + test.fail('bad container spec or missing securityContext'); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const securityContext = |
| 17 | + deployment.spec.template.spec.containers[0].securityContext; |
| 18 | + |
| 19 | + if (!securityContext.capabilities) { |
| 20 | + test.fail('missing capabilities section in pod securityContext'); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + test.same( |
| 25 | + securityContext.capabilities.drop, |
| 26 | + ['ALL'], |
| 27 | + 'all capabilities are dropped', |
| 28 | + ); |
| 29 | + |
| 30 | + if (securityContext.capabilities.add) { |
| 31 | + test.false( |
| 32 | + securityContext.capabilities.add.includes('SYS_ADMIN'), |
| 33 | + 'CAP_SYS_ADMIN not added', |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + test.ok( |
| 38 | + securityContext.readOnlyRootFilesystem === true, |
| 39 | + 'readOnlyRootFilesystem is set', |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +export function validateVolumeMounts(test: tap, deployment: V1Deployment) { |
| 44 | + if ( |
| 45 | + !deployment.spec || |
| 46 | + !deployment.spec.template.spec || |
| 47 | + !deployment.spec.template.spec.containers || |
| 48 | + deployment.spec.template.spec.containers.length === 0 || |
| 49 | + !deployment.spec.template.spec.containers[0].volumeMounts |
| 50 | + ) { |
| 51 | + test.fail('bad container spec or missing volumeMounts'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const volumeMounts = deployment.spec.template.spec.containers[0].volumeMounts; |
| 56 | + |
| 57 | + const temporaryStorageMount = volumeMounts.find( |
| 58 | + (mount) => mount.name === 'temporary-storage', |
| 59 | + ); |
| 60 | + if (!temporaryStorageMount) { |
| 61 | + test.fail('missing deployment mount "temporary-storage"'); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + test.same( |
| 66 | + temporaryStorageMount.mountPath, |
| 67 | + '/var/tmp', |
| 68 | + 'deployment file mounts temporary storage at the expected path', |
| 69 | + ); |
| 70 | + |
| 71 | + const dockerConfigMount = volumeMounts.find( |
| 72 | + (mount) => mount.name === 'docker-config', |
| 73 | + ); |
| 74 | + if (!dockerConfigMount) { |
| 75 | + test.fail('missing deployment mount "docker-config"'); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + test.same( |
| 80 | + dockerConfigMount.readOnly, |
| 81 | + true, |
| 82 | + 'docker-config is a read-only mount', |
| 83 | + ); |
| 84 | + |
| 85 | + test.same( |
| 86 | + dockerConfigMount.mountPath, |
| 87 | + '/root/.docker', |
| 88 | + 'docker-config mount path is as expected', |
| 89 | + ); |
| 90 | +} |
0 commit comments