sopsy 1.1.3

The missing developer experience for SOPS
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
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
//! Integration tests for the `sopsy init` command, exercised through the real
//! compiled binary against **real files**.
//!
//! The happy path uses the real `sops` + `age` toolchain: a genuine age keypair
//! (`age-keygen`) is registered as the recipient, the binary encrypts a real
//! `.env.encrypted`, and we decrypt it back with `SOPS_AGE_KEY_FILE`. The Secure
//! Enclave *generate* path cannot run in CI (it needs Apple hardware), so it is
//! driven through a **fake** `age-plugin-se` and a **fake** `sops` injected via
//! `SOPSY_AGE_PLUGIN_SE_BIN` / `SOPSY_SOPS_BIN`. Env-mutating tests are
//! `#[serial]` because those variables are process-wide.

use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;

use assert_cmd::Command;
use assert_fs::TempDir;
use predicates::prelude::*;
use serial_test::serial;

// ----------------------------------------------------------------------------
// Fixtures (local to this test file)
// ----------------------------------------------------------------------------

/// Initialise a real git repository inside `dir` (config set so commits/ignore
/// checks work deterministically).
fn init_git_repo(dir: &Path) {
    run_git(dir, &["init", "-q"]);
    run_git(dir, &["config", "user.email", "test@example.com"]);
    run_git(dir, &["config", "user.name", "Sopsy Test"]);
}

/// Run a `git` subcommand in `dir`, asserting success.
fn run_git(dir: &Path, args: &[&str]) {
    let status = StdCommand::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .status()
        .expect("git should be installed");
    assert!(status.success(), "git {args:?} failed");
}

/// Generate a real age keypair, returning `(public_key, key_file_path)`.
fn generate_age_key(dir: &Path) -> (String, PathBuf) {
    let key_file = dir.join("age-key.txt");
    let output = StdCommand::new("age-keygen")
        .arg("-o")
        .arg(&key_file)
        .output()
        .expect("age-keygen should be installed");
    assert!(
        output.status.success(),
        "age-keygen failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    let public_key = stderr
        .lines()
        .find_map(|line| line.split("Public key:").nth(1))
        .map(|s| s.trim().to_string())
        .expect("age-keygen should print the public key");
    (public_key, key_file)
}

/// Build a `Command` for the compiled `sopsy` binary rooted at `dir`.
fn sopsy_in(dir: &Path) -> Command {
    let mut cmd = Command::cargo_bin("sopsy").expect("binary `sopsy` should build");
    cmd.current_dir(dir);
    cmd
}

/// Write an executable shell script to `path` with the given body.
fn write_script(path: &Path, body: &str) {
    std::fs::write(path, format!("#!/bin/sh\n{body}")).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = std::fs::metadata(path).unwrap().permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(path, perms).unwrap();
    }
}

// ----------------------------------------------------------------------------
// Real end-to-end happy path (real sops + age)
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn init_with_public_key_encrypts_real_env() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, key_file) = generate_age_key(dir.path());

    // Seed a real `.env` so we can assert an exact decrypt round-trip.
    let plaintext = "PASSWORD=hunter2\nAPI_KEY=abc123\n"; // pragma: allowlist secret
    std::fs::write(dir.path().join(".env"), plaintext).unwrap();

    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
            "--recipient-name",
            "primary",
        ])
        .assert()
        .success();

    // All four bootstrap files were created.
    for name in [".sops.yaml", ".env.example", ".env.encrypted", ".sopsy.yml"] {
        assert!(
            dir.path().join(name).exists(),
            "{name} should have been created"
        );
    }

    // `.env.encrypted` carries real sops/age ciphertext.
    let encrypted = std::fs::read_to_string(dir.path().join(".env.encrypted")).unwrap();
    assert!(encrypted.contains("ENC["), "missing ENC[ markers");
    assert!(encrypted.contains("sops"), "missing sops metadata");
    assert_ne!(encrypted, plaintext, "file was not encrypted");

    // It decrypts back to exactly the seed using the private key.
    let decrypted = StdCommand::new("sops")
        .env("SOPS_AGE_KEY_FILE", &key_file)
        .args([
            "--decrypt",
            "--input-type",
            "dotenv",
            "--output-type",
            "dotenv",
        ])
        .arg(dir.path().join(".env.encrypted"))
        .output()
        .unwrap();
    assert!(
        decrypted.status.success(),
        "decrypt failed: {}",
        String::from_utf8_lossy(&decrypted.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&decrypted.stdout), plaintext);

    // `.gitignore` protects plaintext `.env` but the recipient key is recorded.
    let gitignore = std::fs::read_to_string(dir.path().join(".gitignore")).unwrap();
    assert!(gitignore.lines().any(|l| l == ".env"), "{gitignore}");
    let config = std::fs::read_to_string(dir.path().join(".sopsy.yml")).unwrap();
    assert!(config.contains(&public_key), "recipient key not recorded");
    assert!(config.contains("primary"), "recipient name not recorded");
}

