perf(fermata): large secret files parse in linear time; KeyMatcher compiled once (M-4)

This commit is contained in:
2026-11-07 00:29:45 +01:00
parent 47da98709f
commit 86ba64b22c
5 changed files with 121 additions and 32 deletions
+69
View File
@@ -0,0 +1,69 @@
//! Large secret files parse in linear time: a 2 MB JSON and a 1 MB YAML manifest
//! (generated, fake values) finish under a bound that the quadratic scans missed.
use std::fmt::Write;
use std::time::{Duration, Instant};
use dirigent_fermata::core::secrets::config::SecretsConfig;
use dirigent_fermata::core::secrets::manifest::Manifest;
const BOUND: Duration = Duration::from_secs(60);
/// A fake value of 24 characters with a JSON escape in it, so raw and decoded differ.
fn value(i: usize) -> String {
format!("fake{i:08}-abcdefghij\\n")
}
fn config() -> SecretsConfig {
SecretsConfig::from_toml("[files]\npatterns = [\"secrets.json\", \"secrets.yaml\"]\n").unwrap()
}
#[test]
fn a_2_mb_json_manifest_builds_within_the_bound() {
let dir = tempfile::tempdir().unwrap();
let mut json = String::from("{");
let mut n = 0;
while json.len() < 2_000_000 {
if n > 0 {
json.push(',');
}
write!(json, "\"api_key_{n}\":\"{}\"", value(n)).unwrap();
n += 1;
}
json.push('}');
std::fs::write(dir.path().join("secrets.json"), &json).unwrap();
let start = Instant::now();
let manifest = Manifest::build(&config(), dir.path()).unwrap();
assert!(start.elapsed() < BOUND, "took {:?}", start.elapsed());
// Decoded and raw spelling of every value.
assert!(manifest.len() >= 2 * n, "{} entries for {n} keys", manifest.len());
}
#[test]
fn a_1_mb_yaml_manifest_builds_within_the_bound() {
let dir = tempfile::tempdir().unwrap();
let mut yaml = String::new();
let mut n = 0;
while yaml.len() < 1_000_000 {
writeln!(yaml, "api_key_{n}: \"{}\"", value(n)).unwrap();
n += 1;
}
std::fs::write(dir.path().join("secrets.yaml"), &yaml).unwrap();
let start = Instant::now();
let manifest = Manifest::build(&config(), dir.path()).unwrap();
assert!(start.elapsed() < BOUND, "took {:?}", start.elapsed());
assert!(manifest.len() >= 2 * n, "{} entries for {n} keys", manifest.len());
}
#[test]
fn a_compiled_key_matcher_agrees_with_key_matches() {
let config = SecretsConfig::from_toml("[keys]\ninclude = [\"my_thing_*\", \"[\"]\n").unwrap();
let matcher = config.key_matcher();
for key in ["API_KEY", "api_key", "My_Thing_One", "APP_NAME", "db_password", "["] {
assert_eq!(matcher.is_match(key), config.key_matches(key), "{key}");
}
assert!(matcher.is_match("my_thing_x"));
assert!(!matcher.is_match("APP_NAME"));
}