git.delta.rocks / jrsonnet / refs/commits / faec7071817b

difftreelog

source

crates/fleet-base/src/keys.rs2.0 KiBsourcehistory
1use std::str::FromStr as _;23use age::Recipient;4use anyhow::{Result, anyhow};5use futures::{StreamExt as _, TryStreamExt as _};6use itertools::Itertools as _;7use tracing::warn;89use crate::host::Config;1011impl Config {12	pub fn cached_key(&self, host: &str) -> Option<String> {13		let data = self.data();14		let key = data.hosts.get(host).map(|h| &h.encryption_key);15		if let Some(key) = key16			&& key.is_empty()17		{18			return None;19		}20		key.cloned()21	}22	pub fn update_key(&self, host: &str, key: String) {23		let mut data = self.data_mut();24		let host = data.hosts.entry(host.to_string()).or_default();25		host.encryption_key = key.trim().to_string();26	}2728	pub async fn key(&self, host: &str) -> anyhow::Result<String> {29		if let Some(key) = self.cached_key(host) {30			Ok(key)31		} else {32			warn!("Loading key for {}", host);33			let host = self.host(host)?;34			let mut cmd = host.cmd("cat").await?;35			cmd.arg("/etc/ssh/ssh_host_ed25519_key.pub");36			let key = cmd.run_string().await?;37			self.update_key(&host.name, key.clone());38			Ok(key)39		}40	}41	/// Insecure, requires root42	pub async fn recipient(&self, host: &str) -> anyhow::Result<Box<dyn Recipient>> {43		let key = self.key(host).await?;44		age::ssh::Recipient::from_str(&key)45			.map_err(|e| anyhow!("parse recipient error: {e:?}"))46			.map(|v| Box::new(v) as Box<dyn Recipient>)47	}4849	pub async fn recipients(&self, hosts: Vec<String>) -> Result<Vec<Box<dyn Recipient>>> {50		let hosts = self.expand_owner_set(hosts)?;51		futures::stream::iter(hosts.iter())52			.then(|m| self.recipient(m.as_ref()))53			.try_collect::<Vec<_>>()54			.await55	}5657	#[allow(dead_code)]58	pub async fn orphaned_data(&self) -> Result<Vec<String>> {59		let mut out = Vec::new();60		let host_names = self.list_hosts()?.into_iter().map(|h| h.name).collect_vec();61		for hostname in self62			.data()63			.hosts64			.iter()65			.filter(|(_, host)| !host.encryption_key.is_empty())66			.map(|(n, _)| n)67		{68			if !host_names.contains(hostname) {69				out.push(hostname.to_owned())70			}71		}7273		Ok(out)74	}75}