forjar 1.4.0

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
use super::secrets::*;
use age::secrecy::ExposeSecret;
use base64::Engine;

fn test_keypair() -> (age::x25519::Identity, String) {
    let identity = generate_identity();
    let recipient = identity_to_recipient(&identity);
    (identity, recipient)
}

#[test]
fn test_fj200_encrypt_decrypt_roundtrip() {
    let (identity, recipient) = test_keypair();
    let plaintext = "s3cret-db-passw0rd!";

    let encrypted = encrypt(plaintext, &[&recipient]).unwrap();
    assert!(encrypted.starts_with(ENC_PREFIX));
    assert!(encrypted.ends_with(ENC_SUFFIX));

    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, plaintext);
}

#[test]
fn test_fj200_encrypt_empty_string() {
    let (identity, recipient) = test_keypair();
    let encrypted = encrypt("", &[&recipient]).unwrap();
    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, "");
}

#[test]
fn test_fj200_encrypt_unicode() {
    let (identity, recipient) = test_keypair();
    let plaintext = "日本語パスワード🔑";
    let encrypted = encrypt(plaintext, &[&recipient]).unwrap();
    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, plaintext);
}

#[test]
fn test_fj200_encrypt_multiline() {
    let (identity, recipient) = test_keypair();
    let plaintext = "line1\nline2\nline3";
    let encrypted = encrypt(plaintext, &[&recipient]).unwrap();
    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, plaintext);
}

#[test]
fn test_fj200_encrypt_no_recipients() {
    let err = encrypt("test", &[]).unwrap_err();
    assert!(err.contains("at least one recipient"));
}

#[test]
fn test_fj200_encrypt_invalid_recipient() {
    let err = encrypt("test", &["not-a-valid-key"]).unwrap_err();
    assert!(err.contains("invalid recipient"));
}

#[test]
fn test_fj200_decrypt_invalid_marker() {
    let (identity, _) = test_keypair();
    let err = decrypt_marker("not-a-marker", &[identity]).unwrap_err();
    assert!(err.contains("invalid ENC marker"));
}

#[test]
fn test_fj200_decrypt_invalid_base64() {
    let (identity, _) = test_keypair();
    let err = decrypt_marker("ENC[age,!!!invalid!!!]", &[identity]).unwrap_err();
    assert!(err.contains("base64 decode error"));
}

#[test]
fn test_fj200_decrypt_wrong_key() {
    let (_identity1, recipient1) = test_keypair();
    let (identity2, _recipient2) = test_keypair();

    let encrypted = encrypt("secret", &[&recipient1]).unwrap();
    let err = decrypt_marker(&encrypted, &[identity2]).unwrap_err();
    assert!(err.contains("decryption failed"));
}

#[test]
fn test_fj200_multi_recipient() {
    let (identity1, recipient1) = test_keypair();
    let (identity2, recipient2) = test_keypair();
    let plaintext = "shared-secret";

    let encrypted = encrypt(plaintext, &[&recipient1, &recipient2]).unwrap();

    // Both recipients can decrypt
    let d1 = decrypt_marker(&encrypted, &[identity1]).unwrap();
    assert_eq!(d1, plaintext);
    let d2 = decrypt_marker(&encrypted, &[identity2]).unwrap();
    assert_eq!(d2, plaintext);
}

#[test]
fn test_fj200_has_encrypted_markers() {
    let (_identity, recipient) = test_keypair();
    let real_marker = encrypt("test", &[&recipient]).unwrap();
    assert!(has_encrypted_markers(&format!("foo {real_marker} bar")));
    assert!(has_encrypted_markers(&real_marker));
    // Short/invalid base64 inside markers should NOT trigger
    assert!(!has_encrypted_markers("foo ENC[age,abc] bar"));
    assert!(!has_encrypted_markers("ENC[age,abc]"));
    assert!(!has_encrypted_markers("no markers here"));
    assert!(!has_encrypted_markers("ENC[other,abc]"));
    assert!(!has_encrypted_markers(""));
}

#[test]
fn test_fj200_find_markers() {
    let s = "a=ENC[age,x] b=ENC[age,y]";
    let markers = find_markers(s);
    assert_eq!(markers.len(), 2);
    assert_eq!(&s[markers[0].0..markers[0].1], "ENC[age,x]");
    assert_eq!(&s[markers[1].0..markers[1].1], "ENC[age,y]");
}

#[test]
fn test_fj200_find_markers_none() {
    assert!(find_markers("no markers here").is_empty());
}

#[test]
fn test_fj200_find_markers_unclosed() {
    // Unclosed marker — should not match
    let markers = find_markers("ENC[age,abc");
    assert!(markers.is_empty());
}

#[test]
fn test_fj200_decrypt_all() {
    let (identity, recipient) = test_keypair();
    let enc1 = encrypt("alpha", &[&recipient]).unwrap();
    let enc2 = encrypt("beta", &[&recipient]).unwrap();
    let input = format!("key1={enc1} key2={enc2}");

    let result = decrypt_all(&input, &[identity]).unwrap();
    assert_eq!(result, "key1=alpha key2=beta");
}

