-
Notifications
You must be signed in to change notification settings - Fork 37
/
auth.rs
408 lines (348 loc) · 13.9 KB
/
auth.rs
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::errors::{Error, Result};
use crate::v2::*;
use reqwest::{header::HeaderValue, RequestBuilder, StatusCode, Url};
/// Represents all supported authentication schemes and is stored by `Client`.
#[derive(Debug, Clone)]
pub enum Auth {
Bearer(BearerAuth),
Basic(BasicAuth),
}
impl Auth {
/// Add authentication headers to a request builder.
pub(crate) fn add_auth_headers(&self, request_builder: RequestBuilder) -> RequestBuilder {
match self {
Auth::Bearer(bearer_auth) => request_builder.bearer_auth(bearer_auth.token.clone()),
Auth::Basic(basic_auth) => {
request_builder.basic_auth(basic_auth.user.clone(), basic_auth.password.clone())
}
}
}
}
/// Used for Bearer HTTP Authentication.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct BearerAuth {
token: String,
expires_in: Option<u32>,
issued_at: Option<String>,
refresh_token: Option<String>,
}
impl BearerAuth {
async fn try_from_header_content(
client: Client,
scopes: &[&str],
credentials: Option<(String, String)>,
bearer_header_content: WwwAuthenticateHeaderContentBearer,
) -> Result<Self> {
let auth_ep = bearer_header_content.auth_ep(scopes);
trace!("authenticate: token endpoint: {}", auth_ep);
let url = reqwest::Url::parse(&auth_ep)?;
let auth_req = {
Client {
auth: credentials.map(|(user, password)| {
Auth::Basic(BasicAuth {
user,
password: Some(password),
})
}),
..client
}
}
.build_reqwest(Method::GET, url);
let r = auth_req.send().await?;
let status = r.status();
trace!("authenticate: got status {}", status);
if status != StatusCode::OK {
return Err(Error::UnexpectedHttpStatus(status));
}
let bearer_auth = r.json::<BearerAuth>().await?;
match bearer_auth.token.as_str() {
"unauthenticated" | "" => return Err(Error::InvalidAuthToken(bearer_auth.token)),
_ => {}
};
// mask the token before logging it
let chars_count = bearer_auth.token.chars().count();
let mask_start = std::cmp::min(1, chars_count - 1);
let mask_end = std::cmp::max(chars_count - 1, 1);
let mut masked_token = bearer_auth.token.clone();
masked_token.replace_range(mask_start..mask_end, &"*".repeat(mask_end - mask_start));
trace!("authenticate: got token: {:?}", masked_token);
Ok(bearer_auth)
}
}
/// Used for Basic HTTP Authentication.
#[derive(Debug, Clone)]
pub struct BasicAuth {
user: String,
password: Option<String>,
}
/// Structured representation for the content of the authentication response header.
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub(crate) enum WwwAuthenticateHeaderContent {
Bearer(WwwAuthenticateHeaderContentBearer),
Basic(WwwAuthenticateHeaderContentBasic),
}
const REGEX: &str = r#"(?x)\s*
((?P<method>[A-Z][a-z]+)\s*)?
(
\s*
(?P<key>[a-z]+)
\s*
=
\s*
"(?P<value>[^"]+)"
\s*
)
"#;
#[derive(Debug, thiserror::Error)]
pub enum WwwHeaderParseError {
#[error("header value must conform to {}", REGEX)]
InvalidValue,
#[error("'method' field missing")]
FieldMethodMissing,
}
impl WwwAuthenticateHeaderContent {
/// Create a `WwwAuthenticateHeaderContent` by parsing a `HeaderValue` instance.
pub(crate) fn from_www_authentication_header(header_value: HeaderValue) -> Result<Self> {
let header = String::from_utf8(header_value.as_bytes().to_vec())?;
// This regex will result in multiple captures which will contain one key-value pair each.
// The first capture will be the only one with the "method" group set.
let re = regex::Regex::new(REGEX).expect("this static regex is valid");
let captures = re.captures_iter(&header).collect::<Vec<_>>();
let method = captures
.get(0)
.ok_or(WwwHeaderParseError::InvalidValue)?
.name("method")
.ok_or(WwwHeaderParseError::FieldMethodMissing)?
.as_str()
.to_string();
let serialized_content = {
let serialized_captures = captures
.iter()
.filter_map(|capture| {
match (
capture.name("key").map(|n| n.as_str().to_string()),
capture.name("value").map(|n| n.as_str().to_string()),
) {
(Some(key), Some(value)) => Some(format!(
r#"{}: {}"#,
serde_json::Value::String(key),
serde_json::Value::String(value),
)),
_ => None,
}
})
.collect::<Vec<_>>()
.join(", ");
format!(
r#"{{ {}: {{ {} }} }}"#,
serde_json::Value::String(method),
serialized_captures
)
};
// Deserialize the content
let mut unsupported_keys = std::collections::HashSet::new();
let content: WwwAuthenticateHeaderContent = serde_ignored::deserialize(
&mut serde_json::Deserializer::from_str(&serialized_content),
|path| {
unsupported_keys.insert(path.to_string());
},
)?;
if !unsupported_keys.is_empty() {
warn!(
"skipping unrecognized keys in authentication header: {:#?}",
unsupported_keys
);
}
Ok(content)
}
}
/// Structured content for the Bearer authentication response header.
#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub(crate) struct WwwAuthenticateHeaderContentBearer {
realm: String,
service: Option<String>,
scope: Option<String>,
}
impl WwwAuthenticateHeaderContentBearer {
fn auth_ep(&self, scopes: &[&str]) -> String {
let service = self
.service
.as_ref()
.map(|sv| format!("?service={}", sv))
.unwrap_or_default();
let scope = scopes
.iter()
.enumerate()
.fold(String::new(), |acc, (i, &s)| {
let separator = if i > 0 { "&" } else { "" };
acc + separator + "scope=" + s
});
let scope_prefix = if scopes.is_empty() {
""
} else if service.is_empty() {
"?"
} else {
"&"
};
format!("{}{}{}{}", self.realm, service, scope_prefix, scope)
}
}
/// Structured content for the Basic authentication response header.
#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub(crate) struct WwwAuthenticateHeaderContentBasic {
realm: String,
}
impl Client {
/// Make a request and return the response's www authentication header.
async fn get_www_authentication_header(&self) -> Result<HeaderValue> {
let url = {
let ep = format!("{}/v2/", self.base_url.clone(),);
reqwest::Url::parse(&ep)?
};
let r = self.build_reqwest(Method::GET, url.clone()).send().await?;
trace!("GET '{}' status: {:?}", r.url(), r.status());
r.headers()
.get(reqwest::header::WWW_AUTHENTICATE)
.ok_or(Error::MissingAuthHeader("WWW-Authenticate"))
.map(ToOwned::to_owned)
}
/// Perform registry authentication and return the authenticated client.
///
/// If Bearer authentication is used the returned client will be authorized for the requested scopes.
pub async fn authenticate(mut self, scopes: &[&str]) -> Result<Self> {
let credentials = self.credentials.clone();
let client = Client {
auth: None,
..self.clone()
};
let authentication_header = client.get_www_authentication_header().await?;
let auth = match WwwAuthenticateHeaderContent::from_www_authentication_header(
authentication_header,
)? {
WwwAuthenticateHeaderContent::Basic(_) => {
let basic_auth = credentials
.map(|(user, password)| BasicAuth {
user,
password: Some(password),
})
.ok_or(Error::NoCredentials)?;
Auth::Basic(basic_auth)
}
WwwAuthenticateHeaderContent::Bearer(bearer_header_content) => {
let bearer_auth = BearerAuth::try_from_header_content(
client,
scopes,
credentials,
bearer_header_content,
)
.await?;
Auth::Bearer(bearer_auth)
}
};
trace!("authenticate: login succeeded");
self.auth = Some(auth);
Ok(self)
}
/// Check whether the client can successfully make requests to the registry.
///
/// This could be due to granted anonymous access or valid credentials.
pub async fn is_auth(&self) -> Result<bool> {
let url = {
let ep = format!("{}/v2/", self.base_url.clone(),);
Url::parse(&ep)?
};
let req = self.build_reqwest(Method::GET, url.clone());
trace!("Sending request to '{}'", url);
let resp = req.send().await?;
trace!("GET '{:?}'", resp);
let status = resp.status();
match status {
reqwest::StatusCode::OK => Ok(true),
reqwest::StatusCode::UNAUTHORIZED => Ok(false),
_ => Err(Error::UnexpectedHttpStatus(status)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use test_case::test_case;
#[test]
fn bearer_realm_parses_correctly() -> Result<()> {
let realm = "https://sat-r220-02.lab.eng.rdu2.redhat.com/v2/token";
let service = "sat-r220-02.lab.eng.rdu2.redhat.com";
let scope = "repository:registry:pull,push";
let header_value = HeaderValue::from_str(&format!(
r#"Bearer realm="{}",service="{}",scope="{}""#,
realm, service, scope
)).expect("this statically known header value only contains ASCII chars so it is correct header value");
let content = WwwAuthenticateHeaderContent::from_www_authentication_header(header_value)?;
assert_eq!(
WwwAuthenticateHeaderContent::Bearer(WwwAuthenticateHeaderContentBearer {
realm: realm.to_string(),
service: Some(service.to_string()),
scope: Some(scope.to_string()),
}),
content
);
Ok(())
}
// Testing for this situation to work:
// [TRACE dkregistry::v2::auth] Sending request to 'https://localhost:5000/v2/'
// [TRACE dkregistry::v2::auth] GET 'Response { url: "https://localhost:5000/v2/", status: 401, headers: {"content-type": "application/json; charset=utf-8", "docker-distribution-api-version": "registry/2.0", "www-authenticate": "Basic realm=\"Registry\"", "x-content-type-options": "nosniff", "date": "Thu, 18 Jun 2020 09:04:24 GMT", "content-length": "87"} }'
// [TRACE dkregistry::v2::auth] GET 'https://localhost:5000/v2/' status: 401
// [TRACE dkregistry::v2::auth] Token provider: Registry
// [TRACE dkregistry::v2::auth] login: token endpoint: Registry&scope=repository:cincinnati-ci/ocp-release-dev:pull
// [ERROR graph_builder::graph] failed to fetch all release metadata
// [ERROR graph_builder::graph] failed to parse url from string 'Registry&scope=repository:cincinnati-ci/ocp-release-dev:pull': relative URL without a base
#[test]
fn basic_realm_parses_correctly() -> Result<()> {
let realm = "Registry realm";
let header_value = HeaderValue::from_str(&format!(r#"Basic realm="{}""#, realm)).unwrap();
let content = WwwAuthenticateHeaderContent::from_www_authentication_header(header_value)?;
assert_eq!(
WwwAuthenticateHeaderContent::Basic(WwwAuthenticateHeaderContentBasic {
realm: realm.to_string(),
}),
content
);
Ok(())
}
// The following test checks the url construction within the 'auth_ep'
// method of WwwAuthenticateHeaderContentBearer.
// Tests that the result is correctly parsed by Url::parse and that the
// scopes in the query string are as expected in three situations.
// Tests combination of scopes with service query param also.
#[test_case(&[], true; "Test with no scopes and with service")]
#[test_case(&["repository:test:pull"], true; "Test with single scope and service")]
#[test_case(&["repository:test:pull", "repository:example:pull,push", "repository:another:*"], false;
"Test with multiple scopes")]
fn bearer_auth_ep_scope_construction(scopes: &[&str], include_service: bool) {
let realm = "https://sat-r220-02.lab.eng.rdu2.redhat.com/v2/token";
let service = "sat-r220-02.lab.eng.rdu2.redhat.com";
let bearer_header_content = WwwAuthenticateHeaderContentBearer {
realm: realm.to_string(),
service: if include_service {
Some(service.to_string())
} else {
None
},
scope: None,
};
// build list of expected headers
let mut expected_headers: Vec<(String, String)> = scopes
.iter()
.map(|a| ("scope".to_owned(), a.to_string()))
.collect();
// first one is the service header if specified
if include_service {
expected_headers.insert(0, ("service".to_owned(), service.to_string()));
}
let result = bearer_header_content.auth_ep(scopes);
let url = Url::parse(&result).unwrap();
assert_eq!(
url.query_pairs().into_owned().collect::<Vec<_>>(),
expected_headers
);
}
}