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
use std::time::Duration;
use futures::{Future, Poll};
use futures::future::{FutureResult, result};
use hyper::Uri;
use {AwsCredentials, CredentialsError, ProvideAwsCredentials,
parse_credentials_from_aws_service};
use request::{HttpClient, HttpClientFuture};
const AWS_CREDENTIALS_PROVIDER_IP: &str = "169.254.169.254";
const AWS_CREDENTIALS_PROVIDER_PATH: &str = "latest/meta-data/iam/security-credentials";
#[derive(Clone, Debug)]
pub struct InstanceMetadataProvider {
client: HttpClient,
timeout: Duration
}
impl InstanceMetadataProvider {
pub fn new() -> Self {
InstanceMetadataProvider {
client: HttpClient::new(),
timeout: Duration::from_secs(30)
}
}
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}
}
enum InstanceMetadataFutureState {
Start,
GetRoleName(HttpClientFuture),
GetCredentialsFromRole(HttpClientFuture),
Done(FutureResult<AwsCredentials, CredentialsError>)
}
pub struct InstanceMetadataProviderFuture {
state: InstanceMetadataFutureState,
client: HttpClient,
timeout: Duration
}
impl Future for InstanceMetadataProviderFuture {
type Item = AwsCredentials;
type Error = CredentialsError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let new_state = match self.state {
InstanceMetadataFutureState::Start => {
let new_future = get_role_name(&self.client, self.timeout)?;
InstanceMetadataFutureState::GetRoleName(new_future)
}
InstanceMetadataFutureState::GetRoleName(ref mut future) => {
let role_name = try_ready!(future.poll());
let new_future = get_credentials_from_role(&self.client, self.timeout, &role_name)?;
InstanceMetadataFutureState::GetCredentialsFromRole(new_future)
},
InstanceMetadataFutureState::GetCredentialsFromRole(ref mut future) => {
let cred_str = try_ready!(future.poll());
let new_future = result(parse_credentials_from_aws_service(&cred_str));
InstanceMetadataFutureState::Done(new_future)
},
InstanceMetadataFutureState::Done(ref mut future) => {
return future.poll();
}
};
self.state = new_state;
self.poll()
}
}
impl ProvideAwsCredentials for InstanceMetadataProvider {
type Future = InstanceMetadataProviderFuture;
fn credentials(&self) -> Self::Future {
InstanceMetadataProviderFuture {
state: InstanceMetadataFutureState::Start,
client: self.client.clone(),
timeout: self.timeout
}
}
}
fn get_role_name(client: &HttpClient, timeout: Duration) -> Result<HttpClientFuture, CredentialsError> {
let role_name_address = format!(
"http://{}/{}/",
AWS_CREDENTIALS_PROVIDER_IP,
AWS_CREDENTIALS_PROVIDER_PATH
);
let uri = role_name_address.parse::<Uri>()
.map_err(|err| CredentialsError::new(err))?;
Ok(client.get(uri, timeout))
}
fn get_credentials_from_role(
client: &HttpClient,
timeout: Duration,
role_name: &str
) -> Result<HttpClientFuture, CredentialsError> {
let credentials_provider_url = format!(
"http://{}/{}/{}",
AWS_CREDENTIALS_PROVIDER_IP,
AWS_CREDENTIALS_PROVIDER_PATH,
role_name
);
let uri = credentials_provider_url.parse::<Uri>()
.map_err(|err| CredentialsError::new(err))?;
Ok(client.get(uri, timeout))
}