Files
bft-crdt-experiment/side-node/src/init.rs

114 lines
3.1 KiB
Rust
Raw Normal View History

2024-06-06 14:39:50 +01:00
use std::{
fs::File,
io::Write,
path::{Path, PathBuf},
};
use pem::Pem;
use bft_json_crdt::keypair::{make_keypair, KeyPair};
const KEY_FILE: &str = "keys.pem";
const CONFIG_FILE: &str = "config.toml";
2024-06-06 13:54:15 +01:00
pub(crate) fn init(home: PathBuf) -> Result<(), std::io::Error> {
2024-06-06 15:30:47 +01:00
ensure_side_directory_exists(&home)?;
2024-06-06 14:39:50 +01:00
let (key_path, config_path) = side_path(home.clone());
2024-06-06 14:39:50 +01:00
let pem = create_pem();
write_pem(key_path, pem)?;
let mut file = File::create(config_path)?;
2024-06-06 13:54:15 +01:00
2024-06-06 14:39:50 +01:00
Ok(())
}
/// Ensures that the directory at side_dir exists, so we have a place
/// to store our key file and config file.
2024-06-06 15:30:47 +01:00
fn ensure_side_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)
2024-06-06 14:39:50 +01:00
}
2024-06-06 13:54:15 +01:00
2024-06-06 14:39:50 +01:00
/// Writes a PEM-encoded string to a file at key_path.
fn write_pem(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
println!("Writing key to: {:?}", key_path);
2024-06-06 14:39:50 +01:00
let mut file = File::create(key_path)?;
file.write(pem.to_string().as_bytes())?;
2024-06-06 13:54:15 +01:00
Ok(())
}
2024-06-06 14:39:50 +01:00
/// 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 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)
2024-06-06 14:39:50 +01:00
}
2024-06-06 13:54:15 +01:00
#[cfg(test)]
mod tests {
use std::fs;
2024-06-06 14:39:50 +01:00
use super::*;
2024-06-06 13:54:15 +01:00
#[test]
fn creates_side_node_directory() {
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());
2024-06-06 13:54:15 +01:00
assert!(std::path::Path::new(node_dir).exists());
}
2024-06-06 14:39:50 +01:00
#[test]
fn creates_key_file() {
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());
2024-06-06 14:39:50 +01:00
}
2024-06-06 13:54:15 +01:00
#[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());
}
2024-05-29 16:47:35 +01:00
}