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
use chrono::{Duration, Utc};
use futures::future::{ok, FutureResult};
use crate::{AwsCredentials, CredentialsError, ProvideAwsCredentials};
#[derive(Clone, Debug)]
pub struct StaticProvider {
credentials: AwsCredentials,
valid_for: Option<i64>,
}
impl StaticProvider {
pub fn new(
access_key: String,
secret_access_key: String,
token: Option<String>,
valid_for: Option<i64>,
) -> StaticProvider {
StaticProvider {
credentials: AwsCredentials::new(access_key, secret_access_key, token, None),
valid_for,
}
}
pub fn new_minimal(access_key: String, secret_access_key: String) -> StaticProvider {
StaticProvider {
credentials: AwsCredentials::new(access_key, secret_access_key, None, None),
valid_for: None,
}
}
pub fn get_aws_access_key_id(&self) -> &str {
&self.credentials.key
}
pub fn get_aws_secret_access_key(&self) -> &str {
&self.credentials.secret
}
pub fn has_token(&self) -> bool {
self.credentials.token.is_some()
}
pub fn get_token(&self) -> &Option<String> {
&self.credentials.token
}
pub fn is_valid_for(&self) -> &Option<i64> {
&self.valid_for
}
}
impl ProvideAwsCredentials for StaticProvider {
type Future = FutureResult<AwsCredentials, CredentialsError>;
fn credentials(&self) -> Self::Future {
let mut creds = self.credentials.clone();
creds.expires_at = self.valid_for.map(|v| Utc::now() + Duration::seconds(v));
ok(creds)
}
}
#[cfg(test)]
mod tests {
use futures::Future;
use std::thread;
use std::time;
use super::*;
use crate::test_utils::{is_secret_hidden_behind_asterisks, SECRET};
use crate::ProvideAwsCredentials;
#[test]
fn test_static_provider_creation() {
let result = StaticProvider::new(
"fake-key".to_owned(),
"fake-secret".to_owned(),
Some("token".to_owned()),
Some(300),
)
.credentials()
.wait();
assert!(result.is_ok());
}
#[test]
fn test_static_provider_minimal_creation() {
let result =
StaticProvider::new_minimal("fake-key-2".to_owned(), "fake-secret-2".to_owned())
.credentials()
.wait();
assert!(result.is_ok());
}
#[test]
fn test_static_provider_custom_time_expiration() {
let start_time = Utc::now();
let result = StaticProvider::new(
"fake-key".to_owned(),
"fake-secret".to_owned(),
None,
Some(10000),
)
.credentials()
.wait();
assert!(result.is_ok());
let finalized = result.unwrap();
let expires_at = finalized.expires_at().unwrap().clone();
assert!(start_time + Duration::minutes(100) < expires_at);
assert!(expires_at < start_time + Duration::minutes(200));
}
#[test]
fn test_static_provider_expiration_time_is_recalculated() {
let provider = StaticProvider::new(
"fake-key".to_owned(),
"fake-secret".to_owned(),
None,
Some(10000),
);
let creds1 = provider.credentials().wait().unwrap();
thread::sleep(time::Duration::from_secs(1));
let creds2 = provider.credentials().wait().unwrap();
assert!(creds1.expires_at() < creds2.expires_at());
}
#[cfg(test)]
quickcheck! {
fn test_static_provider_secrets_not_in_debug(
access_key: String,
token: Option<()>,
valid_for: Option<i64>
) -> bool {
let provider = StaticProvider::new(
access_key,
SECRET.to_owned(),
token.map(|_| SECRET.to_owned()),
valid_for
);
is_secret_hidden_behind_asterisks(&provider)
}
}
}