vti-secrets 0.19.0

Pluggable secret-store backends + integration onboarding flow for Verifiable Trust Infrastructure
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
use std::path::Path;

use super::super::SeedStore;
use super::*;
use crate::config::{EncryptedFileConfig, KekSource, SecretBackend, SecretsConfig};

fn seed() -> Vec<u8> {
    (0u8..64).collect()
}

fn passphrase_file_store(dir: &Path, passphrase: &[u8]) -> EncryptedFileSeedStore {
    let pass = dir.join("passphrase");
    std::fs::write(&pass, passphrase).unwrap();
    let cfg = EncryptedFileConfig::new(KekSource::Passphrase {
        file: Some(pass),
        credential: None,
    });
    EncryptedFileSeedStore::from_config(&cfg, dir, "seed.enc").unwrap()
}

/// A systemd-creds store reading from an explicit credentials directory, so
/// the test never touches the process-wide `CREDENTIALS_DIRECTORY`.
fn creds_store(dir: &Path, creds: &Path, kek: &[u8]) -> EncryptedFileSeedStore {
    std::fs::create_dir_all(creds).unwrap();
    std::fs::write(creds.join("vta-kek"), kek).unwrap();
    let cfg = EncryptedFileConfig::new(KekSource::SystemdCreds {
        credential: "vta-kek".into(),
    });
    EncryptedFileSeedStore::from_config(&cfg, dir, "seed.enc")
        .unwrap()
        .with_credentials_directory(creds)
}

fn expect_secret_store_err(r: Result<Option<Vec<u8>>, AppError>, needle: &str) {
    match r {
        Err(AppError::SecretStore(m)) => assert!(m.contains(needle), "want {needle:?}: {m}"),
        Err(other) => panic!("want a SecretStore error ({needle}), got {other:?}"),
        Ok(v) => panic!(
            "want an error ({needle}), got Ok({})",
            if v.is_some() { "Some" } else { "None" }
        ),
    }
}

#[tokio::test]
async fn passphrase_file_round_trip() {
    let dir = tempfile::tempdir().unwrap();
    let store = passphrase_file_store(dir.path(), b"correct horse battery staple\n");
    assert!(store.get().await.unwrap().is_none(), "no file yet");
    store.set(&seed()).await.unwrap();
    assert_eq!(store.get().await.unwrap().unwrap(), seed());

    // The file holds no plaintext.
    let raw = std::fs::read(store.path()).unwrap();
    assert!(!raw.windows(16).any(|w| w == &seed()[..16]));
    assert_eq!(&raw[..8], b"VTISECF\0");
}

#[tokio::test]
async fn passphrase_credential_round_trip() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    std::fs::create_dir_all(&creds).unwrap();
    std::fs::write(creds.join("vta-pass"), b"from a systemd credential").unwrap();
    let cfg = EncryptedFileConfig::new(KekSource::Passphrase {
        file: None,
        credential: Some("vta-pass".into()),
    });
    let store = EncryptedFileSeedStore::from_config(&cfg, dir.path(), "seed.enc")
        .unwrap()
        .with_credentials_directory(&creds);
    store.set(&seed()).await.unwrap();
    assert_eq!(store.get().await.unwrap().unwrap(), seed());
}

#[tokio::test]
async fn systemd_creds_round_trip_raw_and_hex() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let raw_kek = [0x5au8; 32];

    let store = creds_store(dir.path(), &creds, &raw_kek);
    store.set(&seed()).await.unwrap();
    assert_eq!(store.get().await.unwrap().unwrap(), seed());

    // The same KEK written as hex (with a newline) opens the same file.
    let hex_kek = format!("{}\n", hex::encode(raw_kek));
    let store = creds_store(dir.path(), &creds, hex_kek.as_bytes());
    assert_eq!(store.get().await.unwrap().unwrap(), seed());

    // A different KEK does not.
    let store = creds_store(dir.path(), &creds, &[0x5bu8; 32]);
    expect_secret_store_err(store.get().await, "cannot decrypt");

    // A credential of the wrong shape is refused before any decryption.
    let store = creds_store(dir.path(), &creds, b"too short");
    expect_secret_store_err(store.get().await, "32-byte key");
}

