|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | | -#![allow(clippy::derive_partial_eq_without_eq)] |
7 | | - |
8 | | -use std::error::Error; |
9 | | -use std::fmt; |
10 | | - |
11 | | -use aws_smithy_http::middleware::MapRequest; |
12 | | -use aws_smithy_http::operation::Request; |
13 | | -use aws_smithy_types::endpoint::Endpoint as SmithyEndpoint; |
14 | | -use aws_smithy_types::Document; |
15 | | - |
16 | | -use aws_types::region::{Region, SigningRegion}; |
17 | | -use aws_types::SigningName; |
18 | | - |
19 | | -/// Middleware Stage to add authentication information from a Smithy endpoint into the property bag |
20 | | -/// |
21 | | -/// AwsAuthStage implements [`MapRequest`](MapRequest). It will: |
22 | | -/// 1. Load an endpoint from the property bag |
23 | | -/// 2. Set the `SigningRegion` and `SigningName` in the property bag to drive downstream |
24 | | -/// signing middleware. |
25 | | -#[derive(Clone, Debug)] |
26 | | -pub struct AwsAuthStage; |
27 | | - |
28 | | -#[derive(Debug)] |
29 | | -enum AwsAuthStageErrorKind { |
30 | | - NoEndpointResolver, |
31 | | - EndpointResolutionError(Box<dyn Error + Send + Sync>), |
32 | | -} |
33 | | - |
34 | | -#[derive(Debug)] |
35 | | -pub struct AwsAuthStageError { |
36 | | - kind: AwsAuthStageErrorKind, |
37 | | -} |
38 | | - |
39 | | -impl fmt::Display for AwsAuthStageError { |
40 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
41 | | - use AwsAuthStageErrorKind::*; |
42 | | - match &self.kind { |
43 | | - NoEndpointResolver => write!(f, "endpoint resolution failed: no endpoint present"), |
44 | | - EndpointResolutionError(_) => write!(f, "endpoint resolution failed"), |
45 | | - } |
46 | | - } |
47 | | -} |
48 | | - |
49 | | -impl Error for AwsAuthStageError { |
50 | | - fn source(&self) -> Option<&(dyn Error + 'static)> { |
51 | | - use AwsAuthStageErrorKind::*; |
52 | | - match &self.kind { |
53 | | - EndpointResolutionError(source) => Some(source.as_ref() as _), |
54 | | - NoEndpointResolver => None, |
55 | | - } |
56 | | - } |
57 | | -} |
58 | | - |
59 | | -impl From<AwsAuthStageErrorKind> for AwsAuthStageError { |
60 | | - fn from(kind: AwsAuthStageErrorKind) -> Self { |
61 | | - Self { kind } |
62 | | - } |
63 | | -} |
64 | | - |
65 | | -impl MapRequest for AwsAuthStage { |
66 | | - type Error = AwsAuthStageError; |
67 | | - |
68 | | - fn name(&self) -> &'static str { |
69 | | - "resolve_endpoint" |
70 | | - } |
71 | | - |
72 | | - fn apply(&self, request: Request) -> Result<Request, Self::Error> { |
73 | | - request.augment(|http_req, props| { |
74 | | - let endpoint = props |
75 | | - .get::<aws_smithy_types::endpoint::Endpoint>() |
76 | | - .ok_or(AwsAuthStageErrorKind::NoEndpointResolver)?; |
77 | | - let (signing_region_override, signing_name_override) = smithy_to_aws(endpoint) |
78 | | - .map_err(|err| AwsAuthStageErrorKind::EndpointResolutionError(err))?; |
79 | | - |
80 | | - if let Some(signing_region) = signing_region_override { |
81 | | - props.insert(signing_region); |
82 | | - } |
83 | | - if let Some(signing_name) = signing_name_override { |
84 | | - props.insert(signing_name); |
85 | | - } |
86 | | - Ok(http_req) |
87 | | - }) |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -type EndpointMetadata = (Option<SigningRegion>, Option<SigningName>); |
92 | | - |
93 | | -fn smithy_to_aws(value: &SmithyEndpoint) -> Result<EndpointMetadata, Box<dyn Error + Send + Sync>> { |
94 | | - // look for v4 as an auth scheme |
95 | | - let auth_schemes = match value.properties().get("authSchemes") { |
96 | | - Some(Document::Array(schemes)) => schemes, |
97 | | - // no auth schemes: |
98 | | - None => return Ok((None, None)), |
99 | | - _other => return Err("expected an array for authSchemes".into()), |
100 | | - }; |
101 | | - let auth_schemes = auth_schemes |
102 | | - .iter() |
103 | | - .flat_map(|doc| match doc { |
104 | | - Document::Object(map) => Some(map), |
105 | | - _ => None, |
106 | | - }) |
107 | | - .map(|it| { |
108 | | - let name = match it.get("name") { |
109 | | - Some(Document::String(s)) => Some(s.as_str()), |
110 | | - _ => None, |
111 | | - }; |
112 | | - (name, it) |
113 | | - }); |
114 | | - let (_, v4) = auth_schemes |
115 | | - .clone() |
116 | | - .find(|(name, _doc)| name.as_deref() == Some("sigv4")) |
117 | | - .ok_or_else(|| { |
118 | | - format!( |
119 | | - "No auth schemes were supported. The Rust SDK only supports sigv4. \ |
120 | | - The authentication schemes supported by this endpoint were: {:?}", |
121 | | - auth_schemes.flat_map(|(name, _)| name).collect::<Vec<_>>() |
122 | | - ) |
123 | | - })?; |
124 | | - |
125 | | - let signing_scope = match v4.get("signingRegion") { |
126 | | - Some(Document::String(s)) => Some(SigningRegion::from(Region::new(s.clone()))), |
127 | | - None => None, |
128 | | - _ => return Err("unexpected type".into()), |
129 | | - }; |
130 | | - let signing_name = match v4.get("signingName") { |
131 | | - Some(Document::String(s)) => Some(SigningName::from(s.to_string())), |
132 | | - None => None, |
133 | | - _ => return Err("unexpected type".into()), |
134 | | - }; |
135 | | - Ok((signing_scope, signing_name)) |
136 | | -} |
137 | | - |
138 | | -#[cfg(test)] |
139 | | -mod test { |
140 | | - use std::collections::HashMap; |
141 | | - |
142 | | - use aws_smithy_http::body::SdkBody; |
143 | | - use aws_smithy_http::middleware::MapRequest; |
144 | | - use aws_smithy_http::operation; |
145 | | - use aws_smithy_types::endpoint::Endpoint; |
146 | | - use aws_smithy_types::Document; |
147 | | - use http::header::HOST; |
148 | | - |
149 | | - use aws_types::region::{Region, SigningRegion}; |
150 | | - use aws_types::SigningName; |
151 | | - |
152 | | - use crate::AwsAuthStage; |
153 | | - |
154 | | - #[test] |
155 | | - fn default_endpoint_updates_request() { |
156 | | - let endpoint = Endpoint::builder() |
157 | | - .url("kinesis.us-east-1.amazon.com") |
158 | | - .build(); |
159 | | - let req = http::Request::new(SdkBody::from("")); |
160 | | - let region = Region::new("us-east-1"); |
161 | | - let mut req = operation::Request::new(req); |
162 | | - { |
163 | | - let mut props = req.properties_mut(); |
164 | | - props.insert(SigningRegion::from(region.clone())); |
165 | | - props.insert(SigningName::from_static("kinesis")); |
166 | | - props.insert(endpoint); |
167 | | - }; |
168 | | - let req = AwsAuthStage.apply(req).expect("should succeed"); |
169 | | - assert_eq!(req.properties().get(), Some(&SigningRegion::from(region))); |
170 | | - assert_eq!( |
171 | | - req.properties().get(), |
172 | | - Some(&SigningName::from_static("kinesis")) |
173 | | - ); |
174 | | - |
175 | | - assert!(req.http().headers().get(HOST).is_none()); |
176 | | - assert!( |
177 | | - req.properties().get::<Endpoint>().is_some(), |
178 | | - "Endpoint middleware MUST leave the result in the bag" |
179 | | - ); |
180 | | - } |
181 | | - |
182 | | - #[test] |
183 | | - fn sets_service_override_when_set() { |
184 | | - let endpoint = Endpoint::builder() |
185 | | - .url("kinesis.us-east-override.amazon.com") |
186 | | - .property( |
187 | | - "authSchemes", |
188 | | - vec![Document::Object({ |
189 | | - let mut out = HashMap::new(); |
190 | | - out.insert("name".to_string(), "sigv4".to_string().into()); |
191 | | - out.insert( |
192 | | - "signingName".to_string(), |
193 | | - "qldb-override".to_string().into(), |
194 | | - ); |
195 | | - out.insert( |
196 | | - "signingRegion".to_string(), |
197 | | - "us-east-override".to_string().into(), |
198 | | - ); |
199 | | - out |
200 | | - })], |
201 | | - ) |
202 | | - .build(); |
203 | | - let req = http::Request::new(SdkBody::from("")); |
204 | | - let region = Region::new("us-east-1"); |
205 | | - let mut req = operation::Request::new(req); |
206 | | - { |
207 | | - let mut props = req.properties_mut(); |
208 | | - props.insert(region); |
209 | | - props.insert(SigningName::from_static("qldb")); |
210 | | - props.insert(endpoint); |
211 | | - }; |
212 | | - let req = AwsAuthStage.apply(req).expect("should succeed"); |
213 | | - assert_eq!( |
214 | | - req.properties().get(), |
215 | | - Some(&SigningRegion::from_static("us-east-override")) |
216 | | - ); |
217 | | - assert_eq!( |
218 | | - req.properties().get(), |
219 | | - Some(&SigningName::from_static("qldb-override")) |
220 | | - ); |
221 | | - } |
222 | | - |
223 | | - #[test] |
224 | | - fn supports_fallback_when_scope_is_unset() { |
225 | | - let endpoint = Endpoint::builder().url("www.service.com").build(); |
226 | | - let req = http::Request::new(SdkBody::from("")); |
227 | | - let region = SigningRegion::from_static("us-east-1"); |
228 | | - let mut req = operation::Request::new(req); |
229 | | - { |
230 | | - let mut props = req.properties_mut(); |
231 | | - props.insert(region.clone()); |
232 | | - props.insert(SigningName::from_static("qldb")); |
233 | | - props.insert(endpoint); |
234 | | - }; |
235 | | - let req = AwsAuthStage.apply(req).expect("should succeed"); |
236 | | - assert_eq!(req.properties().get(), Some(®ion)); |
237 | | - assert_eq!( |
238 | | - req.properties().get(), |
239 | | - Some(&SigningName::from_static("qldb")) |
240 | | - ); |
241 | | - } |
242 | | -} |
| 6 | +//! This crate is no longer used by the AWS SDK and is deprecated. |
0 commit comments