dugout 0.1.10

Git-native secrets manager for development teams, written in Rust
Documentation
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
//! Tests for `dugout knock`, `dugout pending`, and `dugout admit` commands.

use crate::support::*;
use std::fs;

#[test]
fn test_knock_creates_request_file() {
    let t = Test::init("alice");

    // Setup global identity first
    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    // Run knock
    let output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&output);
    assert_stdout_contains(&output, "created access request");

    // Check that request file exists
    let request_path = t.dir.path().join(".dugout/requests/default/bob.pub");
    assert!(request_path.exists(), "request file should exist");

    // Verify request file contains a valid age public key
    let pubkey = fs::read_to_string(&request_path).unwrap();
    assert!(pubkey.trim().starts_with("age1"));
}

#[test]
fn test_knock_without_global_identity_fails() {
    let t = Test::init("alice");

    let output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_failure(&output);
    assert_stderr_contains(&output, "no identity found");
    assert_stdout_contains(&output, "dugout setup");
}

#[test]
fn test_knock_when_already_member() {
    let t = Test::new();

    // Setup global identity first
    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    // Get the global public key
    let pubkey_path = t.home.path().join(".dugout/identity.pub");
    let global_pubkey = fs::read_to_string(&pubkey_path).unwrap().trim().to_string();

    // Init vault
    let init_output = t
        .cmd()
        .args(["init", "--no-banner", "--name", "alice"])
        .output()
        .unwrap();
    assert_success(&init_output);

    // Manually add the global public key to recipients to simulate already being a member
    let config_path = t.dir.path().join(".dugout.toml");
    let config_content = fs::read_to_string(&config_path).unwrap();

    // Replace the project-specific key with the global key
    let updated_config = config_content
        .lines()
        .map(|line| {
            if line.contains("age1") && !line.contains(&global_pubkey) {
                // Replace any age key with the global key
                format!("alice = \"{}\"", global_pubkey)
            } else {
                line.to_string()
            }
        })
        .collect::<Vec<_>>()
        .join("\n");

    fs::write(&config_path, updated_config).unwrap();

    // Try to knock - should warn already a member
    let output = t.cmd().args(["knock", "alice"]).output().unwrap();
    assert_success(&output);
    assert_stdout_contains(&output, "already have access");
}

#[test]
fn test_pending_lists_requests() {
    let t = Test::init("alice");

    // Setup global identity and create request
    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    let knock_output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&knock_output);

    // List pending requests
    let output = t.cmd().arg("pending").output().unwrap();
    assert_success(&output);
    assert_stdout_contains(&output, "bob");
    assert_stdout_contains(&output, "age1");
}

#[test]
fn test_pending_when_no_requests() {
    let t = Test::init("alice");

    let output = t.cmd().arg("pending").output().unwrap();
    assert_success(&output);
    assert_stdout_contains(&output, "no pending requests");
}

#[test]
fn test_admit_approves_request() {
    let t = Test::init("alice");

    // Setup global identity and create request
    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    let knock_output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&knock_output);

    // Admit bob
    let output = t.cmd().args(["admit", "bob"]).output().unwrap();
    assert_success(&output);
    assert_stdout_contains(&output, "admitted");

    // Request file should be deleted
    let request_path = t.dir.path().join(".dugout/requests/default/bob.pub");
    assert!(!request_path.exists(), "request file should be deleted");

    // Bob should now be in the team
    let team_output = t.team_list();
    assert_success(&team_output);
    assert_stdout_contains(&team_output, "bob");
}

#[test]
fn test_admit_nonexistent_request_fails() {
    let t = Test::init("alice");

    let output = t.cmd().args(["admit", "nonexistent"]).output().unwrap();
    assert_failure(&output);
    assert_stderr_contains(&output, "no pending request");
}

#[test]
fn test_knock_pending_admit_workflow() {
    let t = Test::init("alice");

    // Setup global identity
    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    // Bob knocks
    let knock_output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&knock_output);

    // Check pending - should show bob
    let pending_output = t.cmd().arg("pending").output().unwrap();
    assert_success(&pending_output);
    assert_stdout_contains(&pending_output, "bob");

    // Admit bob
    let admit_output = t.cmd().args(["admit", "bob"]).output().unwrap();
    assert_success(&admit_output);

    // Check pending again - should be empty
    let pending_output2 = t.cmd().arg("pending").output().unwrap();
    assert_success(&pending_output2);
    assert_stdout_contains(&pending_output2, "no pending requests");

    // Check team - bob should be there
    let team_output = t.team_list();
    assert_success(&team_output);
    assert_stdout_contains(&team_output, "bob");
}

