Skip to content

Commit 427a4e4

Browse files
committed
Add SecurityContext for cronJob and remove kolla
We do not need kolla and the whole keystone config to run trust_flush. This patch adds two key items: 1. getCronJobVolumeMounts function to get volumes mounted to the destination path directly 2. BaseSecurityContext to run the resulting Pod as an unprivileged one Signed-off-by: Francesco Pantano <[email protected]>
1 parent b0e2bb6 commit 427a4e4

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

pkg/keystone/const.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const (
2828
KeystonePublicPort int32 = 5000
2929
// KeystoneInternalPort -
3030
KeystoneInternalPort int32 = 5000
31-
// Keystone UID based on kolla
31+
// KeystoneUID is based on kolla
3232
// https://github.com/openstack/kolla/blob/master/kolla/common/users.py
33-
KeystoneUID = 42425
33+
KeystoneUID int64 = 42425
3434
// DefaultFernetMaxActiveKeys -
3535
DefaultFernetMaxActiveKeys = 5
3636
// DefaultFernetRotationDays -

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/funcs.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

pkg/keystone/volumes.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,26 @@ 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+
}

0 commit comments

Comments
 (0)