#[tokio::test]
async fn a_missing_credential_is_an_error_not_no_secret() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let store = creds_store(dir.path(), &creds, &[1u8; 32]);
    store.set(&seed()).await.unwrap();
    std::fs::remove_file(creds.join("vta-kek")).unwrap();
    expect_secret_store_err(store.get().await, "vta-kek");
}

#[tokio::test]
async fn wrong_passphrase_fails_closed() {
    let dir = tempfile::tempdir().unwrap();
    passphrase_file_store(dir.path(), b"right")
        .set(&seed())
        .await
        .unwrap();
    let store = passphrase_file_store(dir.path(), b"wrong");
    expect_secret_store_err(store.get().await, "cannot decrypt");
}

#[tokio::test]
async fn an_empty_passphrase_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let store = passphrase_file_store(dir.path(), b"\n");
    match store.set(&seed()).await {
        Err(AppError::SecretStore(m)) => assert!(m.contains("empty"), "{m}"),
        other => panic!("want an empty-passphrase error, got {other:?}"),
    }
}

#[tokio::test]
async fn tampering_with_ciphertext_or_header_fails_closed() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let store = creds_store(dir.path(), &creds, &[9u8; 32]);
    store.set(&seed()).await.unwrap();
    let good = std::fs::read(store.path()).unwrap();

    // Last ciphertext byte (inside the tag), a middle ciphertext byte, a
    // salt byte and a nonce byte: each parses, none decrypts.
    for i in [good.len() - 1, good.len() - 20, 12, 44] {
        let mut bad = good.clone();
        bad[i] ^= 0x01;
        std::fs::write(store.path(), &bad).unwrap();
        expect_secret_store_err(store.get().await, "cannot decrypt");
    }

    // Header fields that fail to parse are errors too, never "no secret".
    for (i, needle) in [(0, "magic"), (9, "AEAD"), (11, "reserved")] {
        let mut bad = good.clone();
        bad[i] ^= 0x01;
        std::fs::write(store.path(), &bad).unwrap();
        expect_secret_store_err(store.get().await, needle);
    }

    // Truncated and empty files.
    std::fs::write(store.path(), &good[..40]).unwrap();
    expect_secret_store_err(store.get().await, "malformed");
    std::fs::write(store.path(), b"").unwrap();
    expect_secret_store_err(store.get().await, "malformed");
}

#[tokio::test]
async fn tampering_with_the_kdf_parameters_fails_closed() {
    let dir = tempfile::tempdir().unwrap();
    let store = passphrase_file_store(dir.path(), b"pass");
    store.set(&seed()).await.unwrap();
    let good = std::fs::read(store.path()).unwrap();

    // t_cost 3 -> 4: within range, so it parses, but the header is the AAD.
    let mut bad = good.clone();
    assert_eq!(&bad[62..66], &3u32.to_be_bytes());
    bad[65] = 4;
    std::fs::write(store.path(), &bad).unwrap();
    expect_secret_store_err(store.get().await, "cannot decrypt");

    // t_cost 3 -> 2: below the floor, refused before any derivation.
    let mut weak = good;
    weak[65] = 2;
    std::fs::write(store.path(), &weak).unwrap();
    expect_secret_store_err(store.get().await, "outside the accepted range");
}

#[tokio::test]
async fn an_unknown_version_is_an_error() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let store = creds_store(dir.path(), &creds, &[2u8; 32]);
    store.set(&seed()).await.unwrap();
    let mut bytes = std::fs::read(store.path()).unwrap();
    bytes[8] = 2;
    std::fs::write(store.path(), &bytes).unwrap();
    expect_secret_store_err(store.get().await, "unsupported format version 2");
}

#[tokio::test]
async fn a_file_from_another_kek_source_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    creds_store(dir.path(), &creds, &[2u8; 32])
        .set(&seed())
        .await
        .unwrap();
    let store = passphrase_file_store(dir.path(), b"pass");
    expect_secret_store_err(store.get().await, "'systemd_creds'");
}

