Skip to content

Commit

Permalink
Merge pull request #31 from wa5i/rustfmt
Browse files Browse the repository at this point in the history
Update rustfmt.toml
  • Loading branch information
InfoHunter authored Dec 1, 2023
2 parents d77aeaa + de99c3d commit c0884ac
Show file tree
Hide file tree
Showing 52 changed files with 1,754 additions and 1,321 deletions.
2 changes: 1 addition & 1 deletion bin/rusty_vault.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::process::ExitCode;

use clap::{Command};
use clap::Command;
use rusty_vault::cli;

fn main() -> ExitCode {
Expand Down
27 changes: 12 additions & 15 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
edition = "2021"

# We are going to enable these after switching to nightly tool chain
#comment_width = 100
#binop_separator = "Front"
#format_strings = true
#max_width = 100
#merge_derives = true
#imports_granularity = "Crate"
#newline_style = "Unix"
#merge_imports = true
#normalize_comments = true
#normalize_doc_attributes = true
#reorder_imports = true
#report_fixme = "Always"
#report_todo = "Always"
#trailing_comma = "Vertical"
#use_field_init_shorthand = true
binop_separator = "Front"
format_strings = true
max_width = 120
comment_width = 120
merge_derives = false
reorder_imports = true
use_field_init_shorthand = true
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
newline_style = "Unix"
trailing_comma = "Vertical"
use_small_heuristics = "Max"
31 changes: 13 additions & 18 deletions src/cli/command/server.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use std::{
env,
fs,
default::Default,
env, fs,
path::Path,
sync::{Arc, RwLock}
sync::{Arc, RwLock},
};
use clap::{ArgMatches};

use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use clap::ArgMatches;
use sysexits::ExitCode;
use actix_web::{
middleware, web, App, HttpResponse, HttpServer
};

use crate::{
http,
errors::RvError,
EXIT_CODE_OK, EXIT_CODE_INSUFFICIENT_PARAMS, EXIT_CODE_LOAD_CONFIG_FAILURE,
cli::config,
storage::{physical, barrier_aes_gcm},
core::Core
core::Core,
errors::RvError,
http,
storage::{barrier_aes_gcm, physical},
EXIT_CODE_INSUFFICIENT_PARAMS, EXIT_CODE_LOAD_CONFIG_FAILURE, EXIT_CODE_OK,
};

pub const WORK_DIR_PATH_DEFAULT: &str = "/tmp/rusty_vault";
Expand Down Expand Up @@ -107,11 +106,7 @@ pub fn main(config_path: &str) -> Result<(), RvError> {

let barrier = barrier_aes_gcm::AESGCMBarrier::new(Arc::clone(&backend));

let core = Arc::new(RwLock::new(Core {
physical: backend,
barrier: Arc::new(barrier),
..Default::default()
}));
let core = Arc::new(RwLock::new(Core { physical: backend, barrier: Arc::new(barrier), ..Default::default() }));

{
let mut c = core.write()?;
Expand Down Expand Up @@ -148,7 +143,7 @@ pub fn execute(matches: &ArgMatches) -> ExitCode {
println!("server error: {:?}", e);
EXIT_CODE_LOAD_CONFIG_FAILURE
}
}
};
}

return EXIT_CODE_INSUFFICIENT_PARAMS;
Expand Down
6 changes: 3 additions & 3 deletions src/cli/command/status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{ArgMatches};
use clap::ArgMatches;
use sysexits::ExitCode;
use crate::{EXIT_CODE_OK, EXIT_CODE_INSUFFICIENT_PARAMS};
use crate::errors::RvError;

use crate::{errors::RvError, EXIT_CODE_INSUFFICIENT_PARAMS, EXIT_CODE_OK};

