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
use std::env;
use dirs;
use ini::Ini;
use snafu::{ResultExt, Snafu};
#[derive(Debug, Snafu)]
pub enum Error {
VarError {
source: std::env::VarError
},
HomeDir,
IniError {
source: ini::ini::Error
},
#[snafu(display("Section [{}] not found in {}", section, profile))]
AwsMissingSection {
section: String,
profile: String
},
#[snafu(display("Missing {} in {}", part, profile))]
AwsConfigError {
part: String,
profile: String
}
}
type S3Result<T, E = Error> = std::result::Result<T, E>;
#[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(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").context(VarError)?;
let secret_key = env::var("AWS_SECRET_ACCESS_KEY").context(VarError)?;
let token = match env::var("AWS_SESSION_TOKEN") {
Ok(x) => Some(x),
Err(_) => None
};
Ok(Credentials { access_key, secret_key, token, _private: () })
}
fn from_profile(section: Option<String>) -> S3Result<Credentials> {
let home_dir = match dirs::home_dir() {
Some(path) => Ok(path),
None => Err(Error::HomeDir),
};
let profile = format!("{}/.aws/credentials", home_dir?.display());
let conf = Ini::load_from_file(&profile).context(IniError)?;
let section = match section {
Some(s) => s,
None => String::from("default")
};
let data1 = match conf.section(Some(section.clone())) {
Some(d) => Ok(d),
None => Err(Error::AwsMissingSection {section: section.clone(), profile: profile.clone()})
};
let data2 = match conf.section(Some(section.clone())) {
Some(d) => Ok(d),
None => Err(Error::AwsMissingSection {section, profile: profile.clone()})
};
let access_key = match data1?.get("aws_access_key_id") {
Some(x) => Ok(x.to_owned()),
None => Err(Error::AwsConfigError {part: "aws_access_key_id".to_string(), profile: profile.clone()})
};
let secret_key = match data2?.get("aws_secret_access_key") {
Some(x) => Ok(x.to_owned()),
None => Err(Error::AwsConfigError {part: "aws_secret_access_key".to_string(), profile})
};
Ok(Credentials { access_key: access_key?.to_owned(), secret_key: secret_key?.to_owned(), token: None, _private: () })
}
}
impl Default for Credentials {
fn default() -> Self {
Credentials::new(None, None, None, None)
}
}