#[test]
#[serial]
fn init_seeds_from_env_example_when_no_dotenv() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, key_file) = generate_age_key(dir.path());

    // Pre-create a custom `.env.example` and provide NO `.env`, so the seed for
    // `.env.encrypted` must come from `.env.example`.
    let example = "FROM_EXAMPLE=yes\nTOKEN=seed-me\n";
    std::fs::write(dir.path().join(".env.example"), example).unwrap();

    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
        ])
        .assert()
        .success()
        // Confirms init kept the existing `.env.example` (info branch).
        .stdout(predicate::str::contains(".env.example already present"));

    // Decrypting `.env.encrypted` yields exactly the `.env.example` contents.
    let decrypted = StdCommand::new("sops")
        .env("SOPS_AGE_KEY_FILE", &key_file)
        .args([
            "--decrypt",
            "--input-type",
            "dotenv",
            "--output-type",
            "dotenv",
        ])
        .arg(dir.path().join(".env.encrypted"))
        .output()
        .unwrap();
    assert!(
        decrypted.status.success(),
        "decrypt failed: {}",
        String::from_utf8_lossy(&decrypted.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&decrypted.stdout), example);
}

// ----------------------------------------------------------------------------
// Legacy repo: a pre-existing .gitignore must not strand encrypted artifacts
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn init_rescues_encrypted_artifacts_in_legacy_gitignore() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, _key_file) = generate_age_key(dir.path());

    // The canonical real-world dotenv ignore: people almost always have these
    // two lines (and no encrypted-rescue), so `.env.*` already hides every
    // encrypted dotenv artifact.
    std::fs::write(dir.path().join(".gitignore"), ".env\n.env.*\n").unwrap();

    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
        ])
        .assert()
        .success();

    let gitignore = std::fs::read_to_string(dir.path().join(".gitignore")).unwrap();
    // The negations are appended (after the pre-existing `.env.*`), and the
    // legacy content is preserved (not duplicated).
    assert!(gitignore.contains("!*.encrypted"), "{gitignore}");
    assert_eq!(
        gitignore.matches("\n.env.*").count() + usize::from(gitignore.starts_with(".env.*")),
        1,
        "must not duplicate the pre-existing rule:\n{gitignore}"
    );

    // `git check-ignore` exits 0 when ignored, 1 otherwise. Both the encrypted
    // artifact and the plaintext template must stay committable despite `.env.*`.
    for committable in [".env.example.encrypted", ".env.example"] {
        std::fs::write(dir.path().join(committable), "x").unwrap();
        let ignored = StdCommand::new("git")
            .arg("-C")
            .arg(dir.path())
            .args(["check-ignore", committable])
            .status()
            .unwrap()
            .success();
        assert!(
            !ignored,
            "{committable} must be committable, not gitignored"
        );
    }

    // And a real plaintext dotenv stays ignored.
    std::fs::write(dir.path().join(".env.local"), "x").unwrap();
    let env_local_ignored = StdCommand::new("git")
        .arg("-C")
        .arg(dir.path())
        .args(["check-ignore", ".env.local"])
        .status()
        .unwrap()
        .success();
    assert!(env_local_ignored, ".env.local must still be ignored");
}

