Skip to content

fix: set securityContext values for extractContent containers #3270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions pkg/controller/registry/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,6 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name, opmImg, utilImage, img s
},
}

// Determine the security context configuration
var securityContextConfig operatorsv1alpha1.SecurityConfig

// Use the user-provided security context config if it is defined
if source.Spec.GrpcPodConfig != nil && source.Spec.GrpcPodConfig.SecurityContextConfig != "" {
securityContextConfig = source.Spec.GrpcPodConfig.SecurityContextConfig
} else {
// Default to the defaultNamespace based and provided security context config
securityContextConfig = defaultSecurityConfig
}

// Apply the appropriate security context configuration
if securityContextConfig == operatorsv1alpha1.Restricted {
// Apply 'restricted' security settings
addSecurityContext(pod, runAsUser)
}

// Override scheduling options if specified
if source.Spec.GrpcPodConfig != nil {
grpcPodConfig := source.Spec.GrpcPodConfig
Expand Down Expand Up @@ -325,6 +308,23 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name, opmImg, utilImage, img s
}
}

// Determine the security context configuration
var securityContextConfig operatorsv1alpha1.SecurityConfig

// Use the user-provided security context config if it is defined
if source.Spec.GrpcPodConfig != nil && source.Spec.GrpcPodConfig.SecurityContextConfig != "" {
securityContextConfig = source.Spec.GrpcPodConfig.SecurityContextConfig
} else {
// Default to the defaultNamespace based and provided security context config
securityContextConfig = defaultSecurityConfig
}

// Apply the appropriate security context configuration
if securityContextConfig == operatorsv1alpha1.Restricted {
// Apply 'restricted' security settings
addSecurityContext(pod, runAsUser)
}

// Set priorityclass if its annotation exists
if prio, ok := podAnnotations[CatalogPriorityClassKey]; ok && prio != "" {
pod.Spec.PriorityClassName = prio
Expand All @@ -346,10 +346,25 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name, opmImg, utilImage, img s
}

func addSecurityContext(pod *corev1.Pod, runAsUser int64) {
pod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation = ptr.To(false)
pod.Spec.Containers[0].SecurityContext.Capabilities = &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
for i := range pod.Spec.InitContainers {
if pod.Spec.InitContainers[i].SecurityContext == nil {
pod.Spec.InitContainers[i].SecurityContext = &corev1.SecurityContext{}
}
pod.Spec.InitContainers[i].SecurityContext.AllowPrivilegeEscalation = ptr.To(false)
pod.Spec.InitContainers[i].SecurityContext.Capabilities = &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
}
}
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].SecurityContext == nil {
pod.Spec.Containers[i].SecurityContext = &corev1.SecurityContext{}
}
pod.Spec.Containers[i].SecurityContext.AllowPrivilegeEscalation = ptr.To(false)
pod.Spec.Containers[i].SecurityContext.Capabilities = &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
}
}

