Skip to content

Commit 6acc868

Browse files
committed
Merge branch 'container-security' into 'main'
Only set container securityContext to a default value if the pod securityContext is also defaulted See merge request weblogic-cloud/weblogic-kubernetes-operator!4914
2 parents 6f8e2e4 + 2e4b66b commit 6acc868

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;
@@ -514,6 +514,9 @@ V1SecurityContext getInitContainerSecurityContext() {
514514
if (isInitDomainOnPVRunAsRoot()) {
515515
return new V1SecurityContext().runAsGroup(0L).runAsUser(0L);
516516
}
517+
if (getServerSpec().getContainerSecurityContext() != null) {
518+
return getServerSpec().getContainerSecurityContext();
519+
}
517520
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
518521
return PodSecurityHelper.getDefaultContainerSecurityContext();
519522
}

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, 2024, 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;
@@ -587,6 +587,9 @@ EffectiveServerSpec getServerSpec() {
587587

588588
@Override
589589
V1SecurityContext getInitContainerSecurityContext() {
590+
if (getServerSpec().getContainerSecurityContext() != null) {
591+
return getServerSpec().getContainerSecurityContext();
592+
}
590593
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
591594
return PodSecurityHelper.getDefaultContainerSecurityContext();
592595
}
@@ -895,6 +898,9 @@ protected List<String> getContainerCommand() {
895898

896899
@Override
897900
V1SecurityContext getInitContainerSecurityContext() {
901+
if (getServerSpec().getContainerSecurityContext() != null) {
902+
return getServerSpec().getContainerSecurityContext();
903+
}
898904
if (getPodSecurityContext().equals(PodSecurityHelper.getDefaultPodSecurityContext())) {
899905
return PodSecurityHelper.getDefaultContainerSecurityContext();
900906
}

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;
@@ -632,6 +632,26 @@ private void configureDomainWithPodSecurityContext(DomainResource domain) {
632632
configureAdminServer().withPodSecurityContext(new V1PodSecurityContext().runAsNonRoot(false));
633633
}
634634

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

0 commit comments

Comments
 (0)