sopsy 1.3.0

Public/private individual key encryption for repo secrets with biometrics support and explicit approval of who can decrypt. In other words — the missing good UX 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
//! Integration tests for `sopsy check` against **real** `git`, `sops` and
//! `age`.
//!
//! Each test builds a temporary git repository on disk: a real age keypair, a
//! `.sops.yaml` creation rule, a `.sopsy.yml` (with a break-glass recipient),
//! and a real sops-encrypted dotenv `.env.encrypted`. The healthy fixture must
//! make `sopsy check` exit `0`; every other fixture mutates exactly one
//! invariant and must make it exit non-zero while naming the failure on stdout.
//!
//! `sopsy check` never decrypts, so these tests only need the recipient's
//! public key (encryption), not the private key. They are marked `#[serial]`
//! because they shell out to real tools and to keep the suite deterministic.

use std::path::Path;
use std::process::Command;

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

/// Generate an age keypair in `dir`, returning the `age1…` public key.
fn generate_age_key(dir: &Path) -> String {
    let key_file = dir.join("age-key.txt");
    let output = Command::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);
    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")
}

/// Run `git -C <repo> <args…>`, asserting success.
fn git(repo: &Path, args: &[&str]) {
    let output = Command::new("git")
        .arg("-C")
        .arg(repo)
        .args(args)
        .output()
        .expect("git should be installed");
    assert!(
        output.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );
}

