use core::fmt;
use std::collections::{BTreeMap, BTreeSet};
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};
use serde::{Deserialize, Serialize};
use shep_core::secrets::{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>,
}
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 cached = read_cache(cache);
let persisted = cached.values.keys().cloned().collect();
Self {
state: Mutex::new(State {
namespaces: cached.values,
pushed: cached.pushed,
persisted,
}),
writer: Mutex::new(()),
cache: cache.to_path_buf(),
}
}
pub fn put(
&self,
namespace: &str,
environment: &str,
entries: BTreeMap<String, String>,
persist: bool,
) -> std::io::Result<u32> {
let accepted = u32::try_from(entries.len()).unwrap_or(u32::MAX);
let _writer = self.writer.lock().unwrap_or_else(PoisonError::into_inner);
let pending = {
let mut state = self.state();
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))
};
if let Some(file) = pending {
write_cache(&self.cache, &file)?;
}
Ok(accepted)
}
pub fn snapshot(&self) -> ProviderCache {
let state = self.state();
ProviderCache {
values: state.namespaces.clone(),
pushed: 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 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 {
values: file.namespaces,
pushed: file.pushed,
},
_ => ProviderCache::default(),
}
}
fn write_cache(path: &Path, file: &CacheFile) -> std::io::Result<()> {
let parent = path.parent().unwrap_or_else(|| Path::new("."));
let mut tmp = shep_core::atomic_file::create_staging_file(parent, "secrets-cache", ".tmp")?;
let json = serde_json::to_string_pretty(file).map_err(std::io::Error::other)?;
tmp.write_all(json.as_bytes())?;
tmp.write_all(b"\n")?;
tmp.as_file().sync_all()?;
tmp.persist(path).map_err(|err| err.error)?;
shep_core::atomic_file::sync_dir(parent)
}
#[cfg(test)]
mod tests {
use super::*;
#[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(
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into()), ("B".into(), "2".into())]),
false,
)
.unwrap();
store
.put(
"vercel",
"production",
BTreeMap::from([("A".into(), "9".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
let vercel = &snap.values["vercel"];
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(
"v",
"production",
BTreeMap::from([("A".into(), "p".into())]),
false,
)
.unwrap();
store
.put(
"v",
"staging",
BTreeMap::from([("A".into(), "s".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
assert_eq!(snap.values["v"]["A"]["production"], "p");
assert_eq!(snap.values["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(
"kept",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put(
"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(
"unpersisted",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
first
.put(
"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(
"v",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put(
"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("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(
"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(
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
let snap = store.snapshot();
assert!(snap.values.contains_key("vercel"), "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(
"vercel",
"production",
BTreeMap::from([("A".into(), "1".into())]),
true,
)
.unwrap();
first
.put("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(
"unpersisted",
"production",
BTreeMap::from([("A".into(), "1".into())]),
false,
)
.unwrap();
first
.put(
"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(
"v",
"production",
BTreeMap::from([("A".into(), "hunter2".into())]),
false,
)
.unwrap();
let rendered = format!("{store:?}");
assert_eq!(rendered, "ProviderSecrets { namespaces: 1 }");
}
}