Skip to content

Commit 2f9b4ee

Browse files
committed
CLOUDP-347194 - enable Pod Security Admission at restricted level
1 parent 58f3569 commit 2f9b4ee

File tree

14 files changed

+99
-11
lines changed

14 files changed

+99
-11
lines changed

controllers/operator/construct/database_construction_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func Test_buildDatabaseInitContainer(t *testing.T) {
4545
SecurityContext: &corev1.SecurityContext{
4646
ReadOnlyRootFilesystem: ptr.To(true),
4747
AllowPrivilegeEscalation: ptr.To(false),
48+
Capabilities: &corev1.Capabilities{
49+
Drop: []corev1.Capability{"ALL"},
50+
},
4851
},
4952
}
5053
assert.Equal(t, expectedContainer, container)

controllers/operator/construct/opsmanager_construction_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func Test_buildOpsManagerAndBackupInitContainer(t *testing.T) {
4646
SecurityContext: &corev1.SecurityContext{
4747
ReadOnlyRootFilesystem: ptr.To(true),
4848
AllowPrivilegeEscalation: ptr.To(false),
49+
Capabilities: &corev1.Capabilities{
50+
Drop: []corev1.Capability{"ALL"},
51+
},
4952
},
5053
}
5154
assert.Equal(t, expectedContainer, containerObj)

docker/mongodb-kubernetes-tests/kubetester/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ def delete_namespace(name: str):
273273
c = client.CoreV1Api()
274274
c.delete_namespace(name, body=c.V1DeleteOptions())
275275

276+
def label_namespace(name: str, labels: dict):
277+
body = {
278+
"metadata": {
279+
"labels": labels
280+
}
281+
}
282+
client.CoreV1Api().patch_namespace(name, body)
276283

277284
def get_deployments(namespace: str):
278285
return client.AppsV1Api().list_namespaced_deployment(namespace)

docker/mongodb-kubernetes-tests/kubetester/helm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def helm_template(
2828
args = ("helm", "template", *(command_args), _helm_chart_dir(helm_chart_path))
2929
logger.info(" ".join(args))
3030

31-
yaml_file_name = "{}.yaml".format(str(uuid.uuid4()))
31+
home = os.getenv("HOME")
32+
yaml_file_name = os.path.join(home, "{}.yaml".format(str(uuid.uuid4())))
3233
with open(yaml_file_name, "w") as output:
3334
process_run_and_check(" ".join(args), stdout=output, check=True, shell=True)
3435

docker/mongodb-kubernetes-tests/tests/opsmanager/om_ops_manager_backup_sharded_cluster.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
create_or_update_secret,
77
get_default_storage_class,
88
try_load,
9-
wait_until,
9+
wait_until, create_or_update_namespace, label_namespace,
1010
)
1111
from kubetester.awss3client import AwsS3Client
1212
from kubetester.kubetester import KubernetesTester, ensure_ent_version
@@ -53,6 +53,12 @@ def ops_manager(
5353
yaml_fixture("om_ops_manager_backup.yaml"), namespace=namespace
5454
)
5555

56+
# Change pod-security mode from warn to enforce. This will make test fail if operator and deployments don't support enforce mode
57+
label_namespace(namespace, {
58+
"pod-security.kubernetes.io/enforce": None,
59+
"pod-security.kubernetes.io/warn": "restricted"
60+
})
61+
5662
try_load(resource)
5763
return resource
5864

@@ -235,7 +241,7 @@ def test_om_failed_oplog_no_user_ref(self, ops_manager: MongoDBOpsManager):
235241
ops_manager.backup_status().assert_reaches_phase(
236242
Phase.Failed,
237243
msg_regexp=".*is configured to use SCRAM-SHA authentication mode, the user "
238-
"must be specified using 'mongodbUserRef'",
244+
"must be specified using 'mongodbUserRef'",
239245
)
240246

241247
def test_fix_om(self, ops_manager: MongoDBOpsManager, oplog_user: MongoDBUser):

