Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wa5i committed Aug 18, 2024
1 parent 12151d1 commit 44bd136
Show file tree
Hide file tree
Showing 45 changed files with 916 additions and 276 deletions.
8 changes: 2 additions & 6 deletions src/cli/command/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ use openssl::{
use sysexits::ExitCode;

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

pub const WORK_DIR_PATH_DEFAULT: &str = "/tmp/rusty_vault";
Expand Down
8 changes: 1 addition & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ use crate::{
handler::Handler,
logical::{Backend, Request, Response},
module_manager::ModuleManager,
modules::{
auth::AuthModule,
credential::{
userpass::UserPassModule,
},
pki::PkiModule,
},
modules::{auth::AuthModule, credential::userpass::UserPassModule, pki::PkiModule},
mount::MountTable,
router::Router,
shamir::{ShamirSecret, SHAMIR_OVERHEAD},
Expand Down
2 changes: 1 addition & 1 deletion src/http/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
core::Core,
errors::RvError,
http::{request_auth, response_error, response_json_ok, response_ok, Connection},
logical::{Operation, Connection as ReqConnection, Response},
logical::{Connection as ReqConnection, Operation, Response},
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub mod module_manager;
pub mod modules;
pub mod mount;
pub mod router;
#[cfg(feature = "storage_mysql")]
pub mod schema;
pub mod shamir;
pub mod storage;
pub mod utils;
#[cfg(feature = "storage_mysql")]
pub mod schema;

/// Exit ok
pub const EXIT_CODE_OK: sysexits::ExitCode = sysexits::ExitCode::Ok;
Expand Down
16 changes: 8 additions & 8 deletions src/logical/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use std::{collections::HashMap, sync::Arc};
use regex::Regex;
use serde_json::{Map, Value};

use super::{path::Path, request::Request, response::Response, secret::Secret, FieldType, Backend, Operation};
use crate::{
context::Context, errors::RvError
};
use super::{path::Path, request::Request, response::Response, secret::Secret, Backend, FieldType, Operation};
use crate::{context::Context, errors::RvError};

type BackendOperationHandler = dyn Fn(&dyn Backend, &mut Request) -> Result<Option<Response>, RvError> + Send + Sync;

Expand Down Expand Up @@ -262,9 +260,8 @@ mod test {

use super::*;
use crate::{
logical::{Field, field::FieldTrait, FieldType, PathOperation},
new_fields, new_fields_internal, new_path, new_path_internal, new_secret, new_secret_internal,
storage,
logical::{field::FieldTrait, Field, FieldType, PathOperation},
new_fields, new_fields_internal, new_path, new_path_internal, new_secret, new_secret_internal, storage,
};

struct MyTest;
Expand Down Expand Up @@ -440,7 +437,10 @@ mod test {
"mytype": 1,
"mypath": "/pp",
"mypassword": "123qwe",
}).as_object().unwrap().clone();
})
.as_object()
.unwrap()
.clone();
req.body = Some(body);
req.storage = Some(Arc::new(barrier));
assert!(logical_backend.handle_request(&mut req).is_ok());
Expand Down
5 changes: 1 addition & 4 deletions src/logical/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ pub struct Connection {

impl Default for Connection {
fn default() -> Self {
Self {
peer_addr: String::new(),
peer_tls_cert: None,
}
Self { peer_addr: String::new(), peer_tls_cert: None }
}
}
64 changes: 37 additions & 27 deletions src/logical/field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{fmt, time::Duration, collections::HashMap};
use std::{collections::HashMap, fmt, time::Duration};

use enum_map::Enum;
use humantime::parse_duration;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use strum::{Display, EnumString};
use humantime::parse_duration;

use crate::errors::RvError;

Expand Down Expand Up @@ -149,7 +149,7 @@ impl FieldTrait for Value {

fn as_duration(&self) -> Option<Duration> {
if let Some(secs) = self.as_u64() {
return Some(Duration::from_secs(secs))
return Some(Duration::from_secs(secs));
}

if let Some(secs_str) = self.as_str() {
Expand Down Expand Up @@ -225,12 +225,7 @@ impl FieldTrait for Value {

impl Field {
pub fn new() -> Self {
Self {
required: false,
field_type: FieldType::Str,
default: json!(null),
description: String::new(),
}
Self { required: false, field_type: FieldType::Str, default: json!(null), description: String::new() }
}

pub fn check_data_type(&self, data: &Value) -> bool {
Expand All @@ -250,22 +245,22 @@ impl Field {
match &self.field_type {
FieldType::SecretStr | FieldType::Str => {
return Ok(json!(""));
},
}
FieldType::Int => {
return Ok(json!(0));
},
}
FieldType::Bool => {
return Ok(json!(false));
},
}
FieldType::Array => {
return Ok(json!([]));
},
}
FieldType::Map => {
return Ok(serde_json::from_str("{}")?);
},
}
FieldType::DurationSecond => {
return Ok(json!(0));
},
}
FieldType::CommaStringSlice => {
return Ok(json!([]));
}
Expand All @@ -279,21 +274,21 @@ impl Field {
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::Int => {
if self.default.is_i64() {
return Ok(self.default.clone());
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::Bool => {
if self.default.is_boolean() {
return Ok(self.default.clone());
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::Array => {
if self.default.is_array() {
return Ok(self.default.clone());
Expand All @@ -306,7 +301,7 @@ impl Field {
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::Map => {
if self.default.is_object() {
return Ok(self.default.clone());
Expand All @@ -319,14 +314,14 @@ impl Field {
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::DurationSecond => {
if self.default.is_duration() {
return Ok(self.default.clone());
}

return Err(RvError::ErrRustDowncastFailed);
},
}
FieldType::CommaStringSlice => {
if self.default.is_comma_string_slice() {
return Ok(self.default.clone());
Expand Down Expand Up @@ -443,23 +438,38 @@ mod test {

val = json!("aa, bb, cc ,dd");
assert!(val.is_comma_string_slice());
assert_eq!(val.as_comma_string_slice(), Some(vec!["aa".to_string(), "bb".to_string(), "cc".to_string(), "dd".to_string()]));
assert_eq!(
val.as_comma_string_slice(),
Some(vec!["aa".to_string(), "bb".to_string(), "cc".to_string(), "dd".to_string()])
);

val = json!(["aaa", " bbb", "ccc " , " ddd"]);
val = json!(["aaa", " bbb", "ccc ", " ddd"]);
assert!(val.is_comma_string_slice());
assert_eq!(val.as_comma_string_slice(), Some(vec!["aaa".to_string(), "bbb".to_string(), "ccc".to_string(), "ddd".to_string()]));
assert_eq!(
val.as_comma_string_slice(),
Some(vec!["aaa".to_string(), "bbb".to_string(), "ccc".to_string(), "ddd".to_string()])
);

val = json!([11, 22, 33, 44]);
assert!(val.is_comma_string_slice());
assert_eq!(val.as_comma_string_slice(), Some(vec!["11".to_string(), "22".to_string(), "33".to_string(), "44".to_string()]));
assert_eq!(
val.as_comma_string_slice(),
Some(vec!["11".to_string(), "22".to_string(), "33".to_string(), "44".to_string()])
);

val = json!([11, "aa22", 33, 44]);
assert!(val.is_comma_string_slice());
assert_eq!(val.as_comma_string_slice(), Some(vec!["11".to_string(), "aa22".to_string(), "33".to_string(), "44".to_string()]));
assert_eq!(
val.as_comma_string_slice(),
Some(vec!["11".to_string(), "aa22".to_string(), "33".to_string(), "44".to_string()])
);

val = json!("aa, bb, cc ,dd, , 88,");
assert!(val.is_comma_string_slice());
assert_eq!(val.as_comma_string_slice(), Some(vec!["aa".to_string(), "bb".to_string(), "cc".to_string(), "dd".to_string(), "88".to_string()]));
assert_eq!(
val.as_comma_string_slice(),
Some(vec!["aa".to_string(), "bb".to_string(), "cc".to_string(), "dd".to_string(), "88".to_string()])
);

let mut map: HashMap<String, String> = HashMap::new();
map.insert("k1".to_string(), "v1".to_string());
Expand Down
6 changes: 2 additions & 4 deletions src/logical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use enum_map::Enum;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};

use crate::{
context::Context, errors::RvError,
};
use crate::{context::Context, errors::RvError};

pub mod auth;
pub mod backend;
Expand All @@ -33,13 +31,13 @@ pub mod secret;

pub use auth::Auth;
pub use backend::{LogicalBackend, CTX_KEY_BACKEND_PATH};
pub use connection::Connection;
pub use field::{Field, FieldType};
pub use lease::Lease;
pub use path::{Path, PathOperation};
pub use request::Request;
pub use response::Response;
pub use secret::{Secret, SecretData};
pub use connection::Connection;

#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumString, Display, Enum, Serialize, Deserialize)]
pub enum Operation {
Expand Down
12 changes: 8 additions & 4 deletions src/logical/path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{collections::HashMap, fmt, sync::Arc};

use super::{request::Request, response::Response, Backend, Field, Operation};
use crate::{
context::Context, errors::RvError
};
use crate::{context::Context, errors::RvError};

type PathOperationHandler = dyn Fn(&dyn Backend, &mut Request) -> Result<Option<Response>, RvError> + Send + Sync;

Expand All @@ -30,7 +28,13 @@ impl fmt::Debug for PathOperation {

impl Path {
pub fn new(pattern: &str) -> Self {
Self { ctx: Arc::new(Context::new()), pattern: pattern.to_string(), fields: HashMap::new(), operations: Vec::new(), help: String::new() }
Self {
ctx: Arc::new(Context::new()),
pattern: pattern.to_string(),
fields: HashMap::new(),
operations: Vec::new(),
help: String::new(),
}
}

pub fn get_field(&self, key: &str) -> Option<Arc<Field>> {
Expand Down
9 changes: 4 additions & 5 deletions src/logical/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ impl Request {
}

pub fn get_data_as_str(&self, key: &str) -> Result<String, RvError> {
self.get_data(key)?
.as_str()
.ok_or(RvError::ErrRequestFieldInvalid)
.and_then(|s| if s.trim().is_empty() {
self.get_data(key)?.as_str().ok_or(RvError::ErrRequestFieldInvalid).and_then(|s| {
if s.trim().is_empty() {
Err(RvError::ErrResponse(format!("missing {}", key)))
} else {
Ok(s.trim().to_string())
})
}
})
}

pub fn get_field_default_or_zero(&self, key: &str) -> Result<Value, RvError> {
Expand Down
19 changes: 15 additions & 4 deletions src/logical/response.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashMap;

use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use lazy_static::lazy_static;

use crate::{
errors::RvError,
logical::{Auth, secret::SecretData},
logical::{secret::SecretData, Auth},
};

lazy_static! {
Expand All @@ -32,7 +32,15 @@ pub struct Response {

impl Default for Response {
fn default() -> Self {
Response { request_id: String::new(), headers: None, data: None, auth: None, secret: None, redirect: String::new(), warnings: Vec::new(), }
Response {
request_id: String::new(),
headers: None,
data: None,
auth: None,
secret: None,
redirect: String::new(),
warnings: Vec::new(),
}
}
}

Expand Down Expand Up @@ -98,7 +106,10 @@ impl Response {
let mut data: Map<String, Value> = json!({
HTTP_CONTENT_TYPE.to_string(): "application/json",
HTTP_STATUS_CODE.to_string(): code,
}).as_object().unwrap().clone();
})
.as_object()
.unwrap()
.clone();

if let Some(response) = resp {
let raw_body = serde_json::to_value(response).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/modules/auth/token_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use super::{
AUTH_ROUTER_PREFIX,
};
use crate::{
context::Context,
core::Core,
context::Context, errors::RvError,
errors::RvError,
handler::Handler,
logical::{
Auth, Backend, Field, FieldType, Lease, LogicalBackend, Operation, Path, PathOperation, Request, Response,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/credential/userpass/path_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{collections::HashMap, sync::Arc};

use super::{UserPassBackend, UserPassBackendInner};
use crate::{
context::Context, errors::RvError,
context::Context,
errors::RvError,
logical::{Auth, Backend, Field, FieldType, Lease, Operation, Path, PathOperation, Request, Response},
new_fields, new_fields_internal, new_path, new_path_internal,
};
Expand Down
Loading

0 comments on commit 44bd136

Please sign in to comment.