#[cfg(unix)]
#[tokio::test]
async fn the_file_is_owner_only_and_written_atomically() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let store = creds_store(dir.path(), &creds, &[4u8; 32]);
    store.set(&seed()).await.unwrap();
    let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode(store.path()), 0o600);

    // Overwriting a loosened file replaces it with a new 0600 one.
    std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o644)).unwrap();
    store.set(&[1, 2, 3]).await.unwrap();
    assert_eq!(mode(store.path()), 0o600);
    assert_eq!(store.get().await.unwrap().unwrap(), vec![1, 2, 3]);

    // No temporary file is left behind.
    let leftovers: Vec<_> = std::fs::read_dir(dir.path())
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_name().to_string_lossy().contains(".tmp-"))
        .collect();
    assert!(leftovers.is_empty(), "{leftovers:?}");
}

#[cfg(unix)]
#[tokio::test]
async fn a_missing_parent_directory_is_created_owner_only() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    std::fs::create_dir_all(&creds).unwrap();
    std::fs::write(creds.join("k"), [6u8; 32]).unwrap();
    let mut cfg = EncryptedFileConfig::new(KekSource::SystemdCreds {
        credential: "k".into(),
    });
    let nested = dir.path().join("state/sub");
    cfg.path = Some(nested.join("seed.enc"));
    let store = EncryptedFileSeedStore::from_config(&cfg, dir.path(), "unused")
        .unwrap()
        .with_credentials_directory(&creds);
    store.set(&seed()).await.unwrap();
    let mode = std::fs::metadata(&nested).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode, 0o700);
    assert_eq!(store.get().await.unwrap().unwrap(), seed());
}

#[tokio::test]
async fn delete_removes_the_file() {
    let dir = tempfile::tempdir().unwrap();
    let creds = dir.path().join("creds");
    let store = creds_store(dir.path(), &creds, &[4u8; 32]);
    store.set(&seed()).await.unwrap();
    store.delete().await.unwrap();
    assert!(store.get().await.unwrap().is_none());
    store.delete().await.unwrap();
}

fn config_err(cfg: &EncryptedFileConfig) -> String {
    match EncryptedFileSeedStore::from_config(cfg, Path::new("/tmp"), "seed.enc") {
        Err(AppError::Config(m)) => m,
        Err(other) => panic!("want a Config error, got {other:?}"),
        Ok(_) => panic!("want a Config error, got a store"),
    }
}

#[test]
fn config_validation() {
    let both = EncryptedFileConfig::new(KekSource::Passphrase {
        file: Some("/p".into()),
        credential: Some("c".into()),
    });
    assert!(config_err(&both).contains("exactly one"));
    let neither = EncryptedFileConfig::new(KekSource::Passphrase {
        file: None,
        credential: None,
    });
    assert!(config_err(&neither).contains("exactly one"));
    for bad in ["", ".", "..", "a/b"] {
        let cfg = EncryptedFileConfig::new(KekSource::SystemdCreds {
            credential: bad.into(),
        });
        assert!(config_err(&cfg).contains("credential name"), "{bad:?}");
    }
    let tpm = EncryptedFileConfig::new(KekSource::Tpm {
        pcrs: vec![7, 24],
        tcti: None,
    });
    let msg = config_err(&tpm);
    if cfg!(all(feature = "tpm", target_os = "linux")) {
        assert!(msg.contains("out of range"), "{msg}");
    } else {
        assert!(msg.contains("`tpm` feature"), "{msg}");
    }
}

#[test]
fn the_default_path_is_under_the_data_dir() {
    let cfg = EncryptedFileConfig::new(KekSource::SystemdCreds {
        credential: "k".into(),
    });
    let store = EncryptedFileSeedStore::from_config(&cfg, Path::new("/data"), "seed.enc").unwrap();
    assert_eq!(store.path(), Path::new("/data/seed.enc"));
}

#[test]
fn debug_names_the_source_only() {
    let dir = tempfile::tempdir().unwrap();
    let store = passphrase_file_store(dir.path(), b"hunter2");
    let dbg = format!("{store:?}");
    assert!(
        dbg.contains("passphrase") && !dbg.contains("hunter2"),
        "{dbg}"
    );
}

