From 10cdbce7460fb5da82f3aeab9b1f42450e87a389 Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Sat, 15 Apr 2023 13:55:25 +0200 Subject: [PATCH] Move filesystem-related stuff into the config module --- src/config.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- src/keys.rs | 25 ++++--------------------- src/main.rs | 8 ++++---- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/config.rs b/src/config.rs index a44c634..4689adf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,10 @@ use directories::ProjectDirs; use gethostname::gethostname; +use std::{io::Write, path::Path}; +use crate::keys::Key; use once_cell::sync::OnceCell; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; const QUALIFIER: &str = "com"; const ORGANIZATION: &str = "Ero-sennin"; @@ -41,19 +43,52 @@ impl MachineConfig { Ok(()) } - pub(crate) fn path(&self) -> &Path { + pub(crate) fn username(&self) -> anyhow::Result { + Ok(std::fs::read_to_string(self.username_path())?) + } + + pub(crate) fn token(&self) -> anyhow::Result { + Ok(std::fs::read_to_string(self.token_path())?) + } + + pub(crate) fn has_key(&self) -> bool { + self.key_path().is_file() + } + + pub(crate) fn key(&self) -> anyhow::Result { + let key_data = std::fs::read_to_string(self.key_path())?; + Key::try_from_base64(key_data.trim_end()) + } + + pub(crate) fn save_username(&self, username: &str) -> anyhow::Result<()> { + std::fs::write(self.username_path(), username)?; + Ok(()) + } + + pub(crate) fn save_token(&self, token: &str) -> anyhow::Result<()> { + std::fs::write(self.token_path(), token)?; + Ok(()) + } + + pub(crate) fn save_key(&self, key: &Key) -> anyhow::Result<()> { + let mut file = std::fs::File::create(self.key_path())?; + writeln!(file, "{}", key.to_base64())?; + Ok(()) + } + + fn path(&self) -> &Path { &self.path } - pub(crate) fn username_path(&self) -> PathBuf { + fn username_path(&self) -> PathBuf { self.path().join("username") } - pub(crate) fn token_path(&self) -> PathBuf { + fn token_path(&self) -> PathBuf { self.path().join("token") } - pub(crate) fn key_path(&self) -> PathBuf { + fn key_path(&self) -> PathBuf { self.path().join("key") } } diff --git a/src/keys.rs b/src/keys.rs index 8e61062..2ce6f3a 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -1,9 +1,6 @@ use base64::prelude::{Engine as _, BASE64_STANDARD}; -use log::debug; use x25519_dalek::{PublicKey, StaticSecret}; -use std::{io::Write, path::Path}; - use crate::config::MachineConfig; const KEY_SIZE: usize = 32; @@ -12,7 +9,7 @@ const KEY_SIZE: usize = 32; pub(crate) struct Key([u8; KEY_SIZE]); impl Key { - fn try_from_base64(data: &str) -> anyhow::Result { + pub(crate) fn try_from_base64(data: &str) -> anyhow::Result { let mut key_data = [0u8; KEY_SIZE]; let key_bytes = BASE64_STANDARD.decode(data)?; assert_eq!(key_bytes.len(), KEY_SIZE); @@ -23,17 +20,6 @@ impl Key { pub(crate) fn to_base64(&self) -> String { BASE64_STANDARD.encode(self.0) } - - fn load(path: &Path) -> anyhow::Result { - let key_data = std::fs::read_to_string(path)?; - Self::try_from_base64(key_data.trim_end()) - } - - fn save(&self, path: &Path) -> anyhow::Result<()> { - let mut file = std::fs::File::create(path)?; - writeln!(file, "{}", self.to_base64())?; - Ok(()) - } } #[derive(Debug)] @@ -43,14 +29,11 @@ pub(crate) struct WireguardKeyPair { } pub(crate) fn get_keys(machine_config: &MachineConfig) -> Result { - let key_path = machine_config.key_path(); - debug!("key path = {:?}", &key_path); - - let private_key = if key_path.is_file() { - Key::load(&key_path)? + let private_key = if machine_config.has_key() { + machine_config.key()? } else { let key = generate_private_key(); - key.save(&key_path)?; + machine_config.save_key(&key)?; key }; diff --git a/src/main.rs b/src/main.rs index 1f3c03c..7fa8d46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,8 +76,8 @@ fn main() -> Result<(), anyhow::Error> { fn login(opts: &Opts, login_opts: &LoginOpts) -> Result<(), anyhow::Error> { let machine_config = config::MachineConfig::new(opts.machine.as_ref())?; - std::fs::write(machine_config.username_path(), &login_opts.username)?; - std::fs::write(machine_config.token_path(), &login_opts.token)?; + machine_config.save_username(&login_opts.username)?; + machine_config.save_token(&login_opts.token)?; Ok(()) } @@ -131,9 +131,9 @@ fn get_config(opts: &Opts, config_opts: &ConfigOpts) -> Result<(), anyhow::Error .ok_or_else(|| anyhow::anyhow!("no such location: {}", config_opts.location))?; debug!("location = {:?}", &location); let keys = get_keys(&machine_config)?; - let username = std::fs::read_to_string(machine_config.username_path())?; - let token = std::fs::read_to_string(machine_config.token_path())?; debug!("keys = {:?}", &keys); + let username = machine_config.username()?; + let token = machine_config.token()?; let addresses = api::add_ip(&username, &token, &keys.public_key)?; write_config(