Skip to main content

aws_sdk_eksauth/config/
endpoint.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2pub use ::aws_smithy_runtime_api::client::endpoint::EndpointFuture;
3pub use ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver;
4pub use ::aws_smithy_types::endpoint::Endpoint;
5
6/// Interceptor that tracks endpoint override business metric.
7#[derive(Debug, Default)]
8pub(crate) struct EndpointOverrideFeatureTrackerInterceptor;
9
10#[::aws_smithy_runtime_api::client::interceptors::dyn_dispatch_hint]
11impl ::aws_smithy_runtime_api::client::interceptors::Intercept for EndpointOverrideFeatureTrackerInterceptor {
12    fn name(&self) -> &'static str {
13        "EndpointOverrideFeatureTrackerInterceptor"
14    }
15
16    fn read_before_execution(
17        &self,
18        _context: &::aws_smithy_runtime_api::client::interceptors::context::BeforeSerializationInterceptorContextRef<'_>,
19        cfg: &mut ::aws_smithy_types::config_bag::ConfigBag,
20    ) -> ::std::result::Result<(), ::aws_smithy_runtime_api::box_error::BoxError> {
21        if cfg.load::<::aws_types::endpoint_config::EndpointUrl>().is_some() {
22            cfg.interceptor_state()
23                .store_append(::aws_runtime::sdk_feature::AwsSdkFeature::EndpointOverride);
24        }
25        ::std::result::Result::Ok(())
26    }
27}
28
29#[cfg(test)]
30mod test {
31
32    /// For region us-east-1 with FIPS enabled and DualStack enabled
33    #[test]
34    fn test_1() {
35        let params = crate::config::endpoint::Params::builder()
36            .region("us-east-1".to_string())
37            .use_fips(true)
38            .build()
39            .expect("invalid params");
40        let resolver = crate::config::endpoint::DefaultResolver::new();
41        let endpoint = resolver.resolve_endpoint(&params);
42        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth-fips.us-east-1.api.aws");
43        assert_eq!(
44            endpoint,
45            ::aws_smithy_types::endpoint::Endpoint::builder()
46                .url("https://eks-auth-fips.us-east-1.api.aws")
47                .build()
48        );
49    }
50
51    /// For region us-east-1 with FIPS disabled and DualStack enabled
52    #[test]
53    fn test_2() {
54        let params = crate::config::endpoint::Params::builder()
55            .region("us-east-1".to_string())
56            .use_fips(false)
57            .build()
58            .expect("invalid params");
59        let resolver = crate::config::endpoint::DefaultResolver::new();
60        let endpoint = resolver.resolve_endpoint(&params);
61        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth.us-east-1.api.aws");
62        assert_eq!(
63            endpoint,
64            ::aws_smithy_types::endpoint::Endpoint::builder()
65                .url("https://eks-auth.us-east-1.api.aws")
66                .build()
67        );
68    }
69
70    /// For region cn-north-1 with FIPS enabled and DualStack enabled
71    #[test]
72    fn test_3() {
73        let params = crate::config::endpoint::Params::builder()
74            .region("cn-north-1".to_string())
75            .use_fips(true)
76            .build()
77            .expect("invalid params");
78        let resolver = crate::config::endpoint::DefaultResolver::new();
79        let endpoint = resolver.resolve_endpoint(&params);
80        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth-fips.cn-north-1.api.amazonwebservices.com.cn");
81        assert_eq!(
82            endpoint,
83            ::aws_smithy_types::endpoint::Endpoint::builder()
84                .url("https://eks-auth-fips.cn-north-1.api.amazonwebservices.com.cn")
85                .build()
86        );
87    }
88
89    /// For region cn-north-1 with FIPS disabled and DualStack enabled
90    #[test]
91    fn test_4() {
92        let params = crate::config::endpoint::Params::builder()
93            .region("cn-north-1".to_string())
94            .use_fips(false)
95            .build()
96            .expect("invalid params");
97        let resolver = crate::config::endpoint::DefaultResolver::new();
98        let endpoint = resolver.resolve_endpoint(&params);
99        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth.cn-north-1.api.amazonwebservices.com.cn");
100        assert_eq!(
101            endpoint,
102            ::aws_smithy_types::endpoint::Endpoint::builder()
103                .url("https://eks-auth.cn-north-1.api.amazonwebservices.com.cn")
104                .build()
105        );
106    }
107
108    /// For region us-gov-east-1 with FIPS enabled and DualStack enabled
109    #[test]
110    fn test_5() {
111        let params = crate::config::endpoint::Params::builder()
112            .region("us-gov-east-1".to_string())
113            .use_fips(true)
114            .build()
115            .expect("invalid params");
116        let resolver = crate::config::endpoint::DefaultResolver::new();
117        let endpoint = resolver.resolve_endpoint(&params);
118        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth-fips.us-gov-east-1.api.aws");
119        assert_eq!(
120            endpoint,
121            ::aws_smithy_types::endpoint::Endpoint::builder()
122                .url("https://eks-auth-fips.us-gov-east-1.api.aws")
123                .build()
124        );
125    }
126
127    /// For region us-gov-east-1 with FIPS disabled and DualStack enabled
128    #[test]
129    fn test_6() {
130        let params = crate::config::endpoint::Params::builder()
131            .region("us-gov-east-1".to_string())
132            .use_fips(false)
133            .build()
134            .expect("invalid params");
135        let resolver = crate::config::endpoint::DefaultResolver::new();
136        let endpoint = resolver.resolve_endpoint(&params);
137        let endpoint = endpoint.expect("Expected valid endpoint: https://eks-auth.us-gov-east-1.api.aws");
138        assert_eq!(
139            endpoint,
140            ::aws_smithy_types::endpoint::Endpoint::builder()
141                .url("https://eks-auth.us-gov-east-1.api.aws")
142                .build()
143        );
144    }
145
146    /// For custom endpoint with region set and fips disabled and dualstack disabled
147    #[test]
148    fn test_7() {
149        let params = crate::config::endpoint::Params::builder()
150            .region("us-east-1".to_string())
151            .use_fips(false)
152            .endpoint("https://example.com".to_string())
153            .build()
154            .expect("invalid params");
155        let resolver = crate::config::endpoint::DefaultResolver::new();
156        let endpoint = resolver.resolve_endpoint(&params);
157        let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
158        assert_eq!(
159            endpoint,
160            ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
161        );
162    }
163
164    /// For custom endpoint with region not set and fips disabled and dualstack disabled
165    #[test]
166    fn test_8() {
167        let params = crate::config::endpoint::Params::builder()
168            .use_fips(false)
169            .endpoint("https://example.com".to_string())
170            .build()
171            .expect("invalid params");
172        let resolver = crate::config::endpoint::DefaultResolver::new();
173        let endpoint = resolver.resolve_endpoint(&params);
174        let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
175        assert_eq!(
176            endpoint,
177            ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
178        );
179    }
180
181    /// For custom endpoint with fips enabled and dualstack disabled
182    #[test]
183    fn test_9() {
184        let params = crate::config::endpoint::Params::builder()
185            .region("us-east-1".to_string())
186            .use_fips(true)
187            .endpoint("https://example.com".to_string())
188            .build()
189            .expect("invalid params");
190        let resolver = crate::config::endpoint::DefaultResolver::new();
191        let endpoint = resolver.resolve_endpoint(&params);
192        let error = endpoint.expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled and dualstack disabled]");
193        assert_eq!(format!("{}", error), "Invalid Configuration: FIPS and custom endpoint are not supported")
194    }
195
196    /// Missing region
197    #[test]
198    fn test_10() {
199        let params = crate::config::endpoint::Params::builder().build().expect("invalid params");
200        let resolver = crate::config::endpoint::DefaultResolver::new();
201        let endpoint = resolver.resolve_endpoint(&params);
202        let error = endpoint.expect_err("expected error: Invalid Configuration: Missing Region [Missing region]");
203        assert_eq!(format!("{}", error), "Invalid Configuration: Missing Region")
204    }
205}
206
207/// Endpoint resolver trait specific to Amazon EKS Auth
208pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
209    /// Resolve an endpoint with the given parameters
210    fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
211
212    /// Convert this service-specific resolver into a `SharedEndpointResolver`
213    ///
214    /// The resulting resolver will downcast `EndpointResolverParams` into `crate::config::endpoint::Params`.
215    fn into_shared_resolver(self) -> ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver
216    where
217        Self: Sized + 'static,
218    {
219        ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver::new(DowncastParams(self))
220    }
221}
222
223#[derive(Debug)]
224struct DowncastParams<T>(T);
225impl<T> ::aws_smithy_runtime_api::client::endpoint::ResolveEndpoint for DowncastParams<T>
226where
227    T: ResolveEndpoint,
228{
229    fn resolve_endpoint<'a>(
230        &'a self,
231        params: &'a ::aws_smithy_runtime_api::client::endpoint::EndpointResolverParams,
232    ) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
233        let ep = match params.get::<crate::config::endpoint::Params>() {
234            Some(params) => self.0.resolve_endpoint(params),
235            None => ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(Err("params of expected type was not present".into())),
236        };
237        ep
238    }
239}
240
241/// The default endpoint resolver
242#[derive(Debug, Default)]
243pub struct DefaultResolver {
244    partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
245}
246
247impl DefaultResolver {
248    /// Create a new endpoint resolver with default settings
249    pub fn new() -> Self {
250        Self {
251            partition_resolver: crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER.clone(),
252        }
253    }
254
255    fn resolve_endpoint(
256        &self,
257        params: &crate::config::endpoint::Params,
258    ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
259        let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
260        Ok(
261            crate::config::endpoint::internals::resolve_endpoint(params, &mut diagnostic_collector, &self.partition_resolver)
262                .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))?,
263        )
264    }
265}
266
267impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
268    fn resolve_endpoint(&self, params: &crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'_> {
269        ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(self.resolve_endpoint(params))
270    }
271}
272
273#[non_exhaustive]
274#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
275/// Configuration parameters for resolving the correct endpoint
276pub struct Params {
277    /// The AWS region used to dispatch the request.
278    pub(crate) region: ::std::option::Option<::std::string::String>,
279    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
280    pub(crate) use_fips: bool,
281    /// Override the endpoint used to send this request
282    pub(crate) endpoint: ::std::option::Option<::std::string::String>,
283}
284impl Params {
285    /// Create a builder for [`Params`]
286    pub fn builder() -> crate::config::endpoint::ParamsBuilder {
287        crate::config::endpoint::ParamsBuilder::default()
288    }
289    /// The AWS region used to dispatch the request.
290    pub fn region(&self) -> ::std::option::Option<&str> {
291        self.region.as_deref()
292    }
293    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
294    pub fn use_fips(&self) -> ::std::option::Option<bool> {
295        Some(self.use_fips)
296    }
297    /// Override the endpoint used to send this request
298    pub fn endpoint(&self) -> ::std::option::Option<&str> {
299        self.endpoint.as_deref()
300    }
301}
302
303/// Builder for [`Params`]
304#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
305pub struct ParamsBuilder {
306    region: ::std::option::Option<::std::string::String>,
307    use_fips: ::std::option::Option<bool>,
308    endpoint: ::std::option::Option<::std::string::String>,
309}
310impl ParamsBuilder {
311    /// Consume this builder, creating [`Params`].
312    pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
313        if let Some(region) = &self.region {
314            if !crate::endpoint_lib::host::is_valid_host_label(
315                region.as_ref() as &str,
316                true,
317                &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
318            ) {
319                return Err(crate::config::endpoint::InvalidParams::invalid_value(
320                    "region",
321                    "must be a valid host label",
322                ));
323            }
324        };
325        Ok(
326            #[allow(clippy::unnecessary_lazy_evaluations)]
327            crate::config::endpoint::Params {
328                region: self.region,
329                use_fips: self
330                    .use_fips
331                    .or_else(|| Some(false))
332                    .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
333                endpoint: self.endpoint,
334            },
335        )
336    }
337    /// Sets the value for region
338    ///
339    /// The AWS region used to dispatch the request.
340    pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
341        self.region = Some(value.into());
342        self
343    }
344
345    /// Sets the value for region
346    ///
347    /// The AWS region used to dispatch the request.
348    pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
349        self.region = param;
350        self
351    }
352    /// Sets the value for use_fips
353    ///
354    /// When unset, this parameter has a default value of `false`.
355    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
356    pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
357        self.use_fips = Some(value.into());
358        self
359    }
360
361    /// Sets the value for use_fips
362    ///
363    /// When unset, this parameter has a default value of `false`.
364    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
365    pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
366        self.use_fips = param;
367        self
368    }
369    /// Sets the value for endpoint
370    ///
371    /// Override the endpoint used to send this request
372    pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
373        self.endpoint = Some(value.into());
374        self
375    }
376
377    /// Sets the value for endpoint
378    ///
379    /// Override the endpoint used to send this request
380    pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
381        self.endpoint = param;
382        self
383    }
384}
385
386/// An error that occurred during endpoint resolution
387#[derive(Debug)]
388pub struct InvalidParams {
389    field: std::borrow::Cow<'static, str>,
390    kind: InvalidParamsErrorKind,
391}
392
393/// The kind of invalid parameter error
394#[derive(Debug)]
395enum InvalidParamsErrorKind {
396    MissingField,
397    InvalidValue { message: &'static str },
398}
399
400impl InvalidParams {
401    #[allow(dead_code)]
402    fn missing(field: &'static str) -> Self {
403        Self {
404            field: field.into(),
405            kind: InvalidParamsErrorKind::MissingField,
406        }
407    }
408
409    #[allow(dead_code)]
410    fn invalid_value(field: &'static str, message: &'static str) -> Self {
411        Self {
412            field: field.into(),
413            kind: InvalidParamsErrorKind::InvalidValue { message },
414        }
415    }
416}
417
418impl std::fmt::Display for InvalidParams {
419    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
420        match self.kind {
421            InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
422            InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
423        }
424    }
425}
426
427impl std::error::Error for InvalidParams {}
428
429mod internals;