Skip to content

Commit 94bf45f

Browse files
Merge pull request #610 from fmount/sec_context
Run GlanceAPI with GlanceUID user
2 parents b19cd99 + a5ab01f commit 94bf45f

File tree

9 files changed

+78
-43
lines changed

9 files changed

+78
-43
lines changed

api/v1beta1/common_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type GlanceAPITemplate struct {
108108
APITimeout int `json:"apiTimeout,omitempty"`
109109
}
110110

111+
// Storage -
111112
type Storage struct {
112113
// +kubebuilder:validation:Optional
113114
// StorageClass -

api/v1beta1/glance_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
APIEdge = "edge"
3636
)
3737

38-
// GlanceSpec defines the desired state of Glance
38+
// GlanceSpecCore defines the desired state of Glance
3939
type GlanceSpecCore struct {
4040
// +kubebuilder:validation:Optional
4141
// +kubebuilder:default=glance

api/v1beta1/glance_webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ func (r *GlanceSpec) ValidateUpdate(old GlanceSpec, basePath *field.Path) field.
346346
return r.GlanceSpecCore.ValidateUpdate(old.GlanceSpecCore, basePath)
347347
}
348348

349+
// ValidateUpdate -
349350
func (r *GlanceSpecCore) ValidateUpdate(old GlanceSpecCore, basePath *field.Path) field.ErrorList {
350351
var allErrs field.ErrorList
351352

controllers/glanceapi_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,14 @@ func (r *GlanceAPIReconciler) reconcileNormal(
800800
// we can mark the ServiceConfigReady as True and rollout the new pods
801801
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)
802802

803+
// This is currently required because cleaner and pruner cronJobs
804+
// mount the same pvc to clean data present in /var/lib/glance/image-cache
805+
// TODO (fpantano) reference a Glance spec/proposal to move to a different
806+
// approach
807+
if len(instance.Spec.ImageCache.Size) > 0 {
808+
privileged = true
809+
}
810+
803811
// Define a new StatefuleSet object
804812
deplDef, err := glanceapi.StatefulSet(instance,
805813
inputHash,

pkg/glance/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const (
5454
GlanceInternalPort int32 = 9292
5555
// GlanceUID - https://github.com/openstack/kolla/blob/master/kolla/common/users.py
5656
GlanceUID int64 = 42415
57-
// GlanceGid - https://github.com/openstack/kolla/blob/master/kolla/common/users.py
57+
// GlanceGID - https://github.com/openstack/kolla/blob/master/kolla/common/users.py
5858
GlanceGID int64 = 42415
5959
// DefaultsConfigFileName -
6060
DefaultsConfigFileName = "00-config.conf"

pkg/glance/funcs.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package glance
22

33
import (
44
corev1 "k8s.io/api/core/v1"
5+
"k8s.io/utils/ptr"
56
"sigs.k8s.io/controller-runtime/pkg/client"
67
)
78

@@ -13,19 +14,16 @@ func GetOwningGlanceName(instance client.Object) string {
1314
return ownerRef.Name
1415
}
1516
}
16-
1717
return ""
1818
}
1919

2020
// dbSyncSecurityContext - currently used to make sure we don't run db-sync as
2121
// root user
2222
func dbSyncSecurityContext() *corev1.SecurityContext {
23-
runAsUser := int64(GlanceUID)
24-
runAsGroup := int64(GlanceGID)
2523

2624
return &corev1.SecurityContext{
27-
RunAsUser: &runAsUser,
28-
RunAsGroup: &runAsGroup,
25+
RunAsUser: ptr.To(GlanceUID),
26+
RunAsGroup: ptr.To(GlanceGID),
2927
Capabilities: &corev1.Capabilities{
3028
Drop: []corev1.Capability{
3129
"MKNOD",
@@ -40,12 +38,12 @@ func dbSyncSecurityContext() *corev1.SecurityContext {
4038
// BaseSecurityContext - currently used to make sure we don't run cronJob and Log
4139
// Pods as root user, and we drop privileges and Capabilities we don't need
4240
func BaseSecurityContext() *corev1.SecurityContext {
43-
falseVal := true
44-
runAsUser := int64(GlanceUID)
4541

4642
return &corev1.SecurityContext{
47-
RunAsUser: &runAsUser,
48-
AllowPrivilegeEscalation: &falseVal,
43+
RunAsUser: ptr.To(GlanceUID),
44+
RunAsGroup: ptr.To(GlanceGID),
45+
RunAsNonRoot: ptr.To(true),
46+
AllowPrivilegeEscalation: ptr.To(false),
4947
Capabilities: &corev1.Capabilities{
5048
Drop: []corev1.Capability{
5149
"ALL",
@@ -57,11 +55,32 @@ func BaseSecurityContext() *corev1.SecurityContext {
5755
}
5856
}
5957

58+
// APISecurityContext -
59+
func APISecurityContext(userID int64, privileged bool) *corev1.SecurityContext {
60+
61+
return &corev1.SecurityContext{
62+
AllowPrivilegeEscalation: ptr.To(true),
63+
RunAsUser: ptr.To(userID),
64+
Privileged: &privileged,
65+
SeccompProfile: &corev1.SeccompProfile{
66+
Type: corev1.SeccompProfileTypeRuntimeDefault,
67+
},
68+
}
69+
}
70+
6071
// HttpdSecurityContext -
6172
func HttpdSecurityContext() *corev1.SecurityContext {
6273

63-
runAsUser := int64(GlanceUID)
6474
return &corev1.SecurityContext{
65-
RunAsUser: &runAsUser,
75+
Capabilities: &corev1.Capabilities{
76+
Drop: []corev1.Capability{
77+
"MKNOD",
78+
},
79+
},
80+
RunAsUser: ptr.To(GlanceUID),
81+
RunAsGroup: ptr.To(GlanceGID),
82+
SeccompProfile: &corev1.SeccompProfile{
83+
Type: corev1.SeccompProfileTypeRuntimeDefault,
84+
},
6685
}
6786
}

pkg/glanceapi/cachejob.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ limitations under the License.
1616
package glanceapi
1717

1818
import (
19+
"fmt"
1920
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
2021
"github.com/openstack-k8s-operators/glance-operator/pkg/glance"
21-
22-
"fmt"
23-
2422
batchv1 "k8s.io/api/batch/v1"
2523
corev1 "k8s.io/api/core/v1"
2624
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/utils/ptr"
2726
)
2827

2928
// ImageCacheJob -
3029
func ImageCacheJob(
3130
instance *glancev1.GlanceAPI,
3231
cronSpec glance.CronJobSpec,
3332
) *batchv1.CronJob {
34-
runAsUser := int64(0)
3533
var config0644AccessMode int32 = 0644
3634

3735
cronCommand := fmt.Sprintf(
@@ -100,6 +98,9 @@ func ImageCacheJob(
10098
Completions: &completions,
10199
Template: corev1.PodTemplateSpec{
102100
Spec: corev1.PodSpec{
101+
SecurityContext: &corev1.PodSecurityContext{
102+
FSGroup: ptr.To(glance.GlanceUID),
103+
},
103104
Affinity: GetGlanceAPIPodAffinity(instance),
104105
Containers: []corev1.Container{
105106
{
@@ -108,11 +109,9 @@ func ImageCacheJob(
108109
Command: []string{
109110
"/bin/bash",
110111
},
111-
Args: args,
112-
VolumeMounts: cronJobVolumeMounts,
113-
SecurityContext: &corev1.SecurityContext{
114-
RunAsUser: &runAsUser,
115-
},
112+
Args: args,
113+
VolumeMounts: cronJobVolumeMounts,
114+
SecurityContext: glance.BaseSecurityContext(),
116115
},
117116
},
118117
Volumes: cronJobVolume,

pkg/glanceapi/statefulset.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ func StatefulSet(
5050
annotations map[string]string,
5151
privileged bool,
5252
) (*appsv1.StatefulSet, error) {
53-
runAsUser := int64(0)
54-
53+
userID := glance.GlanceUID
5554
startupProbe := &corev1.Probe{
5655
FailureThreshold: 6,
5756
PeriodSeconds: 10,
@@ -180,6 +179,9 @@ func StatefulSet(
180179
Labels: labels,
181180
},
182181
Spec: corev1.PodSpec{
182+
SecurityContext: &corev1.PodSecurityContext{
183+
FSGroup: &userID,
184+
},
183185
ServiceAccountName: instance.Spec.ServiceAccount,
184186
// When using Cinder we run as privileged, but also some
185187
// commands need to be run on the host using nsenter (eg:
@@ -220,16 +222,14 @@ func StatefulSet(
220222
"-c",
221223
string(GlanceServiceCommand),
222224
},
223-
Image: instance.Spec.ContainerImage,
224-
SecurityContext: &corev1.SecurityContext{
225-
RunAsUser: &runAsUser,
226-
},
227-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
228-
VolumeMounts: httpdVolumeMount,
229-
Resources: instance.Spec.Resources,
230-
StartupProbe: startupProbe,
231-
ReadinessProbe: readinessProbe,
232-
LivenessProbe: livenessProbe,
225+
Image: instance.Spec.ContainerImage,
226+
SecurityContext: glance.HttpdSecurityContext(),
227+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
228+
VolumeMounts: httpdVolumeMount,
229+
Resources: instance.Spec.Resources,
230+
StartupProbe: startupProbe,
231+
ReadinessProbe: readinessProbe,
232+
LivenessProbe: livenessProbe,
233233
},
234234
{
235235
Name: glance.ServiceName + "-api",
@@ -243,12 +243,9 @@ func StatefulSet(
243243
"-c",
244244
string(GlanceServiceCommand),
245245
},
246-
Image: instance.Spec.ContainerImage,
247-
SecurityContext: &corev1.SecurityContext{
248-
RunAsUser: &runAsUser,
249-
Privileged: &privileged,
250-
},
251-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
246+
Image: instance.Spec.ContainerImage,
247+
SecurityContext: glance.APISecurityContext(userID, privileged),
248+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
252249
VolumeMounts: append(glance.GetVolumeMounts(
253250
instance.Spec.CustomServiceConfigSecrets,
254251
privileged,

templates/glanceapi/config/glance-api-config.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
{
55
"source": "/var/lib/config-data/default/00-config.conf",
66
"dest": "/etc/glance/glance.conf.d/00-config.conf",
7-
"owner": "glance",
7+
"owner": "glance:glance",
88
"perm": "0600"
99
},
1010
{
1111
"source": "/var/lib/config-data/default/02-config.conf",
1212
"dest": "/etc/glance/glance.conf.d/02-config.conf",
13-
"owner": "glance",
13+
"owner": "glance:glance",
1414
"perm": "0600",
1515
"optional": true
1616
},
1717
{
1818
"source": "/var/lib/config-data/default/03-config.conf",
1919
"dest": "/etc/glance/glance.conf.d/03-config.conf",
20-
"owner": "glance",
20+
"owner": "glance:glance",
2121
"perm": "0640",
2222
"optional": true
2323
},
@@ -68,6 +68,16 @@
6868
"path": "/var/log/glance",
6969
"owner": "glance:glance",
7070
"recurse": true
71+
},
72+
{
73+
"path": "/var/lib/glance",
74+
"owner": "glance:glance",
75+
"recurse": true
76+
},
77+
{
78+
"path": "/etc/glance/glance.conf.d",
79+
"owner": "glance:glance",
80+
"recurse": true
7181
}
7282
]
7383
}

0 commit comments

Comments
 (0)