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
use aws_sdk_sts::error::AssumeRoleErrorKind;
use aws_sdk_sts::operation::AssumeRole;
use aws_types::credentials::{
self, future, CredentialsError, ProvideCredentials, SharedCredentialsProvider,
};
use aws_types::region::Region;
use crate::provider_config::HttpSettings;
use aws_smithy_async::rt::sleep::default_async_sleep;
use tracing::Instrument;
#[derive(Debug)]
pub struct AssumeRoleProvider {
sts: aws_hyper::StandardClient,
conf: aws_sdk_sts::Config,
op: aws_sdk_sts::input::AssumeRoleInput,
}
impl AssumeRoleProvider {
pub fn builder(role: impl Into<String>) -> AssumeRoleProviderBuilder {
AssumeRoleProviderBuilder::new(role.into())
}
}
pub struct AssumeRoleProviderBuilder {
role_arn: String,
external_id: Option<String>,
session_name: Option<String>,
region: Option<Region>,
connection: Option<aws_smithy_client::erase::DynConnector>,
}
impl AssumeRoleProviderBuilder {
pub fn new(role: impl Into<String>) -> Self {
Self {
role_arn: role.into(),
external_id: None,
session_name: None,
region: None,
connection: None,
}
}
pub fn external_id(mut self, id: impl Into<String>) -> Self {
self.external_id = Some(id.into());
self
}
pub fn session_name(mut self, name: impl Into<String>) -> Self {
self.session_name = Some(name.into());
self
}
pub fn region(mut self, region: Region) -> Self {
self.region = Some(region);
self
}
pub fn connection(mut self, conn: impl aws_smithy_client::bounds::SmithyConnector) -> Self {
self.connection = Some(aws_smithy_client::erase::DynConnector::new(conn));
self
}
pub fn build(self, provider: impl Into<SharedCredentialsProvider>) -> AssumeRoleProvider {
let config = aws_sdk_sts::Config::builder()
.credentials_provider(provider.into())
.region(self.region.clone())
.build();
let conn = self.connection.unwrap_or_else(|| {
crate::connector::expect_connector(crate::connector::default_connector(
&HttpSettings::default(),
default_async_sleep(),
))
});
let client = aws_hyper::Client::new(conn);
let session_name = self
.session_name
.unwrap_or_else(|| super::util::default_session_name("assume-role-provider"));
let operation = AssumeRole::builder()
.set_role_arn(Some(self.role_arn))
.set_external_id(self.external_id)
.set_role_session_name(Some(session_name))
.build()
.expect("operation is valid");
AssumeRoleProvider {
sts: client,
conf: config,
op: operation,
}
}
}
impl AssumeRoleProvider {
#[tracing::instrument(
name = "assume_role",
level = "info",
skip(self),
fields(op = ?self.op)
)]
async fn credentials(&self) -> credentials::Result {
tracing::info!("assuming role");
tracing::debug!("retrieving assumed credentials");
let op = self
.op
.clone()
.make_operation(&self.conf)
.expect("valid operation");
let assumed = self.sts.call(op).in_current_span().await;
match assumed {
Ok(assumed) => {
tracing::debug!(
access_key_id = ?assumed.credentials.as_ref().map(|c| &c.access_key_id),
"obtained assumed credentials"
);
super::util::into_credentials(assumed.credentials, "AssumeRoleProvider")
}
Err(aws_hyper::SdkError::ServiceError { err, raw }) => {
match err.kind {
AssumeRoleErrorKind::RegionDisabledException(_)
| AssumeRoleErrorKind::MalformedPolicyDocumentException(_) => {
return Err(CredentialsError::invalid_configuration(
aws_hyper::SdkError::ServiceError { err, raw },
))
}
_ => {}
}
tracing::warn!(error = ?err.message(), "sts refused to grant assume role");
Err(CredentialsError::provider_error(
aws_hyper::SdkError::ServiceError { err, raw },
))
}
Err(err) => Err(CredentialsError::provider_error(err)),
}
}
}
impl ProvideCredentials for AssumeRoleProvider {
fn provide_credentials<'a>(&'a self) -> future::ProvideCredentials
where
Self: 'a,
{
future::ProvideCredentials::new(self.credentials())
}
}