Files
fermata/tests/core_secrets_parser.rs
T
g4borg 246f714a51 feat(fermata): the default secret list gets its formats; key files mask a private body or one long token
tfstate and its backup parse as JSON and stay stopped on Read. tfvars, credentials and
sh, bash, zsh, ini, conf, cfg parse as Env. Key files are OpaqueText: only a private PEM
block's body or a single token of 16 or more characters enters the manifest, past the
key filter, and such a file never fails, so a binary .key refuses nothing under deny.
A certificate-only PEM passes Read with no entries. A UTF-8 BOM is stripped in
parse_content.
2026-11-07 01:14:07 +01:00

526 lines
20 KiB
Rust

//! Integration tests for the multi-format secret file parser.
use dirigent_fermata::core::secrets::parser::{
parse_content, parse_secret_file, FileFormat, ParseError, SecretEntry,
};
use std::path::Path;
use tempfile::NamedTempFile;
fn p(s: &str) -> &Path {
Path::new(s)
}
// ---------------------------------------------------------------------------
// .env parsing
// ---------------------------------------------------------------------------
#[test]
fn env_basic_key_value() {
let entries = parse_content("DATABASE_URL=postgres://localhost/db", FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "DATABASE_URL");
assert_eq!(entries[0].value, "postgres://localhost/db");
}
#[test]
fn env_double_quoted() {
let entries = parse_content(r#"SECRET="hello world""#, FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries[0].value, "hello world");
}
#[test]
fn env_single_quoted() {
let entries = parse_content("SECRET='hello world'", FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries[0].value, "hello world");
}
#[test]
fn env_comments_and_empty_lines() {
let content = "# comment\n\nKEY=value\n # indented comment\n";
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "KEY");
}
#[test]
fn env_export_prefix() {
let content = "export API_KEY=abc123\nexport TOKEN=\"xyz\"";
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].key, "API_KEY");
assert_eq!(entries[0].value, "abc123");
assert_eq!(entries[1].key, "TOKEN");
assert_eq!(entries[1].value, "xyz");
}
#[test]
fn env_whitespace_handling() {
let content = " KEY = value \nKEY2= spaced ";
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
// Key is trimmed; unquoted value trimmed.
assert_eq!(entries[0].key, "KEY");
assert_eq!(entries[0].value, "value");
assert_eq!(entries[1].key, "KEY2");
assert_eq!(entries[1].value, "spaced");
}
#[test]
fn env_escape_sequences_in_double_quotes() {
let content = r#"MSG="line1\nline2""#;
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries[0].value, "line1\nline2");
}
// ---------------------------------------------------------------------------
// TOML parsing
// ---------------------------------------------------------------------------
#[test]
fn toml_flat_table() {
let content = r#"
API_KEY = "abc"
DB_PASS = "secret"
"#;
let entries = parse_content(content, FileFormat::Toml, p("Secrets.toml")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "API_KEY" && e.value == "abc"));
assert!(entries.iter().any(|e| e.key == "DB_PASS" && e.value == "secret"));
}
#[test]
fn toml_nested_tables() {
let content = r#"
[database]
password = "secret"
host = "localhost"
port = 5432
"#;
let entries = parse_content(content, FileFormat::Toml, p("config.toml")).unwrap();
// Only string values extracted; port (integer) skipped.
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "database.password" && e.value == "secret"));
assert!(entries.iter().any(|e| e.key == "database.host" && e.value == "localhost"));
}
#[test]
fn toml_mixed_types_only_strings() {
let content = r#"
name = "app"
debug = true
count = 42
ratio = 3.14
"#;
let entries = parse_content(content, FileFormat::Toml, p("app.toml")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "name");
}
// ---------------------------------------------------------------------------
// JSON parsing
// ---------------------------------------------------------------------------
#[test]
fn json_flat_object() {
let content = r#"{"api_key": "abc", "secret": "xyz"}"#;
let entries = parse_content(content, FileFormat::Json, p("secrets.json")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "api_key" && e.value == "abc"));
}
#[test]
fn json_nested_objects() {
let content = r#"{"db": {"password": "foo", "port": 5432}}"#;
let entries = parse_content(content, FileFormat::Json, p("secrets.json")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "db.password");
assert_eq!(entries[0].value, "foo");
}
#[test]
fn json_arrays() {
let content = r#"{"keys": ["a", "b"]}"#;
let entries = parse_content(content, FileFormat::Json, p("secrets.json")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "keys.0" && e.value == "a"));
assert!(entries.iter().any(|e| e.key == "keys.1" && e.value == "b"));
}
#[test]
fn json_mixed_types() {
let content = r#"{"name": "app", "count": 42, "active": true, "data": null}"#;
let entries = parse_content(content, FileFormat::Json, p("a.json")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "name");
}
// ---------------------------------------------------------------------------
// YAML parsing
// ---------------------------------------------------------------------------
#[test]
fn yaml_flat_map() {
let content = "api_key: abc\nsecret: xyz\n";
let entries = parse_content(content, FileFormat::Yaml, p("secrets.yaml")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "api_key" && e.value == "abc"));
}
#[test]
fn yaml_nested_maps() {
let content = "db:\n password: foo\n port: 5432\n";
let entries = parse_content(content, FileFormat::Yaml, p("secrets.yml")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "db.password");
assert_eq!(entries[0].value, "foo");
}
#[test]
fn yaml_mixed_types() {
let content = "name: app\ncount: 42\nactive: true\n";
let entries = parse_content(content, FileFormat::Yaml, p("a.yaml")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "name");
}
// ---------------------------------------------------------------------------
// Python assignment parsing
// ---------------------------------------------------------------------------
#[test]
fn python_matches_assignments() {
let content = r#"
API_KEY = "abc123"
DB_PASS = 'secret'
import os
x = 42
"#;
let entries = parse_content(content, FileFormat::PythonAssignments, p("settings.py")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "API_KEY" && e.value == "abc123"));
assert!(entries.iter().any(|e| e.key == "DB_PASS" && e.value == "secret"));
}
#[test]
fn python_skips_non_matching() {
let content = "result = some_function()\nfor x in range(10):\n pass\n";
let entries = parse_content(content, FileFormat::PythonAssignments, p("a.py")).unwrap();
assert!(entries.is_empty());
}
// ---------------------------------------------------------------------------
// Properties parsing
// ---------------------------------------------------------------------------
#[test]
fn properties_equals_separator() {
let content = "db.password=secret\ndb.host=localhost";
let entries = parse_content(content, FileFormat::Properties, p("app.properties")).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "db.password" && e.value == "secret"));
}
#[test]
fn properties_colon_separator() {
let content = "db.password: secret";
let entries = parse_content(content, FileFormat::Properties, p("app.properties")).unwrap();
assert_eq!(entries[0].key, "db.password");
assert_eq!(entries[0].value, "secret");
}
#[test]
fn properties_comments() {
let content = "# comment\n! also comment\nkey=value";
let entries = parse_content(content, FileFormat::Properties, p("app.properties")).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "key");
}
#[test]
fn properties_continuation_lines() {
let content = "long.value=hello \\\n world";
let entries = parse_content(content, FileFormat::Properties, p("app.properties")).unwrap();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].key, "long.value");
assert_eq!(entries[0].value, "hello world");
// The pieces as the file holds them.
assert_eq!(entries[1].value, "hello\nworld");
}
// ---------------------------------------------------------------------------
// Auto-detection from file extension
// ---------------------------------------------------------------------------
#[test]
fn format_from_path_env_variants() {
assert_eq!(FileFormat::from_path(p(".env")), Some(FileFormat::Env));
assert_eq!(FileFormat::from_path(p(".env.local")), Some(FileFormat::Env));
assert_eq!(FileFormat::from_path(p(".env.production")), Some(FileFormat::Env));
assert_eq!(FileFormat::from_path(p("staging.env")), Some(FileFormat::Env));
}
#[test]
fn format_from_path_extensions() {
assert_eq!(FileFormat::from_path(p("a.toml")), Some(FileFormat::Toml));
assert_eq!(FileFormat::from_path(p("a.json")), Some(FileFormat::Json));
assert_eq!(FileFormat::from_path(p("a.yaml")), Some(FileFormat::Yaml));
assert_eq!(FileFormat::from_path(p("a.yml")), Some(FileFormat::Yaml));
assert_eq!(FileFormat::from_path(p("a.py")), Some(FileFormat::PythonAssignments));
assert_eq!(FileFormat::from_path(p("a.properties")), Some(FileFormat::Properties));
}
#[test]
fn format_from_path_unknown() {
for name in ["a.xyz", "a.p12", "a.pfx", ".netrc", ".htpasswd", "secrets.txt", "secrets.toml~", "tfstate", "id_rsa.pub"] {
assert_eq!(FileFormat::from_path(p(name)), None, "{name}");
}
}
#[test]
fn format_from_path_key_files_are_opaque() {
for name in ["a.key", "a.pem", "id_rsa", "id_ed25519", "id_ecdsa", "config/master.key"] {
assert_eq!(FileFormat::from_path(p(name)), Some(FileFormat::OpaqueText), "{name}");
}
}
#[test]
fn format_from_path_state_and_settings_files() {
for name in ["terraform.tfstate", "a.tfstate", "a.tfstate.backup"] {
assert_eq!(FileFormat::from_path(p(name)), Some(FileFormat::Json), "{name}");
}
for name in ["terraform.tfvars", "a.auto.tfvars", ".aws/credentials", "credentials", "secrets.sh", "secrets.bash", "secrets.zsh", "secrets.ini", "secrets.conf", "secrets.cfg"] {
assert_eq!(FileFormat::from_path(p(name)), Some(FileFormat::Env), "{name}");
}
}
// ---------------------------------------------------------------------------
// Format hints
// ---------------------------------------------------------------------------
#[test]
fn format_from_hint() {
assert_eq!(FileFormat::from_hint("env"), Some(FileFormat::Env));
assert_eq!(FileFormat::from_hint("dotenv"), Some(FileFormat::Env));
assert_eq!(FileFormat::from_hint("toml"), Some(FileFormat::Toml));
assert_eq!(FileFormat::from_hint("json"), Some(FileFormat::Json));
assert_eq!(FileFormat::from_hint("yaml"), Some(FileFormat::Yaml));
assert_eq!(FileFormat::from_hint("yml"), Some(FileFormat::Yaml));
assert_eq!(FileFormat::from_hint("python-assignments"), Some(FileFormat::PythonAssignments));
assert_eq!(FileFormat::from_hint("python"), Some(FileFormat::PythonAssignments));
assert_eq!(FileFormat::from_hint("properties"), Some(FileFormat::Properties));
assert_eq!(FileFormat::from_hint("java-properties"), Some(FileFormat::Properties));
for hint in ["tfvars", "credentials", "sh", "bash", "zsh", "ini", "conf", "cfg"] {
assert_eq!(FileFormat::from_hint(hint), Some(FileFormat::Env), "{hint}");
}
assert_eq!(FileFormat::from_hint("tfstate"), Some(FileFormat::Json));
assert_eq!(FileFormat::from_hint("opaque"), Some(FileFormat::OpaqueText));
assert_eq!(FileFormat::from_hint("unknown"), None);
}
// ---------------------------------------------------------------------------
// Key filtering
// ---------------------------------------------------------------------------
#[test]
fn filter_by_glob() {
let content = "API_KEY=abc\nDB_HOST=localhost\nDB_PASSWORD=secret\n";
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
assert_eq!(entries.len(), 3);
let filter = vec!["*PASSWORD*".to_string(), "*API_KEY*".to_string()];
let result = parse_secret_file_with_filter(content, &filter);
assert_eq!(result.len(), 2);
assert!(result.iter().any(|e| e.key == "API_KEY"));
assert!(result.iter().any(|e| e.key == "DB_PASSWORD"));
}
/// Helper that parses env content with a key filter (avoids temp files).
fn parse_secret_file_with_filter(content: &str, filter: &[String]) -> Vec<SecretEntry> {
let entries = parse_content(content, FileFormat::Env, p(".env")).unwrap();
// Re-implement the filter logic for testing without disk I/O.
use dirigent_fermata::core::secrets::parser::parse_content as pc;
let all = pc(content, FileFormat::Env, p(".env")).unwrap();
// Apply filter manually using the same approach as parse_secret_file.
let matchers: Vec<_> = filter
.iter()
.filter_map(|p| {
globset::Glob::new(&p.to_ascii_uppercase())
.ok()
.map(|g| g.compile_matcher())
})
.collect();
all.into_iter()
.filter(|entry| {
let upper = entry.key.to_ascii_uppercase();
matchers.iter().any(|m| m.is_match(&upper))
})
.collect()
}
// ---------------------------------------------------------------------------
// Error on unrecognised format
// ---------------------------------------------------------------------------
#[test]
fn error_on_unknown_format() {
use std::io::Write;
let mut tmp = NamedTempFile::with_suffix(".xyz").unwrap();
write!(tmp, "KEY=value").unwrap();
let result = parse_secret_file(tmp.path(), None, None);
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("cannot determine file format"));
}
// ---------------------------------------------------------------------------
// Empty file
// ---------------------------------------------------------------------------
#[test]
fn empty_file_produces_empty_vec() {
let entries = parse_content("", FileFormat::Env, p(".env")).unwrap();
assert!(entries.is_empty());
let entries = parse_content("{}", FileFormat::Json, p("a.json")).unwrap();
assert!(entries.is_empty());
let entries = parse_content("", FileFormat::Toml, p("a.toml")).unwrap();
assert!(entries.is_empty());
}
// ---------------------------------------------------------------------------
// parse_secret_file end-to-end (disk)
// ---------------------------------------------------------------------------
#[test]
fn parse_secret_file_from_disk() {
use std::io::Write;
let mut tmp = NamedTempFile::with_suffix(".env").unwrap();
write!(tmp, "SECRET=hunter2\nPORT=8080").unwrap();
let entries = parse_secret_file(tmp.path(), None, None).unwrap();
assert_eq!(entries.len(), 2);
assert!(entries.iter().any(|e| e.key == "SECRET" && e.value == "hunter2"));
// Source path should match.
assert_eq!(entries[0].source, tmp.path());
}
#[test]
fn parse_secret_file_with_key_filter() {
use std::io::Write;
let mut tmp = NamedTempFile::with_suffix(".env").unwrap();
write!(tmp, "API_KEY=abc\nHOST=localhost\nDB_PASSWORD=secret").unwrap();
let filter = vec!["*PASSWORD*".to_string()];
let entries = parse_secret_file(tmp.path(), None, Some(&filter)).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "DB_PASSWORD");
}
#[test]
fn parse_secret_file_with_format_override() {
use std::io::Write;
// Write env content to a .txt file — format override should work.
let mut tmp = NamedTempFile::with_suffix(".txt").unwrap();
write!(tmp, "KEY=value").unwrap();
let entries = parse_secret_file(tmp.path(), Some(FileFormat::Env), None).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].key, "KEY");
}
// ---------------------------------------------------------------------------
// Errors quote nothing
// ---------------------------------------------------------------------------
#[test]
fn a_format_error_names_the_line_and_never_quotes_it() {
let value = "value-on-the-failing-line";
let cases = [
(FileFormat::Toml, format!("ok = 1\nkey = \"{value}\" broken [\n"), "invalid TOML at line 2"),
(FileFormat::Json, format!("{{\n\"key\": \"{value}\" broken\n}}"), "invalid JSON at line 2"),
(FileFormat::Yaml, format!("ok: 1\nkey: \"{value}\" broken\n"), "invalid YAML at line 2"),
];
for (format, content, expected) in cases {
let error = parse_content(&content, format, Path::new("f")).unwrap_err();
let text = error.to_string();
assert!(text.ends_with(expected) && !text.contains(value), "{format:?}: {text}");
assert!(!format!("{error:?}").contains(value), "{format:?}");
}
}
// ---------------------------------------------------------------------------
// Key files
// ---------------------------------------------------------------------------
fn opaque_values(content: &str) -> Vec<String> {
let entries = parse_content(content, FileFormat::OpaqueText, p("dir/server.key")).unwrap();
assert!(entries.iter().all(|e| e.key == "server.key"), "{entries:?}");
entries.into_iter().map(|e| e.value).collect()
}
#[test]
fn an_opaque_file_gives_a_private_block_body_or_one_long_token() {
let body = "FAKEbodyONE\nFAKEbodyTWO";
for label in ["PRIVATE KEY", "RSA PRIVATE KEY", "OPENSSH PRIVATE KEY", "ENCRYPTED PRIVATE KEY", "PGP PRIVATE KEY BLOCK"] {
assert_eq!(opaque_values(&format!("-----BEGIN {label}-----\n{body}\n-----END {label}-----\n")), [body], "{label}");
}
// Text before it, two blocks, a block nobody closed.
let two = format!("Bag Attributes\n-----BEGIN PRIVATE KEY-----\na-first-body\n-----END PRIVATE KEY-----\n-----BEGIN EC PRIVATE KEY-----\nb-second-body\n");
assert_eq!(opaque_values(&two), ["a-first-body", "b-second-body"]);
// The token starts at 16 characters, counted as characters.
assert_eq!(opaque_values("fake0123456789ab\n"), ["fake0123456789ab"]);
assert_eq!(opaque_values("äääääääääääääää\n"), Vec::<String>::new());
}
#[test]
fn an_opaque_file_gives_nothing_for_any_other_text() {
for content in [
"",
"localhost\n",
"fake0123456789a\n",
"two fake0123456789abcdef tokens\n",
"fake0123456789abcdef\nfake0123456789abcdef\n",
"-----BEGIN CERTIFICATE-----\nfake-certificate-body\n-----END CERTIFICATE-----\n",
"-----BEGIN PUBLIC KEY-----\nfake-public-body\n-----END PUBLIC KEY-----\n",
] {
assert_eq!(opaque_values(content), Vec::<String>::new(), "{content:?}");
}
}
/// Only an opaque file gets past a read that fails. `.env.gpg` bytes stay a failure.
#[test]
fn a_binary_opaque_file_is_empty_and_any_other_binary_file_fails() {
let dir = tempfile::tempdir().unwrap();
for name in ["a.key", "a.pem", "id_rsa"] {
let path = dir.path().join(name);
std::fs::write(&path, b"\x30\x82\xff\xfe\x00fake").unwrap();
assert_eq!(parse_secret_file(&path, None, None).unwrap(), vec![], "{name}");
}
for name in [".env.gpg", "credentials", "secrets.conf", "a.tfstate"] {
let path = dir.path().join(name);
std::fs::write(&path, b"\x85\x02\xff\xfe\x00fake").unwrap();
assert!(matches!(parse_secret_file(&path, None, None), Err(ParseError::Io { .. })), "{name}");
}
}
// ---------------------------------------------------------------------------
// A byte-order mark
// ---------------------------------------------------------------------------
#[test]
fn a_utf8_bom_is_stripped_for_every_format() {
for (format, content, key) in [
(FileFormat::Env, "\u{feff}API_KEY=fake-value", "API_KEY"),
(FileFormat::Toml, "\u{feff}api_key = \"fake-value\"", "api_key"),
(FileFormat::Json, "\u{feff}{\"api_key\": \"fake-value\"}", "api_key"),
(FileFormat::Yaml, "\u{feff}api_key: fake-value", "api_key"),
(FileFormat::PythonAssignments, "\u{feff}API_KEY = \"fake-value\"", "API_KEY"),
(FileFormat::Properties, "\u{feff}api_key=fake-value", "api_key"),
] {
let entries = parse_content(content, format, p("x")).unwrap();
assert!(entries.iter().any(|e| e.key == key && e.value == "fake-value"), "{format:?}: {entries:?}");
}
assert_eq!(opaque_values("\u{feff}fake0123456789ab\n"), ["fake0123456789ab"]);
}