Skip to content

Commit 6193560

Browse files
authored
Enable seccomp on containers (#3193)
As of Kubernetes v1.19, SecurityContext has a seccompProfile field that can be set to RuntimeDefault to limit syscalls. This PR adds that setting to the containers in order to (a) limit syscalls from PGO-managed containers, while (b) not preventing users from using other tools involving sidecars, etc. Issue [sc-11286]
1 parent cc5f2a4 commit 6193560

File tree

11 files changed

+101
-2
lines changed

11 files changed

+101
-2
lines changed

config/manager/manager.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ spec:
3838
allowPrivilegeEscalation: false
3939
readOnlyRootFilesystem: true
4040
runAsNonRoot: true
41+
seccompProfile:
42+
type: RuntimeDefault
4143
serviceAccountName: pgo

internal/controller/postgrescluster/instance_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
563563
privileged: false
564564
readOnlyRootFilesystem: true
565565
runAsNonRoot: true
566+
seccompProfile:
567+
type: RuntimeDefault
566568
volumeMounts:
567569
- mountPath: /etc/pgbackrest/server
568570
name: pgbackrest-server
@@ -610,6 +612,8 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
610612
privileged: false
611613
readOnlyRootFilesystem: true
612614
runAsNonRoot: true
615+
seccompProfile:
616+
type: RuntimeDefault
613617
volumeMounts:
614618
- mountPath: /etc/pgbackrest/server
615619
name: pgbackrest-server
@@ -665,6 +669,8 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
665669
privileged: false
666670
readOnlyRootFilesystem: true
667671
runAsNonRoot: true
672+
seccompProfile:
673+
type: RuntimeDefault
668674
volumeMounts:
669675
- mountPath: /etc/pgbackrest/server
670676
name: pgbackrest-server
@@ -712,6 +718,8 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
712718
privileged: false
713719
readOnlyRootFilesystem: true
714720
runAsNonRoot: true
721+
seccompProfile:
722+
type: RuntimeDefault
715723
volumeMounts:
716724
- mountPath: /etc/pgbackrest/server
717725
name: pgbackrest-server

internal/controller/postgrescluster/pgbackrest_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,8 @@ containers:
25042504
privileged: false
25052505
readOnlyRootFilesystem: true
25062506
runAsNonRoot: true
2507+
seccompProfile:
2508+
type: RuntimeDefault
25072509
volumeMounts:
25082510
- mountPath: /etc/pgbackrest/conf.d
25092511
name: pgbackrest-config

internal/controller/postgrescluster/volumes_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ containers:
991991
privileged: false
992992
readOnlyRootFilesystem: true
993993
runAsNonRoot: true
994+
seccompProfile:
995+
type: RuntimeDefault
994996
terminationMessagePath: /dev/termination-log
995997
terminationMessagePolicy: File
996998
volumeMounts:
@@ -1044,6 +1046,8 @@ containers:
10441046
privileged: false
10451047
readOnlyRootFilesystem: true
10461048
runAsNonRoot: true
1049+
seccompProfile:
1050+
type: RuntimeDefault
10471051
terminationMessagePath: /dev/termination-log
10481052
terminationMessagePolicy: File
10491053
volumeMounts:
@@ -1099,6 +1103,8 @@ containers:
10991103
privileged: false
11001104
readOnlyRootFilesystem: true
11011105
runAsNonRoot: true
1106+
seccompProfile:
1107+
type: RuntimeDefault
11021108
terminationMessagePath: /dev/termination-log
11031109
terminationMessagePolicy: File
11041110
volumeMounts:

internal/initialize/security.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
)
2121

2222
// RestrictedPodSecurityContext returns a v1.PodSecurityContext with safe defaults.
23+
// Note: All current containers have security context set by `RestrictedSecurityContext`
24+
// which has recommended limits; if more pods/containers are added
25+
// make sure to set the SC on the container
2326
// See https://docs.k8s.io/concepts/security/pod-security-standards/
2427
func RestrictedPodSecurityContext() *corev1.PodSecurityContext {
2528
return &corev1.PodSecurityContext{
@@ -43,5 +46,12 @@ func RestrictedSecurityContext() *corev1.SecurityContext {
4346

4447
// Fail to start the container if its image runs as UID 0 (root).
4548
RunAsNonRoot: Bool(true),
49+
50+
// Restrict syscalls with RuntimeDefault seccomp.
51+
// Set this on the container-level to avoid interfering
52+
// with sidecars and injected containers.
53+
SeccompProfile: &corev1.SeccompProfile{
54+
Type: corev1.SeccompProfileTypeRuntimeDefault,
55+
},
4656
}
4757
}

internal/initialize/security_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ func TestRestrictedSecurityContext(t *testing.T) {
9797
"Containers must be required to run as non-root users.")
9898
}
9999

100-
assert.Assert(t, sc.SeccompProfile == nil,
101-
"The RuntimeDefault seccomp profile must be required, or allow specific additional profiles.")
100+
if assert.Check(t, sc.SeccompProfile != nil) {
101+
assert.Assert(t, sc.SeccompProfile.Type == "RuntimeDefault",
102+
"Seccomp profile must be explicitly set to one of the allowed values.")
103+
}
104+
102105
})
103106

