Skip to content

Commit

Permalink
Run GlanceAPI with GlanceUID user
Browse files Browse the repository at this point in the history
When the backend is not Cinder (Cinder still has to be fully tested),
GlanceAPI can reduce the permissions required for glance-api container,
and run as GlanceUID/GlanceGID. This patch introduces scc for both
glanceAPI and Httpd.

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Aug 20, 2024
1 parent c05e854 commit 3643661
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
31 changes: 29 additions & 2 deletions pkg/glance/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ func dbSyncSecurityContext() *corev1.SecurityContext {
// Pods as root user, and we drop privileges and Capabilities we don't need
func BaseSecurityContext() *corev1.SecurityContext {
falseVal := true
trueVal := true
runAsUser := int64(GlanceUID)
runAsGroup := int64(GlanceGID)

return &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsGroup: &runAsGroup,
RunAsNonRoot: &trueVal,
AllowPrivilegeEscalation: &falseVal,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
Expand All @@ -57,11 +61,34 @@ func BaseSecurityContext() *corev1.SecurityContext {
}
}

// APISecurityContext -
func APISecurityContext(userID int64, privileged bool) *corev1.SecurityContext {
runAsUser := int64(userID)
trueVal := true
return &corev1.SecurityContext{
AllowPrivilegeEscalation: &trueVal,
RunAsUser: &runAsUser,
Privileged: &privileged,
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
}

// HttpdSecurityContext -
func HttpdSecurityContext() *corev1.SecurityContext {

runAsUser := int64(GlanceUID)
runAsUser := int64(0)
falseVal := false
return &corev1.SecurityContext{
AllowPrivilegeEscalation: &falseVal,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsUser: &runAsUser,
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
}
36 changes: 16 additions & 20 deletions pkg/glanceapi/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (

const (
// GlanceAPIServiceCommand -
GlanceAPIServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
GlanceAPIServiceCommand = "/usr/local/bin/kolla_start"
// GlanceAPIHttpdCommand -
GlanceAPIHttpdCommand = "/usr/sbin/httpd -DFOREGROUND"
)
Expand All @@ -52,10 +52,11 @@ func StatefulSet(
annotations map[string]string,
privileged bool,
) (*appsv1.StatefulSet, error) {
runAsUser := int64(0)

userID := glance.GlanceUID
if privileged {
userID = int64(0)
}
var config0644AccessMode int32 = 0644

startupProbe := &corev1.Probe{
FailureThreshold: 6,
PeriodSeconds: 10,
Expand Down Expand Up @@ -257,16 +258,14 @@ func StatefulSet(
"-c",
string(GlanceAPIHttpdCommand),
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: httpdVolumeMount,
Resources: instance.Spec.Resources,
StartupProbe: startupProbe,
ReadinessProbe: readinessProbe,
LivenessProbe: livenessProbe,
Image: instance.Spec.ContainerImage,
SecurityContext: glance.HttpdSecurityContext(),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: httpdVolumeMount,
Resources: instance.Spec.Resources,
StartupProbe: startupProbe,
ReadinessProbe: readinessProbe,
LivenessProbe: livenessProbe,
},
{
Name: glance.ServiceName + "-api",
Expand All @@ -280,12 +279,9 @@ func StatefulSet(
"-c",
string(GlanceAPIServiceCommand),
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
Privileged: &privileged,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
Image: instance.Spec.ContainerImage,
SecurityContext: glance.APISecurityContext(userID, privileged),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: append(glance.GetVolumeMounts(
instance.Spec.CustomServiceConfigSecrets,
privileged,
Expand Down
11 changes: 8 additions & 3 deletions templates/glanceapi/config/glance-api-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
{
"source": "/var/lib/config-data/default/00-config.conf",
"dest": "/etc/glance/glance.conf.d/00-config.conf",
"owner": "glance",
"owner": "glance:glance",
"perm": "0600"
},
{
"source": "/var/lib/config-data/default/02-config.conf",
"dest": "/etc/glance/glance.conf.d/02-config.conf",
"owner": "glance",
"owner": "glance:glance",
"perm": "0600",
"optional": true
},
{
"source": "/var/lib/config-data/default/03-config.conf",
"dest": "/etc/glance/glance.conf.d/03-config.conf",
"owner": "glance",
"owner": "glance:glance",
"perm": "0640",
"optional": true
},
Expand Down Expand Up @@ -84,6 +84,11 @@
"path": "/var/log/glance",
"owner": "glance:glance",
"recurse": true
},
{
"path": "/etc/glance/glance.conf.d",
"owner": "glance:glance",
"recurse": true
}
]
}

0 comments on commit 3643661

Please sign in to comment.