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
use regex::Regex;
use rusoto_core::region::Region;
use rusoto_credential::AwsCredentials;
use std::str::FromStr;
use tink_core::{utils::wrap_err, TinkError};
pub const AWS_PREFIX: &str = "aws-kms://";
pub struct AwsClient {
key_uri_prefix: String,
kms: rusoto_kms::KmsClient,
}
impl std::fmt::Debug for AwsClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AwsClient")
.field("key_uri_prefix", &self.key_uri_prefix)
.finish()
}
}
impl AwsClient {
pub fn new(uri_prefix: &str) -> Result<AwsClient, TinkError> {
let r = get_region(uri_prefix)?;
let kms = rusoto_kms::KmsClient::new(r);
Self::new_with_kms(uri_prefix, kms)
}
pub fn new_with_credentials(
uri_prefix: &str,
credential_path: &std::path::Path,
) -> Result<AwsClient, TinkError> {
if !credential_path.exists() {
return Err("invalid credential path".into());
}
let region = get_region(uri_prefix)?;
let request_dispatcher = rusoto_core::request::HttpClient::new()
.map_err(|e| wrap_err("failed to create AWS HTTP client", e))?;
let kms = match extract_creds_csv(credential_path) {
Ok(c) => {
let creds_provider = rusoto_credential::StaticProvider::from(c);
rusoto_kms::KmsClient::new_with(request_dispatcher, creds_provider, region)
}
Err(CredentialsErr::BadFile) => return Err("cannot open credential path".into()),
Err(CredentialsErr::CredCsv) => return Err("malformed credential csv file".into()),
Err(_) => {
let creds_provider = rusoto_credential::ProfileProvider::with_configuration(
credential_path,
"default",
);
rusoto_kms::KmsClient::new_with(request_dispatcher, creds_provider, region)
}
};
Self::new_with_kms(uri_prefix, kms)
}
pub fn new_with_kms(
uri_prefix: &str,
kms: rusoto_kms::KmsClient,
) -> Result<AwsClient, TinkError> {
if !uri_prefix.to_lowercase().starts_with(AWS_PREFIX) {
return Err(format!(
"uri_prefix must start with {}, but got {}",
AWS_PREFIX, uri_prefix
)
.into());
}
Ok(AwsClient {
key_uri_prefix: uri_prefix.to_string(),
kms,
})
}
}
impl tink_core::registry::KmsClient for AwsClient {
fn supported(&self, key_uri: &str) -> bool {
key_uri.starts_with(&self.key_uri_prefix)
}
fn get_aead(&self, key_uri: &str) -> Result<Box<dyn tink_core::Aead>, tink_core::TinkError> {
if !self.supported(key_uri) {
return Err(format!(
"key_uri must start with prefix {}, but got {}",
self.key_uri_prefix, key_uri
)
.into());
}
let uri = if let Some(stripped) = key_uri.strip_prefix(AWS_PREFIX) {
stripped
} else {
key_uri
};
Ok(Box::new(crate::AwsAead::new(uri, self.kms.clone())?))
}
}
enum CredentialsErr {
BadFile,
CredCsv,
BadCsv,
TooFewColumns,
}
fn extract_creds_csv(filename: &std::path::Path) -> Result<AwsCredentials, CredentialsErr> {
let mut rdr = csv::Reader::from_path(filename).map_err(|_| CredentialsErr::BadFile)?;
let mut lines = vec![];
for result in rdr.records() {
match result {
Ok(sr) => lines.push(sr),
Err(_) => return Err(CredentialsErr::BadCsv),
}
}
if !lines.is_empty() && lines[0].len() == 1 {
return Err(CredentialsErr::TooFewColumns);
}
if lines.is_empty() {
return Err(CredentialsErr::CredCsv);
}
if lines[0].len() < 4 {
return Err(CredentialsErr::CredCsv);
}
Ok(rusoto_credential::AwsCredentials::new(
lines[0][2].to_string(),
lines[0][3].to_string(),
None,
None,
))
}
fn get_region(key_uri: &str) -> Result<Region, TinkError> {
let re1 = Regex::new(r"aws-kms://arn:(aws[a-zA-Z0-9-_]*):kms:([a-z0-9-]+):")
.map_err(|e| wrap_err("failed to compile regex", e))?;
let r = re1
.captures(key_uri)
.ok_or_else(|| TinkError::new("extracting region from URI failed"))?;
if r.len() != 3 {
return Err("extracting region from URI failed".into());
}
Region::from_str(&r[2]).map_err(|e| wrap_err("unknown region", e))
}