#[test]
fn test_fj200_decrypt_all_no_markers() {
    let (identity, _) = test_keypair();
    let input = "plain text with no markers";
    let result = decrypt_all(input, &[identity]).unwrap();
    assert_eq!(result, input);
}

#[test]
fn test_fj200_decrypt_all_mixed_content() {
    let (identity, recipient) = test_keypair();
    let enc = encrypt("secret-value", &[&recipient]).unwrap();
    let input = format!("host: localhost\npassword: {enc}\nport: 5432");

    let result = decrypt_all(&input, &[identity]).unwrap();
    assert_eq!(
        result,
        "host: localhost\npassword: secret-value\nport: 5432"
    );
}

#[test]
fn test_fj200_parse_identity_valid() {
    let identity = generate_identity();
    let key_str = identity.to_string();
    let parsed = parse_identity(key_str.expose_secret()).unwrap();
    assert_eq!(
        identity_to_recipient(&parsed),
        identity_to_recipient(&identity)
    );
}

#[test]
fn test_fj200_parse_identity_invalid() {
    let err = parse_identity("not-a-key").err().expect("expected error");
    assert!(err.contains("invalid age identity"));
}

#[test]
fn test_fj200_identity_to_recipient() {
    let identity = generate_identity();
    let recipient = identity_to_recipient(&identity);
    assert!(recipient.starts_with("age1"));
}

#[test]
fn test_fj200_load_identity_from_env_subprocess() {
    // Run a subprocess with FORJAR_AGE_KEY set to verify env-based loading
    let identity = generate_identity();
    let key_str = identity.to_string();

    let exe = std::env::current_exe().unwrap();
    let output = std::process::Command::new(exe)
        .env("FORJAR_AGE_KEY", key_str.expose_secret())
        .arg("--test-threads=1")
        .arg("--exact")
        .arg("core::secrets::tests::test_fj200_load_identity_env_inner")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "subprocess failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_fj200_load_identity_env_inner() {
    // Run via subprocess with FORJAR_AGE_KEY set
    if std::env::var("FORJAR_AGE_KEY").is_err() {
        return; // Skip when not run via subprocess
    }
    let loaded = load_identity_from_env().unwrap();
    let recipient = identity_to_recipient(&loaded);
    assert!(recipient.starts_with("age1"));
}

#[test]
fn test_fj200_load_identity_from_env_error_message() {
    // Verify error message format (don't actually set env vars)
    let result = load_identity_from_env();
    if result.is_ok() {
        return; // FORJAR_AGE_KEY happens to be set in this environment
    }
    let err = result.err().expect("expected error");
    assert!(err.contains("FORJAR_AGE_KEY not set"));
}

#[test]
fn test_fj200_load_identity_file() {
    let identity = generate_identity();
    let key_str = identity.to_string();
    let recipient_expected = identity_to_recipient(&identity);

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("identity.txt");
    std::fs::write(
        &path,
        format!(
            "# created: 2024-01-01\n# public key: {}\n{}\n",
            recipient_expected,
            key_str.expose_secret()
        ),
    )
    .unwrap();

    let loaded = load_identity_file(&path).unwrap();
    assert_eq!(identity_to_recipient(&loaded), recipient_expected);
}

#[test]
fn test_fj200_load_identity_file_not_found() {
    let err = load_identity_file(std::path::Path::new("/nonexistent/key.txt"))
        .err()
        .expect("expected error");
    assert!(err.contains("cannot read identity file"));
}

#[test]
fn test_fj200_load_identity_file_no_key() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("empty.txt");
    std::fs::write(&path, "# just a comment\n").unwrap();

    let err = load_identity_file(&path).err().expect("expected error");
    assert!(err.contains("no AGE-SECRET-KEY found"));
}

#[test]
fn test_fj200_deterministic_marker_format() {
    let (_identity, recipient) = test_keypair();
    let encrypted = encrypt("test", &[&recipient]).unwrap();

    // Verify format
    assert!(encrypted.starts_with("ENC[age,"));
    assert!(encrypted.ends_with(']'));

    // Extract base64 portion and verify it decodes
    let b64 = encrypted
        .strip_prefix(ENC_PREFIX)
        .unwrap()
        .strip_suffix(ENC_SUFFIX)
        .unwrap();
    assert!(B64.decode(b64).is_ok());
}

#[test]
fn test_fj200_large_secret() {
    let (identity, recipient) = test_keypair();
    let plaintext = "x".repeat(100_000);
    let encrypted = encrypt(&plaintext, &[&recipient]).unwrap();
    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, plaintext);
}

#[test]
fn test_fj200_special_chars() {
    let (identity, recipient) = test_keypair();
    let plaintext = r#"p@$$w0rd!@#$%^&*(){}[]|\"':;<>,.?/~`"#;
    let encrypted = encrypt(plaintext, &[&recipient]).unwrap();
    let decrypted = decrypt_marker(&encrypted, &[identity]).unwrap();
    assert_eq!(decrypted, plaintext);
}

