git.delta.rocks / jrsonnet / refs/commits / 17bf21a48a36

difftreelog

refactor use z85

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

5 files changed

modifiedsrc/cmds/build_systems.rsdiffbeforeafterboth
--- a/src/cmds/build_systems.rs
+++ b/src/cmds/build_systems.rs
@@ -47,7 +47,6 @@
 
 impl BuildSystems {
 	pub fn run(self, config: &Config) -> Result<()> {
-		println!("Build");
 		let hosts = config.list_hosts()?;
 
 		for host in hosts.iter() {
@@ -76,7 +75,6 @@
 				));
 
 			if let Some(builders) = &self.builders {
-				println!("Using builders: {}", builders);
 				nix_build.arg("--builders").arg(builders);
 			}
 			if let Some(jobs) = &self.jobs {
modifiedsrc/cmds/secrets/mod.rsdiffbeforeafterboth
before · src/cmds/secrets/mod.rs
1use crate::{fleetdata::FleetSecret, host::Config};2use anyhow::{bail, Result};3use clap::Clap;4use std::io::{self, Cursor, Read};56#[derive(Clap)]7pub enum Secrets {8	/// Force load keys for all defined hosts9	ForceKeys,10	/// Add secret, data should be provided in stdin11	Add {12		/// Secret name13		name: String,14		/// Secret owners15		machines: Vec<String>,16		/// Override secret if already present17		#[clap(long)]18		force: bool,19		#[clap(long)]20		public: Option<String>,21	},22}2324impl Secrets {25	pub fn run(self, config: &Config) -> Result<()> {26		match self {27			Secrets::ForceKeys => {28				for host in config.list_hosts()? {29					if config.should_skip(&host) {30						continue;31					}32					config.key(&host)?;33				}34			}35			Secrets::Add {36				machines,37				name,38				force,39				public,40			} => {41				let recipients = machines42					.iter()43					.map(|m| config.recipient(m))44					.collect::<Result<Vec<_>>>()?;4546				let secret = {47					let mut input = vec![];48					io::stdin().read_to_end(&mut input)?;4950					let mut encrypted = vec![];51					let recipients = recipients52						.iter()53						.cloned()54						.map(|r| Box::new(r) as Box<dyn age::Recipient>)55						.collect();56					let mut encryptor =57						age::Encryptor::with_recipients(recipients).wrap_output(&mut encrypted)?;58					io::copy(&mut Cursor::new(input), &mut encryptor)?;59					ascii85::encode(&encrypted)60				};6162				let mut data = config.data_mut();63				if data.secret.contains_key(&name) && !force {64					bail!("secret already defined");65				}66				data.secret.insert(67					name,68					FleetSecret {69						owners: machines,70						expire_at: None,71						secret,72						public,73					},74				);75			}76		}77		Ok(())78	}79}
after · src/cmds/secrets/mod.rs
1use crate::{fleetdata::FleetSecret, host::Config};2use anyhow::{bail, Result};3use clap::Clap;4use std::io::{self, Cursor, Read};56#[derive(Clap)]7pub enum Secrets {8	/// Force load keys for all defined hosts9	ForceKeys,10	/// Add secret, data should be provided in stdin11	Add {12		/// Secret name13		name: String,14		/// Secret owners15		machines: Vec<String>,16		/// Override secret if already present17		#[clap(long)]18		force: bool,19		#[clap(long)]20		public: Option<String>,21	},22}2324impl Secrets {25	pub fn run(self, config: &Config) -> Result<()> {26		match self {27			Secrets::ForceKeys => {28				for host in config.list_hosts()? {29					if config.should_skip(&host) {30						continue;31					}32					config.key(&host)?;33				}34			}35			Secrets::Add {36				machines,37				name,38				force,39				public,40			} => {41				let recipients = machines42					.iter()43					.map(|m| config.recipient(m))44					.collect::<Result<Vec<_>>>()?;4546				let secret = {47					let mut input = vec![];48					io::stdin().read_to_end(&mut input)?;4950					let mut encrypted = vec![];51					let recipients = recipients52						.iter()53						.cloned()54						.map(|r| Box::new(r) as Box<dyn age::Recipient>)55						.collect();56					let mut encryptor =57						age::Encryptor::with_recipients(recipients).wrap_output(&mut encrypted)?;58					io::copy(&mut Cursor::new(input), &mut encryptor)?;59					encryptor.finish()?;60					encrypted61				};6263				let mut data = config.data_mut();64				if data.secrets.contains_key(&name) && !force {65					bail!("secret already defined");66				}67				data.secrets.insert(68					name,69					FleetSecret {70						owners: machines,71						expire_at: None,72						secret,73						public,74					},75				);76			}77		}78		Ok(())79	}80}
modifiedsrc/fleetdata.rsdiffbeforeafterboth
--- a/src/fleetdata.rs
+++ b/src/fleetdata.rs
@@ -1,5 +1,5 @@
 use chrono::{DateTime, Utc};
-use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
 use std::collections::BTreeMap;
 
 #[derive(Serialize, Deserialize, Default)]
@@ -16,7 +16,7 @@
 	pub hosts: BTreeMap<String, HostData>,
 	#[serde(default)]
 	#[serde(skip_serializing_if = "BTreeMap::is_empty")]
-	pub secret: BTreeMap<String, FleetSecret>,
+	pub secrets: BTreeMap<String, FleetSecret>,
 }
 
 #[derive(Serialize, Deserialize)]
@@ -28,5 +28,22 @@
 	pub expire_at: Option<DateTime<Utc>>,
 	#[serde(skip_serializing_if = "Option::is_none")]
 	pub public: Option<String>,
-	pub secret: String,
+	#[serde(serialize_with = "as_z85", deserialize_with = "from_z85")]
+	pub secret: Vec<u8>,
+}
+
+fn as_z85<S>(key: &[u8], serializer: S) -> Result<S::Ok, S::Error>
+where
+	S: Serializer,
+{
+	serializer.serialize_str(&z85::encode(&key))
+}
+
+fn from_z85<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
+where
+	D: Deserializer<'de>,
+{
+	use serde::de::Error;
+	String::deserialize(deserializer)
+		.and_then(|string| z85::decode(&string).map_err(|err| Error::custom(err.to_string())))
 }
modifiedsrc/host.rsdiffbeforeafterboth
--- a/src/host.rs
+++ b/src/host.rs
@@ -68,8 +68,6 @@
 		let mut str = self.directory.as_os_str().to_owned();
 		str.push("#");
 		str.push(attr_name);
-
-		println!("{:?}", str);
 		str
 	}
 
deletedsrc/nixlike.rsdiffbeforeafterboth

no changes