// ----------------------------------------------------------------------------
// Idempotency
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn rerun_preserves_sops_yaml_unless_forced() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, _key_file) = generate_age_key(dir.path());

    let args = [
        "--non-interactive",
        "init",
        "--no-generate",
        "--public-key",
        public_key.as_str(),
    ];

    sopsy_in(dir.path()).args(args).assert().success();

    // Tag `.sops.yaml` with a sentinel an idempotent re-run must preserve.
    let sops_yaml = dir.path().join(".sops.yaml");
    let original = std::fs::read_to_string(&sops_yaml).unwrap();
    std::fs::write(&sops_yaml, format!("{original}# SENTINEL\n")).unwrap();

    // Re-run without --force: sentinel survives (file left untouched).
    sopsy_in(dir.path()).args(args).assert().success();
    assert!(
        std::fs::read_to_string(&sops_yaml)
            .unwrap()
            .contains("# SENTINEL"),
        "non-forced re-run must not clobber .sops.yaml"
    );

    // Re-run with --force: file is rewritten and the sentinel is gone.
    sopsy_in(dir.path())
        .args(args)
        .arg("--force")
        .assert()
        .success();
    assert!(
        !std::fs::read_to_string(&sops_yaml)
            .unwrap()
            .contains("# SENTINEL"),
        "--force must rewrite .sops.yaml"
    );
}

// ----------------------------------------------------------------------------
// Secure Enclave generate path (fake age-plugin-se + fake sops)
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn init_generates_enclave_identity_with_fakes() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());

    let fake_pubkey = "age1se1qg8vwwqhztnh3vpt2nf2xwn7famktxlmp0nmkfltp8lkvzp8nafkqleh258";

    // Fake `age-plugin-se keygen` emitting a canned identity file.
    let plugin = dir.path().join("age-plugin-se");
    write_script(
        &plugin,
        &format!("cat <<'EOF'\n# public key: {fake_pubkey}\nAGE-PLUGIN-SE-1QFAKEIDENTITY\nEOF\n"),
    );

    // Fake `sops`: answers --version, otherwise records the invocation and
    // writes a marker into the (last-arg) target file to simulate encryption.
    let record = dir.path().join("sops-invocations.log");
    let sops = dir.path().join("sops");
    write_script(
        &sops,
        &format!(
            "case \"$1\" in\n  --version) echo 'sops 9.9.9 (fake)'; exit 0;;\nesac\n\
             echo \"$@\" >> '{record}'\n\
             for a in \"$@\"; do f=\"$a\"; done\n\
             echo 'ENC[fake-sops]' > \"$f\"\n",
            record = record.display()
        ),
    );

    sopsy_in(dir.path())
        .env("SOPSY_AGE_PLUGIN_SE_BIN", &plugin)
        .env("SOPSY_SOPS_BIN", &sops)
        // Keep the generated (fake) identity out of the real per-user keystore.
        .env("SOPSY_KEYS_FILE", dir.path().join("age-keys.txt"))
        .args(["--non-interactive", "init"])
        .assert()
        .success();

    // The fake identity was stored in the redirected keystore, not the real one.
    let keys = std::fs::read_to_string(dir.path().join("age-keys.txt")).unwrap();
    assert!(
        keys.contains("AGE-PLUGIN-SE-1QFAKEIDENTITY"),
        "identity should be persisted to the keystore so sops can decrypt"
    );

    // The generated enclave public key is recorded in `.sopsy.yml`.
    let config = std::fs::read_to_string(dir.path().join(".sopsy.yml")).unwrap();
    assert!(
        config.contains(fake_pubkey),
        "enclave identity not recorded in .sopsy.yml:\n{config}"
    );

    // sops was actually invoked to encrypt `.env.encrypted`.
    let log = std::fs::read_to_string(&record).unwrap();
    assert!(log.contains("--encrypt"), "sops encrypt not invoked: {log}");
    assert!(log.contains("--in-place"), "sops not run in place: {log}");
    let encrypted = std::fs::read_to_string(dir.path().join(".env.encrypted")).unwrap();
    assert!(
        encrypted.contains("ENC[fake-sops]"),
        "fake sops did not encrypt"
    );
}

