mod command;
mod copy;
mod keychain;
mod program;
mod refresh;
#[cfg(all(test, unix))]
mod stand;
use anyhow::{Context as _, Result, bail};
use openmoat_audit::KnownSecrets;
use openmoat_core::{Policy, Secret, SecretSource};
use openmoat_proxy::Broker;
use zeroize::Zeroizing;
pub use refresh::{Masks, Refresher};
pub struct Place<'a> {
pub home: &'a str,
pub state: &'a std::path::Path,
pub writable: &'a [String],
}
pub fn writable(ctx: &openmoat_core::EvalContext, granted: &[String]) -> Vec<String> {
let project = [&ctx.project, &ctx.real_project].into_iter().flatten();
project
.chain(granted)
.map(|p| p.replace('\\', "/").trim_end_matches('/').to_owned())
.filter(|p| !p.is_empty())
.collect()
}
pub fn broker(policy: &Policy, place: &Place<'_>) -> Result<(Broker, Refresher)> {
let home = place.home;
let mut held = Vec::with_capacity(policy.secrets.len());
let mut fixed = Vec::new();
let mut started = Vec::new();
for secret in &policy.secrets {
let reading = || format!("reading secret `{}`", secret.id);
let value = match &secret.source {
SecretSource::Command(source) => {
let program = command::Program::prepare(&secret.id, source, policy, place)
.with_context(reading)?;
let answer = program.fetch().with_context(reading)?;
let until = std::time::Instant::now() + answer.lifetime;
started.push((program, until, answer.value.clone()));
answer.value
}
other => {
let value = read(other, home).with_context(reading)?;
fixed.push(value.clone());
value
}
};
held.push((secret.clone(), value));
}
let broker = Broker::new(held)?;
let mut programs = Vec::with_capacity(started.len());
for (program, until, value) in started {
let rotation = broker
.rotation(program.id())
.context("a brokered secret went missing")?;
rotation.renew(
value.clone(),
until.saturating_duration_since(std::time::Instant::now()),
)?;
programs.push((program, rotation, value));
}
Ok((broker, Refresher::new(programs, fixed)))
}
pub fn known(secrets: &[Secret], home: &str) -> Result<KnownSecrets> {
let values = known_values(secrets, home);
Ok(KnownSecrets::new(values.iter().map(|v| v.as_str()))?)
}
pub fn known_values(secrets: &[Secret], home: &str) -> Vec<Zeroizing<String>> {
secrets
.iter()
.filter(|s| !matches!(s.source, SecretSource::Keychain { .. }) && !s.source.proxy_only())
.filter_map(|s| read(&s.source, home).ok())
.collect()
}
pub fn read(source: &SecretSource, home: &str) -> Result<Zeroizing<String>> {
let mut value = match source {
SecretSource::File(path) => {
let path = match path.strip_prefix("~/") {
Some(rest) => format!("{home}/{rest}"),
None => path.clone(),
};
Zeroizing::new(std::fs::read_to_string(&path).with_context(|| path.clone())?)
}
SecretSource::Env(name) => Zeroizing::new(
std::env::var(name)
.ok()
.with_context(|| format!("environment variable {name} is not set or not UTF-8"))?,
),
SecretSource::Keychain { service, account } => keychain::read(service, account)?,
SecretSource::Command(source) => bail!(
"`command` source {} is run by `openmoat proxy` only",
source.program()
),
};
let end = value.trim_end_matches(['\r', '\n']).len();
value.truncate(end);
Ok(value)
}
#[cfg(test)]
mod tests;