Skip to content

Commit 07a96dc

Browse files
committed
Let secret-operator handle PKCS#12 conversion
Requires stackabletech/secret-operator#286
1 parent a0f4a8a commit 07a96dc

File tree

4 files changed

+14
-123
lines changed

4 files changed

+14
-123
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[workspace]
22
members = ["rust/crd", "rust/operator-binary"]
33

4-
#[patch."https://github.com/stackabletech/operator-rs.git"]
5-
#stackable-operator = { git = "https://github.com/stackabletech//operator-rs.git", branch = "main" }
4+
[patch."https://github.com/stackabletech/operator-rs.git"]
5+
# stackable-operator = { path = "../operator-rs" }
6+
stackable-operator = { git = "https://github.com/stackabletech//operator-rs.git", branch = "feature/secret-format" }
67

78
#[patch.crates-io]
89
# tokio-zookeeper = { git = "https://github.com/stackabletech/tokio-zookeeper.git", branch = "feature/tokio-modernize" }

rust/crd/src/security.rs

Lines changed: 8 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use crate::{
1212

1313
use snafu::{ResultExt, Snafu};
1414
use stackable_operator::{
15-
builder::{ContainerBuilder, PodBuilder, SecretOperatorVolumeSourceBuilder, VolumeBuilder},
15+
builder::{
16+
ContainerBuilder, PodBuilder, SecretFormat, SecretOperatorVolumeSourceBuilder,
17+
VolumeBuilder,
18+
},
1619
client::Client,
1720
commons::authentication::AuthenticationClassProvider,
1821
k8s_openapi::api::core::v1::Volume,
@@ -117,100 +120,26 @@ impl ZookeeperSecurity {
117120
}
118121
}
119122

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-
173123
/// Adds required volumes and volume mounts to the pod and container builders
174124
/// depending on the tls and authentication settings.
175125
pub fn add_volume_mounts(
176126
&self,
177127
pod_builder: &mut PodBuilder,
178-
cb_prepare: &mut ContainerBuilder,
179128
cb_zookeeper: &mut ContainerBuilder,
180129
) {
181130
let tls_secret_class = self.get_tls_secret_class();
182131

183132
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);
190133
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));
196135
}
197136

198137
// 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);
202139
pod_builder.add_volume(Self::create_tls_volume(
203-
"quorum-tls-mount",
140+
"quorum-tls",
204141
&self.quorum_secret_class,
205142
));
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-
);
214143
}
215144

216145
/// Returns required ZooKeeper configuration settings for the `zoo.cfg` properties file
@@ -331,47 +260,9 @@ impl ZookeeperSecurity {
331260
SecretOperatorVolumeSourceBuilder::new(secret_class_name)
332261
.with_pod_scope()
333262
.with_node_scope()
263+
.with_format(SecretFormat::TlsPkcs12)
334264
.build(),
335265
)
336266
.build()
337267
}
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-
}
377268
}

rust/operator-binary/src/zk_controller.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ fn build_server_rolegroup_statefulset(
632632
let mut pod_builder = PodBuilder::new();
633633

634634
// add volumes and mounts depending on tls / auth settings
635-
zookeeper_security.add_volume_mounts(&mut pod_builder, &mut cb_prepare, &mut cb_zookeeper);
635+
zookeeper_security.add_volume_mounts(&mut pod_builder, &mut cb_zookeeper);
636636

637637
let mut args = Vec::new();
638638

@@ -647,7 +647,6 @@ fn build_server_rolegroup_statefulset(
647647
));
648648
}
649649
args.extend(create_init_container_command_args());
650-
args.extend(zookeeper_security.commands());
651650

652651
let container_prepare = cb_prepare
653652
.image_from_product_image(resolved_product_image)

0 commit comments

Comments
 (0)