//! `is_plain_setting` (three rules) and what the manifest keeps of the AUTH family. use std::fs; use dirigent_fermata::core::secrets::config::SecretsConfig; use dirigent_fermata::core::secrets::manifest::Manifest; use dirigent_fermata::core::secrets::value_class::is_plain_setting; #[test] fn rule_one_words_hold_also_when_explicit() { for value in ["true", "FALSE", "Yes", "no", "on", "off", "null", "None", "nil", "enabled", "disabled", "\"true\"", "'off'", " true "] { assert!(is_plain_setting("API_TOKEN", value, false), "{value}"); assert!(is_plain_setting("API_TOKEN", value, true), "{value} explicit"); } assert!(!is_plain_setting("API_TOKEN", "truest", false)); } #[test] fn rule_two_numbers_and_durations_hold_also_when_explicit() { for value in ["3600", "42", "-1", "+5", "1.5", "99999", "15m", "500ms", "30s", "2h", "7d", "1w", "9999s"] { assert!(is_plain_setting("API_TOKEN", value, false), "{value}"); assert!(is_plain_setting("API_TOKEN", value, true), "{value} explicit"); } // Six or more characters of number or duration is long enough to be a secret. for value in ["123456", "1234567890", "12.3456", "15x", "m15", "123456789d", "1000ms"] { assert!(!is_plain_setting("API_TOKEN", value, false), "{value}"); } } #[test] fn rule_three_key_words_only_when_not_explicit() { for key in ["AUTH_ENABLED", "TOKEN_TTL", "auth.timeout", "jwt-algorithm", "DB_HOST", "PASSWORD_POLICY", "SECRET_BACKEND", "AUTH_METHOD", "DB_USER", "TOKEN_PATH"] { assert!(is_plain_setting(key, "abcdefgh", false), "{key}"); assert!(!is_plain_setting(key, "abcdefgh", true), "{key} explicit"); } for key in ["AUTH_TOKEN", "DB_PASSWORD", "API_KEY", "AUTH_HEADER", "DB_URL", "ENABLED_TOKEN"] { assert!(!is_plain_setting(key, "abcdefgh", false), "{key}"); } } fn keys_of(config: &SecretsConfig, env: &str) -> Vec { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join(".env"), env).unwrap(); let manifest = Manifest::build(config, dir.path()).unwrap(); let mut keys: Vec = manifest.entries().iter().map(|e| e.key.clone()).collect(); keys.dedup(); keys } const SETTINGS: &str = "AUTH_ENABLED=true\nAUTH_FLAG=\"true\"\nTOKEN_TTL=3600\nAUTH_TIMEOUT=15m\nPASSWORD_POLICY=strict\n\ AUTHOR=authorname\nAUTHORIZED_KEYS=ssh-keyname\n"; #[test] fn settings_of_the_auth_family_are_not_in_the_manifest() { assert_eq!(keys_of(&SecretsConfig::default(), SETTINGS), Vec::::new()); } #[test] fn real_values_of_the_auth_family_stay() { let env = "AUTH_TOKEN=tok_abcdefgh1234\nTS_AUTHKEY=tskey-abcdefgh\nDB_PASSWORD=postgres\nAUTH=hunter2hunter2\nAUTHORIZATION=Bearer-abcdefgh\n"; let keys = keys_of(&SecretsConfig::default(), env); for key in ["AUTH_TOKEN", "TS_AUTHKEY", "DB_PASSWORD", "AUTH", "AUTHORIZATION"] { assert!(keys.iter().any(|k| k == key), "{key} in {keys:?}"); } } #[test] fn docker_auths_auth_stays() { let dir = tempfile::tempdir().unwrap(); fs::create_dir(dir.path().join(".docker")).unwrap(); fs::write(dir.path().join(".docker/config.json"), r#"{"auths":{"registry.example.com":{"auth":"dXNlcjpwYXNzd29yZA=="}}}"#).unwrap(); let manifest = Manifest::build(&SecretsConfig::default(), dir.path()).unwrap(); assert!(manifest.entries().iter().any(|e| e.key.to_lowercase().ends_with(".auth")), "{:?}", manifest.entries()); } #[test] fn explicit_include_keeps_a_setting_word_key_but_not_a_rule_one_or_two_value() { let config = SecretsConfig::from_toml("[keys]\ninclude = [\"MY_TTL\"]\n").unwrap(); assert_eq!(keys_of(&config, "MY_TTL=abcdefgh\n"), ["MY_TTL"]); assert!(keys_of(&config, "MY_TTL=3600\n").is_empty()); assert!(keys_of(&config, "MY_TTL=true\n").is_empty()); } #[test] fn a_file_override_is_explicit() { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join("my.env"), "MY_TTL=abcdefgh\nOTHER=true\n").unwrap(); let config = SecretsConfig::from_toml("[[file]]\npath = \"my.env\"\nformat = \"env\"\n").unwrap(); let manifest = Manifest::build(&config, dir.path()).unwrap(); let keys: Vec<&str> = manifest.entries().iter().map(|e| e.key.as_str()).collect(); assert_eq!(keys, ["MY_TTL"]); } #[test] fn excluding_auth_star_drops_all_three_patterns() { let config = SecretsConfig::from_toml("[keys]\nexclude = [\"*AUTH*\"]\n").unwrap(); // AUTH_TOKEN stays: `*TOKEN*` still matches it. assert_eq!(keys_of(&config, "AUTH_TOKEN=tok_abcdefgh1234\n"), ["AUTH_TOKEN"]); assert!(keys_of(&config, "AUTH=hunter2hunter2\nTS_AUTHKEY=tskey-abcdefgh\nAUTHORIZATION=Bearer-abcdefgh\n").is_empty()); assert!(!config.effective_key_includes().iter().any(|p| ["*AUTH", "*AUTH[!O]*", "*AUTHORIZATION*"].contains(&p.as_str()))); } #[test] fn from_entries_skips_the_class() { use dirigent_fermata::core::secrets::SecretEntry; let entry = SecretEntry { key: "AUTH_ENABLED".into(), value: "enabled".into(), source: "scan".into() }; assert!(!Manifest::from_entries(vec![entry]).is_empty()); }