This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
keys.rs
351 lines (311 loc) · 13.2 KB
/
keys.rs
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
345
346
347
348
349
350
351
use std::{collections::HashMap, path::PathBuf};
use anyhow::Result;
use clap::Subcommand;
use nkeys::{KeyPair, KeyPairType};
use serde_json::json;
use wash_lib::cli::CommandOutput;
use wash_lib::config::cfg_dir;
use wash_lib::keys::{fs::KeyDir, KeyManager};
const NKEYS_EXTENSION: &str = ".nk";
#[derive(Debug, Clone, Subcommand)]
#[allow(clippy::enum_variant_names)]
pub(crate) enum KeysCliCommand {
#[clap(name = "gen", about = "Generates a keypair")]
GenCommand {
/// The type of keypair to generate. May be Account, User, Module (Actor), Service (Capability Provider), Server, Operator, Cluster
#[clap(ignore_case = true)]
keytype: KeyPairType,
},
#[clap(name = "get", about = "Retrieves a keypair and prints the contents")]
GetCommand {
#[clap(help = "The name of the key to output")]
keyname: String,
#[clap(
short = 'd',
long = "directory",
env = "WASH_KEYS",
hide_env_values = true,
help = "Absolute path to where keypairs are stored. Defaults to `$HOME/.wash/keys`"
)]
directory: Option<PathBuf>,
},
#[clap(name = "list", about = "Lists all keypairs in a directory")]
ListCommand {
#[clap(
short = 'd',
long = "directory",
env = "WASH_KEYS",
hide_env_values = true,
help = "Absolute path to where keypairs are stored. Defaults to `$HOME/.wash/keys`"
)]
directory: Option<PathBuf>,
},
}
pub(crate) fn handle_command(command: KeysCliCommand) -> Result<CommandOutput> {
match command {
KeysCliCommand::GenCommand { keytype } => generate(&keytype),
KeysCliCommand::GetCommand { keyname, directory } => get(&keyname, directory),
KeysCliCommand::ListCommand { directory } => list(directory),
}
}
/// Generates a keypair of the specified KeyPairType
pub(crate) fn generate(kt: &KeyPairType) -> Result<CommandOutput> {
let kp = KeyPair::new(kt.clone());
let seed = kp.seed()?;
let mut map = HashMap::new();
map.insert("public_key".to_string(), json!(kp.public_key()));
map.insert("seed".to_string(), json!(seed));
Ok(CommandOutput::new(
format!(
"Public Key: {}\nSeed: {}\n\nRemember that the seed is private, treat it as a secret.",
kp.public_key(),
seed,
),
map,
))
}
/// Retrieves a keypair by name in a specified directory, or $WASH_KEYS ($HOME/.wash/keys) if directory is not specified
pub(crate) fn get(keyname: &str, directory: Option<PathBuf>) -> Result<CommandOutput> {
let key_dir = KeyDir::new(determine_directory(directory)?)?;
// Trim off the ".nk" for backwards compat
let key = key_dir
.get(keyname.trim_end_matches(NKEYS_EXTENSION))?
.ok_or_else(|| anyhow::anyhow!("Key {} doesn't exist", keyname))?;
Ok(CommandOutput::from_key_and_text("seed", key.seed()?))
}
/// Lists all keypairs (file extension .nk) in a specified directory or $WASH_KEYS($HOME/.wash/keys) if directory is not specified
pub(crate) fn list(directory: Option<PathBuf>) -> Result<CommandOutput> {
let key_dir = KeyDir::new(determine_directory(directory)?)?;
let keys = key_dir.list_names()?;
let mut map = HashMap::new();
map.insert("keys".to_string(), json!(keys));
Ok(CommandOutput::new(
format!(
"====== Keys found in {} ======\n{}",
key_dir.display(),
keys.join("\n")
),
map,
))
}
fn determine_directory(directory: Option<PathBuf>) -> Result<PathBuf> {
if let Some(d) = directory {
Ok(d)
} else {
let d = cfg_dir()?.join("keys");
Ok(d)
}
}
#[cfg(test)]
mod tests {
use super::{generate, KeysCliCommand};
use clap::Parser;
use nkeys::KeyPairType;
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Debug, Parser)]
struct Cmd {
#[clap(subcommand)]
keys: KeysCliCommand,
}
#[test]
fn test_generate_basic_test() {
let kt = KeyPairType::Account;
let keypair = generate(&kt).unwrap();
assert!(keypair.text.contains("Public Key: "));
assert!(keypair.text.contains("Seed: "));
assert!(keypair
.text
.contains("Remember that the seed is private, treat it as a secret."));
assert_ne!(keypair.text, "");
assert!(!keypair.map.is_empty());
}
#[derive(Debug, Clone, Deserialize)]
struct KeyPairJson {
public_key: String,
seed: String,
}
#[test]
fn test_generate_valid_keypair() {
let sample_public_key = "MBBLAHS7MCGNQ6IR4ZDSGRIAF7NVS7FCKFTKGO5JJJKN2QQRVAH7BSIO";
let sample_seed = "SMAH45IUULL57OSX23NOOOTLSVNQOORMDLE3Y3PQLJ4J5MY7MN2K7BIFI4";
let kt = KeyPairType::Module;
let keypair_json = generate(&kt).unwrap();
let keypair: KeyPairJson =
serde_json::from_str(serde_json::to_string(&keypair_json.map).unwrap().as_str())
.unwrap();
assert_eq!(keypair.public_key.len(), sample_public_key.len());
assert_eq!(keypair.seed.len(), sample_seed.len());
assert!(keypair.public_key.starts_with('M'));
assert!(keypair.seed.starts_with("SM"));
}
#[test]
fn test_generate_all_types() {
let sample_public_key = "MBBLAHS7MCGNQ6IR4ZDSGRIAF7NVS7FCKFTKGO5JJJKN2QQRVAH7BSIO";
let sample_seed = "SMAH45IUULL57OSXNOOAKOTLSVNQOORMDLE3Y3PQLJ4J5MY7MN2K7BIFI4";
let account_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Account).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let user_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::User).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let module_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Module).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let service_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Service).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let server_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Server).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let operator_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Operator).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
let cluster_keypair: KeyPairJson = serde_json::from_str(
serde_json::to_string(&generate(&KeyPairType::Cluster).unwrap().map)
.unwrap()
.as_str(),
)
.unwrap();
assert!(account_keypair.public_key.starts_with('A'));
assert_eq!(account_keypair.public_key.len(), sample_public_key.len());
assert!(account_keypair.seed.starts_with("SA"));
assert_eq!(account_keypair.seed.len(), sample_seed.len());
assert!(user_keypair.public_key.starts_with('U'));
assert_eq!(user_keypair.public_key.len(), sample_public_key.len());
assert!(user_keypair.seed.starts_with("SU"));
assert_eq!(user_keypair.seed.len(), sample_seed.len());
assert!(module_keypair.public_key.starts_with('M'));
assert_eq!(module_keypair.public_key.len(), sample_public_key.len());
assert!(module_keypair.seed.starts_with("SM"));
assert_eq!(module_keypair.seed.len(), sample_seed.len());
assert!(service_keypair.public_key.starts_with('V'));
assert_eq!(service_keypair.public_key.len(), sample_public_key.len());
assert!(service_keypair.seed.starts_with("SV"));
assert_eq!(service_keypair.seed.len(), sample_seed.len());
assert!(server_keypair.public_key.starts_with('N'));
assert_eq!(server_keypair.public_key.len(), sample_public_key.len());
assert!(server_keypair.seed.starts_with("SN"));
assert_eq!(server_keypair.seed.len(), sample_seed.len());
assert!(operator_keypair.public_key.starts_with('O'));
assert_eq!(operator_keypair.public_key.len(), sample_public_key.len());
assert!(operator_keypair.seed.starts_with("SO"));
assert_eq!(operator_keypair.seed.len(), sample_seed.len());
assert!(cluster_keypair.public_key.starts_with('C'));
assert_eq!(cluster_keypair.public_key.len(), sample_public_key.len());
assert!(cluster_keypair.seed.starts_with("SC"));
assert_eq!(cluster_keypair.seed.len(), sample_seed.len());
}
#[test]
/// Enumerates multiple options of the `gen` command to ensure API doesn't
/// change between versions. This test will fail if `wash keys gen <type>`
/// changes syntax, ordering of required elements, or flags.
fn test_gen_comprehensive() {
let key_gen_types = [
"account", "user", "module", "service", "server", "operator", "cluster",
];
key_gen_types.iter().for_each(|cmd| {
let gen_cmd: Cmd = clap::Parser::try_parse_from(["keys", "gen", cmd]).unwrap();
match gen_cmd.keys {
KeysCliCommand::GenCommand { keytype } => {
use KeyPairType::*;
match keytype {
Account => assert_eq!(*cmd, "account"),
User => assert_eq!(*cmd, "user"),
Module => assert_eq!(*cmd, "module"),
Service => assert_eq!(*cmd, "service"),
Server => assert_eq!(*cmd, "server"),
Operator => assert_eq!(*cmd, "operator"),
Cluster => assert_eq!(*cmd, "cluster"),
}
}
_ => panic!("`keys gen` constructed incorrect command"),
};
});
key_gen_types.iter().for_each(|cmd| {
let gen_cmd: Cmd = clap::Parser::try_parse_from(["keys", "gen", cmd]).unwrap();
match gen_cmd.keys {
KeysCliCommand::GenCommand { keytype } => {
use KeyPairType::*;
match keytype {
Account => assert_eq!(*cmd, "account"),
User => assert_eq!(*cmd, "user"),
Module => assert_eq!(*cmd, "module"),
Service => assert_eq!(*cmd, "service"),
Server => assert_eq!(*cmd, "server"),
Operator => assert_eq!(*cmd, "operator"),
Cluster => assert_eq!(*cmd, "cluster"),
}
}
_ => panic!("`keys gen` constructed incorrect command"),
};
});
}
#[test]
fn test_get_basic() {
const KEYNAME: &str = "get_basic_test.nk";
const KEYPATH: &str = "./tests/fixtures";
let gen_basic: Cmd =
clap::Parser::try_parse_from(["keys", "get", KEYNAME, "--directory", KEYPATH]).unwrap();
match gen_basic.keys {
KeysCliCommand::GetCommand { keyname, .. } => assert_eq!(keyname, KEYNAME),
other_cmd => panic!("keys get generated other command {other_cmd:?}"),
}
}
#[test]
/// Enumerates multiple options of the `get` command to ensure API doesn't
/// change between versions. This test will fail if `wash keys get`
/// changes syntax, ordering of required elements, or flags.
fn test_get_comprehensive() {
const KEYPATH: &str = "./tests/fixtures";
const KEYNAME: &str = "get_comprehensive_test.nk";
let get_all_flags: Cmd =
clap::Parser::try_parse_from(["keys", "get", KEYNAME, "-d", KEYPATH]).unwrap();
match get_all_flags.keys {
KeysCliCommand::GetCommand { keyname, directory } => {
assert_eq!(keyname, KEYNAME);
assert_eq!(directory, Some(PathBuf::from(KEYPATH)));
}
other_cmd => panic!("keys get generated other command {other_cmd:?}"),
}
}
#[test]
/// Enumerates multiple options of the `list` command to ensure API doesn't
/// change between versions. This test will fail if `wash keys list`
/// changes syntax, ordering of required elements, or flags.
fn test_list_comprehensive() {
const KEYPATH: &str = "./";
let list_basic: Cmd =
clap::Parser::try_parse_from(["keys", "list", "-d", KEYPATH]).unwrap();
match list_basic.keys {
KeysCliCommand::ListCommand { .. } => (),
other_cmd => panic!("keys get generated other command {other_cmd:?}"),
}
let list_all_flags: Cmd =
clap::Parser::try_parse_from(["keys", "list", "-d", KEYPATH]).unwrap();
match list_all_flags.keys {
KeysCliCommand::ListCommand { directory } => {
assert_eq!(directory, Some(PathBuf::from(KEYPATH)));
}
other_cmd => panic!("keys get generated other command {other_cmd:?}"),
}
}
}