#[test]
fn toml_forms_parse_and_unknown_fields_are_refused() {
    let parse = |s: &str| toml::from_str::<SecretsConfig>(s);
    let c = parse(
        r#"
backend = "encrypted_file"
[encrypted_file]
path = "/var/lib/vta/seed.enc"
kek = { source = "passphrase", file = "/etc/vta/pass" }
"#,
    )
    .unwrap();
    assert_eq!(c.backend, Some(SecretBackend::EncryptedFile));
    let ef = c.encrypted_file.unwrap();
    assert_eq!(ef.path.as_deref(), Some(Path::new("/var/lib/vta/seed.enc")));
    assert_eq!(
        ef.kek,
        KekSource::Passphrase {
            file: Some("/etc/vta/pass".into()),
            credential: None
        }
    );

    let c = parse(
        "backend = \"encrypted_file\"\n[encrypted_file.kek]\nsource = \"systemd_creds\"\ncredential = \"vta-kek\"\n",
    )
    .unwrap();
    assert_eq!(
        c.encrypted_file.unwrap().kek,
        KekSource::SystemdCreds {
            credential: "vta-kek".into()
        }
    );

    let c = parse("[encrypted_file]\nkek = { source = \"tpm\", pcrs = [0, 7] }\n").unwrap();
    assert_eq!(
        c.encrypted_file.unwrap().kek,
        KekSource::Tpm {
            pcrs: vec![0, 7],
            tcti: None
        }
    );

    // A passphrase is never accepted inline, and typos are loud.
    assert!(
        parse("[encrypted_file]\nkek = { source = \"passphrase\", passphrase = \"x\" }\n").is_err()
    );
    assert!(
        parse("[encrypted_file]\nkek = { source = \"systemd_creds\", credentail = \"x\" }\n")
            .is_err()
    );
    assert!(parse("[encrypted_file]\npaht = \"/x\"\nkek = { source = \"tpm\" }\n").is_err());
    assert!(parse("[encrypted_file]\nkek = { source = \"kms\" }\n").is_err());

    // Round trip: what serializes, parses back to the same thing.
    let c = secrets_with(
        Some(SecretBackend::EncryptedFile),
        Some(EncryptedFileConfig::new(KekSource::SystemdCreds {
            credential: "vta-kek".into(),
        })),
    );
    let text = toml::to_string(&c).unwrap();
    assert!(text.contains("encrypted_file"), "{text}");
    let back = parse(&text).unwrap();
    assert_eq!(back.encrypted_file, c.encrypted_file);
}

// ── factory selection ──────────────────────────────────────────────────

fn secrets_with(
    backend: Option<SecretBackend>,
    encrypted_file: Option<EncryptedFileConfig>,
) -> SecretsConfig {
    SecretsConfig {
        backend,
        encrypted_file,
        ..Default::default()
    }
}

fn factory_config_err(secrets: &SecretsConfig) -> String {
    match crate::create_seed_store(secrets, Path::new("/tmp")) {
        Err(AppError::Config(m)) => m,
        Err(other) => panic!("want a Config error, got {other:?}"),
        Ok(_) => panic!("want a Config error, got a store"),
    }
}

#[test]
fn the_factory_needs_the_table_for_an_explicit_selection() {
    let secrets = secrets_with(Some(SecretBackend::EncryptedFile), None);
    assert!(factory_config_err(&secrets).contains("[secrets.encrypted_file]"));
}

#[test]
fn implicit_resolution_never_picks_encrypted_file() {
    let mut secrets = secrets_with(
        None,
        Some(EncryptedFileConfig::new(KekSource::SystemdCreds {
            credential: "k".into(),
        })),
    );
    assert!(factory_config_err(&secrets).contains("only ever selected explicitly"));

    // With another backend named, the table is just unused.
    secrets.backend = Some(SecretBackend::Plaintext);
    secrets.allow_plaintext = true;
    assert!(crate::create_seed_store(&secrets, Path::new("/tmp")).is_ok());
}

#[tokio::test]
async fn the_factory_builds_a_working_store() {
    let dir = tempfile::tempdir().unwrap();
    let pass = dir.path().join("pass");
    std::fs::write(&pass, b"factory pass").unwrap();
    let secrets = secrets_with(
        Some(SecretBackend::EncryptedFile),
        Some(EncryptedFileConfig::new(KekSource::Passphrase {
            file: Some(pass),
            credential: None,
        })),
    );
    let store = crate::create_seed_store(&secrets, dir.path()).unwrap();
    store.set(&seed()).await.unwrap();
    assert_eq!(store.get().await.unwrap().unwrap(), seed());
    assert!(
        dir.path()
            .join(crate::seed_store::VTA_ENCRYPTED_FILENAME)
            .is_file()
    );
}