Skip to content

Commit

Permalink
Run manila services using manila user/group
Browse files Browse the repository at this point in the history
This patch represents an improvment of the existing code to make sure we
run manila services using the manila user instead of root.
It also set the right SecurityContext on both dbsync and cronjobs.

Jira: https://issues.redhat.com/browse/OSPRH-9115

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Aug 27, 2024
1 parent 4d3b0d4 commit 6e9ffd5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 38 deletions.
8 changes: 8 additions & 0 deletions pkg/manila/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const (
// is to be generated, e.g. "manila_e5a4", "manila_78bc", etc
DatabaseUsernamePrefix = "manila"

// Manila's uid and gid magic numbers come from the 'manila-user' in
// https://github.com/openstack/kolla/blob/master/kolla/common/users.py

// ManilaUserID -
ManilaUserID int64 = 42429
// ManilaGroupID -
ManilaGroupID int64 = 42429

// ManilaPublicPort -
ManilaPublicPort int32 = 8786
// ManilaInternalPort -
Expand Down
2 changes: 1 addition & 1 deletion pkg/manila/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func CronJob(
},
Args: args,
VolumeMounts: cronJobVolumeMounts,
SecurityContext: GetManilaSecurityContext(),
SecurityContext: cronJobSecurityContext(),
},
},
Volumes: cronJobVolume,
Expand Down
15 changes: 6 additions & 9 deletions pkg/manila/dbsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const (
//DBSyncCommand -
DBSyncCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
DBSyncCommand = "/usr/local/bin/kolla_start"
)

// DbSyncJob func
Expand Down Expand Up @@ -75,7 +75,6 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations
dbSyncMounts = append(dbSyncMounts, instance.Spec.ManilaAPI.TLS.CreateVolumeMounts(nil)...)
}

runAsUser := int64(0)
envVars := map[string]env.Setter{}
envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS")
envVars["KOLLA_BOOTSTRAP"] = env.SetValue("TRUE")
Expand All @@ -100,13 +99,11 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations
Command: []string{
"/bin/bash",
},
Args: args,
Image: instance.Spec.ManilaAPI.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: dbSyncMounts,
Args: args,
Image: instance.Spec.ManilaAPI.ContainerImage,
SecurityContext: dbSyncSecurityContext(),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: dbSyncMounts,
},
},
Volumes: dbSyncVolume,
Expand Down
28 changes: 24 additions & 4 deletions pkg/manila/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func GetOwningManilaName(instance client.Object) string {
return ""
}

// GetManilaSecurityContext - Returns the right set of SecurityContext that
// cronJobSecurityContext - Returns the right set of SecurityContext that
// does not violate the k8s requirements
func GetManilaSecurityContext() *corev1.SecurityContext {
func cronJobSecurityContext() *corev1.SecurityContext {
falseVal := false
trueVal := true
runAsUser := int64(42429)
runAsGroup := int64(42429)
runAsUser := ManilaUserID
runAsGroup := ManilaGroupID
return &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsGroup: &runAsGroup,
Expand All @@ -42,6 +42,26 @@ func GetManilaSecurityContext() *corev1.SecurityContext {
}
}

// dbSyncSecurityContext - currently used to make sure we don't run db-sync as
// root user
func dbSyncSecurityContext() *corev1.SecurityContext {
runAsUser := ManilaUserID
runAsGroup := ManilaGroupID

return &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsGroup: &runAsGroup,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"MKNOD",
},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
}

