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
use crate::{
ciphers::{Cipher, CipherKind},
error::{Error, Result},
keepers::{hkdf::key_cipher_from_pass, SecretKeeper},
util::{FromBech32, ToBech32},
WrappedKey,
};
use async_trait::async_trait;
use bytes::Bytes;
use std::env;
use url::Url;
const ENV_VAR_NAME: &str = "VAULT_PASSWORD";
const SCHEME: &str = "env";
const ENV_KEEPER_CIPHER: CipherKind = CipherKind::XChaCha20Poly1305;
#[derive(Debug)]
pub struct EnvKeeper {}
impl EnvKeeper {
pub fn new() -> Self {
EnvKeeper {}
}
}
#[async_trait]
impl SecretKeeper for EnvKeeper {
fn get_scheme(&self) -> &str {
SCHEME
}
async fn wrap(&self, uri: &str, nonce: &[u8], key: &[u8]) -> Result<WrappedKey, Error> {
let key_cipher = make_key_cipher(uri, nonce, ENV_KEEPER_CIPHER)?;
let encrypted = key_cipher.seal(key, None).await?;
Ok(WrappedKey {
key_enc: encrypted.to_bech32(),
key_uri: String::from(uri),
ident: None,
})
}
async fn unwrap(&self, nonce: &[u8], wk: &WrappedKey) -> Result<Bytes, Error> {
let key_cipher = make_key_cipher(&wk.key_uri, nonce, ENV_KEEPER_CIPHER)?;
let mut encrypted = (&wk.key_enc).from_bech32()?;
key_cipher.open(&mut encrypted, None).await
}
}
fn get_varname(uri: &str) -> Result<String, Error> {
let url = Url::parse(uri).map_err(|e| {
Error::InvalidParameter(format!(
"Invalid uri. Should be 'env://VAR' or 'env:': {}",
e
))
})?;
if url.scheme() != SCHEME {
return Err(Error::InvalidParameter(
"Invalid scheme for env keeper. Uri should begin with 'env:'".to_string(),
));
}
Ok(match url.host_str() {
Some(host) => host,
None => {
if url.path() != "" {
url.path()
} else {
ENV_VAR_NAME
}
}
}
.to_string())
}
fn make_key_cipher(uri: &str, nonce: &[u8], alg: CipherKind) -> Result<Box<dyn Cipher>, Error> {
let varname = get_varname(uri)?;
let passphrase = env::var(&varname).map_err(|_| Error::MissingEnv(varname))?;
key_cipher_from_pass(&passphrase, nonce, alg)
}
#[cfg(test)]
mod test {
use super::{get_varname, make_key_cipher, EnvKeeper, ENV_VAR_NAME};
use crate::{
ciphers::{xchacha20, CipherKind},
error::{Error, Result},
keepers::SecretKeeper,
};
use secret_keeper_test_util::{arrays_eq, random_bytes};
use std::env;
#[test]
fn varname() -> Result<(), Error> {
assert!(get_varname("").is_err(), "empty uri");
assert!(get_varname("abc").is_err(), "no scheme");
assert!(get_varname("env").is_err(), "no colon after scheme");
assert_eq!(get_varname("env:").expect("default"), ENV_VAR_NAME);
assert_eq!(get_varname("env:foo").expect("env:var"), "foo");
assert_eq!(get_varname("env://foo").expect("env://var"), "foo");
assert_eq!(get_varname("env://foo/bar").expect("env://var/path"), "foo");
Ok(())
}
#[test]
fn env_constructor() {
let k = EnvKeeper::new();
assert_eq!(k.get_scheme(), "env", "env keeper scheme");
}
#[tokio::test]
async fn env_register() -> Result<(), Error> {
let k = SecretKeeper::for_uri("env:").await;
assert!(k.is_ok(), "find keeper for uri env:");
Ok(())
}
#[tokio::test]
async fn mk_cipher_xchacha() -> Result<(), Error> {
let nonce = random_bytes(xchacha20::NONCEBYTES);
const TEST_VAR: &str = "test_make_cipher_c5f20e131d04";
let env_uri: String = format!("env:{}", TEST_VAR);
let should_fail = make_key_cipher(&env_uri, &nonce, CipherKind::XChaCha20Poly1305);
assert!(should_fail.is_err());
env::set_var(TEST_VAR, "my-secret-unguessable-passphrase");
let cipher = make_key_cipher(&env_uri, &nonce, CipherKind::XChaCha20Poly1305)?;
assert_eq!(
cipher.nonce_len(),
xchacha20::NONCEBYTES,
"cipher sanity check"
);
env::remove_var(TEST_VAR);
Ok(())
}
#[tokio::test]
async fn wrap_unwrap() -> Result<(), Error> {
const VAR_NAME: &str = "TEST_ENVKEEPER_WRAP_UNWRAP";
let mut uri: String = String::from("env://");
uri.push_str(VAR_NAME);
let rand_phrase = hex::encode(random_bytes(24));
env::set_var(VAR_NAME, rand_phrase);
let keeper = SecretKeeper::for_uri(&uri).await?;
let key = random_bytes(xchacha20::KEYBYTES);
let nonce = random_bytes(xchacha20::NONCEBYTES);
let wrapped = keeper.wrap(&uri, &nonce, &key).await?;
let unwrapped = keeper.unwrap(&nonce, &wrapped).await?;
assert!(
arrays_eq(&key, &unwrapped.as_ref()),
"cipher wrap & unwrap ok"
);
Ok(())
}
}