Skip to content

Commit b82e146

Browse files
committed
Add securityContext runAsNonRoot on Postgres Statefullset if spiloRunAsUser is set and not root (#2081)
1 parent 920f3de commit b82e146

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

pkg/cluster/k8sres.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ func (c *Cluster) generatePodTemplate(
731731
securityContext := v1.PodSecurityContext{}
732732

733733
if spiloRunAsUser != nil {
734-
securityContext.RunAsUser = spiloRunAsUser
734+
var isNoRootPid = (*spiloRunAsUser > int64(0))
735+
securityContext.RunAsUser = spiloRunAsUser q
736+
securityContext.RunAsNonRoot = &isNoRootPid
735737
}
736738

737739
if spiloRunAsGroup != nil {

pkg/cluster/k8sres_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,3 +2837,100 @@ func TestGenerateCapabilities(t *testing.T) {
28372837
}
28382838
}
28392839
}
2840+
2841+
2842+
func TestRunPids(t *testing.T) {
2843+
client, _ := newFakeK8sTestClient()
2844+
clusterName := "acid-test-cluster"
2845+
namespace := "default"
2846+
spiloRunAsUser := int64(999)
2847+
spiloRunAsGroup := int64(100)
2848+
spiloFSGroup := int64(200)
2849+
2850+
pg := acidv1.Postgresql{
2851+
ObjectMeta: metav1.ObjectMeta{
2852+
Name: clusterName,
2853+
Namespace: namespace,
2854+
},
2855+
Spec: acidv1.PostgresSpec{
2856+
TeamID: "myapp", NumberOfInstances: 1,
2857+
Resources: &acidv1.Resources{
2858+
ResourceRequests: acidv1.ResourceDescription{CPU: "1", Memory: "10"},
2859+
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "10"},
2860+
},
2861+
Volume: acidv1.Volume{
2862+
Size: "1G",
2863+
},
2864+
},
2865+
}
2866+
2867+
var cluster = New(
2868+
Config{
2869+
OpConfig: config.Config{
2870+
PodManagementPolicy: "ordered_ready",
2871+
ProtectedRoles: []string{"admin"},
2872+
Resources: config.Resources{
2873+
SpiloRunAsUser: &spiloRunAsUser,
2874+
SpiloRunAsGroup: &spiloRunAsGroup,
2875+
SpiloFSGroup: &spiloFSGroup,
2876+
},
2877+
},
2878+
}, client, pg, logger, eventRecorder)
2879+
2880+
// create a statefulset
2881+
sts, err := cluster.createStatefulSet()
2882+
assert.NoError(t, err)
2883+
2884+
assert.Equal(t, &spiloRunAsUser, sts.Spec.Template.Spec.SecurityContext.RunAsUser, "has a RunAsUser assigned")
2885+
assert.Equal(t, &spiloRunAsGroup, sts.Spec.Template.Spec.SecurityContext.RunAsGroup, "has a RunAsGroup assigned")
2886+
assert.Equal(t, &spiloFSGroup, sts.Spec.Template.Spec.SecurityContext.FSGroup, "has a FSGroup assigned")
2887+
assert.Equal(t, true, *sts.Spec.Template.Spec.SecurityContext.RunAsNonRoot, "has the flag RunAsNonRoot")
2888+
}
2889+
2890+
func TestRunRootPids(t *testing.T) {
2891+
client, _ := newFakeK8sTestClient()
2892+
clusterName := "acid-test-cluster"
2893+
namespace := "default"
2894+
spiloRunAsUser := int64(0)
2895+
spiloRunAsGroup := int64(100)
2896+
spiloFSGroup := int64(200)
2897+
2898+
pg := acidv1.Postgresql{
2899+
ObjectMeta: metav1.ObjectMeta{
2900+
Name: clusterName,
2901+
Namespace: namespace,
2902+
},
2903+
Spec: acidv1.PostgresSpec{
2904+
TeamID: "myapp", NumberOfInstances: 1,
2905+
Resources: &acidv1.Resources{
2906+
ResourceRequests: acidv1.ResourceDescription{CPU: "1", Memory: "10"},
2907+
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "10"},
2908+
},
2909+
Volume: acidv1.Volume{
2910+
Size: "1G",
2911+
},
2912+
},
2913+
}
2914+
2915+
var cluster = New(
2916+
Config{
2917+
OpConfig: config.Config{
2918+
PodManagementPolicy: "ordered_ready",
2919+
ProtectedRoles: []string{"admin"},
2920+
Resources: config.Resources{
2921+
SpiloRunAsUser: &spiloRunAsUser,
2922+
SpiloRunAsGroup: &spiloRunAsGroup,
2923+
SpiloFSGroup: &spiloFSGroup,
2924+
},
2925+
},
2926+
}, client, pg, logger, eventRecorder)
2927+
2928+
// create a statefulset
2929+
sts, err := cluster.createStatefulSet()
2930+
assert.NoError(t, err)
2931+
2932+
assert.Equal(t, &spiloRunAsUser, sts.Spec.Template.Spec.SecurityContext.RunAsUser, "has a RunAsUser assigned")
2933+
assert.Equal(t, &spiloRunAsGroup, sts.Spec.Template.Spec.SecurityContext.RunAsGroup, "has a RunAsGroup assigned")
2934+
assert.Equal(t, &spiloFSGroup, sts.Spec.Template.Spec.SecurityContext.FSGroup, "has a FSGroup assigned")
2935+
assert.Equal(t, false, *sts.Spec.Template.Spec.SecurityContext.RunAsNonRoot, "has the flag RunAsNonRoot")
2936+
}

0 commit comments

Comments
 (0)