From df6d7af355440eab3fae7eea970f9196b5de125f 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 --- pkg/operator/ceph/cluster/crash/crash.go | 4 +++- pkg/operator/ceph/cluster/crash/crash_test.go | 3 +++ pkg/operator/ceph/controller/spec.go | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/operator/ceph/cluster/crash/crash.go b/pkg/operator/ceph/cluster/crash/crash.go index 78a81f2fb..b5a191c15 100644 --- a/pkg/operator/ceph/cluster/crash/crash.go +++ b/pkg/operator/ceph/cluster/crash/crash.go @@ -265,7 +265,9 @@ func getCrashDaemonContainer(cephCluster cephv1.CephCluster, cephVersion cephver Env: envVars, VolumeMounts: volumeMounts, Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources), - SecurityContext: controller.PodSecurityContext(), + // 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 0ddd23c8b..7bbe5100d 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" @@ -151,6 +152,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 2beb6a187..a972c466f 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,6 +54,7 @@ const ( daemonTypeLabel = "ceph_daemon_type" ExternalMgrAppName = "rook-ceph-mgr-external" ServiceExternalMetricName = "http-external-metrics" + CephUserID = 167 livenessProbeTimeoutSeconds int32 = 5 livenessProbeInitialDelaySeconds int32 = 10 startupProbeFailuresDaemonDefault int32 = 6 // multiply by 10 = effective startup timeout @@ -656,6 +658,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