Skip to content

Commit 510dce0

Browse files
committed
Only set container securityContext to a default value if the pod securityContext is also defaulted
1 parent 18e22d5 commit 510dce0

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/JobStepContext.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator.helpers;
@@ -513,6 +513,9 @@ V1SecurityContext getInitContainerSecurityContext() {
513513
if (isInitDomainOnPVRunAsRoot()) {
514514
return new V1SecurityContext().runAsGroup(0L).runAsUser(0L);
515515
}
516+
if (getServerSpec().getContainerSecurityContext() != null) {
517+
return getServerSpec().getContainerSecurityContext();
518+
}
516519
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
517520
return PodSecurityHelper.getDefaultContainerSecurityContext();
518521
}

operator/src/main/java/oracle/kubernetes/operator/helpers/PodHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, 2023, Oracle and/or its affiliates.
1+
// Copyright (c) 2017, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator.helpers;
@@ -572,6 +572,9 @@ EffectiveServerSpec getServerSpec() {
572572

573573
@Override
574574
V1SecurityContext getInitContainerSecurityContext() {
575+
if (getServerSpec().getContainerSecurityContext() != null) {
576+
return getServerSpec().getContainerSecurityContext();
577+
}
575578
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
576579
return PodSecurityHelper.getDefaultContainerSecurityContext();
577580
}
@@ -858,6 +861,9 @@ protected List<String> getContainerCommand() {
858861

859862
@Override
860863
V1SecurityContext getInitContainerSecurityContext() {
864+
if (getServerSpec().getContainerSecurityContext() != null) {
865+
return getServerSpec().getContainerSecurityContext();
866+
}
861867
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
862868
return PodSecurityHelper.getDefaultContainerSecurityContext();
863869
}

operator/src/main/java/oracle/kubernetes/weblogic/domain/model/BaseConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.weblogic.domain.model;
@@ -298,7 +298,13 @@ void setPodSecurityContext(V1PodSecurityContext podSecurityContext) {
298298
}
299299

300300
V1SecurityContext getContainerSecurityContext() {
301-
return Optional.ofNullable(serverPod.getContainerSecurityContext()).orElse(getDefaultContainerSecurityContext());
301+
return Optional.ofNullable(serverPod.getContainerSecurityContext())
302+
.orElseGet(() -> {
303+
if (serverPod.getPodSecurityContext() == null) {
304+
return getDefaultContainerSecurityContext();
305+
}
306+
return null;
307+
});
302308
}
303309

304310
void setContainerSecurityContext(V1SecurityContext containerSecurityContext) {

operator/src/test/java/oracle/kubernetes/weblogic/domain/model/DomainV2Test.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.weblogic.domain.model;
@@ -631,6 +631,26 @@ private void configureDomainWithPodSecurityContext(DomainResource domain) {
631631
configureAdminServer().withPodSecurityContext(new V1PodSecurityContext().runAsNonRoot(false));
632632
}
633633

634+
@Test
635+
void whenDefaultContainerSecurityContextConfiguredOnManagedServer() {
636+
V1SecurityContext ms1ContainerSecSpec =
637+
info.getServer(SERVER1, CLUSTER_NAME).getContainerSecurityContext();
638+
639+
assertThat(ms1ContainerSecSpec.getRunAsNonRoot(), is(true));
640+
assertThat(ms1ContainerSecSpec.getPrivileged(), is(false));
641+
assertThat(ms1ContainerSecSpec.getAllowPrivilegeEscalation(), is(false));
642+
assertThat(ms1ContainerSecSpec.getCapabilities().getDrop(), contains("ALL"));
643+
}
644+
645+
@Test
646+
void whenPodSecurityContextConfiguredNoDefaultContainerSecurityContextOnManagedServer() {
647+
configureDomainWithPodSecurityContext(domain);
648+
V1SecurityContext ms1ContainerSecSpec =
649+
info.getServer(SERVER1, CLUSTER_NAME).getContainerSecurityContext();
650+
651+
assertThat(ms1ContainerSecSpec, nullValue());
652+
}
653+
634654
@Test
635655
void whenContainerSecurityContextConfiguredOnManagedServerOverrideClusterAndDomain() {
636656
configureDomainWithContainerSecurityContext(domain);

0 commit comments

Comments
 (0)