// ─── PMAT-037: has_encrypted_markers false positives ───────────

#[test]
fn test_pmat037_comment_mentioning_enc_not_a_marker() {
    // A YAML comment like "# In production, values use ENC[age,...] markers"
    // should NOT be detected as having encrypted markers — the "..." is not
    // valid base64 ciphertext.
    let content = "# In production, values use ENC[age,...] markers\nAPP_NAME=myapp\n";
    assert!(
        !has_encrypted_markers(content),
        "comment mentioning ENC[age,...] must not trigger decryption"
    );
}

#[test]
fn test_pmat037_real_marker_still_detected() {
    let (_identity, recipient) = test_keypair();
    let encrypted = encrypt("secret", &[&recipient]).unwrap();
    let content = format!("API_KEY={encrypted}\n");
    assert!(
        has_encrypted_markers(&content),
        "real encrypted marker must still be detected"
    );
}

// ─── FJ-201 tests ───────────────────────────────────────────────

#[test]
fn test_fj201_decrypt_all_counted() {
    let (identity, recipient) = test_keypair();
    let enc1 = encrypt("alpha", &[&recipient]).unwrap();
    let enc2 = encrypt("beta", &[&recipient]).unwrap();
    let input = format!("key1={enc1} key2={enc2}");

    let (result, count) = decrypt_all_counted(&input, &[identity]).unwrap();
    assert_eq!(result, "key1=alpha key2=beta");
    assert_eq!(count, 2);
}

#[test]
fn test_fj201_decrypt_all_counted_zero() {
    let (identity, _) = test_keypair();
    let (result, count) = decrypt_all_counted("no markers", &[identity]).unwrap();
    assert_eq!(result, "no markers");
    assert_eq!(count, 0);
}

#[test]
fn test_fj201_rotate_roundtrip() {
    // Encrypt with key1, then re-encrypt with key2
    let (identity1, recipient1) = test_keypair();
    let (identity2, recipient2) = test_keypair();

    let encrypted = encrypt("rotation-secret", &[&recipient1]).unwrap();

    // Decrypt with key1, re-encrypt with key2
    let plaintext = decrypt_marker(&encrypted, &[identity1]).unwrap();
    assert_eq!(plaintext, "rotation-secret");

    let re_encrypted = encrypt(&plaintext, &[&recipient2]).unwrap();
    let decrypted = decrypt_marker(&re_encrypted, &[identity2]).unwrap();
    assert_eq!(decrypted, "rotation-secret");

    // Original key can't decrypt new ciphertext
    let err = decrypt_marker(&re_encrypted, &[generate_identity()]);
    assert!(err.is_err());
}

#[test]
fn test_fj201_rotate_multi_recipient() {
    let (identity1, recipient1) = test_keypair();
    let (identity2, recipient2) = test_keypair();
    let (identity3, recipient3) = test_keypair();

    // Initially encrypted for identity1
    let encrypted = encrypt("team-secret", &[&recipient1]).unwrap();
    let plain = decrypt_marker(&encrypted, &[identity1]).unwrap();

    // Rotate to identity2 + identity3 (identity1 loses access)
    let rotated = encrypt(&plain, &[&recipient2, &recipient3]).unwrap();
    assert_eq!(
        decrypt_marker(&rotated, &[identity2]).unwrap(),
        "team-secret"
    );
    assert_eq!(
        decrypt_marker(&rotated, &[identity3]).unwrap(),
        "team-secret"
    );
    // identity1 can no longer decrypt
    assert!(decrypt_marker(&rotated, &[generate_identity()]).is_err());
}

#[test]
fn test_fj201_secret_accessed_event_serializes() {
    let event = crate::core::types::ProvenanceEvent::SecretAccessed {
        resource: "db-config".to_string(),
        marker_count: 2,
        identity_recipient: "age1abc...".to_string(),
    };
    let json = serde_json::to_string(&event).unwrap();
    assert!(json.contains("secret_accessed"));
    assert!(json.contains("db-config"));
    assert!(json.contains("marker_count"));
}

#[test]
fn test_fj201_secret_rotated_event_serializes() {
    let event = crate::core::types::ProvenanceEvent::SecretRotated {
        file: "forjar.yaml".to_string(),
        marker_count: 3,
        new_recipients: vec!["age1abc...".to_string(), "age1xyz...".to_string()],
    };
    let json = serde_json::to_string(&event).unwrap();
    assert!(json.contains("secret_rotated"));
    assert!(json.contains("forjar.yaml"));
    assert!(json.contains("age1xyz..."));
}

#[test]
fn test_fj201_secret_events_roundtrip_json() {
    let event = crate::core::types::ProvenanceEvent::SecretAccessed {
        resource: "api-config".to_string(),
        marker_count: 1,
        identity_recipient: "age1test".to_string(),
    };
    let json = serde_json::to_string(&event).unwrap();
    let back: crate::core::types::ProvenanceEvent = serde_json::from_str(&json).unwrap();
    let json2 = serde_json::to_string(&back).unwrap();
    assert_eq!(json, json2);
}