aws_config/default_provider/
use_dual_stack.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use crate::environment::parse_bool;
7use crate::provider_config::ProviderConfig;
8use aws_runtime::env_config::EnvConfigValue;
9use aws_smithy_types::error::display::DisplayErrorContext;
10
11mod env {
12    pub(super) const USE_DUAL_STACK: &str = "AWS_USE_DUALSTACK_ENDPOINT";
13}
14
15mod profile_key {
16    pub(super) const USE_DUAL_STACK: &str = "use_dualstack_endpoint";
17}
18
19pub(crate) async fn use_dual_stack_provider(provider_config: &ProviderConfig) -> Option<bool> {
20    let env = provider_config.env();
21    let profiles = provider_config.profile().await;
22
23    EnvConfigValue::new()
24        .env(env::USE_DUAL_STACK)
25        .profile(profile_key::USE_DUAL_STACK)
26        .validate(&env, profiles, parse_bool)
27        .map_err(
28            |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for dual-stack setting"),
29        )
30        .unwrap_or(None)
31}
32
33#[cfg(test)]
34mod test {
35    use crate::default_provider::use_dual_stack::use_dual_stack_provider;
36    #[allow(deprecated)]
37    use crate::profile::profile_file::{ProfileFileKind, ProfileFiles};
38    use crate::provider_config::ProviderConfig;
39    use aws_types::os_shim_internal::{Env, Fs};
40    use tracing_test::traced_test;
41
42    #[tokio::test]
43    #[traced_test]
44    async fn log_error_on_invalid_value() {
45        let conf = ProviderConfig::empty().with_env(Env::from_slice(&[(
46            "AWS_USE_DUALSTACK_ENDPOINT",
47            "not-a-boolean",
48        )]));
49        assert_eq!(use_dual_stack_provider(&conf).await, None);
50        assert!(logs_contain("invalid value for dual-stack setting"));
51        assert!(logs_contain("AWS_USE_DUALSTACK_ENDPOINT"));
52    }
53
54    #[tokio::test]
55    #[traced_test]
56    async fn environment_priority() {
57        let conf = ProviderConfig::empty()
58            .with_env(Env::from_slice(&[("AWS_USE_DUALSTACK_ENDPOINT", "TRUE")]))
59            .with_profile_config(
60                Some(
61                    #[allow(deprecated)]
62                    ProfileFiles::builder()
63                        .with_file(
64                            #[allow(deprecated)]
65                            ProfileFileKind::Config,
66                            "conf",
67                        )
68                        .build(),
69                ),
70                None,
71            )
72            .with_fs(Fs::from_slice(&[(
73                "conf",
74                "[default]\nuse_dualstack_endpoint = false",
75            )]));
76        assert_eq!(use_dual_stack_provider(&conf).await, Some(true));
77    }
78
79    #[tokio::test]
80    #[traced_test]
81    async fn profile_works() {
82        let conf = ProviderConfig::empty()
83            .with_profile_config(
84                Some(
85                    #[allow(deprecated)]
86                    ProfileFiles::builder()
87                        .with_file(
88                            #[allow(deprecated)]
89                            ProfileFileKind::Config,
90                            "conf",
91                        )
92                        .build(),
93                ),
94                None,
95            )
96            .with_fs(Fs::from_slice(&[(
97                "conf",
98                "[default]\nuse_dualstack_endpoint = false",
99            )]));
100        assert_eq!(use_dual_stack_provider(&conf).await, Some(false));
101    }
102}