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
use crate::error::Error;
use diem_config::config::{self, GitHubConfig, OnDiskStorageConfig, Token, VaultConfig};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
path::PathBuf,
str::FromStr,
};
use structopt::StructOpt;
pub const BACKEND: &str = "backend";
pub const DISK: &str = "disk";
pub const GITHUB: &str = "github";
pub const MEMORY: &str = "memory";
pub const VAULT: &str = "vault";
const CONNECTION_TIMEOUT_MS: u64 = 10_000;
const RESPONSE_TIMEOUT_MS: u64 = 10_000;
#[derive(Clone, Debug)]
pub struct SecureBackend {
pub backend: String,
pub parameters: HashMap<String, String>,
}
impl FromStr for SecureBackend {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
SecureBackend::try_from(s)
}
}
impl TryFrom<&str> for SecureBackend {
type Error = Error;
fn try_from(s: &str) -> Result<Self, Error> {
let kvs = s.split(';');
let mut parameters = HashMap::new();
for pair in kvs {
let kv = pair.split('=').collect::<Vec<_>>();
if kv.len() != 2 {
return Err(Error::BackendInvalidKeyValue(format!("{:?}", kv)));
}
parameters.insert(kv[0].into(), kv[1].into());
}
let backend = parameters
.remove(BACKEND)
.ok_or(Error::BackendMissingBackendKey)?;
Ok(Self {
backend,
parameters,
})
}
}
impl TryInto<config::SecureBackend> for SecureBackend {
type Error = Error;
fn try_into(mut self) -> Result<config::SecureBackend, Error> {
let backend = match self.backend.as_ref() {
DISK => {
let mut config = OnDiskStorageConfig::default();
config.set_data_dir(PathBuf::from(""));
let path = self
.parameters
.remove("path")
.ok_or_else(|| Error::BackendParsingError("missing path".into()))?;
config.path = PathBuf::from(path);
config.namespace = self.parameters.remove("namespace");
config::SecureBackend::OnDiskStorage(config)
}
GITHUB => {
let repository_owner = self
.parameters
.remove("repository_owner")
.ok_or_else(|| Error::BackendParsingError("missing repository owner".into()))?;
let repository = self
.parameters
.remove("repository")
.ok_or_else(|| Error::BackendParsingError("missing repository".into()))?;
let branch = self.parameters.remove("branch");
let token = self
.parameters
.remove("token")
.ok_or_else(|| Error::BackendParsingError("missing token".into()))?;
config::SecureBackend::GitHub(GitHubConfig {
namespace: self.parameters.remove("namespace"),
repository_owner,
repository,
branch,
token: Token::FromDisk(PathBuf::from(token)),
})
}
MEMORY => config::SecureBackend::InMemoryStorage,
VAULT => {
let certificate = self.parameters.remove("ca_certificate").map(PathBuf::from);
let server = self
.parameters
.remove("server")
.ok_or_else(|| Error::BackendParsingError("missing server".into()))?;
let token = self
.parameters
.remove("token")
.ok_or_else(|| Error::BackendParsingError("missing token".into()))?;
config::SecureBackend::Vault(VaultConfig {
namespace: self.parameters.remove("namespace"),
server,
ca_certificate: certificate,
token: Token::FromDisk(PathBuf::from(token)),
renew_ttl_secs: None,
disable_cas: Some(true),
connection_timeout_ms: Some(CONNECTION_TIMEOUT_MS),
response_timeout_ms: Some(RESPONSE_TIMEOUT_MS),
})
}
_ => panic!("Invalid backend: {}", self.backend),
};
if !self.parameters.is_empty() {
let error = format!("found extra parameters: {:?}", self.parameters);
return Err(Error::BackendParsingError(error));
}
Ok(backend)
}
}
#[macro_export]
macro_rules! secure_backend {
($struct_name:ident, $field_name:ident, $purpose:literal) => {
secure_backend!($struct_name, $field_name, $purpose, "ignore");
};
($struct_name:ident, $field_name:ident, $purpose:literal, $required:literal) => {
#[derive(Clone, Debug, StructOpt)]
pub struct $struct_name {
#[structopt(long,
help = concat!("Backend for ", $purpose),
required_unless_one(&["config", $required]),
long_help = concat!("Backend for ", $purpose, r#"
Secure backends are represented as a semi-colon deliminted key value
pair: "k0=v0;k1=v1;...". The current supported formats are:
Vault: "backend=vault;server=URL;token=PATH_TO_TOKEN"
an optional namespace: "namespace=NAMESPACE"
an optional server certificate: "ca_certificate=PATH_TO_CERT"
GitHub: "backend=github;repository_owner=REPOSITORY_OWNER;repository=REPOSITORY;token=PATH_TO_TOKEN"
an optional branch: "branch=BRANCH", defaults to master
an optional namespace: "namespace=NAMESPACE"
InMemory: "backend=memory"
OnDisk: "backend=disk;path=LOCAL_PATH"
"#)
)]
pub $field_name: Option<SecureBackend>,
}
impl FromStr for $struct_name {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let secure_backend = SecureBackend::try_from(s)?;
let $field_name = $struct_name {
$field_name: Some(secure_backend),
};
Ok($field_name)
}
}
};
}
secure_backend!(
ValidatorBackend,
validator_backend,
"validator configuration"
);
secure_backend!(SharedBackend, shared_backend, "shared information");
#[allow(dead_code)]
#[cfg(test)]
mod tests {
use super::*;
use std::{fs::File, io::Write};
#[test]
fn test_memory() {
let memory = "backend=memory";
storage(memory).unwrap();
let memory = "backend=memory;extra=stuff";
assert!(storage(memory).is_err());
}
#[test]
fn test_disk() {
let path = diem_temppath::TempPath::new();
path.create_as_file().unwrap();
let disk = format!("backend=disk;path={}", path.path().to_str().unwrap());
storage(&disk).unwrap();
let disk = "backend=disk";
assert!(storage(disk).is_err());
}
#[test]
fn test_github() {
let path = diem_temppath::TempPath::new();
path.create_as_file().unwrap();
let mut file = File::create(path.path()).unwrap();
file.write_all(b"disk_token").unwrap();
let path_str = path.path().to_str().unwrap();
let github = format!(
"backend=github;repository_owner=diem;repository=diem;token={}",
path_str
);
storage(&github).unwrap();
let github = format!(
"backend=github;repository_owner=diem;repository=diem;token={};namespace=test",
path_str
);
storage(&github).unwrap();
let github = format!(
"backend=github;repository_owner=diem;repository=diem;branch=genesis;token={};namespace=test",
path_str
);
storage(&github).unwrap();
let github = "backend=github";
storage(github).unwrap_err();
}
#[test]
fn test_vault() {
let path = diem_temppath::TempPath::new();
path.create_as_file().unwrap();
let mut file = File::create(path.path()).unwrap();
file.write_all(b"disk_token").unwrap();
let path_str = path.path().to_str().unwrap();
let vault = format!(
"backend=vault;server=http://127.0.0.1:8080;token={}",
path_str
);
storage(&vault).unwrap();
let vault = format!(
"backend=vault;server=http://127.0.0.1:8080;token={};namespace=test",
path_str
);
storage(&vault).unwrap();
let vault = "backend=vault";
storage(vault).unwrap_err();
}
fn storage(s: &str) -> Result<config::SecureBackend, Error> {
let management_backend: SecureBackend = s.try_into()?;
management_backend.try_into()
}
}