// ----------------------------------------------------------------------------
// Friendly errors
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn non_interactive_without_key_or_generation_fails_friendly() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());

    sopsy_in(dir.path())
        .args(["--non-interactive", "init", "--no-generate"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("--public-key"));

    // Nothing destructive was written when init bailed out early.
    assert!(!dir.path().join(".sops.yaml").exists());
}

#[test]
fn init_outside_git_repo_fails_friendly() {
    let dir = TempDir::new().unwrap();
    // No `git init`: must produce a clear, friendly error.
    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            "age1abc",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("git repository"));
}

#[test]
#[serial]
fn init_records_username_and_respects_no_break_glass() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, _key_file) = generate_age_key(dir.path());

    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
            "--username",
            "alice",
            "--no-break-glass",
        ])
        .assert()
        .success();

    let config = std::fs::read_to_string(dir.path().join(".sopsy.yml")).unwrap();
    assert!(
        config.contains("username: alice"),
        "username should be recorded"
    );
    assert!(
        !config.contains("break_glass: true"),
        "--no-break-glass must skip break-glass setup"
    );
}

// ----------------------------------------------------------------------------
// Break-glass during init
// ----------------------------------------------------------------------------

#[test]
#[serial]
fn init_without_break_glass_prints_guidance() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, _key_file) = generate_age_key(dir.path());

    // Non-interactive init skips break-glass but tells the owner how to add it.
    sopsy_in(dir.path())
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("sopsy recipient break-glass"));

    let config = std::fs::read_to_string(dir.path().join(".sopsy.yml")).unwrap();
    assert!(
        !config.contains("break_glass: true"),
        "no break-glass should be registered without the flag"
    );
}

#[test]
#[serial]
fn init_break_glass_flag_registers_offline_key() {
    let dir = TempDir::new().unwrap();
    init_git_repo(dir.path());
    let (public_key, key_file) = generate_age_key(dir.path());

    // `--break-glass` runs the ceremony; SOPS_AGE_KEY_FILE lets the implicit
    // re-key decrypt with the owner's key, and SOPSY_ASSUME_YES skips the
    // interactive "copy to 1Password, press ENTER" wait.
    sopsy_in(dir.path())
        .env("SOPS_AGE_KEY_FILE", &key_file)
        .env("SOPSY_ASSUME_YES", "1")
        .args([
            "--non-interactive",
            "init",
            "--no-generate",
            "--public-key",
            &public_key,
            "--break-glass",
        ])
        .assert()
        .success();

    // A break-glass recipient is recorded, and the transient files are gone.
    let config = std::fs::read_to_string(dir.path().join(".sopsy.yml")).unwrap();
    assert!(
        config.contains("break_glass: true"),
        "break-glass recipient should be recorded:\n{config}"
    );
    assert!(!dir.path().join("break-glass.private").exists());
    assert!(!dir.path().join("break-glass.public").exists());

    // The owner key is still a recipient, and the secret decrypts with it.
    let sops_yaml = std::fs::read_to_string(dir.path().join(".sops.yaml")).unwrap();
    assert!(
        sops_yaml.contains(&public_key),
        "owner key must remain a recipient"
    );
    let decrypted = StdCommand::new("sops")
        .env("SOPS_AGE_KEY_FILE", &key_file)
        .args([
            "--decrypt",
            "--input-type",
            "dotenv",
            "--output-type",
            "dotenv",
        ])
        .arg(dir.path().join(".env.encrypted"))
        .output()
        .unwrap();
    assert!(
        decrypted.status.success(),
        "owner should still decrypt after break-glass re-key: {}",
        String::from_utf8_lossy(&decrypted.stderr)
    );
}