use super::super::*;
use super::info;
use shep_core::config::{AppConfig, normalize};
use shep_core::status::ProcStatus;
use crate::testing::test_paths;
#[test]
fn a_resolved_secret_never_reaches_the_muster_roll() {
const SENTINEL: &str = "hunter2-that-must-never-reach-disk";
let mut config = AppConfig::minimal("web", "./srv");
config
.env
.insert("PW".to_string(), "{{secret:DB_PASSWORD}}".to_string());
let app = normalize(config).unwrap();
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let view = shep_core::secrets::SecretView::new(
"production".to_string(),
std::collections::BTreeMap::from([(
"DB_PASSWORD".to_string(),
std::collections::BTreeMap::from([("production".to_string(), SENTINEL.to_string())]),
)]),
shep_core::secrets::ProviderCache::default(),
);
let spec = crate::assemble::assemble(&app, 0, &paths, None, &view).unwrap();
assert_eq!(
spec.env["PW"], SENTINEL,
"the fixture must really resolve, or this case proves nothing"
);
let registry = FlockRegistry::new();
registry.record(&[app]);
let roll = registry.roll(&[info(0, "web", ProcStatus::Online)], 0);
let json = serde_json::to_string(&roll).unwrap();
assert!(!json.contains(SENTINEL), "the roll carries a value: {json}");
assert!(
json.contains("{{secret:DB_PASSWORD}}"),
"the reference is what it carries instead: {json}"
);
let path = dir.path().join("flock.json");
write_atomic(&path, &roll).unwrap();
let on_disk = std::fs::read_to_string(&path).unwrap();
assert!(
!on_disk.contains(SENTINEL),
"the roll on disk carries a value: {on_disk}"
);
}
#[test]
fn debug_does_not_leak_env_values() {
let mut app = AppConfig::minimal("web", "./srv");
app.env
.insert("DATABASE_URL".to_string(), "postgres://secret".to_string());
let roll = FlockSnapshot {
version: SNAPSHOT_VERSION,
saved_at_ms: 0,
apps: vec![SavedApp {
app,
instances_running: 1,
}],
};
let rendered = format!("{roll:?}");
assert!(!rendered.contains("postgres://secret"), "{rendered}");
assert!(rendered.contains("<1 vars>"), "{rendered}");
}