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
use aws_config::{
meta::region::RegionProviderChain,
profile::{ProfileFileCredentialsProvider, ProfileFileRegionProvider},
provider_config::ProviderConfig,
retry::RetryConfig,
};
use aws_types::{region::Region, SdkConfig};
use clap::Args;
const DEFAULT_REGION: &str = "us-east-1";
#[derive(Args, Clone, Debug)]
pub struct RemoteConfig {
#[arg(short, long)]
pub profile: Option<String>,
#[arg(short, long)]
pub region: Option<String>,
#[arg(short, long)]
pub alias: Option<String>,
#[arg(long, default_value = "1")]
retry_attempts: u32,
}
impl RemoteConfig {
pub async fn sdk_config(&self, retry: Option<RetryConfig>) -> SdkConfig {
let explicit_region = self.region.clone().map(Region::new);
let region_provider = RegionProviderChain::first_try(explicit_region.clone())
.or_default_provider()
.or_else(Region::new(DEFAULT_REGION));
let retry =
retry.unwrap_or_else(|| RetryConfig::standard().with_max_attempts(self.retry_attempts));
let mut config_loader = aws_config::from_env()
.region(region_provider)
.retry_config(retry);
if let Some(profile) = &self.profile {
let profile_region = ProfileFileRegionProvider::builder()
.profile_name(profile)
.build();
let region_provider =
RegionProviderChain::first_try(explicit_region).or_else(profile_region);
let region = region_provider.region().await;
let conf = ProviderConfig::default().with_region(region);
let creds_provider = ProfileFileCredentialsProvider::builder()
.profile_name(profile)
.configure(&conf)
.build();
config_loader = config_loader
.region(region_provider)
.credentials_provider(creds_provider);
}
config_loader.load().await
}
}
pub mod aws_sdk_config {
pub use aws_types::SdkConfig;
}
pub use aws_sdk_lambda;
#[cfg(test)]
mod tests {
use aws_credential_types::provider::ProvideCredentials;
use aws_sdk_lambda::Region;
use crate::RemoteConfig;
fn setup() {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
std::env::set_var(
"AWS_CONFIG_FILE",
format!("{manifest_dir}/test-data/aws_config"),
);
std::env::set_var(
"AWS_SHARED_CREDENTIALS_FILE",
format!("{manifest_dir}/test-data/aws_credentials"),
);
}
#[tokio::test]
async fn undefined_profile() {
setup();
let args = RemoteConfig {
profile: Some("durian".to_owned()),
region: None,
alias: None,
retry_attempts: 1,
};
let config = args.sdk_config(None).await;
let creds = config
.credentials_provider()
.unwrap()
.provide_credentials()
.await;
assert_eq!(config.region(), None);
assert!(creds.is_err());
}
#[tokio::test]
async fn undefined_profile_with_creds() {
setup();
let args = RemoteConfig {
profile: Some("cherry".to_owned()),
region: None,
alias: None,
retry_attempts: 1,
};
let config = args.sdk_config(None).await;
let creds = config
.credentials_provider()
.unwrap()
.provide_credentials()
.await
.unwrap();
assert_eq!(config.region(), None);
assert_eq!(creds.access_key_id(), "CCCCCCCCCCCCCCCCCCCC");
}
#[tokio::test]
async fn profile_with_region() {
setup();
let args = RemoteConfig {
profile: Some("apple".to_owned()),
region: None,
alias: None,
retry_attempts: 1,
};
let config = args.sdk_config(None).await;
let creds = config
.credentials_provider()
.unwrap()
.provide_credentials()
.await
.unwrap();
assert_eq!(config.region(), Some(&Region::from_static("ca-central-1")));
assert_eq!(creds.access_key_id(), "AAAAAAAAAAAAAAAAAAAA");
}
#[tokio::test]
async fn profile_without_region() {
setup();
let args = RemoteConfig {
profile: Some("banana".to_owned()),
region: None,
alias: None,
retry_attempts: 1,
};
let config = args.sdk_config(None).await;
let creds = config
.credentials_provider()
.unwrap()
.provide_credentials()
.await
.unwrap();
assert_eq!(config.region(), None);
assert_eq!(creds.access_key_id(), "BBBBBBBBBBBBBBBBBBBB");
}
#[tokio::test]
async fn default_profile() {
setup();
let args = RemoteConfig {
profile: None,
region: None,
alias: None,
retry_attempts: 1,
};
let config = args.sdk_config(None).await;
let creds = config
.credentials_provider()
.unwrap()
.provide_credentials()
.await
.unwrap();
assert_eq!(config.region(), Some(&Region::from_static("af-south-1")));
assert_eq!(creds.access_key_id(), "DDDDDDDDDDDDDDDDDDDD");
}
}