Skip to content

Commit e8fce32

Browse files
James Alsethshaimendel
authored andcommitted
feat: Add support for PodSecurityPolicy
1 parent 82ea562 commit e8fce32

File tree

8 files changed

+102
-1
lines changed

8 files changed

+102
-1
lines changed

snyk-monitor/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ helm upgrade --install ... \
126126
--set no_proxy=long.domain.name.local,example.local
127127
```
128128

129+
## PodSecurityPolicies
130+
**This should not be used when installing on OpenShift.**
131+
132+
Using PodSecurityPolicies can be achieved by setting the following values in the Helm chart:
133+
* psp.enabled - default is `false`. Set to `true` if PodSecurityPolicy is needed
134+
* psp.name - default is empty. Leave it empty if you want us to install the necessary PodSecurityPolicy. Modify it to specify an existing PodSecurityPolicy rather than creating a new one.
135+
136+
For example:
137+
```bash
138+
helm upgrade --install snyk-monitor snyk-charts/snyk-monitor \
139+
--namespace snyk-monitor \
140+
--set clusterName="Production cluster" \
141+
--set psp.enabled=true
142+
```
143+
129144
## Terms and conditions ##
130145

131146
*The Snyk Container Kubernetes integration uses Red Hat UBI (Universal Base Image).*

snyk-monitor/templates/clusterrole.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@ rules:
5353
- get
5454
- list
5555
- watch
56+
{{- if .Values.psp.enabled }}
57+
- apiGroups:
58+
- policy
59+
resources:
60+
- podsecuritypolicies
61+
verbs:
62+
- get
63+
- list
64+
- use
65+
resourceNames:
66+
- {{ if eq .Values.psp.name "" }}{{ include "snyk-monitor.name" . }}{{ else }}{{ .Values.psp.name }}{{- end }}
67+
{{- end }}
5668
{{- end }}

snyk-monitor/templates/psp.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{- if .Values.psp.enabled }}{{- if eq .Values.psp.name "" }}
2+
apiVersion: policy/v1beta1
3+
kind: PodSecurityPolicy
4+
metadata:
5+
name: {{ include "snyk-monitor.name" . }}
6+
spec:
7+
allowPrivilegeEscalation: false
8+
requiredDropCapabilities:
9+
- ALL
10+
fsGroup:
11+
rule: MustRunAs
12+
ranges:
13+
- min: 1
14+
max: 65535
15+
seLinux:
16+
rule: RunAsAny
17+
runAsUser:
18+
rule: MustRunAsNonRoot
19+
supplementalGroups:
20+
rule: MustRunAs
21+
ranges:
22+
- min: 1
23+
max: 65535
24+
hostNetwork: false
25+
hostIPC: false
26+
hostPID: false
27+
readOnlyRootFilesystem: true
28+
volumes:
29+
- secret
30+
- configMap
31+
- emptyDir
32+
{{- end }}{{- end }}

snyk-monitor/templates/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ rules:
5151
- get
5252
- list
5353
- watch
54+
{{- if .Values.psp.enabled }}
55+
- apiGroups:
56+
- policy
57+
resources:
58+
- podsecuritypolicies
59+
verbs:
60+
- get
61+
- list
62+
- use
63+
resourceNames:
64+
- {{ if eq .Values.psp.name "" }}{{ include "snyk-monitor.name" . }}{{ else }}{{ .Values.psp.name }}{{- end }}
65+
{{- end }}
5466
{{- end }}

snyk-monitor/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ https_proxy:
4545
no_proxy:
4646

4747
nodeSelector: {}
48+
49+
psp:
50+
enabled: false
51+
name: ""

test/helpers/kubectl.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,16 @@ async function getLatestStableK8sRelease(): Promise<string> {
211211
console.log(`The latest stable K8s release is ${k8sRelease}`);
212212
return k8sRelease;
213213
}
214+
215+
export async function verifyPodSecurityPolicy(name: string): Promise<boolean> {
216+
console.log(`Trying to find Pod Security Policy ${name}`);
217+
for (let attempt = 0; attempt < 60; attempt++) {
218+
try {
219+
await exec(`./kubectl get podsecuritypolicy ${name}`);
220+
return true;
221+
} catch (err) {
222+
await sleep(500);
223+
}
224+
}
225+
return false;
226+
}

test/integration/kubernetes.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ tap.test('snyk-monitor has nodeSelector', async (t) => {
366366
t.ok('nodeSelector' in spec, 'snyk-monitor has nodeSelector');
367367
});
368368

369+
tap.test('snyk-monitor has PodSecurityPolicy', async (t) => {
370+
t.plan(1);
371+
372+
if (process.env['DEPLOYMENT_TYPE'] !== 'Helm') {
373+
t.pass('Not testing PodSecurityPolicy because we\'re not installing with Helm');
374+
return;
375+
}
376+
377+
const pspExists = await kubectl.verifyPodSecurityPolicy('snyk-monitor');
378+
t.ok(pspExists, 'PodSecurityPolicy was deployed');
379+
});
380+
369381
tap.test('snyk-monitor secure configuration is as expected', async (t) => {
370382
const kubeConfig = new KubeConfig();
371383
kubeConfig.loadFromDefault();

test/setup/deployers/helm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ async function deployKubernetesMonitor(
3030
`--set image.tag=${imageTag} ` +
3131
`--set image.pullPolicy=${imagePullPolicy} ` +
3232
'--set integrationApi=https://kubernetes-upstream.dev.snyk.io ' +
33-
'--set nodeSelector."kubernetes\\.io/os"=linux'
33+
'--set nodeSelector."kubernetes\\.io/os"=linux ' +
34+
'--set psp.enabled=true'
3435
);
3536
console.log(`Deployed ${imageOptions.nameAndTag} with pull policy ${imageOptions.pullPolicy}`);
3637
}

0 commit comments

Comments
 (0)