Skip to content

Commit dab9448

Browse files
committed
refactor: remove hardcoded constants
1 parent 6a60deb commit dab9448

5 files changed

Lines changed: 54 additions & 35 deletions

File tree

rust/operator-binary/src/controller/build/authentication/ldap.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ use super::{
1010
AddLdapVolumesSnafu, ConstructLdapEndpointUrlSnafu, Error, MissingLdapBindCredentialsSnafu,
1111
};
1212
use crate::crd::{
13-
file_reference,
14-
security::{STACKABLE_TLS_DIR, TLS_STORE_PASSWORD, add_cert_to_trust_store_cmd},
13+
STACKABLE_TRUST_STORE_PASSWORD, file_reference,
14+
security::{STACKABLE_TLS_DIR, add_cert_to_trust_store_cmd},
1515
};
1616

17+
const LDAP_AUTHORIZER: &str = "LdapAuthorizer";
18+
1719
fn add_authenticator_config(
1820
provider: &ldap::v1alpha1::AuthenticationProvider,
1921
config: &mut BTreeMap<String, String>,
2022
) -> Result<(), Error> {
2123
config.insert(
2224
"druid.auth.authenticator.Ldap.type".to_string(),
23-
"basic".to_string(),
25+
super::AUTHENTICATOR_TYPE_BASIC.to_string(),
2426
);
2527
config.insert(
2628
"druid.auth.authenticator.Ldap.enableCacheNotifications".to_string(),
@@ -65,7 +67,7 @@ fn add_authenticator_config(
6567
);
6668
config.insert(
6769
"druid.auth.authenticator.Ldap.authorizerName".to_string(),
68-
"LdapAuthorizer".to_string(),
70+
LDAP_AUTHORIZER.to_string(),
6971
);
7072
super::set_authenticator_chain(config, &["Ldap"]);
7173

@@ -77,7 +79,7 @@ pub(super) fn generate_runtime_properties_config(
7779
config: &mut BTreeMap<String, String>,
7880
) -> Result<(), Error> {
7981
add_authenticator_config(provider, config)?;
80-
super::add_authorizer_config(config, "LdapAuthorizer");
82+
super::add_authorizer_config(config, LDAP_AUTHORIZER);
8183

8284
Ok(())
8385
}
@@ -90,7 +92,7 @@ pub(super) fn prepare_container_commands(
9092
command.extend(add_cert_to_trust_store_cmd(
9193
&tls_ca_cert_mount_path,
9294
STACKABLE_TLS_DIR,
93-
TLS_STORE_PASSWORD,
95+
STACKABLE_TRUST_STORE_PASSWORD,
9496
))
9597
}
9698
}

rust/operator-binary/src/controller/build/authentication/mod.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ pub mod oidc;
2626
// It seems this needs to be the same password for Druid to work, so we re-use the existing env variable.
2727
const ESCALATOR_INTERNAL_CLIENT_PASSWORD_ENV: &str = INTERNAL_INITIAL_CLIENT_PASSWORD_ENV;
2828

29+
// Authorizer/authenticator names and types used in the Druid runtime.properties auth config.
30+
// These are shared across the LDAP and OIDC providers (in the child modules).
31+
const DRUID_SYSTEM_AUTHORIZER: &str = "DruidSystemAuthorizer";
32+
const DRUID_SYSTEM_AUTHENTICATOR: &str = "DruidSystemAuthenticator";
33+
/// The `allowAll` authorizer type.
34+
const AUTHORIZER_TYPE_ALLOW_ALL: &str = "allowAll";
35+
/// The `basic` authenticator/escalator type.
36+
const AUTHENTICATOR_TYPE_BASIC: &str = "basic";
37+
2938
type Result<T, E = Error> = std::result::Result<T, E>;
3039

3140
#[derive(Snafu, Debug)]
@@ -61,11 +70,11 @@ pub enum Error {
6170
fn add_authorizer_config(config: &mut BTreeMap<String, String>, authorizer_name: &str) {
6271
config.insert(
6372
"druid.auth.authorizers".to_string(),
64-
format!(r#"["{authorizer_name}", "DruidSystemAuthorizer"]"#),
73+
format!(r#"["{authorizer_name}", "{DRUID_SYSTEM_AUTHORIZER}"]"#),
6574
);
6675
config.insert(
6776
format!("druid.auth.authorizer.{authorizer_name}.type"),
68-
"allowAll".to_string(),
77+
AUTHORIZER_TYPE_ALLOW_ALL.to_string(),
6978
);
7079
}
7180

@@ -75,7 +84,7 @@ fn set_authenticator_chain(
7584
config: &mut BTreeMap<String, String>,
7685
provider_authenticators: &[&str],
7786
) {
78-
let authenticators: Vec<String> = std::iter::once("DruidSystemAuthenticator")
87+
let authenticators: Vec<String> = std::iter::once(DRUID_SYSTEM_AUTHENTICATOR)
7988
.chain(provider_authenticators.iter().copied())
8089
.map(|name| format!("\"{name}\""))
8190
.collect();
@@ -114,7 +123,7 @@ fn generate_common_runtime_properties_config(config: &mut BTreeMap<String, Strin
114123

115124
config.insert(
116125
"druid.auth.authorizer.DruidSystemAuthorizer.type".to_string(),
117-
r#"allowAll"#.to_string(),
126+
AUTHORIZER_TYPE_ALLOW_ALL.to_string(),
118127
);
119128
}
120129

@@ -177,7 +186,7 @@ pub fn add_volumes_and_mounts(
177186
fn add_druid_system_authenticator_config(config: &mut BTreeMap<String, String>) {
178187
config.insert(
179188
"druid.auth.authenticator.DruidSystemAuthenticator.type".to_string(),
180-
"basic".to_string(),
189+
AUTHENTICATOR_TYPE_BASIC.to_string(),
181190
);
182191
config.insert(
183192
"druid.auth.authenticator.DruidSystemAuthenticator.credentialsValidator.type".to_string(),
@@ -191,7 +200,7 @@ fn add_druid_system_authenticator_config(config: &mut BTreeMap<String, String>)
191200
);
192201
config.insert(
193202
"druid.auth.authenticator.DruidSystemAuthenticator.authorizerName".to_string(),
194-
"DruidSystemAuthorizer".to_string(),
203+
DRUID_SYSTEM_AUTHORIZER.to_string(),
195204
);
196205
config.insert(
197206
"druid.auth.authenticator.DruidSystemAuthenticator.skipOnFailure".to_string(),
@@ -202,7 +211,10 @@ fn add_druid_system_authenticator_config(config: &mut BTreeMap<String, String>)
202211
/// Creates the escalator config: <https://druid.apache.org/docs/latest/operations/auth/#escalator>.
203212
/// This configures Druid processes to use the basic auth authentication added in `add_druid_system_authenticator_config` for internal communication.
204213
fn add_escalator_config(config: &mut BTreeMap<String, String>) {
205-
config.insert("druid.escalator.type".to_string(), "basic".to_string());
214+
config.insert(
215+
"druid.escalator.type".to_string(),
216+
AUTHENTICATOR_TYPE_BASIC.to_string(),
217+
);
206218
config.insert(
207219
"druid.escalator.internalClientUsername".to_string(),
208220
"druid_system".to_string(),
@@ -213,7 +225,7 @@ fn add_escalator_config(config: &mut BTreeMap<String, String>) {
213225
);
214226
config.insert(
215227
"druid.escalator.authorizerName".to_string(),
216-
"DruidSystemAuthorizer".to_string(),
228+
DRUID_SYSTEM_AUTHORIZER.to_string(),
217229
);
218230
}
219231

rust/operator-binary/src/controller/build/authentication/oidc.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::{
1717
internal_secret::env_var_from_secret,
1818
};
1919

20+
const OIDC_AUTHORIZER: &str = "OidcAuthorizer";
21+
2022
/// Creates OIDC authenticator config using the pac4j extension for Druid: <https://druid.apache.org/docs/latest/development/extensions-core/druid-pac4j>.
2123
fn add_authenticator_config(
2224
provider: &oidc::v1alpha1::AuthenticationProvider,
@@ -41,7 +43,7 @@ fn add_authenticator_config(
4143
);
4244
config.insert(
4345
"druid.auth.authenticator.Oidc.authorizerName".to_string(),
44-
r#"OidcAuthorizer"#.to_string(),
46+
OIDC_AUTHORIZER.to_string(),
4547
);
4648
config.insert(
4749
"druid.auth.pac4j.cookiePassphrase".to_string(),
@@ -94,7 +96,7 @@ pub(super) fn generate_runtime_properties_config(
9496
}
9597
_ => {
9698
add_authenticator_config(provider, oidc, config)?;
97-
super::add_authorizer_config(config, "OidcAuthorizer");
99+
super::add_authorizer_config(config, OIDC_AUTHORIZER);
98100
}
99101
}
100102
Ok(())

rust/operator-binary/src/controller/build/security.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ use stackable_operator::{
3030
};
3131

3232
use crate::crd::{
33-
DruidRole,
34-
security::{
35-
DruidTlsSecurity, PLAINTEXT_PORT_NAME, STACKABLE_TLS_DIR, TLS_PORT_NAME, TLS_STORE_PASSWORD,
36-
},
33+
DruidRole, STACKABLE_TRUST_STORE_PASSWORD, STACKABLE_TRUST_STORE_TYPE,
34+
security::{DruidTlsSecurity, PLAINTEXT_PORT_NAME, STACKABLE_TLS_DIR, TLS_PORT_NAME},
3735
};
3836

3937
#[derive(Snafu, Debug)]
@@ -85,8 +83,9 @@ const TLS_ALIAS_NAME: &str = "1";
8583
const AUTH_TRUST_STORE_PATH: &str = "druid.auth.basic.ssl.trustStorePath";
8684
const AUTH_TRUST_STORE_TYPE: &str = "druid.auth.basic.ssl.trustStoreType";
8785
const AUTH_TRUST_STORE_PASSWORD: &str = "druid.auth.basic.ssl.trustStorePassword";
88-
// Misc TLS
89-
const TLS_STORE_TYPE: &str = "pkcs12";
86+
// PKCS12 store file names (within the TLS store directory).
87+
const TRUST_STORE_FILE: &str = "truststore.p12";
88+
const KEY_STORE_FILE: &str = "keystore.p12";
9089

9190
// directories
9291
const STACKABLE_MOUNT_TLS_DIR: &str = "/stackable/mount_tls";
@@ -162,7 +161,7 @@ pub fn add_tls_volume_and_volume_mounts(
162161
secret_volume_source_builder
163162
.with_pod_scope()
164163
.with_format(SecretFormat::TlsPkcs12)
165-
.with_tls_pkcs12_password(TLS_STORE_PASSWORD)
164+
.with_tls_pkcs12_password(STACKABLE_TRUST_STORE_PASSWORD)
166165
.with_auto_tls_cert_lifetime(*requested_secret_lifetime);
167166

168167
if let Some(listener_scope) = &listener_scope {
@@ -254,10 +253,13 @@ fn add_pkcs12_store_properties(
254253
path_property.to_string(),
255254
format!("{store_directory}/{store_file}"),
256255
);
257-
config.insert(type_property.to_string(), TLS_STORE_TYPE.to_string());
256+
config.insert(
257+
type_property.to_string(),
258+
STACKABLE_TRUST_STORE_TYPE.to_string(),
259+
);
258260
config.insert(
259261
password_property.to_string(),
260-
TLS_STORE_PASSWORD.to_string(),
262+
STACKABLE_TRUST_STORE_PASSWORD.to_string(),
261263
);
262264
}
263265

@@ -271,7 +273,7 @@ fn add_tls_encryption_config_properties(
271273
add_pkcs12_store_properties(
272274
config,
273275
store_directory,
274-
"truststore.p12",
276+
TRUST_STORE_FILE,
275277
CLIENT_HTTPS_TRUST_STORE_PATH,
276278
CLIENT_HTTPS_TRUST_STORE_TYPE,
277279
CLIENT_HTTPS_TRUST_STORE_PASSWORD,
@@ -280,7 +282,7 @@ fn add_tls_encryption_config_properties(
280282
add_pkcs12_store_properties(
281283
config,
282284
store_directory,
283-
"keystore.p12",
285+
KEY_STORE_FILE,
284286
SERVER_HTTPS_KEY_STORE_PATH,
285287
SERVER_HTTPS_KEY_STORE_TYPE,
286288
SERVER_HTTPS_KEY_STORE_PASSWORD,
@@ -292,7 +294,7 @@ fn add_tls_encryption_config_properties(
292294
add_pkcs12_store_properties(
293295
config,
294296
store_directory,
295-
"truststore.p12",
297+
TRUST_STORE_FILE,
296298
AUTH_TRUST_STORE_PATH,
297299
AUTH_TRUST_STORE_TYPE,
298300
AUTH_TRUST_STORE_PASSWORD,
@@ -307,7 +309,7 @@ fn add_tls_auth_config_properties(
307309
add_pkcs12_store_properties(
308310
config,
309311
store_directory,
310-
"keystore.p12",
312+
KEY_STORE_FILE,
311313
CLIENT_HTTPS_KEY_STORE_PATH,
312314
CLIENT_HTTPS_KEY_STORE_TYPE,
313315
CLIENT_HTTPS_KEY_STORE_PASSWORD,
@@ -319,7 +321,7 @@ fn add_tls_auth_config_properties(
319321
// javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
320322
config.insert(
321323
CLIENT_HTTPS_KEY_MANAGER_PASSWORD.to_string(),
322-
TLS_STORE_PASSWORD.to_string(),
324+
STACKABLE_TRUST_STORE_PASSWORD.to_string(),
323325
);
324326
config.insert(CLIENT_HTTPS_CERT_ALIAS.to_string(), store_alias.to_string());
325327
// FIXME: https://github.com/stackabletech/druid-operator/issues/372
@@ -338,7 +340,7 @@ fn add_tls_auth_config_properties(
338340
add_pkcs12_store_properties(
339341
config,
340342
store_directory,
341-
"truststore.p12",
343+
TRUST_STORE_FILE,
342344
SERVER_HTTPS_TRUST_STORE_PATH,
343345
SERVER_HTTPS_TRUST_STORE_TYPE,
344346
SERVER_HTTPS_TRUST_STORE_PASSWORD,
@@ -350,7 +352,7 @@ fn add_tls_auth_config_properties(
350352
// javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
351353
config.insert(
352354
SERVER_HTTPS_KEY_MANAGER_PASSWORD.to_string(),
353-
TLS_STORE_PASSWORD.to_string(),
355+
STACKABLE_TRUST_STORE_PASSWORD.to_string(),
354356
);
355357
// FIXME: https://github.com/stackabletech/druid-operator/issues/372
356358
// This is required because the client will send its pod ip which is not in the SANs of the certificates
@@ -369,10 +371,12 @@ pub fn build_tls_key_stores_cmd(tls: &DruidTlsSecurity) -> Vec<String> {
369371
// FIXME: *Technically* we should only add the system truststore in case any webPki usage is detected,
370372
// wether that's in S3, LDAP, OIDC, FTE or whatnot.
371373
format!(
372-
"cert-tools generate-pkcs12-truststore --pkcs12 '{STACKABLE_MOUNT_TLS_DIR}/truststore.p12:{TLS_STORE_PASSWORD}' --pem /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem --out {STACKABLE_TLS_DIR}/truststore.p12 --out-password '{TLS_STORE_PASSWORD}'"
374+
"cert-tools generate-pkcs12-truststore --pkcs12 '{STACKABLE_MOUNT_TLS_DIR}/{TRUST_STORE_FILE}:{STACKABLE_TRUST_STORE_PASSWORD}' --pem /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem --out {STACKABLE_TLS_DIR}/{TRUST_STORE_FILE} --out-password '{STACKABLE_TRUST_STORE_PASSWORD}'"
373375
),
374376
// We can copy the keystore as is.
375-
format!("cp {STACKABLE_MOUNT_TLS_DIR}/keystore.p12 {STACKABLE_TLS_DIR}/keystore.p12"),
377+
format!(
378+
"cp {STACKABLE_MOUNT_TLS_DIR}/{KEY_STORE_FILE} {STACKABLE_TLS_DIR}/{KEY_STORE_FILE}"
379+
),
376380
]
377381
}
378382

rust/operator-binary/src/crd/security.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub const PLAINTEXT_PORT_NAME: &str = "http";
1919
pub const TLS_PORT_NAME: &str = "https";
2020

2121
// Misc TLS (shared with the build-side renderer and the LDAP authentication module)
22-
pub const TLS_STORE_PASSWORD: &str = "changeit";
2322
pub const STACKABLE_TLS_DIR: &str = "/stackable/tls";
2423

2524
pub const INTERNAL_INITIAL_CLIENT_PASSWORD_ENV: &str = "INTERNAL_INITIAL_CLIENT_PASSWORD";

0 commit comments

Comments
 (0)