Skip to content

Commit 125187a

Browse files
Merge pull request openstack-k8s-operators#555 from fmount/keystone-user
Run keystone services using keystone user
2 parents e923458 + cad784f commit 125187a

File tree

9 files changed

+139
-47
lines changed

9 files changed

+139
-47
lines changed

pkg/keystone/const.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ const (
2828
KeystonePublicPort int32 = 5000
2929
// KeystoneInternalPort -
3030
KeystoneInternalPort int32 = 5000
31-
31+
// KeystoneUID is based on kolla
32+
// https://github.com/openstack/kolla/blob/master/kolla/common/users.py
33+
KeystoneUID int64 = 42425
3234
// DefaultFernetMaxActiveKeys -
3335
DefaultFernetMaxActiveKeys = 5
3436
// DefaultFernetRotationDays -
3537
DefaultFernetRotationDays = 1
38+
// DBSyncCommand -
39+
DBSyncCommand = "keystone-manage db_sync"
3640
)

pkg/keystone/cronjob.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const (
2828
// TrustFlushCommand -
29-
TrustFlushCommand = "/usr/local/bin/kolla_set_configs && keystone-manage trust_flush"
29+
TrustFlushCommand = "keystone-manage trust_flush"
3030
)
3131

3232
// CronJob func
@@ -35,7 +35,6 @@ func CronJob(
3535
labels map[string]string,
3636
annotations map[string]string,
3737
) *batchv1.CronJob {
38-
runAsUser := int64(0)
3938

4039
args := []string{"-c", TrustFlushCommand + instance.Spec.TrustFlushArgs}
4140

@@ -47,12 +46,12 @@ func CronJob(
4746

4847
// create Volume and VolumeMounts
4948
volumes := getVolumes(instance)
50-
volumeMounts := getVolumeMounts()
49+
volumeMounts := getCronJobVolumeMounts()
5150

5251
// add CA cert if defined
5352
if instance.Spec.TLS.CaBundleSecretName != "" {
5453
volumes = append(getVolumes(instance), instance.Spec.TLS.CreateVolume())
55-
volumeMounts = append(getVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
54+
volumeMounts = append(getCronJobVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
5655
}
5756

5857
cronjob := &batchv1.CronJob{
@@ -81,12 +80,10 @@ func CronJob(
8180
Command: []string{
8281
"/bin/bash",
8382
},
84-
Args: args,
85-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
86-
VolumeMounts: volumeMounts,
87-
SecurityContext: &corev1.SecurityContext{
88-
RunAsUser: &runAsUser,
89-
},
83+
Args: args,
84+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
85+
VolumeMounts: volumeMounts,
86+
SecurityContext: baseSecurityContext(),
9087
},
9188
},
9289
Volumes: volumes,

pkg/keystone/dbsync.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
2727

28-
const (
29-
// DBSyncCommand -
30-
DBSyncCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
31-
)
32-
3328
// DbSyncJob func
3429
func DbSyncJob(
3530
instance *keystonev1.KeystoneAPI,
3631
labels map[string]string,
3732
annotations map[string]string,
3833
) *batchv1.Job {
39-
runAsUser := int64(0)
4034

4135
args := []string{"-c", DBSyncCommand}
4236

@@ -46,13 +40,13 @@ func DbSyncJob(
4640

4741
// create Volume and VolumeMounts
4842
volumes := getVolumes(instance)
49-
volumeMounts := getVolumeMounts()
43+
volumeMounts := getDBSyncVolumeMounts()
5044

5145
// add CA cert if defined
5246
if instance.Spec.TLS.CaBundleSecretName != "" {
5347
//TODO(afaranha): Why not reuse the 'volumes'?
5448
volumes = append(getVolumes(instance), instance.Spec.TLS.CreateVolume())
55-
volumeMounts = append(getVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
49+
volumeMounts = append(getDBSyncVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
5650
}
5751

5852
job := &batchv1.Job{
@@ -75,13 +69,11 @@ func DbSyncJob(
7569
Command: []string{
7670
"/bin/bash",
7771
},
78-
Args: args,
79-
Image: instance.Spec.ContainerImage,
80-
SecurityContext: &corev1.SecurityContext{
81-
RunAsUser: &runAsUser,
82-
},
83-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
84-
VolumeMounts: volumeMounts,
72+
Args: args,
73+
Image: instance.Spec.ContainerImage,
74+
SecurityContext: dbSyncSecurityContext(),
75+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
76+
VolumeMounts: volumeMounts,
8577
},
8678
},
8779
Volumes: volumes,

pkg/keystone/deployment.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
const (
3434
// ServiceCommand -
35-
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
35+
ServiceCommand = "/usr/local/bin/kolla_start"
3636
)
3737

3838
// Deployment func
@@ -43,7 +43,6 @@ func Deployment(
4343
annotations map[string]string,
4444
topology *topologyv1.Topology,
4545
) (*appsv1.Deployment, error) {
46-
runAsUser := int64(0)
4746

4847
livenessProbe := &corev1.Probe{
4948
// TODO might need tuning
@@ -134,16 +133,14 @@ func Deployment(
134133
Command: []string{
135134
"/bin/bash",
136135
},
137-
Args: args,
138-
Image: instance.Spec.ContainerImage,
139-
SecurityContext: &corev1.SecurityContext{
140-
RunAsUser: &runAsUser,
141-
},
142-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
143-
VolumeMounts: volumeMounts,
144-
Resources: instance.Spec.Resources,
145-
ReadinessProbe: readinessProbe,
146-
LivenessProbe: livenessProbe,
136+
Args: args,
137+
Image: instance.Spec.ContainerImage,
138+
SecurityContext: httpdSecurityContext(),
139+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
140+
VolumeMounts: volumeMounts,
141+
Resources: instance.Spec.Resources,
142+
ReadinessProbe: readinessProbe,
143+
LivenessProbe: livenessProbe,
147144
},
148145
},
149146
},

pkg/keystone/funcs.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package keystone
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
"k8s.io/utils/ptr"
6+
)
7+
8+
// baseSecurityContext - currently used to make sure we don't run cronJob and Log
9+
// Pods as root user, and we drop privileges and Capabilities we don't need
10+
func baseSecurityContext() *corev1.SecurityContext {
11+
return &corev1.SecurityContext{
12+
RunAsUser: ptr.To(KeystoneUID),
13+
RunAsGroup: ptr.To(KeystoneUID),
14+
RunAsNonRoot: ptr.To(true),
15+
AllowPrivilegeEscalation: ptr.To(false),
16+
Capabilities: &corev1.Capabilities{
17+
Drop: []corev1.Capability{
18+
"ALL",
19+
},
20+
},
21+
}
22+
}
23+
24+
// dbSyncSecurityContext - currently used to make sure we don't run db-sync as
25+
// root user
26+
func dbSyncSecurityContext() *corev1.SecurityContext {
27+
return &corev1.SecurityContext{
28+
RunAsUser: ptr.To(KeystoneUID),
29+
RunAsGroup: ptr.To(KeystoneUID),
30+
Capabilities: &corev1.Capabilities{
31+
Drop: []corev1.Capability{
32+
"MKNOD",
33+
},
34+
},
35+
}
36+
}
37+
38+
// httpdSecurityContext -
39+
func httpdSecurityContext() *corev1.SecurityContext {
40+
return &corev1.SecurityContext{
41+
Capabilities: &corev1.Capabilities{
42+
Drop: []corev1.Capability{
43+
"MKNOD",
44+
},
45+
},
46+
RunAsUser: ptr.To(KeystoneUID),
47+
RunAsGroup: ptr.To(KeystoneUID),
48+
}
49+
}

pkg/keystone/volumes.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
func getVolumes(instance *keystonev1.KeystoneAPI) []corev1.Volume {
2626
name := instance.Name
2727
var scriptsVolumeDefaultMode int32 = 0755
28-
var config0640AccessMode int32 = 0640
28+
var config0640AccessMode int32 = 0644
2929

3030
fernetKeys := []corev1.KeyToPath{}
3131
numberKeys := int(*instance.Spec.FernetMaxActiveKeys)
@@ -121,3 +121,44 @@ func getVolumeMounts() []corev1.VolumeMount {
121121
},
122122
}
123123
}
124+
125+
// getCronJobVolumeMounts - cronjob volumeMounts
126+
func getCronJobVolumeMounts() []corev1.VolumeMount {
127+
return []corev1.VolumeMount{
128+
{
129+
Name: "config-data",
130+
MountPath: "/etc/keystone/keystone.conf",
131+
SubPath: "keystone.conf",
132+
ReadOnly: true,
133+
},
134+
{
135+
Name: "config-data",
136+
MountPath: "/etc/my.cnf",
137+
SubPath: "my.cnf",
138+
ReadOnly: true,
139+
},
140+
{
141+
Name: "fernet-keys",
142+
MountPath: "/etc/keystone/fernet-keys",
143+
ReadOnly: true,
144+
},
145+
}
146+
}
147+
148+
// getDBSyncVolumeMounts - cronjob volumeMounts
149+
func getDBSyncVolumeMounts() []corev1.VolumeMount {
150+
return []corev1.VolumeMount{
151+
{
152+
Name: "config-data",
153+
MountPath: "/etc/keystone/keystone.conf",
154+
SubPath: "keystone.conf",
155+
ReadOnly: true,
156+
},
157+
{
158+
Name: "config-data",
159+
MountPath: "/etc/my.cnf",
160+
SubPath: "my.cnf",
161+
ReadOnly: true,
162+
},
163+
}
164+
}

templates/keystoneapi/config/keystone-api-config.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@
1616
{
1717
"source": "/var/lib/config-data/default/httpd.conf",
1818
"dest": "/etc/httpd/conf/httpd.conf",
19-
"owner": "apache",
19+
"owner": "keystone:apache",
2020
"perm": "0644"
2121
},
2222
{
2323
"source": "/var/lib/config-data/default/ssl.conf",
2424
"dest": "/etc/httpd/conf.d/ssl.conf",
25-
"owner": "apache",
25+
"owner": "keystone:apache",
2626
"perm": "0644"
2727
},
2828
{
2929
"source": "/var/lib/config-data/tls/certs/*",
3030
"dest": "/etc/pki/tls/certs/",
31-
"owner": "root",
31+
"owner": "keystone:apache",
3232
"perm": "0640",
3333
"optional": true,
3434
"merge": true
3535
},
3636
{
3737
"source": "/var/lib/config-data/tls/private/*",
3838
"dest": "/etc/pki/tls/private/",
39-
"owner": "root",
39+
"owner": "keystone:apache",
4040
"perm": "0600",
4141
"optional": true,
4242
"merge": true
@@ -62,9 +62,21 @@
6262
{
6363
"source": "/var/lib/config-data/default/httpd_custom_*",
6464
"dest": "/etc/httpd/conf/",
65-
"owner": "apache",
65+
"owner": "keystone:apache",
6666
"perm": "0444",
6767
"optional": true
6868
}
69+
],
70+
"permissions": [
71+
{
72+
"path": "/etc/httpd",
73+
"owner": "keystone:apache",
74+
"recurse": true
75+
},
76+
{
77+
"path": "/var/log/keystone",
78+
"owner": "keystone:apache",
79+
"recurse": true
80+
}
6981
]
7082
}

tests/kuttl/common/assert_sample_deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ spec:
6161
containers:
6262
- args:
6363
- -c
64-
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
64+
- /usr/local/bin/kolla_start
6565
command:
6666
- /bin/bash
6767
imagePullPolicy: IfNotPresent

tests/kuttl/tests/keystone_tls/01-assert.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ spec:
3434
containers:
3535
- args:
3636
- -c
37-
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
37+
- /usr/local/bin/kolla_start
3838
volumeMounts:
3939
- mountPath: /usr/local/bin/container-scripts
4040
name: scripts
@@ -78,7 +78,7 @@ spec:
7878
secretName: keystone-scripts
7979
- name: config-data
8080
secret:
81-
defaultMode: 416
81+
defaultMode: 420
8282
secretName: keystone-config-data
8383
- name: fernet-keys
8484
secret:

0 commit comments

Comments
 (0)