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
use crate::parsing::parse_str_as_timeout;
use crate::profile::Profile;
use crate::provider_config::ProviderConfig;
use aws_smithy_types::timeout;
use aws_smithy_types::tristate::TriState;
use aws_types::os_shim_internal::{Env, Fs};
use std::time::Duration;
const PROFILE_VAR_CONNECT_TIMEOUT: &str = "connect_timeout";
const PROFILE_VAR_TLS_NEGOTIATION_TIMEOUT: &str = "tls_negotiation_timeout";
const PROFILE_VAR_READ_TIMEOUT: &str = "read_timeout";
const PROFILE_VAR_API_CALL_ATTEMPT_TIMEOUT: &str = "api_call_attempt_timeout";
const PROFILE_VAR_API_CALL_TIMEOUT: &str = "api_call_timeout";
#[derive(Debug, Default)]
pub struct ProfileFileTimeoutConfigProvider {
fs: Fs,
env: Env,
profile_override: Option<String>,
}
#[derive(Debug, Default)]
pub struct Builder {
config: Option<ProviderConfig>,
profile_override: Option<String>,
}
impl Builder {
pub fn configure(mut self, config: &ProviderConfig) -> Self {
self.config = Some(config.clone());
self
}
pub fn profile_name(mut self, profile_name: impl Into<String>) -> Self {
self.profile_override = Some(profile_name.into());
self
}
pub fn build(self) -> ProfileFileTimeoutConfigProvider {
let conf = self.config.unwrap_or_default();
ProfileFileTimeoutConfigProvider {
env: conf.env(),
fs: conf.fs(),
profile_override: self.profile_override,
}
}
}
impl ProfileFileTimeoutConfigProvider {
pub fn new() -> Self {
Self {
fs: Fs::real(),
env: Env::real(),
profile_override: None,
}
}
pub fn builder() -> Builder {
Builder::default()
}
pub async fn timeout_config(&self) -> Result<timeout::Config, timeout::ConfigError> {
let profile = match super::parser::load(&self.fs, &self.env).await {
Ok(profile) => profile,
Err(err) => {
tracing::warn!(err = %err, "failed to parse profile, skipping it");
return Ok(Default::default());
}
};
let selected_profile = self
.profile_override
.as_deref()
.unwrap_or_else(|| profile.selected_profile());
let selected_profile = match profile.get_profile(selected_profile) {
Some(profile) => profile,
None => {
if self.profile_override.is_some() {
tracing::warn!(
"failed to get selected '{}' profile, skipping it",
selected_profile
);
}
return Ok(timeout::Config::new());
}
};
for timeout in [
PROFILE_VAR_CONNECT_TIMEOUT,
PROFILE_VAR_TLS_NEGOTIATION_TIMEOUT,
PROFILE_VAR_READ_TIMEOUT,
] {
warn_if_unsupported_timeout_is_set(selected_profile, timeout);
}
let api_call_attempt_timeout = construct_timeout_from_profile_var(
selected_profile,
PROFILE_VAR_API_CALL_ATTEMPT_TIMEOUT,
)?;
let api_call_timeout =
construct_timeout_from_profile_var(selected_profile, PROFILE_VAR_API_CALL_TIMEOUT)?;
let api_timeouts = timeout::Api::new()
.with_call_timeout(api_call_timeout)
.with_call_attempt_timeout(api_call_attempt_timeout);
Ok(timeout::Config::new().with_api_timeouts(api_timeouts))
}
}
fn construct_timeout_from_profile_var(
profile: &Profile,
var: &'static str,
) -> Result<TriState<Duration>, timeout::ConfigError> {
let profile_name = format!("aws profile [{}]", profile.name());
match profile.get(var) {
Some(timeout) => {
parse_str_as_timeout(timeout, var.into(), profile_name.into()).map(TriState::Set)
}
None => Ok(TriState::Unset),
}
}
fn warn_if_unsupported_timeout_is_set(profile: &Profile, var: &'static str) {
if profile.get(var).is_some() {
tracing::warn!(
"Profile '{}' set {} timeout but that feature is currently unimplemented so the setting will be ignored. \
To help us prioritize support for this feature, please upvote aws-sdk-rust#151 (https://github.com/awslabs/aws-sdk-rust/issues/151)",
profile.name(),
var
)
}
}