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
use crate::custom::credential::NewAwsCredsForStsCreds;
use crate::{AssumeRoleWithWebIdentityRequest, Sts, StsClient, PolicyDescriptorType};
use rusoto_core::credential::{
AwsCredentials, CredentialsError, ProvideAwsCredentials, Secret, Variable,
};
use rusoto_core::request::HttpClient;
use rusoto_core::{Client, Region};
use async_trait::async_trait;
const AWS_WEB_IDENTITY_TOKEN_FILE: &str = "AWS_WEB_IDENTITY_TOKEN_FILE";
const AWS_ROLE_ARN: &str = "AWS_ROLE_ARN";
const AWS_ROLE_SESSION_NAME: &str = "AWS_ROLE_SESSION_NAME";
#[derive(Debug, Clone)]
pub struct WebIdentityProvider {
pub web_identity_token: Variable<Secret, CredentialsError>,
pub role_arn: Variable<String, CredentialsError>,
pub role_session_name: Option<Variable<Option<String>, CredentialsError>>,
pub duration_seconds: Option<i64>,
pub policy: Option<String>,
pub policy_arns: Option<Vec<PolicyDescriptorType>>,
}
impl WebIdentityProvider {
pub fn new<A, B, C>(web_identity_token: A, role_arn: B, role_session_name: Option<C>) -> Self
where
A: Into<Variable<Secret, CredentialsError>>,
B: Into<Variable<String, CredentialsError>>,
C: Into<Variable<Option<String>, CredentialsError>>,
{
Self {
web_identity_token: web_identity_token.into(),
role_arn: role_arn.into(),
role_session_name: role_session_name.map(|v| v.into()),
duration_seconds: None,
policy: None,
policy_arns: None
}
}
pub fn from_k8s_env() -> Self {
Self::_from_k8s_env(
Variable::from_env_var(AWS_WEB_IDENTITY_TOKEN_FILE),
Variable::from_env_var(AWS_ROLE_ARN),
Variable::from_env_var_optional(AWS_ROLE_SESSION_NAME),
)
}
pub(crate) fn _from_k8s_env(
token_file: Variable<String, CredentialsError>,
role: Variable<String, CredentialsError>,
session_name: Variable<Option<String>, CredentialsError>,
) -> Self {
Self::new(
Variable::dynamic(move || Variable::from_text_file(token_file.resolve()?).resolve()),
role,
Some(session_name),
)
}
#[cfg(test)]
pub(crate) fn load_token(&self) -> Result<Secret, CredentialsError> {
self.web_identity_token.resolve()
}
fn create_session_name() -> String {
"WebIdentitySession".to_string()
}
}
#[async_trait]
impl ProvideAwsCredentials for WebIdentityProvider {
async fn credentials(&self) -> Result<AwsCredentials, CredentialsError> {
let http_client = match HttpClient::new() {
Ok(c) => c,
Err(e) => return Err(CredentialsError::new(e)),
};
let client = Client::new_not_signing(http_client);
let sts = StsClient::new_with_client(client, Region::default());
let mut req = AssumeRoleWithWebIdentityRequest::default();
req.role_arn = self.role_arn.resolve()?;
req.web_identity_token = self.web_identity_token.resolve()?.as_ref().to_string();
req.policy = self.policy.to_owned();
req.duration_seconds = self.duration_seconds.to_owned();
req.policy_arns = self.policy_arns.to_owned();
req.role_session_name = match self.role_session_name {
Some(ref role_session_name) => match role_session_name.resolve()? {
Some(session_name) => session_name,
None => Self::create_session_name(),
},
None => Self::create_session_name(),
};
let assume_role = sts.assume_role_with_web_identity(req).await;
match assume_role {
Err(e) => Err(CredentialsError::new(e)),
Ok(role) => match role.credentials {
None => Err(CredentialsError::new(format!(
"No credentials found in AssumeRoleWithWebIdentityResponse: {:?}",
role
))),
Some(c) => AwsCredentials::new_for_credentials(c),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn api_ergonomy() {
WebIdentityProvider::new(Secret::from("".to_string()), "", Some(Some("".to_string())));
}
#[test]
fn from_k8s_env() -> Result<(), CredentialsError> {
const TOKEN_VALUE: &str = "secret";
const ROLE_ARN: &str = "role";
const SESSION_NAME: &str = "session";
let mut file = NamedTempFile::new()?;
writeln!(file, "{}", TOKEN_VALUE)?;
let p = WebIdentityProvider::_from_k8s_env(
Variable::with_value(file.path().to_string_lossy().to_string()),
Variable::with_value(ROLE_ARN.to_string()),
Variable::with_value(SESSION_NAME.to_string()),
);
let token = p.load_token()?;
assert_eq!(token.as_ref(), TOKEN_VALUE);
Ok(())
}
}