From 16fe760e10fb9fa10a902e7138afd3774efdea23 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 6 Jun 2024 14:39:50 +0100 Subject: [PATCH] Writing pem files to home directory --- Cargo.lock | 11 +++++++ side-node/Cargo.toml | 1 + side-node/src/init.rs | 68 ++++++++++++++++++++++++++++++++++--------- side-node/src/main.rs | 2 +- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14a24d7..0e785eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1517,6 +1517,16 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "pem" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e459365e590736a54c3fa561947c84837534b8e9af6fc5bf781307e82658fae" +dependencies = [ + "base64 0.22.1", + "serde", +] + [[package]] name = "pem-rfc7468" version = "0.6.0" @@ -2145,6 +2155,7 @@ dependencies = [ "bft-crdt-derive", "bft-json-crdt", "clap", + "pem", "serde", "serde_json", "serde_with 3.8.1", diff --git a/side-node/Cargo.toml b/side-node/Cargo.toml index 2168084..fa1f67f 100644 --- a/side-node/Cargo.toml +++ b/side-node/Cargo.toml @@ -17,6 +17,7 @@ serde_with = "3.8.1" sha256 = "1.5.0" tokio = { version = "1.37.0", features = ["full"] } websockets = "0.3.0" +pem = "3.0.4" [features] default = ["bft", "logging-list", "logging-json"] diff --git a/side-node/src/init.rs b/side-node/src/init.rs index 89ba880..676891d 100644 --- a/side-node/src/init.rs +++ b/side-node/src/init.rs @@ -1,34 +1,74 @@ -use std::{path::Path}; +use std::{ + fs::File, + io::Write, + path::{Path, PathBuf}, +}; + +use pem::Pem; + +use bft_json_crdt::keypair::{make_keypair, KeyPair}; + +const KEY_FILE: &str = ".side/node/keys.pem"; pub(crate) fn init() -> Result<(), std::io::Error> { println!("Initializing Side Node"); - let key_path = Path::new("~/.side/node/keys.pem"); + let home = std::env::var("HOME").expect("couldn't read home directory env variable"); + let key_path = key_path(home); - if let Some(parent_dir) = key_path.parent() { - println!("Creating parent directory: {:?}", parent_dir); - std::fs::create_dir_all(parent_dir)?; - } + ensure_directory_exists(&key_path)?; - // let mut file = File::create(key_path)?; + let pem = create_pem(); + write(key_path, pem)?; Ok(()) } +fn ensure_directory_exists(key_path: &PathBuf) -> Result<(), std::io::Error> { + Ok(if let Some(parent_dir) = key_path.parent() { + println!("Creating parent directory: {:?}", parent_dir); + std::fs::create_dir_all(parent_dir)?; + }) +} + +/// Writes a PEM-encoded string to a file at key_path. +fn write(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> { + let mut file = File::create(key_path)?; + file.write(pem.to_string().as_bytes())?; + Ok(()) +} + +/// Makes an Ed25519 keypair and returns the public and private keys as a PEM-encoded string. +fn create_pem() -> String { + let keys = make_keypair(); + + let public_pem = Pem::new("PUBLIC", keys.public().to_string()); + let private_pem = Pem::new("PRIVATE", keys.private().to_string()); + pem::encode_many(&[public_pem, private_pem]) +} + +/// Returns the path to the key file for this host OS. +fn key_path(home: String) -> PathBuf { + let key_path = format!("{home}/.{KEY_FILE}"); + let path = Path::new(&key_path).to_owned(); + path +} + #[cfg(test)] mod tests { - + use super::*; #[test] fn creates_side_node_directory() { - let node_dir = "~/.side/node"; + let node_dir = Path::new(KEY_FILE).parent().unwrap().to_str().unwrap(); + let _ = init(); assert!(std::path::Path::new(node_dir).exists()); } - // #[test] - // fn creates_stable_key_file() { - // let keyfile = "~/.side/node/keys.pem"; - // assert!(std::path::Path::new(keyfile).exists()); - // } + #[test] + fn creates_key_file() { + let _ = init(); + assert!(std::path::Path::new(KEY_FILE).exists()); + } // #[test] // fn creates_config_file() {} diff --git a/side-node/src/main.rs b/side-node/src/main.rs index 603aa48..7c1ee2b 100644 --- a/side-node/src/main.rs +++ b/side-node/src/main.rs @@ -11,7 +11,7 @@ async fn main() { match &args.command { Some(Commands::Init {}) => { - init::init(); + let _ = init::init(); } Some(Commands::Run {}) => { let (mut bft_crdt, keys) = list_transaction_crdt::new();