@@ -18,14 +18,6 @@ package keystone
1818import (
1919 "encoding/base64"
2020 "math/rand"
21- "strconv"
22-
23- keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
24- "github.com/openstack-k8s-operators/lib-common/modules/common/env"
25-
26- batchv1 "k8s.io/api/batch/v1"
27- corev1 "k8s.io/api/core/v1"
28- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2921)
3022
3123// GenerateFernetKey -
@@ -36,141 +28,3 @@ func GenerateFernetKey() string {
3628 }
3729 return base64 .StdEncoding .EncodeToString (data )
3830}
39-
40- const (
41- // FernetRotationCommand -
42- FernetRotationCommand = `
43- echo $(date -u) Starting...
44- case $MAX_ACTIVE_KEYS in
45- ''|*[!0-9]*)
46- echo "MAX_ACTIVE_KEYS is not a number, exiting."
47- exit 1
48- ;;
49- [01])
50- echo "MAX_ACTIVE_KEYS ($MAX_ACTIVE_KEYS) -lt 2, exiting."
51- exit 1
52- esac
53-
54- cd /var/lib/fernet-keys
55- mkdir /tmp/keys
56- for file in [0-9]*;
57- do
58- cat "$file" > /tmp/keys/"${file}"
59- done
60-
61- cd /tmp/keys
62-
63- number_of_keys=$(ls -1 | wc -l)
64- max_key=$(ls -1 | sort -n | tail -1)
65-
66- if [ $((number_of_keys - 1)) != $max_key ]; then
67- echo "Corrupted FernetKeys secret, exiting."
68- exit 1
69- fi
70-
71- mv 0 $((max_key + 1))
72- dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64 > 0
73-
74- while [ -f "$MAX_ACTIVE_KEYS" ]; do
75- i=2
76- while [ -f "$i" ]; do
77- mv $i $((i-1))
78- i=$((i+1))
79- done
80- done
81-
82- echo '{"stringData": {' > /tmp/patch_file.json
83- i=0
84- while [ -f "$((i+1))" ]; do
85- echo '"FernetKeys'$i'": "'$(cat $i)'",' >> /tmp/patch_file.json
86- i=$((i+1))
87- done
88- echo '"FernetKeys'$i'": "'$(cat $i)'"' >> /tmp/patch_file.json
89- echo '}}' >> /tmp/patch_file.json
90-
91- kubectl patch secret -n $NAMESPACE $SECRET_NAME \
92- --patch-file=/tmp/patch_file.json
93- echo $(date -u) $((i+1)) keys rotated.
94- `
95- )
96-
97- // FernetCronJob func
98- func FernetCronJob (
99- keystoneapiinstance * keystonev1.KeystoneAPI ,
100- labels map [string ]string ,
101- annotations map [string ]string ,
102- ) * batchv1.CronJob {
103- instance := & keystonev1.KeystoneAPIFernet {KeystoneAPI : keystoneapiinstance }
104- runAsUser := int64 (0 )
105- suspend := false
106- successfulJobsHistoryLimit := int32 (3 )
107- failedJobsHistoryLimit := int32 (1 )
108-
109- args := []string {"-c" , FernetRotationCommand }
110-
111- envVars := map [string ]env.Setter {}
112- envVars ["KOLLA_CONFIG_STRATEGY" ] = env .SetValue ("COPY_ALWAYS" )
113- envVars ["SECRET_NAME" ] = env .SetValue (ServiceName )
114- envVars ["MAX_ACTIVE_KEYS" ] = env .SetValue (
115- strconv .Itoa (int (* instance .Spec .FernetMaxActiveKeys )))
116-
117- backoffLimit := int32 (0 )
118- parallelism := int32 (1 )
119- completions := int32 (1 )
120-
121- // create Volume and VolumeMounts
122- volumes := getVolumes (keystoneapiinstance )
123- volumeMounts := getVolumeMounts ()
124-
125- cronjob := & batchv1.CronJob {
126- ObjectMeta : metav1.ObjectMeta {
127- Name : ServiceName + "-fernet-cronjob" ,
128- Namespace : instance .Namespace ,
129- },
130- Spec : batchv1.CronJobSpec {
131- Schedule : instance .Spec .FernetRotationSchedule ,
132- Suspend : & suspend ,
133- ConcurrencyPolicy : batchv1 .ForbidConcurrent ,
134- SuccessfulJobsHistoryLimit : & successfulJobsHistoryLimit ,
135- FailedJobsHistoryLimit : & failedJobsHistoryLimit ,
136- JobTemplate : batchv1.JobTemplateSpec {
137- ObjectMeta : metav1.ObjectMeta {
138- Annotations : annotations ,
139- Labels : labels ,
140- },
141- Spec : batchv1.JobSpec {
142- BackoffLimit : & backoffLimit ,
143- Parallelism : & parallelism ,
144- Completions : & completions ,
145- Template : corev1.PodTemplateSpec {
146- Spec : corev1.PodSpec {
147- Containers : []corev1.Container {
148- {
149- Name : ServiceName + "-fernet-job" ,
150- Image : instance .Spec .FernetRotationContainerImage ,
151- Command : []string {
152- "/bin/bash" ,
153- },
154- Args : args ,
155- Env : env .MergeEnvs ([]corev1.EnvVar {}, envVars ),
156- VolumeMounts : volumeMounts ,
157- SecurityContext : & corev1.SecurityContext {
158- RunAsUser : & runAsUser ,
159- },
160- },
161- },
162- Volumes : volumes ,
163- RestartPolicy : corev1 .RestartPolicyNever ,
164- ServiceAccountName : instance .RbacResourceName (),
165- },
166- },
167- },
168- },
169- },
170- }
171- if instance .Spec .NodeSelector != nil && len (instance .Spec .NodeSelector ) > 0 {
172- cronjob .Spec .JobTemplate .Spec .Template .Spec .NodeSelector = instance .Spec .NodeSelector
173- }
174-
175- return cronjob
176- }
0 commit comments