Writing pem and config file during node init
This commit is contained in:
@@ -10,6 +10,7 @@ base64 = "0.21.7"
|
||||
bft-json-crdt = { path = "../crates/bft-json-crdt" }
|
||||
bft-crdt-derive = { path = "../crates/bft-json-crdt/bft-crdt-derive" }
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
dirs = "5.0.1"
|
||||
# serde_cbor = "0.11.2" # move to this once we need to pack things in CBOR
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.117"
|
||||
|
||||
@@ -8,29 +8,35 @@ use pem::Pem;
|
||||
|
||||
use bft_json_crdt::keypair::{make_keypair, KeyPair};
|
||||
|
||||
const KEY_FILE: &str = "side/node/keys.pem";
|
||||
const KEY_FILE: &str = "keys.pem";
|
||||
const CONFIG_FILE: &str = "config.toml";
|
||||
|
||||
pub(crate) fn init(home: String) -> Result<(), std::io::Error> {
|
||||
println!("Initializing Side Node at {home}");
|
||||
let key_path = key_path(home);
|
||||
pub(crate) fn init(home: PathBuf) -> Result<(), std::io::Error> {
|
||||
ensure_directory_exists(&home)?;
|
||||
|
||||
ensure_directory_exists(&key_path)?;
|
||||
let (key_path, config_path) = side_path(home.clone());
|
||||
|
||||
let pem = create_pem();
|
||||
write(key_path, pem)?;
|
||||
write_pem(key_path, pem)?;
|
||||
|
||||
let mut file = File::create(config_path)?;
|
||||
|
||||
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)?;
|
||||
})
|
||||
/// Ensures that the directory at side_dir exists, so we have a place
|
||||
/// to store our key file and config file.
|
||||
fn ensure_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error> {
|
||||
if side_dir.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
println!("Creating side config directory: {:?}", side_dir);
|
||||
std::fs::create_dir_all(side_dir)
|
||||
}
|
||||
|
||||
/// Writes a PEM-encoded string to a file at key_path.
|
||||
fn write(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
|
||||
fn write_pem(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
|
||||
println!("Writing key to: {:?}", key_path);
|
||||
let mut file = File::create(key_path)?;
|
||||
file.write(pem.to_string().as_bytes())?;
|
||||
Ok(())
|
||||
@@ -46,19 +52,31 @@ fn create_pem() -> String {
|
||||
}
|
||||
|
||||
/// 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
|
||||
fn side_path(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
||||
let mut key_path = prefix.clone();
|
||||
key_path.push(KEY_FILE);
|
||||
|
||||
let mut config_path = prefix.clone();
|
||||
config_path.push(CONFIG_FILE);
|
||||
|
||||
(key_path, config_path)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn creates_side_node_directory() {
|
||||
let test_home = format!("/tmp/{KEY_FILE}");
|
||||
let mut test_home = PathBuf::new();
|
||||
let side_dir = "/tmp/side";
|
||||
|
||||
// clean up any previous test runs
|
||||
fs::remove_dir_all(side_dir).expect("couldn't remove side directory during test");
|
||||
|
||||
test_home.push(side_dir);
|
||||
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
||||
let _ = init(test_home.clone());
|
||||
assert!(std::path::Path::new(node_dir).exists());
|
||||
@@ -66,12 +84,30 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn creates_key_file() {
|
||||
let test_home = format!("/tmp/{KEY_FILE}");
|
||||
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
||||
let _ = init(test_home.clone());
|
||||
assert!(std::path::Path::new(node_dir).exists());
|
||||
let mut file_path = PathBuf::new();
|
||||
file_path.push("/tmp/side");
|
||||
let side_dir = file_path.clone();
|
||||
file_path.push(KEY_FILE);
|
||||
|
||||
let _ = init(side_dir.clone());
|
||||
assert!(file_path.exists());
|
||||
|
||||
// check that the pem is readable
|
||||
let pem = fs::read_to_string(file_path).unwrap();
|
||||
assert!(pem.contains("PUBLIC"));
|
||||
assert!(pem.contains("PRIVATE"));
|
||||
|
||||
assert!(pem::parse_many(&pem).is_ok());
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn creates_config_file() {}
|
||||
#[test]
|
||||
fn creates_config_file() {
|
||||
let mut file_path = PathBuf::new();
|
||||
file_path.push("/tmp/side");
|
||||
let side_dir = file_path.clone();
|
||||
file_path.push(CONFIG_FILE);
|
||||
|
||||
let _ = init(side_dir.clone());
|
||||
assert!(file_path.exists());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,7 @@ async fn main() {
|
||||
|
||||
match &args.command {
|
||||
Some(Commands::Init {}) => {
|
||||
let home = std::env::var("HOME").expect("couldn't read home directory env variable");
|
||||
let _ = init::init(home);
|
||||
let _ = init::init(home());
|
||||
}
|
||||
Some(Commands::Run {}) => {
|
||||
let (mut bft_crdt, keys) = list_transaction_crdt::new();
|
||||
@@ -21,3 +20,9 @@ async fn main() {
|
||||
None => println!("No command provided. Exiting. See --help for more information."),
|
||||
}
|
||||
}
|
||||
|
||||
fn home() -> std::path::PathBuf {
|
||||
let mut path = dirs::home_dir().unwrap();
|
||||
path.push(".side");
|
||||
path
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user