104107
if assert.Check(t, sc.ReadOnlyRootFilesystem != nil) {

internal/pgadmin/reconcile_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ containers:
241241
privileged: false
242242
readOnlyRootFilesystem: true
243243
runAsNonRoot: true
244+
seccompProfile:
245+
type: RuntimeDefault
244246
volumeMounts:
245247
- mountPath: /etc/pgadmin
246248
name: pgadmin-startup
@@ -278,6 +280,8 @@ initContainers:
278280
privileged: false
279281
readOnlyRootFilesystem: true
280282
runAsNonRoot: true
283+
seccompProfile:
284+
type: RuntimeDefault
281285
volumeMounts:
282286
- mountPath: /etc/pgadmin
283287
name: pgadmin-startup
@@ -473,6 +477,8 @@ containers:
473477
privileged: false
474478
readOnlyRootFilesystem: true
475479
runAsNonRoot: true
480+
seccompProfile:
481+
type: RuntimeDefault
476482
volumeMounts:
477483
- mountPath: /etc/pgadmin
478484
name: pgadmin-startup
@@ -514,6 +520,8 @@ initContainers:
514520
privileged: false
515521
readOnlyRootFilesystem: true
516522
runAsNonRoot: true
523+
seccompProfile:
524+
type: RuntimeDefault
517525
volumeMounts:
518526
- mountPath: /etc/pgadmin
519527
name: pgadmin-startup

internal/pgbackrest/reconcile_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ func TestAddServerToInstancePod(t *testing.T) {
571571
privileged: false
572572
readOnlyRootFilesystem: true
573573
runAsNonRoot: true
574+
seccompProfile:
575+
type: RuntimeDefault
574576
volumeMounts:
575577
- mountPath: /etc/pgbackrest/server
576578
name: pgbackrest-server
@@ -617,6 +619,8 @@ func TestAddServerToInstancePod(t *testing.T) {
617619
privileged: false
618620
readOnlyRootFilesystem: true
619621
runAsNonRoot: true
622+
seccompProfile:
623+
type: RuntimeDefault
620624
volumeMounts:
621625
- mountPath: /etc/pgbackrest/server
622626
name: pgbackrest-server
@@ -701,6 +705,8 @@ func TestAddServerToRepoPod(t *testing.T) {
701705
privileged: false
702706
readOnlyRootFilesystem: true
703707
runAsNonRoot: true
708+
seccompProfile:
709+
type: RuntimeDefault
704710
volumeMounts:
705711
- mountPath: /etc/pgbackrest/server
706712
name: pgbackrest-server
@@ -743,6 +749,8 @@ func TestAddServerToRepoPod(t *testing.T) {
743749
privileged: false
744750
readOnlyRootFilesystem: true
745751
runAsNonRoot: true
752+
seccompProfile:
753+
type: RuntimeDefault
746754
volumeMounts:
747755
- mountPath: /etc/pgbackrest/server
748756
name: pgbackrest-server

internal/pgbouncer/reconcile_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ containers:
141141
privileged: false
142142
readOnlyRootFilesystem: true
143143
runAsNonRoot: true
144+
seccompProfile:
145+
type: RuntimeDefault
144146
volumeMounts:
145147
- mountPath: /etc/pgbouncer
146148
name: pgbouncer-config
@@ -169,6 +171,8 @@ containers:
169171
privileged: false
170172
readOnlyRootFilesystem: true
171173
runAsNonRoot: true
174+
seccompProfile:
175+
type: RuntimeDefault
172176
volumeMounts:
173177
- mountPath: /etc/pgbouncer
174178
name: pgbouncer-config
@@ -245,6 +249,8 @@ containers:
245249
privileged: false
246250
readOnlyRootFilesystem: true
247251
runAsNonRoot: true
252+
seccompProfile:
253+
type: RuntimeDefault
248254
volumeMounts:
249255
- mountPath: /etc/pgbouncer
250256
name: pgbouncer-config
@@ -278,6 +284,8 @@ containers:
278284
privileged: false
279285
readOnlyRootFilesystem: true
280286
runAsNonRoot: true
287+
seccompProfile:
288+
type: RuntimeDefault
281289
volumeMounts:
282290
- mountPath: /etc/pgbouncer
283291
name: pgbouncer-config
@@ -345,6 +353,8 @@ containers:
345353
privileged: false
346354
readOnlyRootFilesystem: true
347355
runAsNonRoot: true
356+
seccompProfile:
357+
type: RuntimeDefault
348358
volumeMounts:
349359
- mountPath: /etc/pgbouncer
350360
name: pgbouncer-config
@@ -377,6 +387,8 @@ containers:
377387
privileged: false
378388
readOnlyRootFilesystem: true
379389
runAsNonRoot: true
390+
seccompProfile:
391+
type: RuntimeDefault
380392
volumeMounts:
381393
- mountPath: /etc/pgbouncer
382394
name: pgbouncer-config

internal/postgres/reconcile_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ containers:
143143
privileged: false
144144
readOnlyRootFilesystem: true
145145
runAsNonRoot: true
146+
seccompProfile:
147+
type: RuntimeDefault
146148
volumeMounts:
147149
- mountPath: /pgconf/tls
148150
name: cert-volume
@@ -181,6 +183,8 @@ containers:
181183
privileged: false
182184
readOnlyRootFilesystem: true
183185
runAsNonRoot: true
186+
seccompProfile:
187+
type: RuntimeDefault
184188
volumeMounts:
185189
- mountPath: /pgconf/tls
186190
name: cert-volume
@@ -247,6 +251,8 @@ initContainers:
247251
privileged: false
248252
readOnlyRootFilesystem: true
249253
runAsNonRoot: true
254+
seccompProfile:
255+
type: RuntimeDefault
250256
volumeMounts:
251257
- mountPath: /pgconf/tls
252258
name: cert-volume

0 commit comments

Comments
 (0)