/// Encrypt `file` (relative to `repo`) in place as a sops dotenv file.
///
/// sops discovers `.sops.yaml` by walking up from the current working
/// directory, so the command runs with its cwd set to `repo`.
fn sops_encrypt_dotenv(repo: &Path, file: &str) {
    let output = Command::new("sops")
        .current_dir(repo)
        .args([
            "--encrypt",
            "--input-type",
            "dotenv",
            "--output-type",
            "dotenv",
            "--in-place",
        ])
        .arg(file)
        .output()
        .expect("sops should be installed");
    assert!(
        output.status.success(),
        "sops encrypt failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// Build a repository in a fresh temp dir per the [`FixtureOpts`], returning the
/// `TempDir` (kept alive for the duration of the test).
struct FixtureOpts {
    /// Use the healthy `.sops.yaml` rule (`\.encrypted$`). When `false`, the
    /// caller's `sops_yaml_override` is used as the final `.sops.yaml`.
    sops_yaml_override: Option<String>,
    /// Add `.env` to `.gitignore`.
    gitignore_env: bool,
    /// Configure a break-glass recipient in `.sopsy.yml`.
    break_glass: bool,
    /// Force-track a plaintext `.env` file.
    track_plaintext_env: bool,
    /// Track an extra plaintext `server.key` file.
    track_key_file: bool,
    /// Overwrite `.env.encrypted` with plaintext (no sops metadata) after
    /// encryption.
    corrupt_encrypted: bool,
    /// Delete `.sops.yaml` entirely before commit.
    delete_sops_yaml: bool,
}

impl Default for FixtureOpts {
    fn default() -> Self {
        Self {
            sops_yaml_override: None,
            gitignore_env: true,
            break_glass: true,
            track_plaintext_env: false,
            track_key_file: false,
            corrupt_encrypted: false,
            delete_sops_yaml: false,
        }
    }
}

/// Build a repo per `opts` and return its `TempDir`.
fn build_repo(opts: FixtureOpts) -> TempDir {
    let dir = TempDir::new().unwrap();
    let repo = dir.path();

    let public_key = generate_age_key(repo);

    git(repo, &["init", "-q"]);
    git(repo, &["config", "user.email", "ci@example.com"]);
    git(repo, &["config", "user.name", "CI"]);

    // `.sops.yaml` — a single rule covering `*.encrypted` so sops can encrypt.
    let sops_yaml =
        format!("creation_rules:\n  - path_regex: \\.encrypted$\n    age: {public_key}\n");
    std::fs::write(repo.join(".sops.yaml"), &sops_yaml).unwrap();

    // `.sopsy.yml` — sopsy's metadata, with recipients + optional break-glass.
    let mut sopsy = String::from("encrypted_globs:\n  - \"*.encrypted\"\nrecipients:\n");
    sopsy.push_str(&format!("  - name: dev\n    public_key: {public_key}\n"));
    if opts.break_glass {
        sopsy.push_str(&format!(
            "  - name: break-glass\n    public_key: {public_key}\n    break_glass: true\n"
        ));
    }
    std::fs::write(repo.join(".sopsy.yml"), sopsy).unwrap();

    // `.gitignore` — ignore `.env` (the real plaintext secret) by default.
    let gitignore = if opts.gitignore_env {
        ".env\n"
    } else {
        "target\n"
    };
    std::fs::write(repo.join(".gitignore"), gitignore).unwrap();

    // Real sops-encrypted dotenv artifact.
    let encrypted = repo.join(".env.encrypted");
    std::fs::write(&encrypted, "TOKEN=secret123\nAPI_KEY=abc\n").unwrap();
    sops_encrypt_dotenv(repo, ".env.encrypted");

    // `.env.example` — a safe, committed template (must not trip invariant 5).
    std::fs::write(repo.join(".env.example"), "TOKEN=\nAPI_KEY=\n").unwrap();

    // ---- Per-fixture mutations -------------------------------------------
    if let Some(override_yaml) = &opts.sops_yaml_override {
        let rendered = override_yaml.replace("{KEY}", &public_key);
        std::fs::write(repo.join(".sops.yaml"), rendered).unwrap();
    }
    if opts.corrupt_encrypted {
        std::fs::write(&encrypted, "TOKEN=plaintext\nAPI_KEY=plaintext\n").unwrap();
    }
    if opts.delete_sops_yaml {
        std::fs::remove_file(repo.join(".sops.yaml")).unwrap();
    }
    if opts.track_key_file {
        std::fs::write(repo.join("server.key"), "-----BEGIN PRIVATE KEY-----\n").unwrap();
    }

    // Track everything that isn't gitignored.
    git(repo, &["add", "-A"]);

    if opts.track_plaintext_env {
        std::fs::write(repo.join(".env"), "TOKEN=secret123\n").unwrap();
        git(repo, &["add", "-f", ".env"]);
    }

    git(repo, &["commit", "-qm", "fixture"]);
    dir
}

/// Run `sopsy check` inside `repo`, returning the assert.
fn run_check(repo: &Path) -> assert_cmd::assert::Assert {
    AssertCommand::cargo_bin("sopsy")
        .expect("binary `sopsy` should build")
        .arg("check")
        .current_dir(repo)
        .assert()
}

#[test]
#[serial]
fn healthy_repo_passes() {
    let dir = build_repo(FixtureOpts::default());
    run_check(dir.path())
        .success()
        .stdout(predicate::str::contains("all checks passed"));
}

#[test]
#[serial]
fn tracked_env_fails() {
    // (a) `.env` is force-tracked.
    let dir = build_repo(FixtureOpts {
        track_plaintext_env: true,
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(".env is tracked by git"));
}

#[test]
#[serial]
fn env_not_ignored_fails() {
    // (b) `.env` missing from `.gitignore`.
    let dir = build_repo(FixtureOpts {
        gitignore_env: false,
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(".env is not gitignored"));
}

#[test]
#[serial]
fn missing_sops_yaml_fails() {
    // (c) `.sops.yaml` deleted.
    let dir = build_repo(FixtureOpts {
        delete_sops_yaml: true,
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(".sops.yaml is missing"));
}

#[test]
#[serial]
fn corrupt_sops_yaml_fails() {
    // (c') `.sops.yaml` present but not valid YAML.
    let dir = build_repo(FixtureOpts {
        sops_yaml_override: Some("creation_rules: [this: is, not: valid".to_string()),
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(".sops.yaml is not valid YAML"));
}

#[test]
#[serial]
fn unmatched_encrypted_file_fails() {
    // (d) Encrypt normally, then narrow `.sops.yaml` so the rule no longer
    // matches `.env.encrypted` (the file stays valid sops on disk).
    let dir = build_repo(FixtureOpts {
        sops_yaml_override: Some(
            "creation_rules:\n  - path_regex: doesnotmatch\\.encrypted$\n    age: {KEY}\n"
                .to_string(),
        ),
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(
            "matches no .sops.yaml creation rule",
        ));
}

#[test]
#[serial]
fn tracked_key_file_fails() {
    // (e) A `*.key` file is tracked.
    let dir = build_repo(FixtureOpts {
        track_key_file: true,
        ..Default::default()
    });
    run_check(dir.path()).failure().stdout(
        predicate::str::contains("server.key")
            .and(predicate::str::contains("looks like a plaintext secret")),
    );
}

#[test]
#[serial]
fn malformed_encrypted_file_fails() {
    // (f) `.env.encrypted` overwritten with plaintext (no sops metadata).
    let dir = build_repo(FixtureOpts {
        corrupt_encrypted: true,
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains("missing sops metadata"));
}

#[test]
#[serial]
fn no_break_glass_recipient_fails() {
    // (g) `.sopsy.yml` has no break-glass recipient.
    let dir = build_repo(FixtureOpts {
        break_glass: false,
        ..Default::default()
    });
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains("no break-glass recipient"));
}

/// Build a repository whose `.sops.yaml`, `.sopsy.yml`, and `.gitignore` are
/// committed, then create a real sops-encrypted `.env.encrypted` that is left
/// **untracked** on disk (mirroring the state right after `sopsy init`, before
/// the user commits). The file is always encrypted with a matching rule first;
/// `match_rule`/`valid_meta` then mutate it to drive the failure cases.
fn build_untracked_encrypted(match_rule: bool, valid_meta: bool) -> TempDir {
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    let public_key = generate_age_key(repo);

    git(repo, &["init", "-q"]);
    git(repo, &["config", "user.email", "ci@example.com"]);
    git(repo, &["config", "user.name", "CI"]);

    std::fs::write(
        repo.join(".sops.yaml"),
        format!("creation_rules:\n  - path_regex: \\.encrypted$\n    age: {public_key}\n"),
    )
    .unwrap();
    std::fs::write(
        repo.join(".sopsy.yml"),
        format!(
            "encrypted_globs:\n  - \"*.encrypted\"\nrecipients:\n  \
             - name: dev\n    public_key: {public_key}\n  \
             - name: break-glass\n    public_key: {public_key}\n    break_glass: true\n"
        ),
    )
    .unwrap();
    std::fs::write(repo.join(".gitignore"), ".env\n").unwrap();

    // Commit only the configuration; the encrypted artifact stays untracked.
    git(repo, &["add", ".sops.yaml", ".sopsy.yml", ".gitignore"]);
    git(repo, &["commit", "-qm", "init"]);

    // Create + encrypt the real artifact (untracked on disk).
    let encrypted = repo.join(".env.encrypted");
    std::fs::write(&encrypted, "TOKEN=secret123\n").unwrap();
    sops_encrypt_dotenv(repo, ".env.encrypted");

    if !match_rule {
        // Narrow the rule so it no longer covers `.env.encrypted`.
        std::fs::write(
            repo.join(".sops.yaml"),
            format!(
                "creation_rules:\n  - path_regex: doesnotmatch\\.encrypted$\n    age: {public_key}\n"
            ),
        )
        .unwrap();
    }
    if !valid_meta {
        // Strip the sops metadata by overwriting with plaintext.
        std::fs::write(&encrypted, "TOKEN=plaintext\n").unwrap();
    }

    dir
}

#[test]
#[serial]
fn untracked_encrypted_file_is_validated() {
    // The real bug: an on-disk `.env.encrypted` that is not yet committed must
    // still be validated (not skipped as "no encrypted files to verify").
    let dir = build_untracked_encrypted(true, true);

    // Sanity: the artifact really is untracked.
    let tracked = Command::new("git")
        .arg("-C")
        .arg(dir.path())
        .args(["ls-files", ".env.encrypted"])
        .output()
        .unwrap();
    assert!(
        String::from_utf8_lossy(&tracked.stdout).trim().is_empty(),
        ".env.encrypted should be untracked for this fixture"
    );

    run_check(dir.path())
        .success()
        .stdout(
            predicate::str::contains("matches a .sops.yaml creation rule")
                .and(predicate::str::contains("contains valid sops metadata"))
                .and(predicate::str::contains("all checks passed")),
        )
        // Must NOT vacuously skip the (untracked) artifact.
        .stdout(predicate::str::contains("no encrypted files to verify").not());
}

#[test]
#[serial]
fn untracked_encrypted_unmatched_rule_fails() {
    // With the old (tracked-only) logic this passed vacuously; it must now fail.
    let dir = build_untracked_encrypted(false, true);
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(
            "matches no .sops.yaml creation rule",
        ));
}

#[test]
#[serial]
fn untracked_encrypted_missing_metadata_fails() {
    // Likewise, a broken (plaintext) untracked artifact must now be caught.
    let dir = build_untracked_encrypted(true, false);
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains("missing sops metadata"));
}

/// Build a minimal sopsy-managed repo containing **no** encrypted files at all.
fn build_repo_without_encrypted(sops_yaml: &str, sopsy_yml: &str) -> TempDir {
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    let public_key = generate_age_key(repo);

    git(repo, &["init", "-q"]);
    git(repo, &["config", "user.email", "ci@example.com"]);
    git(repo, &["config", "user.name", "CI"]);

    std::fs::write(
        repo.join(".sops.yaml"),
        sops_yaml.replace("{KEY}", &public_key),
    )
    .unwrap();
    std::fs::write(
        repo.join(".sopsy.yml"),
        sopsy_yml.replace("{KEY}", &public_key),
    )
    .unwrap();
    std::fs::write(repo.join(".gitignore"), ".env\n").unwrap();
    git(repo, &["add", "-A"]);
    git(repo, &["commit", "-qm", "init"]);
    dir
}

#[test]
#[serial]
fn no_encrypted_files_passes_vacuously() {
    // A managed repo with no artifacts: the two "no encrypted files" branches.
    let dir = build_repo_without_encrypted(
        "creation_rules:\n  - path_regex: \\.encrypted$\n    age: {KEY}\n",
        "encrypted_globs:\n  - \"*.encrypted\"\nrecipients:\n  - name: bg\n    public_key: {KEY}\n    break_glass: true\n",
    );
    run_check(dir.path()).success().stdout(
        predicate::str::contains("no encrypted files to verify against creation rules")
            .and(predicate::str::contains("no encrypted files to parse")),
    );
}

#[test]
#[serial]
fn sops_yaml_without_creation_rules_fails() {
    // `.sops.yaml` parses but defines no creation rules.
    let dir = build_repo_without_encrypted(
        "other_key: 1\n",
        "encrypted_globs:\n  - \"*.encrypted\"\nrecipients:\n  - name: bg\n    public_key: {KEY}\n    break_glass: true\n",
    );
    run_check(dir.path())
        .failure()
        .stdout(predicate::str::contains(".sops.yaml has no creation_rules"));
}

#[test]
#[serial]
fn invalid_sopsy_yml_surfaces_error() {
    // A malformed `.sopsy.yml` is a hard error (not a vacuous skip): exercises
    // the non-`FileNotFound` error arm of `Config::load_from_dir`.
    let dir = TempDir::new().unwrap();
    let repo = dir.path();
    git(repo, &["init", "-q"]);
    git(repo, &["config", "user.email", "ci@example.com"]);
    git(repo, &["config", "user.name", "CI"]);
    std::fs::write(repo.join(".sopsy.yml"), "recipients: [unterminated\n").unwrap();
    run_check(repo).failure();
}