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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#![deny(unsafe_code, missing_docs)]
mod error;
mod resources;
mod service_account;
mod token;
pub use crate::error::Error;
use crate::service_account::SERVICE_ACCOUNT;
use crate::token::Token;
use chrono as chr;
use lazy_static::lazy_static;
use openssl::{hash::MessageDigest, pkey::PKey, sha, sign::Signer};
use reqwest::header::*;
use std::{collections::HashMap, sync::Mutex};
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET};
lazy_static! {
static ref TOKEN_CACHE: Mutex<Token> = Mutex::new(Token::new(
"https://www.googleapis.com/auth/devstorage.full_control",
));
static ref IAM_TOKEN_CACHE: Mutex<Token> = Mutex::new(Token::new(
"https://www.googleapis.com/auth/iam"
));
}
pub struct Bucket {
name: String,
}
fn percent_encode_noslash(input: &str) -> String {
percent_encode(input).replace("%2F", "/")
}
fn percent_encode(input: &str) -> String {
utf8_percent_encode(input, PATH_SEGMENT_ENCODE_SET)
.to_string()
.replace("&", "%26")
}
impl Bucket {
pub fn existing(name: &str) -> Self {
Self {
name: name.to_string(),
}
}
fn get_headers() -> HeaderMap {
let mut result = HeaderMap::new();
let token = TOKEN_CACHE.lock().unwrap().get();
result.insert(AUTHORIZATION, format!("Bearer {}", token).parse().unwrap());
result
}
pub fn create(name: &str) -> Result<Self, Error> {
const BASE_URL: &str = "https://www.googleapis.com/storage/v1/b";
let url = format!("{}?project={}", BASE_URL, SERVICE_ACCOUNT.project_id);
let client = reqwest::Client::new();
let mut body = HashMap::new();
body.insert("name", name);
body.insert("location", "europe-west4");
let mut response = client
.post(&url)
.headers(Self::get_headers())
.json(&body)
.send()?;
if response.status() == 200 {
Ok(Self {
name: name.to_string(),
})
} else {
Err(Error::new(&response.text().unwrap()))
}
}
pub fn upload(&self, file: &[u8], filename: &str, mime_type: &str) -> Result<(), Error> {
const BASE_URL: &str = "https://www.googleapis.com/upload/storage/v1/b";
let filename = percent_encode(filename);
let client = reqwest::Client::new();
let url = &format!(
"{}/{}/o?uploadType=media&name={}",
BASE_URL, self.name, filename
);
let mut headers = Self::get_headers();
headers.insert(CONTENT_TYPE, mime_type.to_string().parse().unwrap());
headers.insert(CONTENT_LENGTH, file.len().to_string().parse().unwrap());
let mut response = client
.post(url)
.headers(headers)
.body(file.to_owned())
.send()?;
if response.status() == 200 {
Ok(())
} else {
Err(Error::new(&response.text().unwrap()))
}
}
pub fn update(&self, old_name: &str, new_name: &str) -> Result<(), Error> {
let old_name = percent_encode(old_name.trim_matches('/'));
let new_name = percent_encode(new_name.trim_matches('/'));
let url = &format!(
"https://www.googleapis.com/storage/v1/b/{}/o/{}/rewriteTo/b/{}/o/{}",
self.name, old_name, self.name, new_name
);
let mut headers = Self::get_headers();
headers.insert(CONTENT_LENGTH, "0".parse().unwrap());
let client = reqwest::Client::new();
let mut response = client.post(url).headers(headers).send()?;
if response.status() == 200 {
Ok(())
} else {
Err(Error::new(&response.text().unwrap()))
}
}
pub fn delete(&self, name: &str) -> Result<(), Error> {
let name = percent_encode(name.trim_matches('/'));
let url = &format!(
"https://www.googleapis.com/storage/v1/b/{}/o/{}",
self.name, name
);
let client = reqwest::Client::new();
let mut response = client.delete(url).headers(Self::get_headers()).send()?;
if response.status() == 200 || response.status() == 204 {
Ok(())
} else {
Err(Error::new(&response.text().unwrap()))
}
}
pub fn download_url(&self, file_path: &str, duration: u32) -> String {
self.sign(file_path, duration, "GET")
}
#[inline(always)]
fn sign(&self, file_path: &str, duration: u32, http_verb: &str) -> String {
let issue_date = chr::Utc::now();
let file_path = self.path_to_resource(file_path);
let query_string = Self::get_canonical_query_string(&issue_date, duration);
let canonical_request = self.get_canonical_request(&file_path, &query_string, http_verb);
let hash = sha::sha256(canonical_request.as_bytes());
let hex_hash = hex::encode(hash);
let string_to_sign = format!(
"{signing_algorithm}\n{current_datetime}\n{credential_scope}\n{hashed_canonical_request}",
signing_algorithm="GOOG4-RSA-SHA256",
current_datetime=issue_date.format("%Y%m%dT%H%M%SZ"),
credential_scope=Self::get_credential_scope(&issue_date),
hashed_canonical_request=hex_hash,
);
let buffer = Self::sign_str(&string_to_sign);
let signature = hex::encode(&buffer);
format!(
"https://storage.googleapis.com{path_to_resource}?{query_string}&X-Goog-Signature={request_signature}",
path_to_resource=file_path,
query_string=query_string,
request_signature=signature,
)
}
#[inline(always)]
fn get_canonical_request(
&self,
path: &str,
query_string: &str,
http_verb: &str
) -> String {
format!(
"{http_verb}\n{path_to_resource}\n{canonical_query_string}\n{canonical_headers}\n\n{signed_headers}\n{payload}",
http_verb=http_verb,
path_to_resource=path,
canonical_query_string=query_string,
canonical_headers="host:storage.googleapis.com",
signed_headers="host",
payload="UNSIGNED-PAYLOAD",
)
}
#[inline(always)]
fn get_canonical_query_string(date: &chr::DateTime<chr::Utc>, exp: u32) -> String {
let credential = format!(
"{authorizer}/{scope}",
authorizer = SERVICE_ACCOUNT.client_email,
scope = Self::get_credential_scope(date),
);
format!(
"X-Goog-Algorithm={algo}&X-Goog-Credential={cred}&X-Goog-Date={date}&X-Goog-Expires={exp}&X-Goog-SignedHeaders={signed}",
algo="GOOG4-RSA-SHA256",
cred=percent_encode(&credential).replace("@", "%40"),
date=date.format("%Y%m%dT%H%M%SZ"),
exp=exp,
signed="host",
)
}
#[inline(always)]
fn path_to_resource(&self, path: &str) -> String {
format!(
"/{bucket}/{file_path}",
bucket = self.name,
file_path = percent_encode_noslash(path),
)
}
#[inline(always)]
fn get_credential_scope(date: &chr::DateTime<chr::Utc>) -> String {
format!("{}/henk/storage/goog4_request", date.format("%Y%m%d"))
}
#[inline(always)]
fn sign_str(message: &str) -> Vec<u8> {
let key = PKey::private_key_from_pem(SERVICE_ACCOUNT.private_key.as_bytes()).unwrap();
let mut signer = Signer::new(MessageDigest::sha256(), &key).unwrap();
signer.update(message.as_bytes()).unwrap();
signer.sign_to_vec().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_bucket() {
Bucket::create("tmp-bckt").unwrap();
}
}