pod.Spec.SecurityContext = &corev1.PodSecurityContext{
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
Expand Down
231 changes: 221 additions & 10 deletions pkg/controller/registry/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,20 @@ func serviceAccount(namespace, name string) *corev1.ServiceAccount {

func TestPodExtractContent(t *testing.T) {
var testCases = []struct {
name string
input *v1alpha1.CatalogSource
expected *corev1.Pod
name string
input *v1alpha1.CatalogSource
securityContextConfig v1alpha1.SecurityConfig
expected *corev1.Pod
}{
{
name: "content extraction not requested",
name: "content extraction not requested - legacy security context config",
input: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
},
securityContextConfig: v1alpha1.Legacy,
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand Down Expand Up @@ -263,7 +265,7 @@ func TestPodExtractContent(t *testing.T) {
},
},
{
name: "content extraction expected",
name: "content extraction expected - legacy security context config",
input: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand All @@ -278,6 +280,7 @@ func TestPodExtractContent(t *testing.T) {
},
},
},
securityContextConfig: v1alpha1.Legacy,
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand Down Expand Up @@ -377,14 +380,222 @@ func TestPodExtractContent(t *testing.T) {
},
},
},
{
name: "content extraction not requested - restricted security context config",
input: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
},
securityContextConfig: v1alpha1.Restricted,
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "testns",
Labels: map[string]string{"olm.pod-spec-hash": "3sDLk8MMNptrqUfdnruY2gUi1g8O4wpMWC6Q52", "olm.managed": "true"},
Annotations: map[string]string{"cluster-autoscaler.kubernetes.io/safe-to-evict": "true"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "name",
Image: "image",
Ports: []corev1.ContainerPort{{Name: "grpc", ContainerPort: 50051}},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
InitialDelaySeconds: 0,
TimeoutSeconds: 5,
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
InitialDelaySeconds: 0,
TimeoutSeconds: 5,
},
StartupProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
FailureThreshold: 10,
PeriodSeconds: 10,
TimeoutSeconds: 5,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
},
ImagePullPolicy: image.InferImagePullPolicy("image"),
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
AllowPrivilegeEscalation: ptr.To(false),
ReadOnlyRootFilesystem: ptr.To(false),
},
TerminationMessagePolicy: "FallbackToLogsOnError",
},
},
NodeSelector: map[string]string{"kubernetes.io/os": "linux"},
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: ptr.To(int64(workloadUserID)),
RunAsNonRoot: ptr.To(true),
SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault},
},
ServiceAccountName: "service-account",
},
},
},
{
name: "content extraction expected - restricted security context config",
input: &v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
Spec: v1alpha1.CatalogSourceSpec{
GrpcPodConfig: &v1alpha1.GrpcPodConfig{
ExtractContent: &v1alpha1.ExtractContentConfig{
CacheDir: "/tmp/cache",
CatalogDir: "/catalog",
},
},
},
},
securityContextConfig: v1alpha1.Restricted,
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "testns",
Labels: map[string]string{"olm.pod-spec-hash": "1X4YqbfXuc9SB9ztW03WNOyanr9aIhKfijeBHH", "olm.managed": "true"},
Annotations: map[string]string{"cluster-autoscaler.kubernetes.io/safe-to-evict": "true"},
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "utilities",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
{
Name: "catalog-content",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
},
InitContainers: []corev1.Container{
{
Name: "extract-utilities",
Image: "utilImage",
Command: []string{"cp"},
Args: []string{"/bin/copy-content", "/utilities/copy-content"},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
AllowPrivilegeEscalation: ptr.To(false),
},
VolumeMounts: []corev1.VolumeMount{{Name: "utilities", MountPath: "/utilities"}},
TerminationMessagePolicy: "FallbackToLogsOnError",
},
{
Name: "extract-content",
Image: "image",
ImagePullPolicy: image.InferImagePullPolicy("image"),
Command: []string{"/utilities/copy-content"},
Args: []string{
"--catalog.from=/catalog",
"--catalog.to=/extracted-catalog/catalog",
"--cache.from=/tmp/cache",
"--cache.to=/extracted-catalog/cache",
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
AllowPrivilegeEscalation: ptr.To(false),
},
VolumeMounts: []corev1.VolumeMount{
{Name: "utilities", MountPath: "/utilities"},
{Name: "catalog-content", MountPath: "/extracted-catalog"},
},
TerminationMessagePolicy: "FallbackToLogsOnError",
},
},
Containers: []corev1.Container{
{
Name: "name",
Image: "opmImage",
Command: []string{"/bin/opm"},
Args: []string{"serve", "/extracted-catalog/catalog", "--cache-dir=/extracted-catalog/cache"},
Ports: []corev1.ContainerPort{{Name: "grpc", ContainerPort: 50051}},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
InitialDelaySeconds: 0,
TimeoutSeconds: 5,
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
InitialDelaySeconds: 0,
TimeoutSeconds: 5,
},
StartupProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=:50051"},
},
},
FailureThreshold: 10,
PeriodSeconds: 10,
TimeoutSeconds: 5,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
},
ImagePullPolicy: image.InferImagePullPolicy("image"),
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
AllowPrivilegeEscalation: ptr.To(false),
ReadOnlyRootFilesystem: ptr.To(false),
},
TerminationMessagePolicy: "FallbackToLogsOnError",
VolumeMounts: []corev1.VolumeMount{{Name: "catalog-content", MountPath: "/extracted-catalog"}},
},
},
NodeSelector: map[string]string{"kubernetes.io/os": "linux"},
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: ptr.To(int64(workloadUserID)),
RunAsNonRoot: ptr.To(true),
SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault},
},
ServiceAccountName: "service-account",
},
},
},
}

for _, testCase := range testCases {
pod, err := Pod(testCase.input, "name", "opmImage", "utilImage", "image", serviceAccount("", "service-account"), map[string]string{}, map[string]string{}, int32(0), int32(0), int64(workloadUserID), v1alpha1.Legacy)
require.NoError(t, err)
if diff := cmp.Diff(pod, testCase.expected); diff != "" {
t.Errorf("got incorrect pod: %v", diff)
}
t.Run(testCase.name, func(t *testing.T) {
pod, err := Pod(testCase.input, "name", "opmImage", "utilImage", "image", serviceAccount("", "service-account"), map[string]string{}, map[string]string{}, int32(0), int32(0), int64(workloadUserID), testCase.securityContextConfig)
require.NoError(t, err)
if diff := cmp.Diff(testCase.expected, pod); diff != "" {
t.Errorf("got incorrect pod: %v", diff)
}
})
}
}

Expand Down
Loading