//! 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")); }