Skip to content

Commit af248f3

Browse files
authored
Create a default security context for Coherence Pods (#764)
The Operator configures a default Pod securityContext if one is not configured in the Coherence resource spec
1 parent 5bc0ada commit af248f3

File tree

7 files changed

+71
-5
lines changed

7 files changed

+71
-5
lines changed

api/v1/coherenceresourcespec_types.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ type CoherenceResourceSpec struct {
193193
// +optional
194194
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
195195
// SecurityContext is the PodSecurityContext that will be added to all the Pods in this deployment.
196+
// If no security context is specified the Operator will create one with the following spec
197+
//
198+
// securityContext:
199+
// runAsNonRoot: true
200+
// runAsUser: 1000
201+
// runAsGroup: 2000
202+
// fsGroup: 2000
203+
// fsGroupChangePolicy: "OnRootMismatch"
204+
//
196205
// See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
197206
// +optional
198207
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
@@ -739,7 +748,7 @@ func (in *CoherenceResourceSpec) CreatePodTemplateSpec(deployment CoherenceResou
739748
ReadinessGates: in.ReadinessGates,
740749
RuntimeClassName: in.RuntimeClassName,
741750
SchedulerName: notNilString(in.SchedulerName),
742-
SecurityContext: in.SecurityContext,
751+
SecurityContext: in.GetSecurityContext(),
743752
ServiceAccountName: in.GetServiceAccountName(),
744753
ShareProcessNamespace: in.ShareProcessNamespace,
745754
Tolerations: in.Tolerations,
@@ -821,6 +830,24 @@ func (in *CoherenceResourceSpec) GetImagePullSecrets() []corev1.LocalObjectRefer
821830
return secrets
822831
}
823832

833+
// GetSecurityContext returns the Pod security context to use.
834+
func (in *CoherenceResourceSpec) GetSecurityContext() *corev1.PodSecurityContext {
835+
if in == nil || in.SecurityContext == nil {
836+
return DefaultSecurityContext()
837+
}
838+
return in.SecurityContext
839+
}
840+
841+
func DefaultSecurityContext() *corev1.PodSecurityContext {
842+
return &corev1.PodSecurityContext{
843+
RunAsNonRoot: ptr.To(DefaultRunAsNonRoot),
844+
RunAsUser: ptr.To(DefaultRunAsUser),
845+
RunAsGroup: ptr.To(DefaultRunAsGroup),
846+
FSGroup: ptr.To(DefaultFsGroup),
847+
FSGroupChangePolicy: ptr.To(DefaultFSGroupChangePolicy),
848+
}
849+
}
850+
824851
// GetServiceAccountName returns the service account name for the cluster.
825852
func (in *CoherenceResourceSpec) GetServiceAccountName() string {
826853
if in != nil {

api/v1/common_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ func createMinimalExpectedPodSpec(deployment coh.CoherenceResource) corev1.PodTe
503503
VolumeSource: emptyVolume,
504504
},
505505
},
506+
SecurityContext: coh.DefaultSecurityContext(),
506507
TopologySpreadConstraints: spec.EnsureTopologySpreadConstraints(deployment),
507508
Affinity: spec.CreateDefaultPodAffinity(deployment),
508509
ServiceAccountName: spec.GetServiceAccountName(),

api/v1/constants.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package v1
88

9-
import "github.com/oracle/coherence-operator/pkg/operator"
9+
import (
10+
"github.com/oracle/coherence-operator/pkg/operator"
11+
corev1 "k8s.io/api/core/v1"
12+
)
1013

1114
const (
1215
// DefaultReplicas is the default number of replicas that will be created for a deployment if no value is specified in the spec
@@ -74,6 +77,17 @@ const (
7477
// DefaultServiceAccount is the default k8s service account name.
7578
DefaultServiceAccount = "default"
7679

80+
// DefaultRunAsNonRoot is the default value for the runAsNonRoot field in the Pod security context
81+
DefaultRunAsNonRoot = true
82+
// DefaultRunAsUser is the default value for the runAsUser field in the Pod security context
83+
DefaultRunAsUser int64 = 1000
84+
// DefaultRunAsGroup is the default value for the runAsGroup field in the Pod security context
85+
DefaultRunAsGroup int64 = 2000
86+
// DefaultFsGroup is the default value for the fsGroup field in the Pod security context
87+
DefaultFsGroup int64 = DefaultRunAsGroup
88+
// DefaultFSGroupChangePolicy is the default value for the fsGroup field in the Pod security context
89+
DefaultFSGroupChangePolicy = corev1.FSGroupChangeOnRootMismatch
90+
7791
// ContainerNameCoherence is the Coherence container name
7892
ContainerNameCoherence = "coherence"
7993
// ContainerNameOperatorInit is the Operator init-container name

api/v1/create_statefulset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ func TestCreateStatefulSetWithTolerations(t *testing.T) {
405405

406406
func TestCreateStatefulSetWithSecurityContext(t *testing.T) {
407407
ctx := corev1.PodSecurityContext{
408-
RunAsUser: ptr.To(int64(1000)),
409-
RunAsNonRoot: boolPtr(true),
408+
RunAsUser: ptr.To(int64(5000)),
409+
RunAsNonRoot: boolPtr(false),
410410
}
411411

412412
spec := coh.CoherenceResourceSpec{

docs/about/04_coherence_spec.adoc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,16 @@ For example: +
332332
effect: "NoSchedule" + +
333333
+
334334
ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/ + m| []https://{k8s-doc-link}/#toleration-v1-core[corev1.Toleration] | false
335-
m| securityContext | SecurityContext is the PodSecurityContext that will be added to all the Pods in this deployment. See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ m| *https://{k8s-doc-link}/#podsecuritycontext-v1-core[corev1.PodSecurityContext] | false
335+
m| securityContext | SecurityContext is the PodSecurityContext that will be added to all the Pods in this deployment. If no security context is specified the Operator will create one with the following spec +
336+
+
337+
securityContext: + +
338+
runAsNonRoot: true + +
339+
runAsUser: 1000 + +
340+
runAsGroup: 2000 + +
341+
fsGroup: 2000 + +
342+
fsGroupChangePolicy: "OnRootMismatch" + +
343+
+
344+
See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ m| *https://{k8s-doc-link}/#podsecuritycontext-v1-core[corev1.PodSecurityContext] | false
336345
m| containerSecurityContext | ContainerSecurityContext is the SecurityContext that will be added to the Coherence container in each Pod in this deployment. See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ m| *https://{k8s-doc-link}/#securitycontext-v1-core[corev1.SecurityContext] | false
337346
m| shareProcessNamespace | Share a single process namespace between all the containers in a pod. When this is set containers will be able to view and signal processes from other containers in the same pod, and the first process in each container will not be assigned PID 1. HostPID and ShareProcessNamespace cannot both be set. Optional: Default to false. m| *bool | false
338347
m| hostIPC | Use the host's ipc namespace. Optional: Default to false. m| *bool | false

docs/other/045_security_context.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ Kubernetes allows you to configure a https://kubernetes.io/docs/tasks/configure-
1616
1717
For more details see the Kubernetes https://kubernetes.io/docs/tasks/configure-pod-container/security-context/[Security Context] documentation.
1818
19+
The Coherence Operator configures a default security context for the Coherence Pods is none is specified in the `Coherence` resource yaml.
20+
The default security context looks like this:
21+
[source,yaml]
22+
----
23+
securityContext:
24+
runAsNonRoot: true
25+
runAsUser: 1000
26+
runAsGroup: 2000
27+
fsGroup: 2000
28+
fsGroupChangePolicy: "OnRootMismatch"
29+
----
30+
31+
It is possible to override this as described below.
32+
1933
=== Setting the Pod Security Context
2034
2135
To specify security settings for a Pod, include the `securityContext` field in the Coherence resource specification.

test/e2e/remote/persistence-on-demand.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ spec:
1616
jvm:
1717
args:
1818
- "-Dcoherence.operator.health.logs=true"
19+
- "-Dcoherence.distributed.persistence.base.dir=/coherence"
1920
application:
2021
main: com.oracle.coherence.k8s.testing.RestServer
2122
ports:

0 commit comments

Comments
 (0)