helm_chart/templates/operator.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ spec:
3636
securityContext:
3737
runAsNonRoot: true
3838
runAsUser: 2000
39+
seccompProfile:
40+
type: RuntimeDefault
3941
{{- end }}
40-
{{- if .Values.registry.imagePullSecrets}}
42+
{{- if .Values.registry.imagePullSecrets }}
4143
imagePullSecrets:
4244
- name: {{ .Values.registry.imagePullSecrets }}
4345
{{- end }}
@@ -74,6 +76,13 @@ spec:
7476
requests:
7577
cpu: {{ .Values.operator.resources.requests.cpu }}
7678
memory: {{ .Values.operator.resources.requests.memory }}
79+
{{- if not .Values.managedSecurityContext }}
80+
securityContext:
81+
allowPrivilegeEscalation: false
82+
capabilities:
83+
drop:
84+
- ALL
85+
{{- end }}
7786
env:
7887
- name: OPERATOR_ENV
7988
value: {{ .Values.operator.env }}

mongodb-community-operator/pkg/kube/container/containers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,11 @@ func WithSecurityContext(context corev1.SecurityContext) Modification {
206206
func DefaultSecurityContext() corev1.SecurityContext {
207207
readOnlyRootFilesystem := true
208208
allowPrivilegeEscalation := false
209-
return corev1.SecurityContext{ReadOnlyRootFilesystem: &readOnlyRootFilesystem, AllowPrivilegeEscalation: &allowPrivilegeEscalation}
209+
return corev1.SecurityContext{
210+
ReadOnlyRootFilesystem: &readOnlyRootFilesystem,
211+
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
212+
Capabilities: &corev1.Capabilities{
213+
Drop: []corev1.Capability{"ALL"},
214+
},
215+
}
210216
}

mongodb-community-operator/pkg/kube/podtemplatespec/podspec_template.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ func DefaultPodSecurityContext() corev1.PodSecurityContext {
197197
runAsNonRoot := true
198198
runAsUser := int64(2000)
199199
fsGroup := int64(2000)
200-
return corev1.PodSecurityContext{RunAsUser: &runAsUser, RunAsNonRoot: &runAsNonRoot, FSGroup: &fsGroup}
200+
201+
return corev1.PodSecurityContext{
202+
RunAsUser: &runAsUser,
203+
RunAsNonRoot: &runAsNonRoot,
204+
FSGroup: &fsGroup,
205+
SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault},
206+
}
201207
}
202208

203209
// WithImagePullSecrets adds an ImagePullSecrets local reference with the given name

mongodb-community-operator/pkg/kube/podtemplatespec/podspec_template_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"k8s.io/utils/ptr"
78

89
corev1 "k8s.io/api/core/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -441,6 +442,12 @@ func TestMergeContainer(t *testing.T) {
441442
},
442443
},
443444
ReadinessProbe: otherDefaultContainer.ReadinessProbe,
445+
SecurityContext: &corev1.SecurityContext{
446+
AllowPrivilegeEscalation: ptr.To(false),
447+
Capabilities: &corev1.Capabilities{
448+
Drop: []corev1.Capability{"ALL"},
449+
},
450+
},
444451
}
445452
assert.Equal(t, secondExpected, mergedSpec.Spec.Containers[2])
446453
}

mongodb-community-operator/scripts/dev/e2e.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def _prepare_test_environment(namespace) -> None:
4545

4646
print("Creating Namespace")
4747
k8s_conditions.ignore_if_already_exists(
48-
lambda: corev1.create_namespace(client.V1Namespace(metadata=dict(name=namespace)))
48+
lambda: corev1.create_namespace(
49+
client.V1Namespace(metadata=dict(name=namespace, labels={"pod-security.kubernetes.io/warn": "restricted"}))
50+
)
4951
)
5052

5153
print("Creating Cluster Role Binding and Service Account for test pod")

0 commit comments

Comments
 (0)