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 Sep 11, 2024
1 parent 50b7ba5 commit 4f61b6b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
6 changes: 6 additions & 0 deletions controllers/glanceapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,12 @@ func (r *GlanceAPIReconciler) reconcileNormal(
// we can mark the ServiceConfigReady as True and rollout the new pods
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)

// This is currently required because cleaner and pruner cronJobs
// mount the same pvc to clean data present in /var/lib/glance/image-cache
if len(instance.Spec.ImageCache.Size) > 0 {
privileged = true
}

// Define a new StatefuleSet object
deplDef, err := glanceapi.StatefulSet(instance,
inputHash,
Expand Down
33 changes: 30 additions & 3 deletions pkg/glance/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ func dbSyncSecurityContext() *corev1.SecurityContext {
// BaseSecurityContext - currently used to make sure we don't run cronJob and Log
// Pods as root user, and we drop privileges and Capabilities we don't need
func BaseSecurityContext() *corev1.SecurityContext {
falseVal := true
falseVal := false
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,
},
}
}
30 changes: 12 additions & 18 deletions pkg/glanceapi/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func StatefulSet(
annotations map[string]string,
privileged bool,
) (*appsv1.StatefulSet, error) {
runAsUser := int64(0)

userID := glance.GlanceUID
startupProbe := &corev1.Probe{
FailureThreshold: 6,
PeriodSeconds: 10,
Expand Down Expand Up @@ -220,16 +219,14 @@ func StatefulSet(
"-c",
string(GlanceServiceCommand),
},
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 @@ -243,12 +240,9 @@ func StatefulSet(
"-c",
string(GlanceServiceCommand),
},
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
16 changes: 13 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 @@ -68,6 +68,16 @@
"path": "/var/log/glance",
"owner": "glance:glance",
"recurse": true
},
{
"path": "/var/lib/glance",
"owner": "glance:glance",
"recurse": true
},
{
"path": "/etc/glance/glance.conf.d",
"owner": "glance:glance",
"recurse": true
}
]
}

0 comments on commit 4f61b6b

Please sign in to comment.