-
Notifications
You must be signed in to change notification settings - Fork 37
/
cli.rs
733 lines (667 loc) · 22 KB
/
cli.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Tests for the CLI
mod test_util;
use std::ffi::OsStr;
use std::path::Path;
use bindle::signature::KeyRingLoader;
use test_util::*;
use bindle::client::{tokens::TokenManager, Client};
use bindle::{testing, SignatureRole};
const ENV_BINDLE_KEYRING: &str = "BINDLE_KEYRING";
const KEYRING_FILE: &str = "keyring.toml";
const SECRETS_FILE: &str = "secret_keys.toml";
const TEST_LABEL: &str = "Benjamin Sisko <[email protected]>";
// Inserts data into the test server for fetching
async fn setup_data<T: TokenManager>(client: &Client<T>) {
// For now let's just put in a simple manifest and one with a lot of parcels
for name in &["valid_v1", "lotsa_parcels"] {
let scaffold = testing::Scaffold::load(name).await;
client
.create_invoice(scaffold.invoice.clone())
.await
.expect("Unable to insert invoice");
for parcel in scaffold.parcel_files.values() {
match client
.create_parcel(
&scaffold.invoice.bindle.id,
&parcel.sha,
parcel.data.clone(),
)
.await
{
Ok(_) => continue,
Err(e) if matches!(e, bindle::client::ClientError::ParcelAlreadyExists) => continue,
Err(e) => panic!("Unable to insert parcel: {}", e),
};
}
}
}
#[tokio::test]
async fn test_push() {
let controller = TestController::new(BINARY_NAME).await;
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Unable to get project directory");
let path = std::path::PathBuf::from(root).join("test/data/standalone");
// TODO: Figure out how to dedup these outputs. I tried doing something but `args` returns an `&mut` which complicates things
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"push",
"-p",
path.to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to push a full bindle");
}
#[tokio::test]
async fn test_push_tarball() {
let controller = TestController::new(BINARY_NAME).await;
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Unable to get project directory");
let path = std::path::PathBuf::from(root).join("test/data/standalone");
// TODO: Figure out how to dedup these outputs. I tried doing something but `args` returns an `&mut` which complicates things
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"push",
"-p",
path.to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to push a full bindle");
}
#[tokio::test]
async fn test_push_invoice_and_file() {
let controller = TestController::new(BINARY_NAME).await;
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Unable to get project directory");
let base = std::path::PathBuf::from(root).join("tests/scaffolds/valid_v1");
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"push-invoice",
base.join("invoice.toml").to_str().unwrap(),
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to push an invoice");
// Now try to push a file from the bindle
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"push-file",
"enterprise.com/warpcore/1.0.0",
base.join("parcels/parcel.dat").to_str().unwrap(),
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to push a file");
}
#[tokio::test]
async fn test_get() {
let controller = TestController::new(BINARY_NAME).await;
setup_data(&controller.client).await;
let cachedir = tempfile::tempdir().expect("unable to set up tempdir");
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"-d",
cachedir.path().to_str().unwrap(),
"get",
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.env(ENV_BINDLE_KEYRING, &controller.keyring_path)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to get an invoice");
// This is a sanity check test to make sure a second call triggers the code path for successfully fetching from the cache
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"-d",
cachedir.path().to_str().unwrap(),
"get",
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.env(ENV_BINDLE_KEYRING, &controller.keyring_path)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to get an invoice");
// Now try and export to a test directory and make sure it is there
let tempdir = tempfile::tempdir().expect("Unable to set up tempdir");
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"-d",
cachedir.path().to_str().unwrap(),
"get",
"-e",
tempdir.path().to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.env(ENV_BINDLE_KEYRING, &controller.keyring_path)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to get a bindle");
assert!(
tokio::fs::metadata(
tempdir
.path()
.join("1927aefa8fdc8327499e918300e2e49ecb271321530cc5881fcd069ca8372dcd.tar.gz")
)
.await
.expect("Unable to read exported bindle")
.is_file(),
"Expected exported bindle tarball"
)
}
#[tokio::test]
async fn test_get_invoice() {
let controller = TestController::new(BINARY_NAME).await;
setup_data(&controller.client).await;
let tempdir = tempfile::tempdir().expect("Unable to set up tempdir");
let cachedir = tempfile::tempdir().expect("Unable to set up tempdir");
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"-d",
cachedir.path().to_str().unwrap(),
"get-invoice",
"-o",
tempdir.path().join("invoice.toml").to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.env(ENV_BINDLE_KEYRING, &controller.keyring_path)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to get an invoice");
assert!(
tokio::fs::metadata(tempdir.path().join("invoice.toml"))
.await
.expect("Unable to read exported invoice")
.is_file(),
"Expected invoice file"
)
}
#[tokio::test]
async fn test_create_key_and_sign_invoice() {
let tempdir = tempfile::tempdir().expect("Unable to set up tempdir");
// Create a signing key
{
let cmd = format!(
"run --features cli --bin bindle -- keys create testkey -f {}",
tempdir.path().join("testkey.toml").to_str().unwrap()
);
let output = std::process::Command::new("cargo")
.args(cmd.split(' '))
.output()
.expect("Key should get created");
assert_status(output, "Key should be generated");
assert!(
tokio::fs::metadata(tempdir.path().join("testkey.toml"))
.await
.expect("Unable to read keyfile")
.is_file(),
"Expected key file"
);
}
// Use the new key to sign an invoice
{
let cmd = format!(
"run --features cli --bin bindle -- sign-invoice ./test/data/simple-invoice.toml -o {} -f {}",
tempdir.path().join("signed-invoice.toml").to_str().unwrap(),
tempdir.path().join("testkey.toml").to_str().unwrap()
);
let output = std::process::Command::new("cargo")
.args(cmd.split(' '))
.output()
.expect("Invoice should get signed");
assert_status(output, "Invoice should get signed");
assert!(
tokio::fs::metadata(tempdir.path().join("signed-invoice.toml"))
.await
.expect("Unable to read invoice")
.is_file(),
"Expected signed invoice"
)
}
}
#[tokio::test]
async fn test_get_parcel() {
let controller = TestController::new(BINARY_NAME).await;
setup_data(&controller.client).await;
let tempdir = tempfile::tempdir().expect("Unable to set up tempdir");
let cachedir = tempfile::tempdir().expect("Unable to set up tempdir");
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"-d",
cachedir.path().to_str().unwrap(),
"get-parcel",
"-o",
tempdir.path().join("parcel.dat").to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
"23f310b54076878fd4c36f0c60ec92011a8b406349b98dd37d08577d17397de5",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to get a parcel");
assert!(
tokio::fs::metadata(tempdir.path().join("parcel.dat"))
.await
.expect("Unable to read exported invoice")
.is_file(),
"Expected parcel file"
)
}
#[tokio::test]
async fn test_yank() {
let controller = TestController::new(BINARY_NAME).await;
setup_data(&controller.client).await;
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"yank",
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert_status(output, "Should be able to yank a bindle");
}
#[tokio::test]
async fn test_no_bindles() {
let controller = TestController::new(BINARY_NAME).await;
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"search",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.output()
.expect("Should be able to run command");
assert!(
output.status.success(),
"Should be able to search for bindles"
);
assert!(
String::from_utf8_lossy(&output.stdout)
.trim_end()
.ends_with("No matching bindles were found"),
"Should get no bindles found message"
);
}
#[tokio::test]
async fn test_package() {
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Unable to get project directory");
let path = std::path::PathBuf::from(root).join("test/data/standalone");
// TODO: Figure out how to dedup these outputs. I tried doing something but `args` returns an `&mut` which complicates things
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"package",
"-p",
path.to_str().unwrap(),
"-e",
tempdir.path().to_str().unwrap(),
"enterprise.com/warpcore/1.0.0",
])
.env(ENV_BINDLE_URL, "https://127.0.0.1:8080/v1/")
.output()
.expect("Should be able to run command");
assert_status(
output,
"Should be able to package a standalone bindle directory",
);
assert!(
tokio::fs::metadata(
tempdir
.path()
.join("1927aefa8fdc8327499e918300e2e49ecb271321530cc5881fcd069ca8372dcd.tar.gz")
)
.await
.expect("Unable to read packaged bindle")
.is_file(),
"Expected packaged bindle tarball"
)
}
#[tokio::test]
async fn test_key_create_and_keyring() {
// Tempdir for keyring and secret-file
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let secrets_file = tempdir.path().join(SECRETS_FILE);
let keyring_file = tempdir.path().join(KEYRING_FILE);
create_key(&keyring_file, &secrets_file, TEST_LABEL, false);
assert!(
tokio::fs::read_to_string(&secrets_file)
.await
.expect("Unable to read secrets file")
.contains(TEST_LABEL),
"Newly created key should be saved in file"
);
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert_eq!(
keyring.key.len(),
1,
"Only 1 entry should exist in the keyring"
);
assert_eq!(
keyring.key[0].label, TEST_LABEL,
"The keyring key should be from the newly created key"
);
// Create one more secret and make sure the keyring now has 2 entries
let second_label = "Kira Nerys <[email protected]>";
create_key(&keyring_file, &secrets_file, second_label, false);
assert!(
tokio::fs::read_to_string(&secrets_file)
.await
.expect("Unable to read secrets file")
.contains(second_label),
"Newly created key should be saved in file"
);
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert_eq!(keyring.key.len(), 2, "Should have 2 entries in keyring");
assert_eq!(
keyring.key[0].label, TEST_LABEL,
"Previously created key should still exist"
);
assert_eq!(
keyring.key[1].label, second_label,
"Newly created key should exist in keyring"
);
// Create a third and skip the keyring
let third_label = "Jadzia Dax <[email protected]>";
create_key(&keyring_file, &secrets_file, third_label, true);
assert!(
tokio::fs::read_to_string(&secrets_file)
.await
.expect("Unable to read secrets file")
.contains(third_label),
"Newly created key should be saved in file"
);
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert_eq!(
keyring.key.len(),
2,
"Newly created key should not have been added to keyring"
);
for key in keyring.key.iter() {
assert_ne!(
key.label, third_label,
"Newly created key should not exist in keyring"
);
}
}
#[tokio::test]
async fn test_keyring_add() {
// Tempdir for keyring
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let keyring_file = tempdir.path().join(KEYRING_FILE);
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"keys",
"add",
TEST_LABEL,
"approver",
"XbhLeOX4BtvUnT+o7xyi2waw5WXGOl3H/l3b5h97Dk4=",
])
.env(ENV_BINDLE_KEYRING, &keyring_file)
.output()
.expect("Should be able to run command");
assert!(output.status.success(), "Should be able to add a key");
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert_eq!(keyring.key.len(), 1, "Should have 1 entry in keyring");
assert_eq!(
keyring.key[0].label, TEST_LABEL,
"Correct label should have been added"
);
assert_eq!(keyring.key[0].roles.len(), 1, "Should only have 1 role");
assert_eq!(
keyring.key[0].roles[0],
SignatureRole::Approver,
"Should have approver role"
);
assert_eq!(
keyring.key[0].key, "XbhLeOX4BtvUnT+o7xyi2waw5WXGOl3H/l3b5h97Dk4=",
"Should have correct key"
);
}
#[tokio::test]
async fn test_keyring_add_to_existing() {
// Tempdir for keyring
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let secrets_file = tempdir.path().join(SECRETS_FILE);
let keyring_file = tempdir.path().join(KEYRING_FILE);
// Create a key to make sure one exists first
create_key(&keyring_file, &secrets_file, TEST_LABEL, false);
let second_label = "Miles O'Brien <[email protected]>";
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"keys",
"add",
second_label,
"approver",
"XbhLeOX4BtvUnT+o7xyi2waw5WXGOl3H/l3b5h97Dk4=",
])
.env(ENV_BINDLE_KEYRING, &keyring_file)
.output()
.expect("Should be able to run command");
assert!(output.status.success(), "Should be able to add a key");
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert_eq!(keyring.key.len(), 2, "Should have 2 entries in keyring");
assert_eq!(
keyring.key[1].label, second_label,
"New key should have been added"
);
}
#[tokio::test]
async fn test_fetch_host_keys() {
// Tempdir for keyring
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let keyring_file = tempdir.path().join(KEYRING_FILE);
let controller = TestController::new(BINARY_NAME).await;
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"keys",
"fetch",
])
.env(ENV_BINDLE_URL, &controller.base_url)
.env(ENV_BINDLE_KEYRING, &keyring_file)
.output()
.expect("Should be able to run command");
assert!(
output.status.success(),
"Should be able to fetch host keys Stdout: {}\n Stderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert!(
!keyring.key.is_empty(),
"Should have at least 1 entry in keyring"
);
}
#[tokio::test]
async fn test_fetch_host_keys_from_specific_host() {
// Tempdir for keyring
let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
let keyring_file = tempdir.path().join(KEYRING_FILE);
let controller = TestController::new(BINARY_NAME).await;
let output = std::process::Command::new("cargo")
.args([
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"keys",
"fetch",
"--key-server",
&format!("{}bindle-keys", controller.base_url),
])
.env(ENV_BINDLE_URL, "https://not-real.com")
.env(ENV_BINDLE_KEYRING, &keyring_file)
.output()
.expect("Should be able to run command");
assert!(
output.status.success(),
"Should be able to fetch host keys Stdout: {}\n Stderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let keyring = keyring_file
.load()
.await
.expect("Should be able to load keyring file");
assert!(
!keyring.key.is_empty(),
"Should have at least 1 entry in keyring"
);
}
fn create_key(
keyring_file: impl AsRef<OsStr>,
secrets_file: &Path,
label: &str,
skip_keyring: bool,
) {
let mut args = vec![
"run",
"--features",
"cli",
"--bin",
"bindle",
"--",
"keys",
"create",
"--secrets-file",
secrets_file.to_str().unwrap(),
"--roles",
"creator,approver",
];
if skip_keyring {
args.push("--skip-keyring");
}
args.push(label);
let output = std::process::Command::new("cargo")
.args(args)
.env(ENV_BINDLE_KEYRING, keyring_file)
.output()
.expect("Should be able to run command");
assert!(output.status.success(), "Should be able to create a key");
}
fn assert_status(output: std::process::Output, message: &str) {
assert!(
output.status.success(),
"{}:\nStdout:\n {}\nStderr:\n{}",
message,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}