use core::fmt;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};
use serde::{Deserialize, Serialize};
use shep_core::secrets::{
ALL_ENVIRONMENTS, NamespaceValues, PROVIDER_CACHE_VERSION, ProviderCache, PushedPairs,
};
#[derive(Default, Serialize, Deserialize)]
struct CacheFile {
version: u32,
namespaces: NamespaceValues,
pushed: PushedPairs,
}
impl fmt::Debug for CacheFile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CacheFile")
.field("version", &self.version)
.field("namespaces", &self.namespaces.len())
.field("pushed", &self.pushed.len())
.finish()
}
}
#[derive(Default)]
struct State {
namespaces: NamespaceValues,
pushed: PushedPairs,
persisted: BTreeSet<String>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Pushed {
pub accepted: u32,
pub changed: BTreeSet<String>,
}
#[derive(Debug)]
pub struct CacheWriteFailed {
pub source: std::io::Error,
pub changed: BTreeSet<String>,
}
impl fmt::Display for CacheWriteFailed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.source, f)
}
}
impl core::error::Error for CacheWriteFailed {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Some(&self.source)
}
}
pub struct ProviderSecrets {
state: Mutex<State>,
writer: Mutex<()>,
cache: PathBuf,
}
impl fmt::Debug for ProviderSecrets {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProviderSecrets")
.field("namespaces", &self.state().namespaces.len())
.finish()
}
}
impl ProviderSecrets {
pub fn load(cache: &Path) -> Self {
let (namespaces, pushed) = read_cache(cache).into_parts();
let persisted = namespaces.keys().cloned().collect();
Self {
state: Mutex::new(State {
namespaces,
pushed,
persisted,
}),
writer: Mutex::new(()),
cache: cache.to_path_buf(),
}
}
pub fn put(
&self,
host_environment: &str,
namespace: &str,
environment: &str,
entries: BTreeMap<String, String>,
persist: bool,
) -> Result<Pushed, CacheWriteFailed> {
let accepted = u32::try_from(entries.len()).unwrap_or(u32::MAX);
let _writer = self.writer.lock().unwrap_or_else(PoisonError::into_inner);
let (pending, changed) = {
let mut state = self.state();
let changed = changed_keys(
state.namespaces.get(namespace),
host_environment,
environment,
&entries,
);
let table = state.namespaces.entry(namespace.to_owned()).or_default();
table.retain(|_, by_environment| {
by_environment.remove(environment);
!by_environment.is_empty()
});
for (key, value) in entries {
table
.entry(key)
.or_default()
.insert(environment.to_owned(), value);
}
state
.pushed
.entry(namespace.to_owned())
.or_default()
.insert(environment.to_owned());
let rewrite = if persist {
state.persisted.insert(namespace.to_owned());
true
} else {
state.persisted.remove(namespace)
};
(rewrite.then(|| persisted_view(&state)), changed)
};
if let Some(file) = pending {
write_cache(&self.cache, &file).map_err(|source| CacheWriteFailed {
source,
changed: changed.clone(),
})?;
}
Ok(Pushed { accepted, changed })
}
pub fn snapshot(&self) -> ProviderCache {
let state = self.state();
ProviderCache::new(state.namespaces.clone(), state.pushed.clone())
}
pub fn pushed(&self) -> PushedPairs {
self.state().pushed.clone()
}
fn state(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
}
fn changed_keys(
table: Option<&BTreeMap<String, BTreeMap<String, String>>>,
host_environment: &str,
environment: &str,
entries: &BTreeMap<String, String>,
) -> BTreeSet<String> {
let visible = environment == host_environment || environment == ALL_ENVIRONMENTS;
if !visible {
return BTreeSet::new();
}
let replaced = table
.into_iter()
.flatten()
.filter(|(_, slots)| slots.contains_key(environment))
.map(|(key, _)| key);
replaced
.chain(entries.keys())
.filter(|key| {
let slots = table.and_then(|table| table.get(*key));
let slot = |name: &str| slots.and_then(|slots| slots.get(name)).map(String::as_str);
let before = slot(host_environment).or_else(|| slot(ALL_ENVIRONMENTS));
let pushed = entries.get(*key).map(String::as_str);
let after = if environment == host_environment {
pushed.or_else(|| slot(ALL_ENVIRONMENTS))
} else {
slot(host_environment).or(pushed)
};
before != after
})
.cloned()
.collect()
}
fn persisted_view(state: &State) -> CacheFile {
CacheFile {
version: PROVIDER_CACHE_VERSION,
namespaces: state
.persisted
.iter()
.filter_map(|name| {
state
.namespaces
.get(name)
.map(|table| (name.clone(), table.clone()))
})
.collect(),
pushed: state
.persisted
.iter()
.filter_map(|name| {
state
.pushed
.get(name)
.map(|environments| (name.clone(), environments.clone()))
})
.collect(),
}
}
fn read_cache(path: &Path) -> ProviderCache {
let Ok(raw) = std::fs::read_to_string(path) else {
return ProviderCache::default();
};
match serde_json::from_str::<CacheFile>(&raw) {
Ok(file) if file.version == PROVIDER_CACHE_VERSION => {
ProviderCache::new(file.namespaces, file.pushed)
}
_ => ProviderCache::default(),
}
}
fn write_cache(path: &Path, file: &CacheFile) -> std::io::Result<()> {
shep_core::atomic_file::write_json(path, "secrets-cache", file)
}
#[cfg(test)]
mod tests {
use super::*;
const HOST: &str = "production";
#[test]
fn a_push_replaces_that_namespace_and_environment_rather_than_merging() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let store = ProviderSecrets::load(&cache);
store
.put(
HOST,
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into()), ("B".into(), "2".into())]),
false,
)
.unwrap();
store
.put(
HOST,
"vercel",
"production",
BTreeMap::from([("A".into(), "9".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
let vercel = snap.namespace("vercel").unwrap();
assert_eq!(vercel["A"]["production"], "9");
assert!(
!vercel.contains_key("B"),
"a replaced push drops what it omits"
);
}
#[test]
fn one_environment_does_not_disturb_another() {
let dir = tempfile::tempdir().unwrap();
let store = ProviderSecrets::load(&dir.path().join("secrets-cache.json"));
store
.put(
HOST,
"v",
"production",
BTreeMap::from([("A".into(), "p".into())]),
false,
)
.unwrap();
store
.put(
HOST,
"v",
"staging",
BTreeMap::from([("A".into(), "s".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
let v = snap.namespace("v").unwrap();
assert_eq!(v["A"]["production"], "p");
assert_eq!(v["A"]["staging"], "s");
assert_eq!(
snap.pushed["v"],
BTreeSet::from(["production".to_string(), "staging".to_string()]),
"both pairs carry a push"
);
}
#[test]
fn a_persisted_push_survives_a_reload_and_an_unpersisted_one_does_not() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let first = ProviderSecrets::load(&cache);
first
.put(
HOST,
"kept",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put(
HOST,
"gone",
"production",
BTreeMap::from([("B".into(), "2".into())]),
false,
)
.unwrap();
let second = ProviderSecrets::load(&cache);
assert!(second.pushed().contains_key("kept"));
assert!(!second.pushed().contains_key("gone"));
}
#[test]
fn an_unpersisted_namespace_is_not_swept_in_by_a_later_persisted_write() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let first = ProviderSecrets::load(&cache);
first
.put(
HOST,
"unpersisted",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
first
.put(
HOST,
"persisted",
"production",
BTreeMap::from([("B".into(), "2".into())]),
true,
)
.unwrap();
let second = ProviderSecrets::load(&cache);
assert_eq!(
second.pushed().into_keys().collect::<BTreeSet<_>>(),
BTreeSet::from(["persisted".to_string()])
);
let raw = std::fs::read_to_string(&cache).unwrap();
assert!(
!raw.contains("unpersisted"),
"the unpersisted namespace reached disk: {raw}"
);
}
#[test]
fn dropping_persist_removes_that_namespace_from_the_cache() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let first = ProviderSecrets::load(&cache);
first
.put(
HOST,
"v",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put(
HOST,
"v",
"production",
BTreeMap::from([("A".into(), "2".into())]),
false,
)
.unwrap();
assert!(ProviderSecrets::load(&cache).pushed().is_empty());
}
#[test]
fn a_namespace_is_known_as_soon_as_it_is_pushed_even_when_empty() {
let dir = tempfile::tempdir().unwrap();
let store = ProviderSecrets::load(&dir.path().join("secrets-cache.json"));
store
.put(HOST, "v", "production", BTreeMap::new(), false)
.unwrap();
assert!(store.pushed()["v"].contains("production"));
}
#[test]
#[cfg(unix)]
fn the_cache_is_owner_only() {
use std::os::unix::fs::PermissionsExt as _;
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
ProviderSecrets::load(&cache)
.put(
HOST,
"v",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
let mode = std::fs::metadata(&cache).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
}
#[test]
fn an_unreadable_cache_loads_as_empty() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
std::fs::write(&cache, "{ not json").unwrap();
assert!(ProviderSecrets::load(&cache).pushed().is_empty());
}
#[test]
fn a_push_registers_the_pair_it_carried_and_not_the_namespace() {
let dir = tempfile::tempdir().unwrap();
let store = ProviderSecrets::load(&dir.path().join("secrets-cache.json"));
store
.put(
HOST,
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
assert!(snap.namespace("vercel").is_some(), "the values are there");
assert_eq!(
snap.pushed["vercel"],
BTreeSet::from(["production".to_string()])
);
}
#[test]
fn the_pairs_survive_a_reload_through_the_cache() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let first = ProviderSecrets::load(&cache);
first
.put(
HOST,
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put(HOST, "vercel", "staging", BTreeMap::new(), true)
.unwrap();
let second = ProviderSecrets::load(&cache);
assert_eq!(
second.pushed()["vercel"],
BTreeSet::from(["production".to_string(), "staging".to_string()]),
"an empty push is a push"
);
}
#[test]
fn an_unpersisted_namespaces_pairs_do_not_reach_the_file() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("secrets-cache.json");
let first = ProviderSecrets::load(&cache);
first
.put(
HOST,
"unpersisted",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
first
.put(
HOST,
"persisted",
"production",
BTreeMap::from([("B".into(), "2".into())]),
true,
)
.unwrap();
let raw = std::fs::read_to_string(&cache).unwrap();
assert!(
!raw.contains("unpersisted"),
"neither its values nor its pairs: {raw}"
);
}
#[test]
fn a_cache_file_debug_never_prints_a_value() {
let file = CacheFile {
version: PROVIDER_CACHE_VERSION,
namespaces: BTreeMap::from([(
"vercel".to_string(),
BTreeMap::from([(
"API_KEY".to_string(),
BTreeMap::from([("production".to_string(), "sk_live".to_string())]),
)]),
)]),
pushed: BTreeMap::from([(
"vercel".to_string(),
BTreeSet::from(["production".to_string()]),
)]),
};
let rendered = format!("{file:?}");
assert_eq!(
rendered,
"CacheFile { version: 2, namespaces: 1, pushed: 1 }"
);
assert!(!rendered.contains("sk_live"));
}
#[test]
fn debug_never_prints_a_value() {
let dir = tempfile::tempdir().unwrap();
let store = ProviderSecrets::load(&dir.path().join("secrets-cache.json"));
store
.put(
HOST,
"v",
"production",
BTreeMap::from([("A".into(), "hunter2".into())]),
false,
)
.unwrap();
let rendered = format!("{store:?}");
assert_eq!(rendered, "ProviderSecrets { namespaces: 1 }");
}
#[test]
fn a_push_reports_only_the_keys_whose_resolved_value_changed() {
let dir = tempfile::tempdir().unwrap();
let store = ProviderSecrets::load(&dir.path().join("secrets-cache.json"));
let pairs = |pairs: &[(&str, &str)]| -> BTreeMap<String, String> {
pairs
.iter()
.map(|(key, value)| ((*key).to_string(), (*value).to_string()))
.collect()
};
let changed = |keys: &[&str]| -> BTreeSet<String> {
keys.iter().map(|key| (*key).to_string()).collect()
};
let put = |environment: &str, entries: &[(&str, &str)]| {
store
.put("production", "v", environment, pairs(entries), false)
.unwrap()
.changed
};
assert_eq!(
put(
"production",
&[("SAME", "1"), ("MOVED", "1"), ("DROPPED", "1")]
),
changed(&["SAME", "MOVED", "DROPPED"]),
"a key the host could not resolve before and can now"
);
assert_eq!(
put(
"production",
&[("SAME", "1"), ("MOVED", "2"), ("ADDED", "1")]
),
changed(&["MOVED", "DROPPED", "ADDED"]),
);
assert_eq!(
put(
"production",
&[("SAME", "1"), ("MOVED", "2"), ("ADDED", "1")]
),
changed(&[])
);
assert_eq!(
put("staging", &[("SAME", "9"), ("OTHER", "9")]),
changed(&[])
);
assert_eq!(
put("all", &[("SAME", "9"), ("MOVED", "9"), ("FALLBACK", "9")]),
changed(&["FALLBACK"]),
"SAME and MOVED keep their production value"
);
assert_eq!(
put("production", &[("SAME", "1"), ("ADDED", "1")]),
changed(&["MOVED"]),
"MOVED falls back to its `all` slot, which is a different value"
);
assert_eq!(put("production", &[("SAME", "1")]), changed(&["ADDED"]));
}
}