// GetPodAffinity - Returns a corev1.Affinity reference for the specified component.
func GetPodAffinity(componentName string) *corev1.Affinity {
// If possible two pods of the same component (e.g manila-share) should not
Expand Down
8 changes: 4 additions & 4 deletions pkg/manilaapi/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

const (
// ServiceCommand -
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
ServiceCommand = "/usr/local/bin/kolla_start"
)

// StatefulSet func
Expand All @@ -37,7 +37,7 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) (*appsv1.StatefulSet, error) {
runAsUser := int64(0)
manilaUser := manila.ManilaUserID

livenessProbe := &corev1.Probe{
TimeoutSeconds: 10,
Expand Down Expand Up @@ -134,7 +134,7 @@ func StatefulSet(
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: &manilaUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: []corev1.VolumeMount{GetLogVolumeMount()},
Expand All @@ -154,7 +154,7 @@ func StatefulSet(
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: &manilaUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: volumeMounts,
Expand Down
11 changes: 4 additions & 7 deletions pkg/manilascheduler/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const (
// ServiceCommand -
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
ServiceCommand = "/usr/local/bin/kolla_start"
)

// StatefulSet func
Expand All @@ -35,11 +35,8 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
rootUser := int64(0)
// manila's uid and gid magic numbers come from the 'manila-user' in
// https://github.com/openstack/kolla/blob/master/kolla/common/users.py
manilaUser := int64(42429)
manilaGroup := int64(42429)
manilaUser := manila.ManilaUserID
manilaGroup := manila.ManilaGroupID

livenessProbe := &corev1.Probe{
// TODO might need tuning
Expand Down Expand Up @@ -115,7 +112,7 @@ func StatefulSet(
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &rootUser,
RunAsUser: &manilaUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: volumeMounts,
Expand Down
12 changes: 5 additions & 7 deletions pkg/manilashare/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const (
// ServiceCommand -
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
ServiceCommand = "/usr/local/bin/kolla_start"
)

// StatefulSet func
Expand All @@ -36,11 +36,9 @@ func StatefulSet(
annotations map[string]string,
) *appsv1.StatefulSet {
trueVar := true
rootUser := int64(0)
// Manila's uid and gid magic numbers come from the 'manila-user' in
// https://github.com/openstack/kolla/blob/master/kolla/common/users.py
manilaUser := int64(42429)
manilaGroup := int64(42429)

manilaUser := manila.ManilaUserID
manilaGroup := manila.ManilaGroupID

// TODO until we determine how to properly query for these
livenessProbe := &corev1.Probe{
Expand Down Expand Up @@ -129,7 +127,7 @@ func StatefulSet(
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &rootUser,
RunAsUser: &manilaUser,
Privileged: &trueVar,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
Expand Down
1 change: 1 addition & 0 deletions templates/manila/config/httpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-A
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
CustomLog /dev/stdout combined env=!forwarded
CustomLog /dev/stdout proxy env=forwarded
ErrorLog /dev/stdout

# XXX: To disable SSL
#Include conf.d/*.conf
Expand Down
11 changes: 8 additions & 3 deletions templates/manila/config/manila-api-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
{
"source": "/var/lib/config-data/default/ssl.conf",
"dest": "/etc/httpd/conf.d/ssl.conf",
"owner": "root",
"owner": "manila",
"perm": "0644"
},
{
"source": "/var/lib/config-data/tls/certs/*",
"dest": "/etc/pki/tls/certs/",
"owner": "root",
"owner": "manila",
"perm": "0640",
"optional": true,
"merge": true
},
{
"source": "/var/lib/config-data/tls/private/*",
"dest": "/etc/pki/tls/private/",
"owner": "root",
"owner": "manila",
"perm": "0600",
"optional": true,
"merge": true
Expand All @@ -41,6 +41,11 @@
"path": "/var/log/manila",
"owner": "manila:apache",
"recurse": true
},
{
"path": "/etc/httpd/run",
"owner": "manila:apache",
"recurse": true
}
]
}
6 changes: 3 additions & 3 deletions test/kuttl/tests/manila-tls/03-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ spec:
- --
- /bin/bash
- -c
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
- /usr/local/bin/kolla_start
command:
- /usr/bin/dumb-init
volumeMounts:
Expand Down Expand Up @@ -365,7 +365,7 @@ spec:
- --
- /bin/bash
- -c
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
- /usr/local/bin/kolla_start
command:
- /usr/bin/dumb-init
volumeMounts:
Expand Down Expand Up @@ -488,7 +488,7 @@ spec:
- --
- /bin/bash
- -c
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
- /usr/local/bin/kolla_start
command:
- /usr/bin/dumb-init
volumeMounts:
Expand Down

0 comments on commit 6e9ffd5

Please sign in to comment.