aws_config/default_provider/
checksums.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use crate::provider_config::ProviderConfig;
use aws_runtime::env_config::EnvConfigValue;
use aws_smithy_types::error::display::DisplayErrorContext;
use aws_types::sdk_config::{RequestChecksumCalculation, ResponseChecksumValidation};
use std::str::FromStr;

mod env {
    pub(super) const REQUEST_CHECKSUM_CALCULATION: &str = "AWS_REQUEST_CHECKSUM_CALCULATION";
    pub(super) const RESPONSE_CHECKSUM_VALIDATION: &str = "AWS_RESPONSE_CHECKSUM_VALIDATION";
}

mod profile_key {
    pub(super) const REQUEST_CHECKSUM_CALCULATION: &str = "request_checksum_calculation";
    pub(super) const RESPONSE_CHECKSUM_VALIDATION: &str = "response_checksum_validation";
}

/// Load the value for `request_checksum_calculation`
///
/// This checks the following sources:
/// 1. The environment variable `AWS_REQUEST_CHECKSUM_CALCULATION=WHEN_SUPPORTED/WHEN_REQUIRED`
/// 2. The profile key `request_checksum_calculation=WHEN_SUPPORTED/WHEN_REQUIRED`
///
/// If invalid values are found, the provider will return `None` and an error will be logged.
pub async fn request_checksum_calculation_provider(
    provider_config: &ProviderConfig,
) -> Option<RequestChecksumCalculation> {
    let env = provider_config.env();
    let profiles = provider_config.profile().await;

    let loaded = EnvConfigValue::new()
         .env(env::REQUEST_CHECKSUM_CALCULATION)
         .profile(profile_key::REQUEST_CHECKSUM_CALCULATION)
         .validate(&env, profiles, RequestChecksumCalculation::from_str)
         .map_err(
             |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for request_checksum_calculation setting"),
         )
         .unwrap_or(None);

    // request_checksum_calculation should always have a non-None value and the
    // default is WhenSupported
    loaded.or(Some(RequestChecksumCalculation::WhenSupported))
}

/// Load the value for `response_checksum_validation`
///
/// This checks the following sources:
/// 1. The environment variable `AWS_RESPONSE_CHECKSUM_VALIDATION=WHEN_SUPPORTED/WHEN_REQUIRED`
/// 2. The profile key `response_checksum_validation=WHEN_SUPPORTED/WHEN_REQUIRED`
///
/// If invalid values are found, the provider will return `None` and an error will be logged.
pub async fn response_checksum_validation_provider(
    provider_config: &ProviderConfig,
) -> Option<ResponseChecksumValidation> {
    let env = provider_config.env();
    let profiles = provider_config.profile().await;

    let loaded = EnvConfigValue::new()
         .env(env::RESPONSE_CHECKSUM_VALIDATION)
         .profile(profile_key::RESPONSE_CHECKSUM_VALIDATION)
         .validate(&env, profiles, ResponseChecksumValidation::from_str)
         .map_err(
             |err| tracing::warn!(err = %DisplayErrorContext(&err), "invalid value for response_checksum_validation setting"),
         )
         .unwrap_or(None);

    // response_checksum_validation should always have a non-None value and the
    // default is WhenSupported
    loaded.or(Some(ResponseChecksumValidation::WhenSupported))
}

#[cfg(test)]
mod test {
    use crate::default_provider::checksums::{
        request_checksum_calculation_provider, response_checksum_validation_provider,
    };
    #[allow(deprecated)]
    use crate::profile::profile_file::{ProfileFileKind, ProfileFiles};
    use crate::provider_config::ProviderConfig;
    use aws_smithy_types::checksum_config::{
        RequestChecksumCalculation, ResponseChecksumValidation,
    };
    use aws_types::os_shim_internal::{Env, Fs};
    use tracing_test::traced_test;

    #[tokio::test]
    #[traced_test]
    async fn log_error_on_invalid_value_request() {
        let conf = ProviderConfig::empty().with_env(Env::from_slice(&[(
            "AWS_REQUEST_CHECKSUM_CALCULATION",
            "not-a-valid-value",
        )]));
        assert_eq!(
            request_checksum_calculation_provider(&conf).await,
            Some(RequestChecksumCalculation::WhenSupported)
        );
        assert!(logs_contain(
            "invalid value for request_checksum_calculation setting"
        ));
        assert!(logs_contain("AWS_REQUEST_CHECKSUM_CALCULATION"));
    }

