From 9db8cd1be03802823e2f4bfe28bb389ff5443f3c Mon Sep 17 00:00:00 2001 From: Travis Nielsen Date: Wed, 26 Oct 2022 16:34:20 -0600 Subject: [PATCH] security: run the crash collector as ceph user The crash collector does not have the command line arguments to run as ceph user id 167, so we set the security context to run as the ceph user in the main crash collector container. Signed-off-by: Travis Nielsen (cherry picked from commit df6d7af355440eab3fae7eea970f9196b5de125f) --- pkg/operator/ceph/cluster/crash/crash.go | 12 +++++++----- pkg/operator/ceph/cluster/crash/crash_test.go | 3 +++ pkg/operator/ceph/controller/spec.go | 12 +++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/operator/ceph/cluster/crash/crash.go b/pkg/operator/ceph/cluster/crash/crash.go index df9bd1aee..bf0a75306 100644 --- a/pkg/operator/ceph/cluster/crash/crash.go +++ b/pkg/operator/ceph/cluster/crash/crash.go @@ -258,11 +258,13 @@ func getCrashDaemonContainer(cephCluster cephv1.CephCluster, cephVersion cephver Command: []string{ "ceph-crash", }, - Image: cephImage, - Env: envVars, - VolumeMounts: volumeMounts, - Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources), - SecurityContext: controller.PodSecurityContext(), + Image: cephImage, + Env: envVars, + VolumeMounts: volumeMounts, + Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources), + // Initialize the security context with the ceph user since the ceph-crash script does not have an argument + // to run as the ceph user + SecurityContext: controller.CephSecurityContext(), } return container diff --git a/pkg/operator/ceph/cluster/crash/crash_test.go b/pkg/operator/ceph/cluster/crash/crash_test.go index 55e233b5e..efd99903e 100644 --- a/pkg/operator/ceph/cluster/crash/crash_test.go +++ b/pkg/operator/ceph/cluster/crash/crash_test.go @@ -25,6 +25,7 @@ import ( rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake" "github.com/rook/rook/pkg/client/clientset/versioned/scheme" "github.com/rook/rook/pkg/clusterd" + "github.com/rook/rook/pkg/operator/ceph/controller" cephver "github.com/rook/rook/pkg/operator/ceph/version" "github.com/rook/rook/pkg/operator/k8sutil" "github.com/rook/rook/pkg/operator/test" @@ -152,6 +153,8 @@ func TestCreateOrUpdateCephCrash(t *testing.T) { assert.Equal(t, tolerations, podSpec.Spec.Tolerations) assert.Equal(t, false, podSpec.Spec.HostNetwork) assert.Equal(t, "", podSpec.Spec.PriorityClassName) + assert.Equal(t, int64(controller.CephUserID), *podSpec.Spec.Containers[0].SecurityContext.RunAsUser) + assert.Equal(t, int64(controller.CephUserID), *podSpec.Spec.Containers[0].SecurityContext.RunAsGroup) cephCluster.Spec.Labels[cephv1.KeyCrashCollector] = map[string]string{"foo": "bar"} cephCluster.Spec.Network.HostNetwork = true diff --git a/pkg/operator/ceph/controller/spec.go b/pkg/operator/ceph/controller/spec.go index 853ab2747..66ce8ba5c 100644 --- a/pkg/operator/ceph/controller/spec.go +++ b/pkg/operator/ceph/controller/spec.go @@ -38,6 +38,7 @@ import ( kerrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" ) const ( @@ -53,7 +54,8 @@ const ( daemonTypeLabel = "ceph_daemon_type" ExternalMgrAppName = "rook-ceph-mgr-external" ServiceExternalMetricName = "http-external-metrics" - livenessProbeTimeoutSeconds int32 = 2 + CephUserID = 167 + livenessProbeTimeoutSeconds int32 = 5 livenessProbeInitialDelaySeconds int32 = 10 startupProbeFailuresDaemonDefault int32 = 6 // multiply by 10 = effective startup timeout // The OSD requires a long timeout in case the OSD is taking extra time to @@ -660,6 +662,14 @@ func PodSecurityContext() *v1.SecurityContext { } } +// PodSecurityContext detects if the pod needs privileges to run +func CephSecurityContext() *v1.SecurityContext { + context := PodSecurityContext() + context.RunAsUser = pointer.Int64(CephUserID) + context.RunAsGroup = pointer.Int64(CephUserID) + return context +} + // PrivilegedContext returns a privileged Pod security context func PrivilegedContext(runAsRoot bool) *v1.SecurityContext { privileged := true