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
#![warn(missing_docs)]
#![recursion_limit = "1024"]
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate hyper;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate regex;
#[macro_use]
extern crate reqwest;
extern crate rustc_serialize;
use backend::Backend;
use std::cell::RefCell;
use std::convert::AsRef;
use std::default::Default;
use std::ops::DerefMut;
use std::path::Path;
use std::sync::{Mutex, MutexGuard};
pub use secretfile::{Secretfile, SecretfileKeys};
pub use errors::{Error, ErrorKind, Result, ResultExt};
mod backend;
mod chained;
mod envvar;
mod errors;
mod secretfile;
mod vault;
pub struct Options {
secretfile: Option<Secretfile>,
allow_override: bool,
}
impl Default for Options {
fn default() -> Options {
Options {
secretfile: None,
allow_override: true,
}
}
}
impl Options {
pub fn secretfile(mut self, secretfile: Secretfile) -> Options {
self.secretfile = Some(secretfile);
self
}
pub fn allow_override(mut self, allow_override: bool) -> Options {
self.allow_override = allow_override;
self
}
}
pub struct Client {
secretfile: Secretfile,
backend: chained::Client,
}
impl Client {
pub fn new(options: Options) -> Result<Client> {
let secretfile = match options.secretfile {
Some(sf) => sf,
None => Secretfile::default()?,
};
let over = options.allow_override;
Ok(Client {
secretfile: secretfile,
backend: chained::Client::with_default_backends(over)?,
})
}
pub fn default() -> Result<Client> {
Client::new(Default::default())
}
pub fn with_secretfile(secretfile: Secretfile) -> Result<Client> {
Client::new(Options::default().secretfile(secretfile))
}
pub fn secretfile(&self) -> &Secretfile {
&self.secretfile
}
pub fn var<S: AsRef<str>>(&mut self, name: S) -> Result<String> {
let name_ref = name.as_ref();
trace!("getting secure credential {}", name_ref);
self.backend
.var(&self.secretfile, name_ref)
.chain_err(|| ErrorKind::Credential(name_ref.to_owned()))
.map_err(|err| {
error!("{}", &err);
err
})
}
pub fn file<S: AsRef<Path>>(&mut self, path: S) -> Result<String> {
let path_ref = path.as_ref();
let path_str = path_ref.to_str()
.ok_or_else(|| {
let err: Error = ErrorKind::NonUnicodePath(path_ref.to_owned()).into();
err
})
.chain_err(|| ErrorKind::Credential(format!("{}", path_ref.display())))?;
trace!("getting secure credential {}", path_str);
self.backend
.file(&self.secretfile, path_str)
.chain_err(|| ErrorKind::Credential(path_str.to_owned()))
.map_err(|err| {
error!("{}", &err);
err
})
}
}
lazy_static! {
static ref CLIENT: Mutex<RefCell<Option<Client>>> =
Mutex::new(RefCell::new(None));
}
fn with_client<F>(body: F) -> Result<String>
where F: FnOnce(&mut Client) -> Result<String>
{
let client_cell: MutexGuard<_> = CLIENT.lock().unwrap();
if client_cell.borrow().is_none() {
*client_cell.borrow_mut() = Some(Client::default()?);
}
let result = match client_cell.borrow_mut().deref_mut() {
&mut Some(ref mut client) => body(client),
&mut None => panic!("Should have a client, but we don't"),
};
result
}
pub fn var<S: AsRef<str>>(name: S) -> Result<String> {
with_client(|client| client.var(name))
}
pub fn file<S: AsRef<Path>>(path: S) -> Result<String> {
with_client(|client| client.file(path))
}
#[cfg(test)]
mod test {
use std::fs;
use std::io::Read;
use super::file;
#[test]
fn test_file() {
let mut f = fs::File::open("Cargo.toml").unwrap();
let mut expected = String::new();
f.read_to_string(&mut expected).unwrap();
assert_eq!(expected, file("Cargo.toml").unwrap());
assert!(file("nosuchfile.txt").is_err());
}
}