-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathentra.rs
More file actions
365 lines (322 loc) · 11.2 KB
/
Copy pathentra.rs
File metadata and controls
365 lines (322 loc) · 11.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use std::{collections::HashMap, path::Path};
use hyper::StatusCode;
use reqwest::ClientBuilder;
use serde::Deserialize;
use snafu::{ResultExt, Snafu};
use stackable_opa_operator::crd::user_info_fetcher::v1alpha1;
use stackable_operator::commons::{networking::HostName, tls_verification::TlsClientDetails};
use url::Url;
use crate::{
UserInfo, UserInfoRequest, http_error,
utils::{self, http::send_json_request},
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to get access_token"))]
AccessToken { source: crate::utils::http::Error },
#[snafu(display("failed to search for user with username {username:?}"))]
SearchForUser {
source: crate::utils::http::Error,
username: String,
},
#[snafu(display("failed to search for user with id {user_id:?}"))]
UserNotFoundById {
source: crate::utils::http::Error,
user_id: String,
},
#[snafu(display(
"failed to request groups for user with username {username:?} (user_id: {user_id:?})"
))]
RequestUserGroups {
source: crate::utils::http::Error,
username: String,
user_id: String,
},
#[snafu(display("failed to to build entra endpoint for {endpoint}"))]
BuildEntraEndpointFailed {
source: url::ParseError,
endpoint: String,
},
#[snafu(display("failed to construct HTTP client"))]
ConstructHttpClient { source: reqwest::Error },
#[snafu(display("failed to configure TLS"))]
ConfigureTls { source: utils::tls::Error },
#[snafu(display("failed to read client ID from {path:?}"))]
ReadClientId {
source: std::io::Error,
path: String,
},
#[snafu(display("failed to read client secret from {path:?}"))]
ReadClientSecret {
source: std::io::Error,
path: String,
},
}
impl http_error::Error for Error {
fn status_code(&self) -> StatusCode {
match self {
Self::AccessToken { .. } => StatusCode::BAD_GATEWAY,
Self::SearchForUser { .. } => StatusCode::BAD_GATEWAY,
Self::UserNotFoundById { .. } => StatusCode::NOT_FOUND,
Self::RequestUserGroups { .. } => StatusCode::BAD_GATEWAY,
Self::BuildEntraEndpointFailed { .. } => StatusCode::BAD_REQUEST,
Self::ConstructHttpClient { .. } => StatusCode::SERVICE_UNAVAILABLE,
Self::ConfigureTls { .. } => StatusCode::SERVICE_UNAVAILABLE,
Self::ReadClientId { .. } => StatusCode::SERVICE_UNAVAILABLE,
Self::ReadClientSecret { .. } => StatusCode::SERVICE_UNAVAILABLE,
}
}
}
#[derive(Deserialize)]
struct OAuthResponse {
access_token: String,
}
#[derive(Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct UserMetadata {
id: String,
user_principal_name: String,
#[serde(default)]
attributes: HashMap<String, serde_json::Value>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GroupMembershipResponse {
value: Vec<GroupMembership>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GroupMembership {
display_name: Option<String>,
}
/// Entra backend with resolved credentials.
///
/// This struct combines the CRD configuration with credentials loaded from the filesystem.
/// Credentials and the HTTP client are initialized once at startup and stored internally.
pub struct ResolvedEntraBackend {
config: v1alpha1::EntraBackend,
client_id: String,
client_secret: String,
http_client: reqwest::Client,
}
impl ResolvedEntraBackend {
/// Resolves an Entra backend by loading credentials from the filesystem.
///
/// Reads `clientId` and `clientSecret` from the credentials directory and initializes
/// the HTTP client with appropriate TLS configuration.
pub async fn resolve(
config: v1alpha1::EntraBackend,
credentials_dir: &Path,
) -> Result<Self, Error> {
let client_id_path = credentials_dir.join("clientId");
let client_secret_path = credentials_dir.join("clientSecret");
let client_id =
tokio::fs::read_to_string(&client_id_path)
.await
.context(ReadClientIdSnafu {
path: client_id_path.display().to_string(),
})?;
let client_secret = tokio::fs::read_to_string(&client_secret_path)
.await
.context(ReadClientSecretSnafu {
path: client_secret_path.display().to_string(),
})?;
let mut client_builder = ClientBuilder::new();
client_builder = utils::tls::configure_reqwest(
&TlsClientDetails {
tls: config.tls.clone(),
},
client_builder,
)
.await
.context(ConfigureTlsSnafu)?;
let http_client = client_builder.build().context(ConstructHttpClientSnafu)?;
Ok(Self {
config,
client_id,
client_secret,
http_client,
})
}
pub(crate) async fn get_user_info(&self, req: &UserInfoRequest) -> Result<UserInfo, Error> {
let v1alpha1::EntraBackend {
client_credentials_secret: _,
token_hostname,
user_info_hostname,
port,
tenant_id,
tls,
} = &self.config;
let entra_backend = EntraBackend::try_new(
token_hostname,
user_info_hostname,
*port,
tenant_id,
TlsClientDetails { tls: tls.clone() }.uses_tls(),
)?;
let token_url = entra_backend.oauth2_token();
let authn = send_json_request::<OAuthResponse>(self.http_client.post(token_url).form(&[
("client_id", self.client_id.as_str()),
("client_secret", self.client_secret.as_str()),
("scope", "https://graph.microsoft.com/.default"),
("grant_type", "client_credentials"),
]))
.await
.context(AccessTokenSnafu)?;
let user_info = match req {
UserInfoRequest::UserInfoRequestById(req) => {
let user_id = &req.id;
send_json_request::<UserMetadata>(
self.http_client
.get(entra_backend.user_info(user_id))
.bearer_auth(&authn.access_token),
)
.await
.with_context(|_| UserNotFoundByIdSnafu {
user_id: user_id.clone(),
})?
}
UserInfoRequest::UserInfoRequestByName(req) => {
let username = &req.username;
send_json_request::<UserMetadata>(
self.http_client
.get(entra_backend.user_info(username))
.bearer_auth(&authn.access_token),
)
.await
.with_context(|_| SearchForUserSnafu {
username: username.clone(),
})?
}
};
let groups = send_json_request::<GroupMembershipResponse>(
self.http_client
.get(entra_backend.group_info(&user_info.id))
.bearer_auth(&authn.access_token),
)
.await
.with_context(|_| RequestUserGroupsSnafu {
username: user_info.user_principal_name.clone(),
user_id: user_info.id.clone(),
})?
.value;
Ok(UserInfo {
id: Some(user_info.id),
username: Some(user_info.user_principal_name),
groups: groups.into_iter().filter_map(|g| g.display_name).collect(),
custom_attributes: user_info.attributes,
})
}
}
struct EntraBackend {
token_endpoint_url: Url,
user_info_endpoint_url: Url,
}
impl EntraBackend {
pub fn try_new(
token_endpoint: &HostName,
user_info_endpoint: &HostName,
port: Option<u16>,
tenant_id: &str,
uses_tls: bool,
) -> Result<Self, Error> {
let schema = if uses_tls { "https" } else { "http" };
let port = port.unwrap_or(if uses_tls { 443 } else { 80 });
let token_endpoint =
format!("{schema}://{token_endpoint}:{port}/{tenant_id}/oauth2/v2.0/token");
let token_endpoint_url =
Url::parse(&token_endpoint).context(BuildEntraEndpointFailedSnafu {
endpoint: token_endpoint,
})?;
let user_info_endpoint = format!("{schema}://{user_info_endpoint}:{port}");
let user_info_endpoint_url =
Url::parse(&user_info_endpoint).context(BuildEntraEndpointFailedSnafu {
endpoint: user_info_endpoint,
})?;
Ok(Self {
token_endpoint_url,
user_info_endpoint_url,
})
}
pub fn oauth2_token(&self) -> Url {
self.token_endpoint_url.clone()
}
// Works both with id/oid and userPrincipalName
pub fn user_info(&self, user: &str) -> Url {
let mut user_info_url = self.user_info_endpoint_url.clone();
user_info_url.set_path(&format!("/v1.0/users/{user}"));
user_info_url
}
pub fn group_info(&self, user: &str) -> Url {
let mut user_info_url = self.user_info_endpoint_url.clone();
user_info_url.set_path(&format!("/v1.0/users/{user}/memberOf"));
user_info_url
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
#[test]
fn test_entra_defaults_id() {
let tenant_id = "1234-5678-1234-5678";
let user = "1234-5678-1234-5678";
let entra = EntraBackend::try_new(
&HostName::from_str("login.microsoft.com").unwrap(),
&HostName::from_str("graph.microsoft.com").unwrap(),
None,
tenant_id,
true,
)
.unwrap();
assert_eq!(
entra.oauth2_token(),
Url::parse(&format!(
"https://login.microsoft.com/{tenant_id}/oauth2/v2.0/token"
))
.unwrap()
);
assert_eq!(
entra.user_info(user),
Url::parse(&format!("https://graph.microsoft.com/v1.0/users/{user}")).unwrap()
);
assert_eq!(
entra.group_info(user),
Url::parse(&format!(
"https://graph.microsoft.com/v1.0/users/{user}/memberOf"
))
.unwrap()
);
}
#[test]
fn test_entra_custom_id() {
let tenant_id = "1234-5678-1234-5678";
let user = "1234-5678-1234-5678";
let entra = EntraBackend::try_new(
&HostName::from_str("login.mock.com").unwrap(),
&HostName::from_str("graph.mock.com").unwrap(),
Some(8080),
tenant_id,
false,
)
.unwrap();
assert_eq!(
entra.oauth2_token(),
Url::parse(&format!(
"http://login.mock.com:8080/{tenant_id}/oauth2/v2.0/token"
))
.unwrap()
);
assert_eq!(
entra.user_info(user),
Url::parse(&format!("http://graph.mock.com:8080/v1.0/users/{user}")).unwrap()
);
assert_eq!(
entra.group_info(user),
Url::parse(&format!(
"http://graph.mock.com:8080/v1.0/users/{user}/memberOf"
))
.unwrap()
);
}
}