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 - | use aws_smithy_runtime::client::http::test_util::{ReplayEvent, StaticReplayClient};
|
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 http::Uri;
|
11 + | use http_1x::Uri;
|
12 12 | use kms::config::{Config, Credentials, Region};
|
13 13 |
|
14 14 | // TODO(DVR): having the full HTTP requests right in the code is a bit gross, consider something
|
15 15 | // like https://github.com/davidbarsky/sigv4/blob/master/aws-sigv4/src/lib.rs#L283-L315 to store
|
16 16 | // the requests/responses externally
|
17 17 |
|
18 18 | /// Validate that for CN regions we set the URI correctly
|
19 19 | #[tokio::test]
|
20 20 | async fn generate_random_cn() {
|
21 21 | let http_client= StaticReplayClient::new(vec![ReplayEvent::new(
|
22 - | http::Request::builder()
|
22 + | http_1x::Request::builder()
|
23 23 | .uri(Uri::from_static("https://kms.cn-north-1.amazonaws.com.cn/"))
|
24 24 | .body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
25 - | http::Response::builder()
|
26 - | .status(http::StatusCode::from_u16(200).unwrap())
|
25 + | http_1x::Response::builder()
|
26 + | .status(http_1x::StatusCode::from_u16(200).unwrap())
|
27 27 | .body(SdkBody::from(r#"{"Plaintext":"6CG0fbzzhg5G2VcFCPmJMJ8Njv3voYCgrGlp3+BZe7eDweCXgiyDH9BnkKvLmS7gQhnYDUlyES3fZVGwv5+CxA=="}"#)).unwrap())
|
28 28 | ]);
|
29 29 | let conf = Config::builder()
|
30 30 | .http_client(http_client.clone())
|
31 31 | .region(Region::new("cn-north-1"))
|
32 32 | .credentials_provider(Credentials::for_tests())
|
33 33 | .build();
|
34 34 | let client = kms::Client::from_conf(conf);
|
35 35 | let _ = client
|
36 36 | .generate_random()
|
37 37 | .number_of_bytes(64)
|
38 38 | .send()
|
39 39 | .await
|
40 40 | .expect("success");
|
41 41 |
|
42 42 | assert_eq!(http_client.actual_requests().count(), 1);
|
43 43 | http_client.assert_requests_match(&[]);
|
44 44 | }
|
45 45 |
|
46 46 | #[cfg(feature = "test-util")]
|
47 47 | #[tokio::test]
|
48 48 | async fn generate_random() {
|
49 49 | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
50 - | http::Request::builder()
|
50 + | http_1x::Request::builder()
|
51 51 | .header("content-type", "application/x-amz-json-1.1")
|
52 52 | .header("x-amz-target", "TrentService.GenerateRandom")
|
53 53 | .header("content-length", "20")
|
54 54 | .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 55 | .header("x-amz-date", "20090213T233130Z")
|
56 56 | .header("user-agent", "aws-sdk-rust/0.123.test os/windows/XPSP3 lang/rust/1.50.0")
|
57 57 | .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 58 | .uri(Uri::from_static("https://kms.us-east-1.amazonaws.com/"))
|
59 59 | .body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
60 - | http::Response::builder()
|
61 - | .status(http::StatusCode::from_u16(200).unwrap())
|
60 + | http_1x::Response::builder()
|
61 + | .status(http_1x::StatusCode::from_u16(200).unwrap())
|
62 62 | .body(SdkBody::from(r#"{"Plaintext":"6CG0fbzzhg5G2VcFCPmJMJ8Njv3voYCgrGlp3+BZe7eDweCXgiyDH9BnkKvLmS7gQhnYDUlyES3fZVGwv5+CxA=="}"#)).unwrap())
|
63 63 | ]);
|
64 64 | let conf = Config::builder()
|
65 65 | .http_client(http_client.clone())
|
66 66 | .region(Region::new("us-east-1"))
|
67 67 | .credentials_provider(Credentials::for_tests_with_session_token())
|
68 68 | .with_test_defaults()
|
69 69 | .build();
|
70 70 | let client = kms::Client::from_conf(conf);
|
71 71 | let resp = client
|
72 72 | .generate_random()
|
73 73 | .number_of_bytes(64)
|
74 74 | .customize()
|
75 75 | .mutate_request(|req| {
|
76 76 | // Remove the invocation ID since the signed request above doesn't have it
|
77 77 | req.headers_mut().remove("amz-sdk-invocation-id");
|
78 78 | })
|
79 79 | .send()
|
80 80 | .await
|
81 81 | .expect("request should succeed");
|
82 82 | // primitive checksum
|
83 83 | assert_eq!(
|
84 84 | resp.plaintext
|
85 85 | .expect("blob should exist")
|
86 86 | .as_ref()
|
87 87 | .iter()
|
88 88 | .map(|i| *i as u32)
|
89 89 | .sum::<u32>(),
|
90 90 | 8562
|
91 91 | );
|
92 92 | http_client.relaxed_requests_match();
|
93 93 | }
|
94 94 |
|
95 95 | #[tokio::test]
|
96 96 | async fn generate_random_malformed_response() {
|
97 97 | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
98 - | http::Request::builder().body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
99 - | http::Response::builder()
|
100 - | .status(http::StatusCode::from_u16(200).unwrap())
|
98 + | http_1x::Request::builder().body(SdkBody::from(r#"{"NumberOfBytes":64}"#)).unwrap(),
|
99 + | http_1x::Response::builder()
|
100 + | .status(http_1x::StatusCode::from_u16(200).unwrap())
|
101 101 | // last `}` replaced with a space, invalid JSON
|
102 102 | .body(SdkBody::from(r#"{"Plaintext":"6CG0fbzzhg5G2VcFCPmJMJ8Njv3voYCgrGlp3+BZe7eDweCXgiyDH9BnkKvLmS7gQhnYDUlyES3fZVGwv5+CxA==" "#)).unwrap())
|
103 103 | ]);
|
104 104 | let conf = Config::builder()
|
105 105 | .http_client(http_client.clone())
|
106 106 | .region(Region::new("us-east-1"))
|
107 107 | .credentials_provider(Credentials::for_tests())
|
108 108 | .build();
|
109 109 | let client = kms::Client::from_conf(conf);
|
110 110 | client
|
111 111 | .generate_random()
|
112 112 | .number_of_bytes(64)
|
113 113 | .send()
|
114 114 | .await
|
115 115 | .expect_err("response was malformed");
|
116 116 | }
|
117 117 |
|
118 118 | #[cfg(feature = "test-util")]
|
119 119 | #[tokio::test]
|
120 120 | async fn generate_random_keystore_not_found() {
|
121 121 | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
122 - | http::Request::builder()
|
122 + | http_1x::Request::builder()
|
123 123 | .header("content-type", "application/x-amz-json-1.1")
|
124 124 | .header("x-amz-target", "TrentService.GenerateRandom")
|
125 125 | .header("content-length", "56")
|
126 126 | .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 127 | .header("x-amz-date", "20090213T233130Z")
|
128 128 | .header("user-agent", "aws-sdk-rust/0.123.test os/windows/XPSP3 lang/rust/1.50.0")
|
129 129 | .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 130 | .uri(Uri::from_static("https://kms.us-east-1.amazonaws.com/"))
|
131 131 | .body(SdkBody::from(r#"{"NumberOfBytes":64,"CustomKeyStoreId":"does not exist"}"#)).unwrap(),
|
132 - | http::Response::builder()
|
133 - | .status(http::StatusCode::from_u16(400).unwrap())
|
132 + | http_1x::Response::builder()
|
133 + | .status(http_1x::StatusCode::from_u16(400).unwrap())
|
134 134 | .header("x-amzn-requestid", "bfe81a0a-9a08-4e71-9910-cdb5ab6ea3b6")
|
135 135 | .header("cache-control", "no-cache, no-store, must-revalidate, private")
|
136 136 | .header("expires", "0")
|
137 137 | .header("pragma", "no-cache")
|
138 138 | .header("date", "Fri, 05 Mar 2021 15:01:40 GMT")
|
139 139 | .header("content-type", "application/x-amz-json-1.1")
|
140 140 | .header("content-length", "44")
|
141 141 | .body(SdkBody::from(r#"{"__type":"CustomKeyStoreNotFoundException"}"#)).unwrap())
|
142 142 | ]);
|
143 143 | let conf = Config::builder()
|