1 + | /*
|
2 + | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 + | * SPDX-License-Identifier: Apache-2.0
|
4 + | */
|
5 + |
|
6 + | use std::collections::HashMap;
|
7 + |
|
8 + | use aws_config::{provider_config::ProviderConfig, Region};
|
9 + | use aws_runtime::user_agent::test_util::{
|
10 + | assert_ua_contains_metric_values, get_sdk_metric_str_from_request,
|
11 + | };
|
12 + | use aws_sdk_s3::{
|
13 + | config::{
|
14 + | http::{HttpRequest, HttpResponse},
|
15 + | HttpClient, RuntimeComponents,
|
16 + | },
|
17 + | Client, Config,
|
18 + | };
|
19 + | use aws_smithy_http_client::test_util::capture_request;
|
20 + | use aws_smithy_runtime_api::client::http::{
|
21 + | HttpConnector, HttpConnectorFuture, HttpConnectorSettings, SharedHttpConnector,
|
22 + | };
|
23 + | use aws_smithy_types::body::SdkBody;
|
24 + | use aws_types::os_shim_internal::{Env, Fs};
|
25 + |
|
26 + | #[tokio::test]
|
27 + | async fn profile_and_sso_ua_features() {
|
28 + | let (http_client, request) = capture_request(None);
|
29 + |
|
30 + | #[derive(Debug)]
|
31 + | struct ClientInner {
|
32 + | expected_token: &'static str,
|
33 + | }
|
34 + | impl HttpConnector for ClientInner {
|
35 + | fn call(&self, request: HttpRequest) -> HttpConnectorFuture {
|
36 + | assert_eq!(
|
37 + | self.expected_token,
|
38 + | request.headers().get("x-amz-sso_bearer_token").unwrap()
|
39 + | );
|
40 + | HttpConnectorFuture::ready(Ok(HttpResponse::new(
|
41 + | 200.try_into().unwrap(),
|
42 + | SdkBody::from("{\"roleCredentials\":{\"accessKeyId\":\"ASIARTESTID\",\"secretAccessKey\":\"TESTSECRETKEY\",\"sessionToken\":\"TESTSESSIONTOKEN\",\"expiration\": 1651516560000}}"),
|
43 + | )))
|
44 + | }
|
45 + | }
|
46 + | #[derive(Debug)]
|
47 + | struct CredsClient {
|
48 + | inner: SharedHttpConnector,
|
49 + | }
|
50 + | impl CredsClient {
|
51 + | fn new(expected_token: &'static str) -> Self {
|
52 + | Self {
|
53 + | inner: SharedHttpConnector::new(ClientInner { expected_token }),
|
54 + | }
|
55 + | }
|
56 + | }
|
57 + | impl HttpClient for CredsClient {
|
58 + | fn http_connector(
|
59 + | &self,
|
60 + | _settings: &HttpConnectorSettings,
|
61 + | _components: &RuntimeComponents,
|
62 + | ) -> SharedHttpConnector {
|
63 + | self.inner.clone()
|
64 + | }
|
65 + | }
|
66 + |
|
67 + | let fs = Fs::from_map({
|
68 + | let mut map = HashMap::new();
|
69 + | map.insert(
|
70 + | "/home/.aws/config".to_string(),
|
71 + | br#"
|
72 + | [profile default]
|
73 + | sso_session = dev
|
74 + | sso_account_id = 012345678901
|
75 + | sso_role_name = SampleRole
|
76 + | region = us-east-1
|
77 + |
|
78 + | [sso-session dev]
|
79 + | sso_region = us-east-1
|
80 + | sso_start_url = https://d-abc123.awsapps.com/start
|
81 + | "#
|
82 + | .to_vec(),
|
83 + | );
|
84 + | map.insert(
|
85 + | "/home/.aws/sso/cache/34c6fceca75e456f25e7e99531e2425c6c1de443.json".to_string(),
|
86 + | br#"
|
87 + | {
|
88 + | "accessToken": "secret-access-token",
|
89 + | "expiresAt": "2199-11-14T04:05:45Z",
|
90 + | "refreshToken": "secret-refresh-token",
|
91 + | "clientId": "ABCDEFG323242423121312312312312312",
|
92 + | "clientSecret": "ABCDE123",
|
93 + | "registrationExpiresAt": "2199-03-06T19:53:17Z",
|
94 + | "region": "us-east-1",
|
95 + | "startUrl": "https://d-abc123.awsapps.com/start"
|
96 + | }
|
97 + | "#
|
98 + | .to_vec(),
|
99 + | );
|
100 + | map
|
101 + | });
|
102 + | let provider_config = ProviderConfig::empty()
|
103 + | .with_fs(fs.clone())
|
104 + | .with_env(Env::from_slice(&[("HOME", "/home")]))
|
105 + | .with_http_client(CredsClient::new("secret-access-token"));
|
106 + | let provider = aws_config::profile::credentials::Builder::default()
|
107 + | .configure(&provider_config)
|
108 + | .build();
|
109 + |
|
110 + | let config = Config::builder()
|
111 + | .with_test_defaults()
|
112 + | .region(Region::from_static("fake"))
|
113 + | .http_client(http_client.clone())
|
114 + | .credentials_provider(provider)
|
115 + | .build();
|
116 + |
|
117 + | let client = Client::from_conf(config);
|
118 + |
|
119 + | let _ = client
|
120 + | .head_bucket()
|
121 + | .bucket("fake")
|
122 + | .send()
|
123 + | .await
|
124 + | .expect("XXXXXXXXXXX");
|
125 + |
|
126 + | let request = request.expect_request();
|
127 + | let ua = get_sdk_metric_str_from_request(&request);
|
128 + | assert_ua_contains_metric_values(ua, &["n", "s"]);
|
129 + | }
|