pub fn main() -> Result<(), RvError> {
println!("status: ok");
Expand Down
94 changes: 45 additions & 49 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::{
fs,
path::Path,
collections::HashMap,
};
use serde::{Serialize, Deserialize, Deserializer};
use serde_json::{Value};
use crate::{
errors::RvError,
};
use std::{collections::HashMap, fs, path::Path};

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;

use crate::errors::RvError;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
Expand Down Expand Up @@ -52,7 +48,7 @@ pub struct Storage {

fn parse_bool_string<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
D: Deserializer<'de>,
{
let value: Value = Deserialize::deserialize(deserializer)?;
match value {
Expand All @@ -68,7 +64,7 @@ D: Deserializer<'de>,

fn validate_storage<'de, D>(deserializer: D) -> Result<HashMap<String, Storage>, D::Error>
where
D: serde::Deserializer<'de>,
D: serde::Deserializer<'de>,
{
let storage: HashMap<String, Storage> = Deserialize::deserialize(deserializer)?;

Expand All @@ -83,7 +79,7 @@ D: serde::Deserializer<'de>,

fn validate_listener<'de, D>(deserializer: D) -> Result<HashMap<String, Listener>, D::Error>
where
D: serde::Deserializer<'de>,
D: serde::Deserializer<'de>,
{
let listener: HashMap<String, Listener> = Deserialize::deserialize(deserializer)?;

Expand Down Expand Up @@ -191,10 +187,10 @@ fn set_config_type_field(config: &mut Config) -> Result<(), RvError> {

#[cfg(test)]
mod test {
use std::env;
use std::fs;
use std::io::prelude::*;
use std::{env, fs, io::prelude::*};

use go_defer::defer;

use super::*;

fn write_file(path: &str, config: &str) -> Result<(), RvError> {
Expand All @@ -212,8 +208,8 @@ mod test {
let dir = env::temp_dir().join("rusty_vault_config_test");
assert!(fs::create_dir(&dir).is_ok());
defer! (
assert!(fs::remove_dir_all(&dir).is_ok());
);
assert!(fs::remove_dir_all(&dir).is_ok());
);

let file_path = dir.join("config.hcl");
let path = file_path.to_str().unwrap_or("config.hcl");
Expand All @@ -233,14 +229,14 @@ mod test {
pid_file = "/tmp/rusty_vault.pid"
"#;

assert!(write_file(path, hcl_config).is_ok());
assert!(write_file(path, hcl_config).is_ok());

let config = load_config(path);
assert!(config.is_ok());
let hcl_config = config.unwrap();
println!("hcl config: {:?}", hcl_config);
let config = load_config(path);
assert!(config.is_ok());
let hcl_config = config.unwrap();
println!("hcl config: {:?}", hcl_config);

let json_config = r#"{
let json_config = r#"{
"storage": {
"file": {
"path": "./vault/data"
Expand All @@ -257,32 +253,32 @@ mod test {
"pid_file": "/tmp/rusty_vault.pid"
}"#;

let file_path = dir.join("config.json");
let path = file_path.to_str().unwrap_or("config.json");
assert!(write_file(path, json_config).is_ok());
let file_path = dir.join("config.json");
let path = file_path.to_str().unwrap_or("config.json");
assert!(write_file(path, json_config).is_ok());

let config = load_config(path);
assert!(config.is_ok());
let json_config = config.unwrap();
println!("json config: {:?}", json_config);
let config = load_config(path);
assert!(config.is_ok());
let json_config = config.unwrap();
println!("json config: {:?}", json_config);

let hcl_config_value = serde_json::to_value(&hcl_config);
assert!(hcl_config_value.is_ok());
let hcl_config_value: Value = hcl_config_value.unwrap();
let hcl_config_value = serde_json::to_value(&hcl_config);
assert!(hcl_config_value.is_ok());
let hcl_config_value: Value = hcl_config_value.unwrap();

let json_config_value = serde_json::to_value(&json_config);
assert!(json_config_value.is_ok());
let json_config_value: Value = json_config_value.unwrap();
assert_eq!(hcl_config_value, json_config_value);
let json_config_value = serde_json::to_value(&json_config);
assert!(json_config_value.is_ok());
let json_config_value: Value = json_config_value.unwrap();
assert_eq!(hcl_config_value, json_config_value);
}

#[test]
fn test_load_config_dir() {
let dir = env::temp_dir().join("rusty_vault_config_dir_test");
assert!(fs::create_dir(&dir).is_ok());
defer! (
assert!(fs::remove_dir_all(&dir).is_ok());
);
assert!(fs::remove_dir_all(&dir).is_ok());
);

let file_path = dir.join("config1.hcl");
let path = file_path.to_str().unwrap_or("config1.hcl");
Expand All @@ -303,12 +299,12 @@ mod test {
pid_file = "/tmp/rusty_vault.pid"
"#;

assert!(write_file(path, hcl_config).is_ok());
assert!(write_file(path, hcl_config).is_ok());

let file_path = dir.join("config2.hcl");
let path = file_path.to_str().unwrap_or("config2.hcl");
let file_path = dir.join("config2.hcl");
let path = file_path.to_str().unwrap_or("config2.hcl");

let hcl_config = r#"
let hcl_config = r#"
storage "file" {
address = "127.0.0.1:8899"
}
Expand All @@ -321,11 +317,11 @@ mod test {
log_level = "info"
"#;

assert!(write_file(path, hcl_config).is_ok());
assert!(write_file(path, hcl_config).is_ok());

let config = load_config(dir.to_str().unwrap());
assert!(config.is_ok());
let hcl_config = config.unwrap();
println!("hcl config: {:?}", hcl_config);
let config = load_config(dir.to_str().unwrap());
assert!(config.is_ok());
let hcl_config = config.unwrap();
println!("hcl config: {:?}", hcl_config);
}
}
35 changes: 15 additions & 20 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use clap::{Arg, ArgAction, ArgMatches, Command};
use sysexits::ExitCode;
use clap::{Arg, ArgMatches, ArgAction, Command};

pub mod command;
pub mod config;

/// Defines command line options
pub fn define_command_line_options(mut app: Command) -> Command {
app = app
.subcommands([
Command::new("server")
.about("Start a rusty_vault server")
.arg(
Arg::new("config")
.short('c')
.long("config")
.value_name("CONFIG")
.num_args(1)
.action(ArgAction::Set)
.required(true)
.help("[CONFIG] Path to a configuration file or directory of configuration files.")),
Command::new("status")
.about("Print seal and HA status")
]);
app = app.subcommands([
Command::new("server").about("Start a rusty_vault server").arg(
Arg::new("config")
.short('c')
.long("config")
.value_name("CONFIG")
.num_args(1)
.action(ArgAction::Set)
.required(true)
.help("[CONFIG] Path to a configuration file or directory of configuration files."),
),
Command::new("status").about("Print seal and HA status"),
]);

app
}
Expand All @@ -31,8 +28,6 @@ pub fn run(matches: &ArgMatches) -> ExitCode {
match matches.subcommand() {
Some(("server", server_matches)) => command::server::execute(&server_matches),
Some(("status", status_matches)) => command::status::execute(&status_matches),
_ => {
crate::EXIT_CODE_INSUFFICIENT_PARAMS
}
_ => crate::EXIT_CODE_INSUFFICIENT_PARAMS,
}
}
14 changes: 7 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::any::Any;
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::{
any::Any,
cell::RefCell,
collections::HashMap,
sync::{Arc, Mutex},
};

pub struct Context {
data_map: Mutex<HashMap<String, Arc<RefCell<dyn Any>>>>,
}

impl Context {
pub fn new() -> Self {
Self {
data_map: Mutex::new(HashMap::new()),
}
Self { data_map: Mutex::new(HashMap::new()) }
}

pub fn set(&self, key: &str, data: Arc<RefCell<dyn Any>>) {
Expand Down
Loading

0 comments on commit c0884ac

Please sign in to comment.