git.delta.rocks / jrsonnet / refs/commits / 5eda0552da5c

difftreelog

refactor move keys command to secrets

Yaroslav Bolyukin2021-09-18parent: #fad91f8.patch.diff
in: trunk

3 files changed

deletedsrc/cmds/fetch_keys.rsdiffbeforeafterboth
--- a/src/cmds/fetch_keys.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-use crate::host::FleetOpts;
-use anyhow::Result;
-use clap::Clap;
-use log::{info, warn};
-
-#[derive(Clap)]
-pub struct FetchKeys {
-	#[clap(flatten)]
-	fleet_opts: FleetOpts,
-
-	/// If true - remove orphaned keys
-	#[clap(long)]
-	cleanup: bool,
-}
-
-impl FetchKeys {
-	pub fn run(self) -> Result<()> {
-		let fleet = self.fleet_opts.build()?;
-		let hosts = fleet.list_hosts()?;
-		for host in hosts.iter() {
-			if host.skip() {
-				warn!("Skipped host {}", host.hostname);
-				continue;
-			}
-			host.key()?;
-		}
-		let orphans: Vec<_> = fleet.list_orphaned_keys()?;
-		if !orphans.is_empty() {
-			if self.cleanup {
-				info!("Removed orphan host keys:");
-			} else {
-				info!("Orphan host keys found, run with --cleanup to remove them from db:");
-			}
-			for (name, path) in orphans {
-				info!("- {}", name);
-				if self.cleanup {
-					std::fs::remove_file(path)?;
-				}
-			}
-		}
-		Ok(())
-	}
-}
addedsrc/cmds/secrets/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/src/cmds/secrets/mod.rs
@@ -0,0 +1,51 @@
+use std::io::Write;
+
+use anyhow::Result;
+use clap::Clap;
+
+use crate::host::Config;
+
+#[derive(Clap)]
+pub enum Secrets {
+	/// Force load keys for all defined hosts
+	ForceKeys,
+	/// Add secret, data should be provided in stdin
+	Add {
+		/// Secret owner
+		machine: String,
+		/// Secret name
+		name: String,
+	},
+}
+
+impl Secrets {
+	pub fn run(self, config: &Config) -> Result<()> {
+		match self {
+			Secrets::ForceKeys => {
+				for host in config.list_hosts()? {
+					if config.should_skip(&host) {
+						continue;
+					}
+					config.key(&host)?;
+				}
+			}
+			Secrets::Add { machine, name } => {
+				let recipient = config.recipient(&machine)?;
+				let encryptor = age::Encryptor::with_recipients(vec![Box::new(recipient)]);
+
+				let mut encrypted = vec![];
+				{
+					let mut w = encryptor.wrap_output(&mut encrypted)?;
+
+					let stdin = std::io::stdin();
+					let mut lock = stdin.lock();
+					std::io::copy(&mut lock, &mut w)?;
+					w.flush()?;
+				}
+
+				config.update_secret(&machine, &name, &encrypted)
+			}
+		}
+		Ok(())
+	}
+}
modifiedsrc/main.rsdiffbeforeafterboth
15#[derive(Clap)]15#[derive(Clap)]
16#[clap(version = "1.0", author = "CertainLach <iam@lach.pw>")]16#[clap(version = "1.0", author = "CertainLach <iam@lach.pw>")]
17enum Opts {17enum Opts {
18 /// Fetch encryption (ssh) public keys from remote hosts
19 FetchKeys(FetchKeys),
20 /// Force generation of missing secrets18 /// Force generation of missing secrets
21 GenerateSecrets(GenerateSecrets),19 GenerateSecrets(GenerateSecrets),
22 /// Prepare systems for deployments20 /// Prepare systems for deployments
23 BuildSystems(BuildSystems),21 BuildSystems(BuildSystems),
22 /// Secret management
23 Secrets(Secrets),
24}24}
2525
26fn main() -> Result<()> {26fn main() -> Result<()> {