Skip to content

Commit 8381e79

Browse files
committed
fix: various securityContext fixes
Following best practices (as recommended by kubesec) we now explicitly unset privileged, set runAsNonRoot, and drop capabilities. We cannot yet set "readOnlyRootFilesystem" because skopeo pulls docker-archive files to /var/tmp and this cannot be configured.
1 parent 042f018 commit 8381e79

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

snyk-monitor-deployment.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ spec:
6060
securityContext:
6161
runAsUser: 10001
6262
runAsGroup: 10001
63+
privileged: false
64+
runAsNonRoot: true
65+
allowPrivilegeEscalation: false
66+
readOnlyRootFilesystem: false
67+
capabilities:
68+
drop:
69+
- ALL
6370
securityContext: {}
6471
volumes:
6572
- name: docker-config

snyk-monitor/templates/deployment.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ spec:
5454
securityContext:
5555
runAsUser: 10001
5656
runAsGroup: 10001
57+
privileged: false
58+
runAsNonRoot: true
59+
allowPrivilegeEscalation: false
60+
readOnlyRootFilesystem: false
61+
capabilities:
62+
drop:
63+
- ALL
5764
volumes:
5865
- name: docker-config
5966
secret:

test/unit/deployment-files.test.ts

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

Comments
 (0)