#[test]
fn test_full_onboarding_with_separate_identities() {
    // Simulates: Alice creates vault, Bob knocks, Alice admits, Bob can decrypt
    let t = Test::new();

    // --- Alice's setup ---
    // Create a separate home for Alice
    let alice_home = tempfile::TempDir::new().unwrap();

    // Alice sets up her global identity
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .arg("setup")
        .output()
        .unwrap();
    assert_success(&output);

    let alice_pubkey = fs::read_to_string(alice_home.path().join(".dugout/identity.pub"))
        .unwrap()
        .trim()
        .to_string();

    // Alice inits the vault (uses her global identity)
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .args(["init", "--name", "alice"])
        .output()
        .unwrap();
    assert_success(&output);

    // Alice sets a secret
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .args(["set", "API_KEY", "super_secret_123"])
        .output()
        .unwrap();
    assert_success(&output);

    // Verify Alice can read it
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .args(["get", "API_KEY"])
        .output()
        .unwrap();
    assert_success(&output);
    assert_eq!(stdout(&output).trim(), "super_secret_123");

    // --- Bob's setup ---
    let bob_home = tempfile::TempDir::new().unwrap();

    // Bob sets up his global identity
    let output = t
        .cmd()
        .env("HOME", bob_home.path())
        .env("USERPROFILE", bob_home.path())
        .arg("setup")
        .output()
        .unwrap();
    assert_success(&output);

    let bob_pubkey = fs::read_to_string(bob_home.path().join(".dugout/identity.pub"))
        .unwrap()
        .trim()
        .to_string();

    // Keys should be different
    assert_ne!(
        alice_pubkey, bob_pubkey,
        "alice and bob should have different keys"
    );

    // --- Bob knocks ---
    let output = t
        .cmd()
        .env("HOME", bob_home.path())
        .env("USERPROFILE", bob_home.path())
        .args(["knock", "bob"])
        .output()
        .unwrap();
    assert_success(&output);

    // Verify the request file contains Bob's public key
    let request_path = t.dir.path().join(".dugout/requests/default/bob.pub");
    assert!(request_path.exists());
    let request_key = fs::read_to_string(&request_path).unwrap();
    assert_eq!(request_key.trim(), bob_pubkey);

    // --- Alice admits Bob ---
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .args(["admit", "bob"])
        .output()
        .unwrap();
    assert_success(&output);

    // Request file should be cleaned up
    assert!(!request_path.exists());

    // --- Verify team has both members ---
    let output = t
        .cmd()
        .env("HOME", alice_home.path())
        .env("USERPROFILE", alice_home.path())
        .args(["team", "list", "--json"])
        .output()
        .unwrap();
    assert_success(&output);
    let team_json = stdout(&output);
    assert!(
        team_json.contains(&alice_pubkey),
        "team should contain alice's key"
    );
    assert!(
        team_json.contains(&bob_pubkey),
        "team should contain bob's key"
    );

    // --- Bob can now decrypt ---
    let output = t
        .cmd()
        .env("HOME", bob_home.path())
        .env("USERPROFILE", bob_home.path())
        .args(["get", "API_KEY"])
        .output()
        .unwrap();
    assert_success(&output);
    assert_eq!(stdout(&output).trim(), "super_secret_123");
}

#[test]
fn test_knock_uses_global_identity_key() {
    // Verify knock writes the GLOBAL identity key, not a project key
    let t = Test::init("alice");

    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    let global_pubkey = fs::read_to_string(t.home.path().join(".dugout/identity.pub"))
        .unwrap()
        .trim()
        .to_string();

    let output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&output);

    let request_key = fs::read_to_string(t.dir.path().join(".dugout/requests/default/bob.pub"))
        .unwrap()
        .trim()
        .to_string();

    assert_eq!(
        request_key, global_pubkey,
        "knock should use the global identity key"
    );
}

#[test]
fn test_knock_output_includes_instructions() {
    let t = Test::init("alice");

    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    let output = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&output);

    // Should show success and hint about sharing the request file
    assert_stdout_contains(&output, "created access request");
    assert_stdout_contains(&output, ".dugout/requests/default/bob.pub");
}

#[test]
fn test_knock_rejects_invalid_member_name() {
    let t = Test::init("alice");

    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    let output = t.cmd().args(["knock", "../bob"]).output().unwrap();
    assert_failure(&output);
    assert_stderr_contains(&output, "invalid member name");

    let request_path = t.dir.path().join(".dugout/requests/default/../bob.pub");
    assert!(
        !request_path.exists(),
        "invalid names must not create request files"
    );
}

#[test]
fn test_knock_idempotent_same_key() {
    // Running knock twice with the same identity should be idempotent
    let t = Test::init("alice");

    let setup_output = t.cmd().arg("setup").output().unwrap();
    assert_success(&setup_output);

    // First knock
    let output1 = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&output1);
    assert_stdout_contains(&output1, "created access request");

    // Second knock with same identity
    let output2 = t.cmd().args(["knock", "bob"]).output().unwrap();
    assert_success(&output2);
    assert_stdout_contains(&output2, "already exists with your key");
}

#[test]
fn test_knock_different_key_fails() {
    // If someone else already requested with this name, reject
    let t = Test::init("alice");

    // Create first identity
    let home1 = tempfile::TempDir::new().unwrap();
    let output = t
        .cmd()
        .env("HOME", home1.path())
        .env("USERPROFILE", home1.path())
        .arg("setup")
        .output()
        .unwrap();
    assert_success(&output);

    // First person knocks as "bob"
    let output = t
        .cmd()
        .env("HOME", home1.path())
        .env("USERPROFILE", home1.path())
        .args(["knock", "bob"])
        .output()
        .unwrap();
    assert_success(&output);

    // Create second identity
    let home2 = tempfile::TempDir::new().unwrap();
    let output = t
        .cmd()
        .env("HOME", home2.path())
        .env("USERPROFILE", home2.path())
        .arg("setup")
        .output()
        .unwrap();
    assert_success(&output);

    // Second person tries to knock as "bob" - should fail
    let output = t
        .cmd()
        .env("HOME", home2.path())
        .env("USERPROFILE", home2.path())
        .args(["knock", "bob"])
        .output()
        .unwrap();
    assert_failure(&output);
    assert_stderr_contains(&output, "already exists with a different key");
}

#[test]
fn test_admit_corrupted_request_file() {
    let t = Test::init("alice");

    // Create a corrupted request file
    let request_dir = t.dir.path().join(".dugout/requests/default");
    fs::create_dir_all(&request_dir).unwrap();
    fs::write(request_dir.join("baduser.pub"), "not-a-valid-age-key\n").unwrap();

    // Try to admit - should fail with invalid key error
    let output = t.cmd().args(["admit", "baduser"]).output().unwrap();
    assert_failure(&output);
    // Should mention invalid key format
    assert_stderr_contains(&output, "invalid");
}