@@ -12,7 +12,10 @@ use crate::{
12
12
13
13
use snafu:: { ResultExt , Snafu } ;
14
14
use stackable_operator:: {
15
- builder:: { ContainerBuilder , PodBuilder , SecretOperatorVolumeSourceBuilder , VolumeBuilder } ,
15
+ builder:: {
16
+ ContainerBuilder , PodBuilder , SecretFormat , SecretOperatorVolumeSourceBuilder ,
17
+ VolumeBuilder ,
18
+ } ,
16
19
client:: Client ,
17
20
commons:: authentication:: AuthenticationClassProvider ,
18
21
k8s_openapi:: api:: core:: v1:: Volume ,
@@ -117,100 +120,26 @@ impl ZookeeperSecurity {
117
120
}
118
121
}
119
122
120
- /// Returns required (init) container commands to generate keystores and truststores
121
- /// depending on the tls and authentication settings.
122
- pub fn commands ( & self ) -> Vec < String > {
123
- let mut args = vec ! [ ] ;
124
- // Quorum
125
- args. push ( Self :: generate_password ( Self :: STORE_PASSWORD_ENV ) ) ;
126
- args. extend ( Self :: create_key_and_trust_store_cmd (
127
- Self :: QUORUM_TLS_MOUNT_DIR ,
128
- Self :: QUORUM_TLS_DIR ,
129
- "quorum-tls" ,
130
- Self :: STORE_PASSWORD_ENV ,
131
- ) ) ;
132
- args. extend ( vec ! [
133
- Self :: write_store_password_to_config(
134
- Self :: SSL_QUORUM_KEY_STORE_PASSWORD ,
135
- STACKABLE_RW_CONFIG_DIR ,
136
- Self :: STORE_PASSWORD_ENV ,
137
- ) ,
138
- Self :: write_store_password_to_config(
139
- Self :: SSL_QUORUM_TRUST_STORE_PASSWORD ,
140
- STACKABLE_RW_CONFIG_DIR ,
141
- Self :: STORE_PASSWORD_ENV ,
142
- ) ,
143
- ] ) ;
144
-
145
- // server-tls or client-auth-tls (only the certificates specified are accepted)
146
- if self . tls_enabled ( ) {
147
- args. push ( Self :: generate_password ( Self :: STORE_PASSWORD_ENV ) ) ;
148
-
149
- args. extend ( Self :: create_key_and_trust_store_cmd (
150
- Self :: SERVER_TLS_MOUNT_DIR ,
151
- Self :: SERVER_TLS_DIR ,
152
- "server-tls" ,
153
- Self :: STORE_PASSWORD_ENV ,
154
- ) ) ;
155
-
156
- args. extend ( vec ! [
157
- Self :: write_store_password_to_config(
158
- Self :: SSL_KEY_STORE_PASSWORD ,
159
- STACKABLE_RW_CONFIG_DIR ,
160
- Self :: STORE_PASSWORD_ENV ,
161
- ) ,
162
- Self :: write_store_password_to_config(
163
- Self :: SSL_TRUST_STORE_PASSWORD ,
164
- STACKABLE_RW_CONFIG_DIR ,
165
- Self :: STORE_PASSWORD_ENV ,
166
- ) ,
167
- ] ) ;
168
- }
169
-
170
- args
171
- }
172
-
173
123
/// Adds required volumes and volume mounts to the pod and container builders
174
124
/// depending on the tls and authentication settings.
175
125
pub fn add_volume_mounts (
176
126
& self ,
177
127
pod_builder : & mut PodBuilder ,
178
- cb_prepare : & mut ContainerBuilder ,
179
128
cb_zookeeper : & mut ContainerBuilder ,
180
129
) {
181
130
let tls_secret_class = self . get_tls_secret_class ( ) ;
182
131
183
132
if let Some ( secret_class) = tls_secret_class {
184
- // mounts for secret volume
185
- cb_prepare. add_volume_mount ( "server-tls-mount" , Self :: SERVER_TLS_MOUNT_DIR ) ;
186
- cb_zookeeper. add_volume_mount ( "server-tls-mount" , Self :: SERVER_TLS_MOUNT_DIR ) ;
187
- pod_builder. add_volume ( Self :: create_tls_volume ( "server-tls-mount" , secret_class) ) ;
188
- // empty mount for trust and keystore
189
- cb_prepare. add_volume_mount ( "server-tls" , Self :: SERVER_TLS_DIR ) ;
190
133
cb_zookeeper. add_volume_mount ( "server-tls" , Self :: SERVER_TLS_DIR ) ;
191
- pod_builder. add_volume (
192
- VolumeBuilder :: new ( "server-tls" )
193
- . with_empty_dir ( Some ( "" ) , None )
194
- . build ( ) ,
195
- ) ;
134
+ pod_builder. add_volume ( Self :: create_tls_volume ( "server-tls" , secret_class) ) ;
196
135
}
197
136
198
137
// quorum
199
- // mounts for secret volume
200
- cb_prepare. add_volume_mount ( "quorum-tls-mount" , Self :: QUORUM_TLS_MOUNT_DIR ) ;
201
- cb_zookeeper. add_volume_mount ( "quorum-tls-mount" , Self :: QUORUM_TLS_MOUNT_DIR ) ;
138
+ cb_zookeeper. add_volume_mount ( "quorum-tls" , Self :: QUORUM_TLS_DIR ) ;
202
139
pod_builder. add_volume ( Self :: create_tls_volume (
203
- "quorum-tls-mount " ,
140
+ "quorum-tls" ,
204
141
& self . quorum_secret_class ,
205
142
) ) ;
206
- // empty mount for trust and keystore
207
- cb_prepare. add_volume_mount ( "quorum-tls" , Self :: QUORUM_TLS_DIR ) ;
208
- cb_zookeeper. add_volume_mount ( "quorum-tls" , Self :: QUORUM_TLS_DIR ) ;
209
- pod_builder. add_volume (
210
- VolumeBuilder :: new ( "quorum-tls" )
211
- . with_empty_dir ( Some ( "" ) , None )
212
- . build ( ) ,
213
- ) ;
214
143
}
215
144
216
145
/// Returns required ZooKeeper configuration settings for the `zoo.cfg` properties file
@@ -331,47 +260,9 @@ impl ZookeeperSecurity {
331
260
SecretOperatorVolumeSourceBuilder :: new ( secret_class_name)
332
261
. with_pod_scope ( )
333
262
. with_node_scope ( )
263
+ . with_format ( SecretFormat :: TlsPkcs12 )
334
264
. build ( ) ,
335
265
)
336
266
. build ( )
337
267
}
338
-
339
- /// Generates the shell script to retrieve a random 20 character password
340
- fn generate_password ( store_password_env_var : & str ) -> String {
341
- format ! (
342
- "export {store_password_env_var}=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 20 ; echo '')" ,
343
- )
344
- }
345
-
346
- /// Generates the shell script to append the generated password from `generate_password()`
347
- /// to the zoo.cfg to set key and truststore passwords.
348
- fn write_store_password_to_config (
349
- property : & str ,
350
- rw_conf_dir : & str ,
351
- store_password_env_var : & str ,
352
- ) -> String {
353
- format ! (
354
- "echo {property}=${store_password_env_var} >> {rw_conf_dir}/{ZOOKEEPER_PROPERTIES_FILE}" ,
355
- )
356
- }
357
-
358
- /// Generates the shell script to create key and trust stores from the certificates provided
359
- /// by the secret operator
360
- fn create_key_and_trust_store_cmd (
361
- mount_directory : & str ,
362
- stackable_directory : & str ,
363
- alias_name : & str ,
364
- store_password_env_var : & str ,
365
- ) -> Vec < String > {
366
- vec ! [
367
- format!( "echo [{stackable_directory}] Cleaning up truststore - just in case" ) ,
368
- format!( "rm -f {stackable_directory}/truststore.p12" ) ,
369
- format!( "echo [{stackable_directory}] Creating truststore" ) ,
370
- format!( "keytool -importcert -file {mount_directory}/ca.crt -keystore {stackable_directory}/truststore.p12 -storetype pkcs12 -noprompt -alias {alias_name} -storepass ${store_password_env_var}" ) ,
371
- format!( "echo [{stackable_directory}] Creating certificate chain" ) ,
372
- format!( "cat {mount_directory}/ca.crt {mount_directory}/tls.crt > {stackable_directory}/chain.crt" ) ,
373
- format!( "echo [{stackable_directory}] Creating keystore" ) ,
374
- format!( "openssl pkcs12 -export -in {stackable_directory}/chain.crt -inkey {mount_directory}/tls.key -out {stackable_directory}/keystore.p12 --passout pass:${store_password_env_var}" ) ,
375
- ]
376
- }
377
268
}
0 commit comments