aws_config/default_provider/
request_min_compression_size_bytes.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_uint;
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 REQUEST_MIN_COMPRESSION_SIZE_BYTES: &str =
13        "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES";
14}
15
16mod profile_key {
17    pub(super) const REQUEST_MIN_COMPRESSION_SIZE_BYTES: &str =
18        "request_min_compression_size_bytes";
19}
20
21/// Load the value for "request minimum compression size bytes".
22///
23/// This checks the following sources:
24/// 1. The environment variable `AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES=10240`
25/// 2. The profile key `request_min_compression_size_bytes=10240`
26///
27/// If invalid values are found, the provider will return None and an error will be logged.
28pub(crate) async fn request_min_compression_size_bytes_provider(
29    provider_config: &ProviderConfig,
30) -> Option<u32> {
31    let env = provider_config.env();
32    let profiles = provider_config.profile().await;
33
34    EnvConfigValue::new()
35        .env(env::REQUEST_MIN_COMPRESSION_SIZE_BYTES)
36        .profile(profile_key::REQUEST_MIN_COMPRESSION_SIZE_BYTES)
37        .validate(&env, profiles, parse_uint)
38        .map_err(
39            |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for `request minimum compression size bytes` setting"),
40        )
41        .unwrap_or(None)
42}
43
44#[cfg(test)]
45mod test {
46    use super::request_min_compression_size_bytes_provider;
47    #[allow(deprecated)]
48    use crate::profile::profile_file::{ProfileFileKind, ProfileFiles};
49    use crate::provider_config::ProviderConfig;
50    use aws_types::os_shim_internal::{Env, Fs};
51    use tracing_test::traced_test;
52
53    #[tokio::test]
54    #[traced_test]
55    async fn log_error_on_invalid_value() {
56        let conf = ProviderConfig::empty().with_env(Env::from_slice(&[(
57            "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES",
58            "not-a-uint",
59        )]));
60        assert_eq!(
61            request_min_compression_size_bytes_provider(&conf).await,
62            None
63        );
64        assert!(logs_contain(
65            "invalid value for `request minimum compression size bytes` setting"
66        ));
67        assert!(logs_contain("AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES"));
68    }
69
70    #[tokio::test]
71    #[traced_test]
72    async fn environment_priority() {
73        let conf = ProviderConfig::empty()
74            .with_env(Env::from_slice(&[(
75                "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES",
76                "99",
77            )]))
78            .with_profile_config(
79                Some(
80                    #[allow(deprecated)]
81                    ProfileFiles::builder()
82                        .with_file(
83                            #[allow(deprecated)]
84                            ProfileFileKind::Config,
85                            "conf",
86                        )
87                        .build(),
88                ),
89                None,
90            )
91            .with_fs(Fs::from_slice(&[(
92                "conf",
93                "[default]\nrequest_min_compression_size_bytes = 100",
94            )]));
95        assert_eq!(
96            request_min_compression_size_bytes_provider(&conf).await,
97            Some(99)
98        );
99    }
100
101    #[tokio::test]
102    #[traced_test]
103    async fn profile_config_works() {
104        let conf = ProviderConfig::empty()
105            .with_profile_config(
106                Some(
107                    #[allow(deprecated)]
108                    ProfileFiles::builder()
109                        .with_file(
110                            #[allow(deprecated)]
111                            ProfileFileKind::Config,
112                            "conf",
113                        )
114                        .build(),
115                ),
116                None,
117            )
118            .with_fs(Fs::from_slice(&[(
119                "conf",
120                "[default]\nrequest_min_compression_size_bytes = 22",
121            )]));
122        assert_eq!(
123            request_min_compression_size_bytes_provider(&conf).await,
124            Some(22)
125        );
126    }
127}