A Telegram bot has a single getUpdates consumer, so a broker per host would split approval taps between them. mars now runs the gateway: the bot's only poller, relaying prompts and taps for terra's and titan's brokers over the tailnet (:8097) without ever seeing an item. Each broker authenticates with its own pass_broker_gateway_token; mars holds only the sha256, and the prompt is labelled with the host that token belongs to. The bot token and chat id move to mars and are dropped from the other hosts' sops files (terra.yaml also loses its leftover LibreChat keys and gains protonvpn_wg_private_key, which a later commit uses). Covered by checks.pass-broker-gateway (two nodes: labels, cancel, expiry, per-host token isolation, gateway down); the fakes are shared with checks.pass-broker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.2 KiB
Rust
148 lines
4.2 KiB
Rust
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 approval: ApprovalConfig,
|
|
/// First match wins; no match means deny.
|
|
pub rules: Vec<Rule>,
|
|
}
|
|
|
|
/// Who shows the prompt: this broker's own bot, or the shared gateway.
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum ApprovalConfig {
|
|
Telegram(TelegramConfig),
|
|
Gateway(GatewayClientConfig),
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct TelegramConfig {
|
|
pub api_base: String,
|
|
pub token_file: PathBuf,
|
|
pub chat_id_file: PathBuf,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct GatewayClientConfig {
|
|
pub url: String,
|
|
pub token_file: PathBuf,
|
|
}
|
|
|
|
/// Written by services/identity/pass-broker-gateway.nix; path in
|
|
/// $PASS_BROKER_GATEWAY_CONFIG.
|
|
#[derive(Deserialize)]
|
|
pub struct GatewayConfig {
|
|
pub listen: String,
|
|
pub telegram: TelegramConfig,
|
|
/// Hashes only: the gateway host never holds a broker's token.
|
|
pub hosts: Vec<GatewayHost>,
|
|
pub max_timeout_secs: u64,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct GatewayHost {
|
|
pub name: String,
|
|
pub token_sha256: String,
|
|
}
|
|
|
|
pub fn load<T: serde::de::DeserializeOwned>(var: &str) -> Result<T> {
|
|
let path = std::env::var(var).with_context(|| format!("{var} unset"))?;
|
|
let text = std::fs::read_to_string(&path).with_context(|| format!("reading {path}"))?;
|
|
serde_json::from_str(&text).with_context(|| format!("parsing {path}"))
|
|
}
|
|
|
|
#[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> {
|
|
load("PASS_BROKER_CONFIG")
|
|
}
|
|
|
|
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"));
|
|
}
|
|
}
|