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

difftreelog

refactor remove key db

Yaroslav Bolyukin2021-03-09parent: #238480b.patch.diff
in: trunk

2 files changed

deletedsrc/db/keys.rsdiffbeforeafterboth
before · src/db/keys.rs
1use std::{collections::BTreeMap, process::Command};23use anyhow::Result;4use log::*;56use crate::{command::CommandExt, nix::HOSTS_ATTRIBUTE};78use serde::{Deserialize, Serialize};910use super::db::DbData;1112pub fn list_hosts() -> Result<Vec<String>> {13	Ok(Command::new("nix")14		.args(&[15			"eval",16			HOSTS_ATTRIBUTE,17			"--apply",18			"builtins.attrNames",19			"--json",20		])21		.inherit_stdio()22		.run_json()?)23}2425#[derive(Serialize, Deserialize, Default)]26pub struct KeyDb {27	host_keys: BTreeMap<String, String>,28}29impl DbData for KeyDb {30	const DB_NAME: &'static str = "keys";31}3233impl KeyDb {34	pub fn fetch_key(&mut self, host: &str) -> Result<()> {35		info!("Fetching key for {}", host);36		let key = Command::ssh_on(host, "cat")37			.arg("/etc/ssh/ssh_host_ed25519_key.pub")38			.run_string()?;39		self.host_keys.insert(host.to_owned(), key);40		Ok(())41	}4243	pub fn ensure_key_loaded(&mut self, host: &str, force: bool) -> Result<()> {44		if !self.host_keys.contains_key(host) || force {45			self.fetch_key(host)?;46		}47		Ok(())48	}4950	pub fn get_host_key(&self, host: &str) -> Result<String> {51		Ok(self52			.host_keys53			.get(host)54			.ok_or_else(|| anyhow::anyhow!("no host key for {}", host))?55			.to_owned())56	}5758	pub fn has_key(&self, key: &str) -> bool {59		self.host_keys.contains_key(key)60	}6162	pub fn remove_key(&mut self, host: &str) {63		self.host_keys.remove(host);64	}65}
modifiedsrc/db/mod.rsdiffbeforeafterboth
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,5 +1,4 @@
 mod db;
-pub mod keys;
 pub mod secret;
 
 pub use db::*;