-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoidc.rs
More file actions
247 lines (226 loc) · 8.56 KB
/
oidc.rs
File metadata and controls
247 lines (226 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use std::collections::BTreeMap;
use snafu::ResultExt;
use stackable_operator::{
builder::pod::{PodBuilder, container::ContainerBuilder},
crd::authentication::oidc,
k8s_openapi::api::core::v1::EnvVar,
};
use super::{AddOidcVolumesSnafu, ConstructOidcWellKnownUrlSnafu, Error};
use crate::{
crd::{COOKIE_PASSPHRASE_ENV, DruidRole, security::add_cert_to_jvm_trust_store_cmd},
internal_secret::env_var_from_secret,
};
/// Creates OIDC authenticator config using the pac4j extension for Druid: <https://druid.apache.org/docs/latest/development/extensions-core/druid-pac4j>.
fn add_authenticator_config(
provider: &oidc::v1alpha1::AuthenticationProvider,
oidc: &oidc::v1alpha1::ClientAuthenticationOptions,
config: &mut BTreeMap<String, Option<String>>,
) -> Result<(), Error> {
let well_known_url = &provider
.well_known_config_url()
.context(ConstructOidcWellKnownUrlSnafu)?;
let (oidc_client_id_env, oidc_client_secret_env) =
oidc::v1alpha1::AuthenticationProvider::client_credentials_env_names(
&oidc.client_credentials_secret_ref,
);
let mut scopes = provider.scopes.clone();
scopes.extend_from_slice(&oidc.extra_scopes);
config.insert(
"druid.auth.authenticator.Oidc.type".to_string(),
Some(r#"pac4j"#.to_string()),
);
config.insert(
"druid.auth.authenticator.Oidc.authorizerName".to_string(),
Some(r#"OidcAuthorizer"#.to_string()),
);
config.insert(
"druid.auth.pac4j.cookiePassphrase".to_string(),
Some(format!("${{env:{COOKIE_PASSPHRASE_ENV}}}").to_string()),
);
config.insert(
"druid.auth.pac4j.oidc.clientID".to_string(),
Some(format!("${{env:{oidc_client_id_env}}}").to_string()),
);
config.insert(
"druid.auth.pac4j.oidc.clientSecret".to_string(),
Some(format!("${{env:{oidc_client_secret_env}}}").to_string()),
);
config.insert(
"druid.auth.pac4j.oidc.discoveryURI".to_string(),
Some(well_known_url.to_string()),
);
config.insert(
"druid.auth.pac4j.oidc.oidcClaim".to_string(),
Some(provider.principal_claim.to_string()),
);
config.insert(
"druid.auth.pac4j.oidc.scope".to_string(),
Some(scopes.join(" ")),
);
// Serialize the enum to get the snake_case string representation
let method_string =
serde_json::to_value(oidc.client_authentication_method).expect("serializing ClientAuthenticationMethod to string");
let method_string = method_string
.as_str()
.expect("ClientAuthenticationMethod should serialize to a string");
config.insert(
"druid.auth.pac4j.oidc.clientAuthenticationMethod".to_string(),
Some(method_string.to_string()),
);
config.insert(
"druid.auth.authenticatorChain".to_string(),
Some(r#"["DruidSystemAuthenticator", "Oidc"]"#.to_string()),
);
Ok(())
}
fn add_authorizer_config(config: &mut BTreeMap<String, Option<String>>) {
config.insert(
"druid.auth.authorizers".to_string(),
Some(r#"["OidcAuthorizer", "DruidSystemAuthorizer"]"#.to_string()),
);
config.insert(
"druid.auth.authorizer.OidcAuthorizer.type".to_string(),
Some(r#"allowAll"#.to_string()),
);
}
/// Creates the OIDC parts of the runtime.properties config file.
/// OIDC authentication is not configured on middlemanagers, because end users don't interact with them directly using the web console and
/// turning on OIDC will lead to problems with the communication with coordinators during data ingest.
pub fn generate_runtime_properties_config(
provider: &oidc::v1alpha1::AuthenticationProvider,
oidc: &oidc::v1alpha1::ClientAuthenticationOptions,
role: &DruidRole,
config: &mut BTreeMap<String, Option<String>>,
) -> Result<(), Error> {
match role {
DruidRole::MiddleManager => {
config.insert(
"druid.auth.authenticatorChain".to_string(),
Some(r#"["DruidSystemAuthenticator"]"#.to_string()),
);
}
_ => {
add_authenticator_config(provider, oidc, config)?;
add_authorizer_config(config)
}
}
Ok(())
}
pub fn main_container_commands(
provider: &oidc::v1alpha1::AuthenticationProvider,
command: &mut Vec<String>,
) {
if let Some(tls_ca_cert_mount_path) = provider.tls.tls_ca_cert_mount_path() {
command.extend(add_cert_to_jvm_trust_store_cmd(&tls_ca_cert_mount_path))
}
}
/// Mounts the OIDC credentials secret and the auto-generated internal secret containing the cookie passphrase.
/// Not necessary on middlemanagers, because OIDC is not configured on them.
pub fn get_env_var_mounts(
role: &DruidRole,
oidc: &oidc::v1alpha1::ClientAuthenticationOptions,
internal_secret_name: &str,
) -> Vec<EnvVar> {
let mut envs = vec![];
match role {
DruidRole::MiddleManager => (),
_ => {
envs.extend(
oidc::v1alpha1::AuthenticationProvider::client_credentials_env_var_mounts(
oidc.client_credentials_secret_ref.to_owned(),
),
);
envs.push(env_var_from_secret(
internal_secret_name,
None,
COOKIE_PASSPHRASE_ENV,
))
}
}
envs
}
pub fn add_volumes_and_mounts(
provider: &oidc::v1alpha1::AuthenticationProvider,
pb: &mut PodBuilder,
cb_druid: &mut ContainerBuilder,
cb_prepare: &mut ContainerBuilder,
) -> Result<(), Error> {
provider
.tls
.add_volumes_and_mounts(pb, vec![cb_druid, cb_prepare])
.context(AddOidcVolumesSnafu)
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use stackable_operator::commons::tls_verification::{Tls, TlsClientDetails};
use super::*;
#[rstest]
#[case("/realms/sdp")]
#[case("/realms/sdp/")]
#[case("/realms/sdp/////")]
fn test_add_authenticator_config(#[case] root_path: String) {
use stackable_operator::{
commons::tls_verification::{CaCert, TlsServerVerification, TlsVerification},
crd::authentication::oidc,
};
let mut properties = BTreeMap::new();
let provider = oidc::v1alpha1::AuthenticationProvider::new(
"keycloak.mycorp.org".to_owned().try_into().unwrap(),
Some(443),
root_path,
TlsClientDetails {
tls: Some(Tls {
verification: TlsVerification::Server(TlsServerVerification {
ca_cert: CaCert::WebPki {},
}),
}),
},
"preferred_username".to_owned(),
vec!["openid".to_owned()],
Some(oidc::v1alpha1::IdentityProviderHint::Keycloak),
);
let oidc = oidc::v1alpha1::ClientAuthenticationOptions {
client_credentials_secret_ref: "nifi-keycloak-client".to_owned(),
extra_scopes: vec![],
client_authentication_method: oidc::v1alpha1::ClientAuthenticationMethod::ClientSecretPost,
product_specific_fields: (),
};
add_authenticator_config(&provider, &oidc, &mut properties)
.expect("OIDC config adding failed");
assert_eq!(
properties.get("druid.auth.authenticator.Oidc.type"),
Some(&Some("pac4j".to_owned()))
);
assert_eq!(
properties.get("druid.auth.pac4j.oidc.oidcClaim"),
Some(&Some("preferred_username".to_owned()))
);
assert_eq!(
properties.get("druid.auth.pac4j.oidc.scope"),
Some(&Some("openid".to_owned()))
);
assert_eq!(
properties.get("druid.auth.authenticator.Oidc.authorizerName"),
Some(&Some("OidcAuthorizer".to_owned()))
);
assert_eq!(
properties.get("druid.auth.authenticatorChain"),
Some(&Some("[\"DruidSystemAuthenticator\", \"Oidc\"]".to_owned()))
);
assert_eq!(
properties.get("druid.auth.pac4j.oidc.discoveryURI"),
Some(&Some(
"https://keycloak.mycorp.org/realms/sdp/.well-known/openid-configuration"
.to_owned()
))
);
assert_eq!(
properties.get("druid.auth.pac4j.oidc.clientAuthenticationMethod"),
Some(&Some("client_secret_post".to_owned()))
);
assert!(properties.contains_key("druid.auth.pac4j.oidc.clientID"));
assert!(properties.contains_key("druid.auth.pac4j.oidc.clientSecret"));
assert!(properties.contains_key("druid.auth.pac4j.cookiePassphrase"));
}
}