1 1 | /*
|
2 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 3 | * SPDX-License-Identifier: Apache-2.0
|
4 4 | */
|
5 5 |
|
6 6 | use aws_sdk_kms as kms;
|
7 7 | use aws_sdk_kms::operation::RequestId;
|
8 8 | use aws_smithy_http_client::test_util::{ReplayEvent, StaticReplayClient};
|
9 9 | use aws_smithy_runtime_api::client::result::SdkError;
|
10 10 | use aws_smithy_types::body::SdkBody;
|
11 + | use aws_smithy_types::retry::RetryConfig;
|
11 12 | use http_1x::Uri;
|
12 13 | use kms::config::{Config, Credentials, Region};
|
13 14 |
|
14 15 | // TODO(DVR): having the full HTTP requests right in the code is a bit gross, consider something
|
15 16 | // like https://github.com/davidbarsky/sigv4/blob/master/aws-sigv4/src/lib.rs#L283-L315 to store
|
16 17 | // the requests/responses externally
|
17 18 |
|
18 19 | /// Validate that for CN regions we set the URI correctly
|
19 20 | #[tokio::test]
|
20 21 | async fn generate_random_cn() {
|
21 22 | let http_client= StaticReplayClient::new(vec![ReplayEvent::new(
|
22 23 | http_1x::Request::builder()
|
23 24 | .uri(Uri::from_static("https://kms.cn-north-1.amazonaws.com.cn/"))
|
24 25 | .body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
25 26 | http_1x::Response::builder()
|
26 27 | .status(http_1x::StatusCode::from_u16(200).unwrap())
|
27 28 | .body(SdkBody::from(r#"{"Plaintext":"6CG0fbzzhg5G2VcFCPmJMJ8Njv3voYCgrGlp3+BZe7eDweCXgiyDH9BnkKvLmS7gQhnYDUlyES3fZVGwv5+CxA=="}"#)).unwrap())
|
28 29 | ]);
|
29 30 | let conf = Config::builder()
|
30 31 | .http_client(http_client.clone())
|
31 32 | .region(Region::new("cn-north-1"))
|
32 33 | .credentials_provider(Credentials::for_tests())
|
33 34 | .build();
|
34 35 | let client = kms::Client::from_conf(conf);
|
35 36 | let _ = client
|
36 37 | .generate_random()
|
37 38 | .number_of_bytes(64)
|
38 39 | .send()
|
39 40 | .await
|
40 41 | .expect("success");
|
41 42 |
|
42 43 | assert_eq!(http_client.actual_requests().count(), 1);
|
43 44 | http_client.assert_requests_match(&[]);
|
44 45 | }
|
45 46 |
|
46 47 | #[cfg(feature = "test-util")]
|
47 48 | #[tokio::test]
|
48 49 | async fn generate_random() {
|
49 50 | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
50 51 | http_1x::Request::builder()
|
51 52 | .header("content-type", "application/x-amz-json-1.1")
|
52 53 | .header("x-amz-target", "TrentService.GenerateRandom")
|
53 54 | .header("content-length", "20")
|
54 55 | .header("authorization", "AWS4-HMAC-SHA256 Credential=ANOTREAL/20090213/us-east-1/kms/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-target;x-amz-user-agent, Signature=53dcf70f6f852cb576185dcabef5aaa3d068704cf1b7ea7dc644efeaa46674d7")
|
55 56 | .header("x-amz-date", "20090213T233130Z")
|
56 57 | .header("user-agent", "aws-sdk-rust/0.123.test os/windows/XPSP3 lang/rust/1.50.0")
|
57 58 | .header("x-amz-user-agent", "aws-sdk-rust/0.123.test api/test-service/0.123 os/windows/XPSP3 lang/rust/1.50.0")
|
58 59 | .uri(Uri::from_static("https://kms.us-east-1.amazonaws.com/"))
|
59 60 | .body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
60 61 | http_1x::Response::builder()
|
61 62 | .status(http_1x::StatusCode::from_u16(200).unwrap())
|
62 63 | .body(SdkBody::from(r#"{"Plaintext":"6CG0fbzzhg5G2VcFCPmJMJ8Njv3voYCgrGlp3+BZe7eDweCXgiyDH9BnkKvLmS7gQhnYDUlyES3fZVGwv5+CxA=="}"#)).unwrap())
|
63 64 | ]);
|
64 65 | let conf = Config::builder()
|
65 66 | .http_client(http_client.clone())
|
66 67 | .region(Region::new("us-east-1"))
|
67 68 | .credentials_provider(Credentials::for_tests_with_session_token())
|
68 69 | .with_test_defaults()
|
70 + | .retry_config(RetryConfig::disabled())
|
69 71 | .build();
|
70 72 | let client = kms::Client::from_conf(conf);
|
71 73 | let resp = client
|
72 74 | .generate_random()
|
73 75 | .number_of_bytes(64)
|
74 76 | .customize()
|
75 77 | .mutate_request(|req| {
|
76 78 | // Remove the invocation ID since the signed request above doesn't have it
|
77 79 | req.headers_mut().remove("amz-sdk-invocation-id");
|
78 80 | })
|
118 120 | #[cfg(feature = "test-util")]
|
119 121 | #[tokio::test]
|
120 122 | async fn generate_random_keystore_not_found() {
|
121 123 | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
122 124 | http_1x::Request::builder()
|
123 125 | .header("content-type", "application/x-amz-json-1.1")
|
124 126 | .header("x-amz-target", "TrentService.GenerateRandom")
|
125 127 | .header("content-length", "56")
|
126 128 | .header("authorization", "AWS4-HMAC-SHA256 Credential=ANOTREAL/20090213/us-east-1/kms/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-target, Signature=ffef92c6b75d66cc511daa896eb4a085ec053a2592e17d1f22ecaf167f2fa4bb")
|
127 129 | .header("x-amz-date", "20090213T233130Z")
|
128 130 | .header("user-agent", "aws-sdk-rust/0.123.test os/windows/XPSP3 lang/rust/1.50.0")
|
129 131 | .header("x-amz-user-agent", "aws-sdk-rust/0.123.test api/test-service/0.123 os/windows/XPSP3 lang/rust/1.50.0")
|
130 132 | .uri(Uri::from_static("https://kms.us-east-1.amazonaws.com/"))
|
131 133 | .body(SdkBody::from(r#"{"NumberOfBytes":64,"CustomKeyStoreId":"does not exist"}"#)).unwrap(),
|
132 134 | http_1x::Response::builder()
|
133 135 | .status(http_1x::StatusCode::from_u16(400).unwrap())
|
134 136 | .header("x-amzn-requestid", "bfe81a0a-9a08-4e71-9910-cdb5ab6ea3b6")
|
135 137 | .header("cache-control", "no-cache, no-store, must-revalidate, private")
|
136 138 | .header("expires", "0")
|
137 139 | .header("pragma", "no-cache")
|
138 140 | .header("date", "Fri, 05 Mar 2021 15:01:40 GMT")
|
139 141 | .header("content-type", "application/x-amz-json-1.1")
|
140 142 | .header("content-length", "44")
|
141 143 | .body(SdkBody::from(r#"{"__type":"CustomKeyStoreNotFoundException"}"#)).unwrap())
|
142 144 | ]);
|
143 145 | let conf = Config::builder()
|
144 146 | .http_client(http_client.clone())
|
145 147 | .region(Region::new("us-east-1"))
|
146 148 | .credentials_provider(Credentials::for_tests_with_session_token())
|
147 149 | .with_test_defaults()
|
150 + | .retry_config(RetryConfig::disabled())
|
148 151 | .build();
|
149 152 | let client = kms::Client::from_conf(conf);
|
150 153 |
|
151 154 | let err = client
|
152 155 | .generate_random()
|
153 156 | .number_of_bytes(64)
|
154 157 | .custom_key_store_id("does not exist")
|
155 158 | .send()
|
156 159 | .await
|
157 160 | .expect_err("key store doesn't exist");
|