feat(terra): pass-broker for Telegram-approved Proton Pass access
A Rust daemon running as its own system user holds terra's only pass-cli session, an agent PAT scoped to the HomeLab vaults. `pass-broker get` asks it for an item; per-item rules decide auto/ask/deny, and "ask" sends a Telegram prompt that only darman's account can approve (10-min grant for darman@*/root@*). Requests whose client is gone are dropped or withdrawn, and the client gives up after 30s without an answer. scripts/deploy uses the broker exclusively when its socket exists. Covered by checks.pass-broker (fake pass-cli + Telegram). Secrets added for all hosts; only terra runs the broker so far. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Written by services/identity/pass-broker.nix; path in $PASS_BROKER_CONFIG.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub host: String,
|
||||
pub default_vault: String,
|
||||
/// Peer uids are checked against these; root is always allowed.
|
||||
pub allowed_users: Vec<String>,
|
||||
pub pass_cli: String,
|
||||
pub session_dir: PathBuf,
|
||||
pub pat_file: PathBuf,
|
||||
pub approval_timeout_secs: u64,
|
||||
pub telegram: TelegramConfig,
|
||||
/// First match wins; no match means deny.
|
||||
pub rules: Vec<Rule>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct TelegramConfig {
|
||||
pub api_base: String,
|
||||
pub token_file: PathBuf,
|
||||
pub chat_id_file: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Rule {
|
||||
pub vault: Option<String>,
|
||||
/// Item title; `*` matches any run of characters.
|
||||
pub item: String,
|
||||
pub approval: Approval,
|
||||
/// After an approval, further requests for the item skip the prompt for this long.
|
||||
pub grant_minutes: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Copy, PartialEq, Debug)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Approval {
|
||||
Auto,
|
||||
Ask,
|
||||
Deny,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self> {
|
||||
let path = std::env::var("PASS_BROKER_CONFIG").context("PASS_BROKER_CONFIG unset")?;
|
||||
let text = std::fs::read_to_string(&path).with_context(|| format!("reading {path}"))?;
|
||||
serde_json::from_str(&text).with_context(|| format!("parsing {path}"))
|
||||
}
|
||||
|
||||
pub fn rule_for(&self, vault: &str, item: &str) -> Option<&Rule> {
|
||||
self.rules.iter().find(|r| {
|
||||
r.vault.as_deref().unwrap_or(&self.default_vault) == vault && glob(&r.item, item)
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolved once at startup: a user added later needs a broker restart.
|
||||
pub fn allowed_uids(&self) -> Result<Vec<u32>> {
|
||||
let passwd = std::fs::read_to_string("/etc/passwd")?;
|
||||
let mut uids = vec![0];
|
||||
for name in &self.allowed_users {
|
||||
let uid = passwd
|
||||
.lines()
|
||||
.map(|l| l.split(':').collect::<Vec<_>>())
|
||||
.find(|f| f.len() > 2 && f[0] == name)
|
||||
.and_then(|f| f[2].parse().ok());
|
||||
match uid {
|
||||
Some(uid) => uids.push(uid),
|
||||
None => bail!("allowed user {name:?} not in /etc/passwd"),
|
||||
}
|
||||
}
|
||||
Ok(uids)
|
||||
}
|
||||
}
|
||||
|
||||
fn glob(pat: &str, s: &str) -> bool {
|
||||
let parts: Vec<&str> = pat.split('*').collect();
|
||||
if parts.len() == 1 {
|
||||
return pat == s;
|
||||
}
|
||||
let (first, last) = (parts[0], parts[parts.len() - 1]);
|
||||
if !s.starts_with(first) || s.len() < first.len() + last.len() || !s.ends_with(last) {
|
||||
return false;
|
||||
}
|
||||
let mut rest = &s[first.len()..s.len() - last.len()];
|
||||
for mid in &parts[1..parts.len() - 1] {
|
||||
match rest.find(mid) {
|
||||
Some(i) => rest = &rest[i + mid.len()..],
|
||||
None => return false,
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::glob;
|
||||
|
||||
#[test]
|
||||
fn globs() {
|
||||
assert!(glob("root@*", "root@jupiter"));
|
||||
assert!(!glob("root@*", "darman@jupiter"));
|
||||
assert!(glob("*", ""));
|
||||
assert!(glob("a*b*c", "aXbYc"));
|
||||
assert!(!glob("a*b*c", "acb"));
|
||||
assert!(!glob("ab*ba", "aba"));
|
||||
assert!(glob("exact", "exact"));
|
||||
assert!(!glob("exact", "exact2"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user