aws_config/default_provider/
endpoint_url.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_url;
7use crate::provider_config::ProviderConfig;
8use aws_runtime::env_config::EnvConfigValue;
9use aws_smithy_types::error::display::DisplayErrorContext;
10use aws_types::origin::Origin;
11
12mod env {
13    pub(super) const ENDPOINT_URL: &str = "AWS_ENDPOINT_URL";
14}
15
16mod profile_key {
17    pub(super) const ENDPOINT_URL: &str = "endpoint_url";
18}
19
20/// Load the value for an endpoint URL
21///
22/// This checks the following sources:
23/// 1. The environment variable `AWS_ENDPOINT_URL=http://localhost`
24/// 2. The profile key `endpoint_url=http://localhost`
25///
26/// If invalid values are found, the provider will return None and an error will be logged.
27pub async fn endpoint_url_provider(provider_config: &ProviderConfig) -> Option<String> {
28    let env = provider_config.env();
29    let profiles = provider_config.profile().await;
30
31    EnvConfigValue::new()
32        .env(env::ENDPOINT_URL)
33        .profile(profile_key::ENDPOINT_URL)
34        .validate(&env, profiles, parse_url)
35        .map_err(
36            |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for endpoint URL setting"),
37        )
38        .unwrap_or(None)
39}
40
41/// Load the value for an endpoint URL
42///
43/// This checks the following sources:
44/// 1. The environment variable `AWS_ENDPOINT_URL=http://localhost`
45/// 2. The profile key `endpoint_url=http://localhost`
46///
47/// If invalid values are found, the provider will return None and an error will be logged.
48pub async fn endpoint_url_provider_with_origin(
49    provider_config: &ProviderConfig,
50) -> (Option<String>, Origin) {
51    let env = provider_config.env();
52    let profiles = provider_config.profile().await;
53
54    EnvConfigValue::new()
55        .env(env::ENDPOINT_URL)
56        .profile(profile_key::ENDPOINT_URL)
57        .validate_and_return_origin(&env, profiles, parse_url)
58        .map_err(
59            |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for endpoint URL setting"),
60        )
61        .unwrap_or_default()
62}
63
64#[cfg(test)]
65mod test {
66    use super::endpoint_url_provider;
67    use super::env;
68    #[allow(deprecated)]
69    use crate::profile::profile_file::{ProfileFileKind, ProfileFiles};
70    use crate::provider_config::ProviderConfig;
71    use aws_types::os_shim_internal::{Env, Fs};
72    use tracing_test::traced_test;
73
74    #[tokio::test]
75    #[traced_test]
76    async fn log_error_on_invalid_value() {
77        let conf =
78            ProviderConfig::empty().with_env(Env::from_slice(&[(env::ENDPOINT_URL, "not-a-url")]));
79        assert_eq!(None, endpoint_url_provider(&conf).await);
80        assert!(logs_contain("invalid value for endpoint URL setting"));
81        assert!(logs_contain(env::ENDPOINT_URL));
82    }
83
84    #[tokio::test]
85    #[traced_test]
86    async fn environment_priority() {
87        let conf = ProviderConfig::empty()
88            .with_env(Env::from_slice(&[(env::ENDPOINT_URL, "http://localhost")]))
89            .with_profile_config(
90                Some(
91                    #[allow(deprecated)]
92                    ProfileFiles::builder()
93                        .with_file(
94                            #[allow(deprecated)]
95                            ProfileFileKind::Config,
96                            "conf",
97                        )
98                        .build(),
99                ),
100                None,
101            )
102            .with_fs(Fs::from_slice(&[(
103                "conf",
104                "[default]\nendpoint_url = http://production",
105            )]));
106        assert_eq!(
107            Some("http://localhost".to_owned()),
108            endpoint_url_provider(&conf).await,
109        );
110    }
111}