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
use dirs;
use error::{S3Error, S3Result};
use ini::Ini;
use std::collections::HashMap;
use std::env;
use std::process::Command;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Credentials {
pub access_key: String,
pub secret_key: String,
pub token: Option<String>,
_private: (),
}
impl Credentials {
pub fn new(
access_key: Option<String>,
secret_key: Option<String>,
token: Option<String>,
profile: Option<String>,
) -> Credentials {
let credentials = match access_key {
Some(key) => match secret_key {
Some(secret) => match token {
Some(t) => Some(Credentials {
access_key: key,
secret_key: secret,
token: Some(t),
_private: (),
}),
None => Some(Credentials {
access_key: key,
secret_key: secret,
token: None,
_private: (),
}),
},
None => None,
},
None => None,
};
match credentials {
Some(c) => c,
None => match Credentials::from_env() {
Ok(c) => c,
Err(_) => match Credentials::from_profile(profile) {
Ok(c) => c,
Err(_) => match Credentials::from_instance_metadata() {
Ok(c) => c,
Err(e) => panic!("No credentials provided as arguments, in the environment or in the profile file. \n {}", e)
}
}
}
}
}
fn from_env() -> S3Result<Credentials> {
let access_key = env::var("AWS_ACCESS_KEY_ID")?;
let secret_key = env::var("AWS_SECRET_ACCESS_KEY")?;
let token = match env::var("AWS_SESSION_TOKEN") {
Ok(x) => Some(x),
Err(_) => None,
};
Ok(Credentials {
access_key,
secret_key,
token,
_private: (),
})
}
fn from_instance_metadata() -> S3Result<Credentials> {
if !Credentials::is_ec2() {
return Err(S3Error::from("Not an EC2 instance"));
}
let resp: HashMap<String, String> =
reqwest::get("http://169.254.169.254/latest/meta-data/iam/info")?.json()?;
let credentials = if let Some(arn) = resp.get("InstanceProfileArn") {
if let Some(role) = arn.split('/').last() {
let resp: HashMap<String, String> = reqwest::get(&format!(
"http://169.254.169.254/latest/meta-data/iam/security-credentials/{}",
role
))?
.json()?;
let access_key = resp.get("AccessKeyId").unwrap().clone();
let secret_key = resp.get("SecretAccessKey").unwrap().clone();
let token = Some(resp.get("Token").unwrap().clone());
Some(Credentials {
access_key,
secret_key,
token,
_private: (),
})
} else {
None
}
} else {
None
};
Ok(credentials.unwrap())
}
fn is_ec2() -> bool {
if let Ok(uuid) = std::fs::read_to_string("/sys/hypervisor/uuid") {
if uuid.len() >= 3 && &uuid[..3] == "ec2" {
return true;
}
}
if let Ok(uuid) = std::fs::read_to_string("/sys/class/dmi/id/board_vendor") {
if uuid.len() >= 10 && &uuid[..10] == "Amazon EC2" {
return true;
}
}
false
}
pub fn from_profile(section: Option<String>) -> S3Result<Credentials> {
let home_dir = match dirs::home_dir() {
Some(path) => Ok(path),
None => Err(S3Error::from("Invalid home dir")),
};
let profile = format!("{}/.aws/credentials", home_dir?.display());
let conf = Ini::load_from_file(&profile)?;
let section = match section {
Some(s) => s,
None => String::from("default"),
};
let mut access_key = Err(S3Error::from("Missing aws_access_key_id section"));
let mut secret_key = Err(S3Error::from("Missing aws_secret_access_key section"));
let mut token = None;
if let Some(data) = conf.section(Some(section)) {
access_key = match data.get("aws_access_key_id") {
Some(x) => Ok(x.to_owned()),
None => Err(S3Error::from("Missing aws_access_key_id section")),
};
secret_key = match data.get("aws_secret_access_key") {
Some(x) => Ok(x.to_owned()),
None => Err(S3Error::from("Missing aws_secret_access_key section")),
};
token = match data.get("aws_security_token") {
Some(x) => Some(x.to_owned()),
None => None,
}
}
Ok(Credentials {
access_key: access_key?.to_owned(),
secret_key: secret_key?.to_owned(),
token,
_private: (),
})
}
}
impl Default for Credentials {
fn default() -> Self {
Credentials::new(None, None, None, None)
}
}