    #[tokio::test]
    #[traced_test]
    async fn environment_priority_request() {
        let conf = ProviderConfig::empty()
            .with_env(Env::from_slice(&[(
                "AWS_REQUEST_CHECKSUM_CALCULATION",
                "WHEN_REQUIRED",
            )]))
            .with_profile_config(
                Some(
                    #[allow(deprecated)]
                    ProfileFiles::builder()
                        .with_file(
                            #[allow(deprecated)]
                            ProfileFileKind::Config,
                            "conf",
                        )
                        .build(),
                ),
                None,
            )
            .with_fs(Fs::from_slice(&[(
                "conf",
                "[default]\nrequest_checksum_calculation = WHEN_SUPPORTED",
            )]));
        assert_eq!(
            request_checksum_calculation_provider(&conf).await,
            Some(RequestChecksumCalculation::WhenRequired)
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn profile_works_request() {
        let conf = ProviderConfig::empty()
            .with_profile_config(
                Some(
                    #[allow(deprecated)]
                    ProfileFiles::builder()
                        .with_file(
                            #[allow(deprecated)]
                            ProfileFileKind::Config,
                            "conf",
                        )
                        .build(),
                ),
                None,
            )
            .with_fs(Fs::from_slice(&[(
                "conf",
                "[default]\nrequest_checksum_calculation = WHEN_REQUIRED",
            )]));
        assert_eq!(
            request_checksum_calculation_provider(&conf).await,
            Some(RequestChecksumCalculation::WhenRequired)
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn default_works_request() {
        let conf = ProviderConfig::empty();
        assert_eq!(
            request_checksum_calculation_provider(&conf).await,
            Some(RequestChecksumCalculation::WhenSupported)
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn log_error_on_invalid_value_response() {
        let conf = ProviderConfig::empty().with_env(Env::from_slice(&[(
            "AWS_RESPONSE_CHECKSUM_VALIDATION",
            "not-a-valid-value",
        )]));
        assert_eq!(
            response_checksum_validation_provider(&conf).await,
            Some(ResponseChecksumValidation::WhenSupported)
        );
        assert!(logs_contain(
            "invalid value for response_checksum_validation setting"
        ));
        assert!(logs_contain("AWS_RESPONSE_CHECKSUM_VALIDATION"));
    }

    #[tokio::test]
    #[traced_test]
    async fn environment_priority_response() {
        let conf = ProviderConfig::empty()
            .with_env(Env::from_slice(&[(
                "AWS_RESPONSE_CHECKSUM_VALIDATION",
                "WHEN_SUPPORTED",
            )]))
            .with_profile_config(
                Some(
                    #[allow(deprecated)]
                    ProfileFiles::builder()
                        .with_file(
                            #[allow(deprecated)]
                            ProfileFileKind::Config,
                            "conf",
                        )
                        .build(),
                ),
                None,
            )
            .with_fs(Fs::from_slice(&[(
                "conf",
                "[default]\response_checksum_validation = WHEN_REQUIRED",
            )]));
        assert_eq!(
            response_checksum_validation_provider(&conf).await,
            Some(ResponseChecksumValidation::WhenSupported)
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn profile_works_response() {
        let conf = ProviderConfig::empty()
            .with_profile_config(
                Some(
                    #[allow(deprecated)]
                    ProfileFiles::builder()
                        .with_file(
                            #[allow(deprecated)]
                            ProfileFileKind::Config,
                            "conf",
                        )
                        .build(),
                ),
                None,
            )
            .with_fs(Fs::from_slice(&[(
                "conf",
                "[default]\nresponse_checksum_validation = WHEN_REQUIRED",
            )]));
        assert_eq!(
            response_checksum_validation_provider(&conf).await,
            Some(ResponseChecksumValidation::WhenRequired)
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn default_works_response() {
        let conf = ProviderConfig::empty();
        assert_eq!(
            response_checksum_validation_provider(&conf).await,
            Some(ResponseChecksumValidation::WhenSupported)
        );
    }
}