-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
259 lines (231 loc) · 9.42 KB
/
Copy pathmod.rs
File metadata and controls
259 lines (231 loc) · 9.42 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
248
249
250
251
252
253
254
255
256
257
258
259
use std::collections::BTreeMap;
use snafu::Snafu;
use stackable_operator::{
builder::pod::{PodBuilder, container::ContainerBuilder},
crd::authentication,
k8s_openapi::api::core::v1::EnvVar,
};
use crate::{
crd::{
DruidRole,
authentication::{AuthenticationClassResolved, AuthenticationClassesResolved},
security::{ESCALATOR_INTERNAL_CLIENT_PASSWORD_ENV, INTERNAL_INITIAL_CLIENT_PASSWORD_ENV},
v1alpha1,
},
internal_secret::{build_shared_internal_secret_name, env_var_from_secret},
};
pub mod ldap;
pub mod oidc;
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to create LDAP endpoint url."))]
ConstructLdapEndpointUrl {
source: stackable_operator::crd::authentication::ldap::v1alpha1::Error,
},
#[snafu(display("failed to create the OIDC well-known url."))]
ConstructOidcWellKnownUrl {
source: stackable_operator::crd::authentication::oidc::v1alpha1::Error,
},
#[snafu(display("failed to add LDAP Volumes and VolumeMounts to the Pod and containers"))]
AddLdapVolumes {
source: stackable_operator::crd::authentication::ldap::v1alpha1::Error,
},
#[snafu(display("failed to add OIDC Volumes and VolumeMounts to the Pod and containers"))]
AddOidcVolumes {
source: stackable_operator::commons::tls_verification::TlsClientDetailsError,
},
#[snafu(display(
"failed to access bind credentials although they are required for LDAP to work"
))]
MissingLdapBindCredentials,
}
#[derive(Clone, Debug)]
pub enum DruidAuthenticationConfig {
Tls {},
Ldap {
provider: authentication::ldap::v1alpha1::AuthenticationProvider,
},
Oidc {
provider: authentication::oidc::v1alpha1::AuthenticationProvider,
oidc: authentication::oidc::v1alpha1::ClientAuthenticationOptions,
},
}
impl DruidAuthenticationConfig {
pub fn try_from(
auth_classes_resolved: AuthenticationClassesResolved,
) -> Result<Option<Self>, Error> {
// Currently only one auth mechanism is supported in Druid. This is checked in
// `rust/crd/src/authentication.rs` and just a fail-safe here. For Future changes,
// this is not just a "from" without error handling.
match auth_classes_resolved.auth_classes.first() {
None => Ok(None),
Some(auth_class_resolved) => match &auth_class_resolved {
AuthenticationClassResolved::Tls { .. } => Ok(Some(Self::Tls {})),
AuthenticationClassResolved::Ldap { provider, .. } => Ok(Some(Self::Ldap {
provider: provider.clone(),
})),
AuthenticationClassResolved::Oidc { provider, oidc, .. } => Ok(Some(Self::Oidc {
provider: provider.clone(),
oidc: oidc.clone(),
})),
},
}
}
/// Creates the authentication and authorization parts of the runtime.properties config file.
/// Configuration related to TLS authentication is added in `rust/crd/src/security.rs`.
pub fn generate_runtime_properties_config(
&self,
role: &DruidRole,
) -> Result<BTreeMap<String, Option<String>>, Error> {
let mut config: BTreeMap<String, Option<String>> = BTreeMap::new();
match self {
DruidAuthenticationConfig::Ldap { provider, .. } => {
self.generate_common_runtime_properties_config(&mut config);
ldap::generate_runtime_properties_config(provider, &mut config)?
}
DruidAuthenticationConfig::Oidc { provider, oidc, .. } => {
self.generate_common_runtime_properties_config(&mut config);
oidc::generate_runtime_properties_config(provider, oidc, role, &mut config)?
}
DruidAuthenticationConfig::Tls { .. } => (),
}
Ok(config)
}
/// Creates authentication config that is required by LDAP and OIDC and doesn't depend on user input.
fn generate_common_runtime_properties_config(
&self,
config: &mut BTreeMap<String, Option<String>>,
) {
self.add_druid_system_authenticator_config(config);
self.add_escalator_config(config);
config.insert(
"druid.auth.authorizer.DruidSystemAuthorizer.type".to_string(),
Some(r#"allowAll"#.to_string()),
);
}
pub fn main_container_commands(&self) -> Vec<String> {
let mut command = vec![];
if let DruidAuthenticationConfig::Oidc { provider, .. } = self {
oidc::main_container_commands(provider, &mut command)
}
command
}
pub fn prepare_container_commands(&self) -> Vec<String> {
let mut command = vec![];
if let DruidAuthenticationConfig::Ldap { provider } = self {
ldap::prepare_container_commands(provider, &mut command)
}
command
}
pub fn get_env_var_mounts(
&self,
druid: &v1alpha1::DruidCluster,
role: &DruidRole,
) -> Vec<EnvVar> {
let mut envs = vec![];
let internal_secret_name = build_shared_internal_secret_name(druid);
envs.push(env_var_from_secret(
&internal_secret_name,
None,
INTERNAL_INITIAL_CLIENT_PASSWORD_ENV,
));
if let DruidAuthenticationConfig::Oidc { oidc, .. } = self {
envs.extend(oidc::get_env_var_mounts(role, oidc, &internal_secret_name))
}
envs
}
pub fn add_volumes_and_mounts(
&self,
pb: &mut PodBuilder,
cb_druid: &mut ContainerBuilder,
cb_prepare: &mut ContainerBuilder,
) -> Result<(), Error> {
match self {
DruidAuthenticationConfig::Ldap { provider, .. } => {
ldap::add_volumes_and_mounts(provider, pb, cb_druid, cb_prepare)
}
DruidAuthenticationConfig::Oidc { provider, .. } => {
oidc::add_volumes_and_mounts(provider, pb, cb_druid, cb_prepare)
}
DruidAuthenticationConfig::Tls { .. } => Ok(()),
}
}
/// Creates the authenticatior config for the internal communication by Druid processes using basic auth.
/// When using LDAP or OIDC the DruidSystemAuthenticator is always tried first and skipped if no basic auth credentials were supplied.
/// We don't want to create an admin user for the internal authentication, so this line is left out of the config:
/// # druid.auth.authenticator.DruidSystemAuthenticator.initialAdminPassword: XXX
fn add_druid_system_authenticator_config(&self, config: &mut BTreeMap<String, Option<String>>) {
config.insert(
"druid.auth.authenticator.DruidSystemAuthenticator.type".to_string(),
Some("basic".to_string()),
);
config.insert(
"druid.auth.authenticator.DruidSystemAuthenticator.credentialsValidator.type"
.to_string(),
Some("metadata".to_string()),
);
config.insert(
"druid.auth.authenticator.DruidSystemAuthenticator.initialInternalClientPassword"
.to_string(),
Some(format!("${{env:{INTERNAL_INITIAL_CLIENT_PASSWORD_ENV}}}").to_string()),
);
config.insert(
"druid.auth.authenticator.DruidSystemAuthenticator.authorizerName".to_string(),
Some("DruidSystemAuthorizer".to_string()),
);
config.insert(
"druid.auth.authenticator.DruidSystemAuthenticator.skipOnFailure".to_string(),
Some("true".to_string()),
);
}
/// Creates the escalator config: <https://druid.apache.org/docs/latest/operations/auth/#escalator>.
/// This configures Druid processes to use the basic auth authentication added in `add_druid_system_authenticator_config` for internal communication.
fn add_escalator_config(&self, config: &mut BTreeMap<String, Option<String>>) {
config.insert(
"druid.escalator.type".to_string(),
Some("basic".to_string()),
);
config.insert(
"druid.escalator.internalClientUsername".to_string(),
Some("druid_system".to_string()),
);
config.insert(
"druid.escalator.internalClientPassword".to_string(),
Some(format!("${{env:{ESCALATOR_INTERNAL_CLIENT_PASSWORD_ENV}}}").to_string()),
);
config.insert(
"druid.escalator.authorizerName".to_string(),
Some("DruidSystemAuthorizer".to_string()),
);
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ldap_config_is_added() {
let auth_config = DruidAuthenticationConfig::try_from(AuthenticationClassesResolved {
auth_classes: vec![AuthenticationClassResolved::Ldap {
auth_class_name: "ldap".to_string(),
provider: serde_yaml::from_str::<
authentication::ldap::v1alpha1::AuthenticationProvider,
>(
"
hostname: openldap
searchBase: ou=users,dc=example,dc=org
searchFilter: (uid=%s)
",
)
.unwrap(),
}],
})
.unwrap()
.unwrap();
let role = DruidRole::Coordinator;
let got = auth_config
.generate_runtime_properties_config(&role)
.unwrap();
assert!(got.contains_key("druid.auth.authenticator.Ldap.type"));
}
}