Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Glance services with glance user/group when possible #609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/glance/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func DBPurgeJob(
instance *glancev1.Glance,
cronSpec CronJobSpec,
) *batchv1.CronJob {
runAsUser := int64(0)
var config0644AccessMode int32 = 0644

cronCommand := fmt.Sprintf(
Expand Down Expand Up @@ -130,11 +129,9 @@ func DBPurgeJob(
Command: []string{
"/bin/bash",
},
Args: args,
VolumeMounts: cronJobVolumeMounts,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
},
Args: args,
VolumeMounts: cronJobVolumeMounts,
SecurityContext: BaseSecurityContext(),
},
},
Volumes: cronJobVolume,
Expand Down
2 changes: 1 addition & 1 deletion pkg/glance/dbsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func DbSyncJob(
},
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: glanceSecurityContext(),
SecurityContext: dbSyncSecurityContext(),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: dbSyncMounts,
},
Expand Down
42 changes: 36 additions & 6 deletions pkg/glance/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,51 @@ func GetOwningGlanceName(instance client.Object) string {
return ""
}

// glanceSecurityContext - currently used to make sure we don't run db-sync as
// dbSyncSecurityContext - currently used to make sure we don't run db-sync as
// root user
func glanceSecurityContext() *corev1.SecurityContext {
trueVal := true
func dbSyncSecurityContext() *corev1.SecurityContext {
runAsUser := int64(GlanceUID)
runAsGroup := int64(GlanceGID)

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

// 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
runAsUser := int64(GlanceUID)

return &corev1.SecurityContext{
RunAsUser: &runAsUser,
AllowPrivilegeEscalation: &falseVal,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
}

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

runAsUser := int64(GlanceUID)
return &corev1.SecurityContext{
RunAsUser: &runAsUser,
}
}
19 changes: 9 additions & 10 deletions pkg/glanceapi/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func StatefulSet(
privileged bool,
) (*appsv1.StatefulSet, error) {
runAsUser := int64(0)

var config0644AccessMode int32 = 0644

startupProbe := &corev1.Probe{
Expand Down Expand Up @@ -235,16 +236,14 @@ func StatefulSet(
"-c",
"/usr/bin/tail -n+1 -F " + LogFile + " 2>/dev/null",
},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
},
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: glance.GetLogVolumeMount(),
Resources: instance.Spec.Resources,
StartupProbe: startupProbe,
ReadinessProbe: readinessProbe,
LivenessProbe: livenessProbe,
Image: instance.Spec.ContainerImage,
SecurityContext: glance.BaseSecurityContext(),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: glance.GetLogVolumeMount(),
Resources: instance.Spec.Resources,
StartupProbe: startupProbe,
ReadinessProbe: readinessProbe,
LivenessProbe: livenessProbe,
},
{
Name: glance.ServiceName + "-httpd",
Expand Down
Loading