Skip to content

Commit a7aed58

Browse files
committed
chore: move some tap tests to jest
1 parent fe485bd commit a7aed58

16 files changed

+531
-638
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"scripts": {
1313
"pretest": "./scripts/docker/build-image.sh",
1414
"test": "npm run lint && npm run build && npm run test:unit:tap && npm run test:unit:jest && npm run test:integration:kind:helm",
15-
"test:unit:tap": "NODE_ENV=test tap test/unit/*.test.ts test/unit/**/*.test.ts --timeout=300",
16-
"test:unit:jest": "jest --logHeapUsage --ci test/unit",
15+
"test:unit:tap": "NODE_ENV=test tap test/unit/**/*.test.ts --timeout=300",
16+
"test:unit:jest": "jest --logHeapUsage --ci --bail --forceExit test/unit",
1717
"test:system": "jest --logHeapUsage --ci --maxWorkers=1 test/system",
1818
"test:integration:kind:yaml": "DEPLOYMENT_TYPE=YAML TEST_PLATFORM=kind CREATE_CLUSTER=true jest --logHeapUsage --ci --maxWorkers=1 test/integration/kubernetes.spec.ts",
1919
"test:integration:kind:helm": "DEPLOYMENT_TYPE=Helm TEST_PLATFORM=kind CREATE_CLUSTER=true jest --logHeapUsage --ci --maxWorkers=1 test/integration/kubernetes.spec.ts",

test/helpers/deployment.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.

test/unit/deployment-files.spec.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

test/unit/deployment-files.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as credentials from '../../../src/scanner/images/credentials';
2+
3+
describe('ECR image parsing tests', () => {
4+
test('ecrRegionFromFullImageName()', async () => {
5+
const imageFullNameTemplate = 'aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app:latest';
6+
const ecrRegionTemplate = credentials.ecrRegionFromFullImageName(imageFullNameTemplate);
7+
expect(ecrRegionTemplate).toEqual('region');
8+
9+
const imageFullName = '291964488713.dkr.ecr.us-east-2.amazonaws.com/snyk/debian:10';
10+
const ecrRegion = credentials.ecrRegionFromFullImageName(imageFullName);
11+
expect(ecrRegion).toEqual('us-east-2');
12+
13+
expect(() => {credentials.ecrRegionFromFullImageName('');}).toThrow();
14+
expect(() => {credentials.ecrRegionFromFullImageName('dkr.ecr.region.amazonaws.com/my-web-app:latest');}).toThrow();
15+
expect(() => {credentials.ecrRegionFromFullImageName('aws_account_id.dkr.ecr.amazonaws.com/my-web-app:latest');}).toThrow();
16+
expect(() => {credentials.ecrRegionFromFullImageName('aws_account_id.dkr.ecr.region.amazonaws.com');}).toThrow();
17+
});
18+
19+
test('isEcrSource()', async () => {
20+
const sourceCredentialsForRandomImageName = credentials.isEcrSource('derka');
21+
expect(sourceCredentialsForRandomImageName).toEqual(false);
22+
23+
const sourceCredentialsForInvalidEcrImage = credentials.isEcrSource('derka.ecr.derka');
24+
expect(sourceCredentialsForInvalidEcrImage).toEqual(false);
25+
26+
const sourceCredentialsForEcrImage = credentials.isEcrSource('aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app:latest');
27+
expect(sourceCredentialsForEcrImage).toEqual(true);
28+
29+
const sourceCredentialsForEcrImageWithRepo = credentials.isEcrSource('a291964488713.dkr.ecr.us-east-2.amazonaws.com/snyk/debian:10');
30+
expect(sourceCredentialsForEcrImageWithRepo).toEqual(true);
31+
32+
const sourceCredentialsForEcrImageMixedCase = credentials.isEcrSource('aws_account_id.dKr.ecR.region.amazonAWS.cOm/my-web-app:latest');
33+
expect(sourceCredentialsForEcrImageMixedCase).toEqual(true);
34+
});
35+
});

test/unit/scanner/image-registry-credentials.test.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)