Skip to content

Commit

Permalink
cargo fmt (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
wa5i authored Jan 14, 2025
1 parent 6acb4ae commit 718a88d
Show file tree
Hide file tree
Showing 57 changed files with 1,798 additions and 815 deletions.
2 changes: 1 addition & 1 deletion bin/rusty_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//! [documentation site]: https://www.tongsuo.net
use std::process::ExitCode;
use clap::{Parser, CommandFactory};

use clap::{CommandFactory, Parser};
use rusty_vault::cli::Cli;

fn main() -> ExitCode {
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fs, env, path::Path};
use std::{env, fs, path::Path};

use toml::Value;

// This is not going to happen any more since we have a default feature definition in Cargo.toml
Expand Down
84 changes: 29 additions & 55 deletions src/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
use std::{
fs,
io::BufReader,
sync::Arc,
time::Duration,
path::PathBuf,
collections::HashMap,
};
use std::{collections::HashMap, fs, io::BufReader, path::PathBuf, sync::Arc, time::Duration};

use serde_json::{Map, Value};
use better_default::Default;
use ureq::AgentBuilder;
use rustls::{
pki_types::{PrivateKeyDer, pem::PemObject},
ALL_VERSIONS, ClientConfig, RootCertStore,
pki_types::{pem::PemObject, PrivateKeyDer},
ClientConfig, RootCertStore, ALL_VERSIONS,
};
use serde_json::{Map, Value};
use ureq::AgentBuilder;
use webpki_roots::TLS_SERVER_ROOTS;

use super::HttpResponse;

use crate::{
errors::RvError,
utils::cert::DisabledVerifier,
};
use crate::{errors::RvError, utils::cert::DisabledVerifier};

#[derive(Clone)]
pub struct TLSConfig {
Expand Down Expand Up @@ -65,7 +54,11 @@ impl TLSConfigBuilder {
self
}

pub fn with_client_cert_path(mut self, client_cert_path: &PathBuf, client_key_path: &PathBuf) -> Result<Self, RvError> {
pub fn with_client_cert_path(
mut self,
client_cert_path: &PathBuf,
client_key_path: &PathBuf,
) -> Result<Self, RvError> {
let cert_data = fs::read(client_cert_path)?;
self.client_cert_pem = Some(cert_data);

Expand Down Expand Up @@ -99,9 +92,7 @@ impl TLSConfigBuilder {

let builder = if self.insecure {
log::debug!("Certificate verification disabled");
builder
.dangerous()
.with_custom_certificate_verifier(Arc::new(DisabledVerifier))
builder.dangerous().with_custom_certificate_verifier(Arc::new(DisabledVerifier))
} else {
if let Some(server_ca) = &self.server_ca_pem {
let mut cert_reader = BufReader::new(&server_ca[..]);
Expand All @@ -111,26 +102,23 @@ impl TLSConfigBuilder {
let (_added, _ignored) = root_store.add_parsable_certificates(root_certs);
builder.with_root_certificates(root_store)
} else {
let root_store = RootCertStore {
roots: TLS_SERVER_ROOTS.to_vec(),
};
let root_store = RootCertStore { roots: TLS_SERVER_ROOTS.to_vec() };
builder.with_root_certificates(root_store)
}
};

let client_config = if let (Some(client_cert_pem), Some(client_key_pem)) = (&self.client_cert_pem, &self.client_key_pem) {
let mut cert_reader = BufReader::new(&client_cert_pem[..]);
let client_certs = rustls_pemfile::certs(&mut cert_reader).collect::<Result<Vec<_>, _>>()?;
let client_key = PrivateKeyDer::from_pem_slice(client_key_pem)?;
let client_config =
if let (Some(client_cert_pem), Some(client_key_pem)) = (&self.client_cert_pem, &self.client_key_pem) {
let mut cert_reader = BufReader::new(&client_cert_pem[..]);
let client_certs = rustls_pemfile::certs(&mut cert_reader).collect::<Result<Vec<_>, _>>()?;
let client_key = PrivateKeyDer::from_pem_slice(client_key_pem)?;

builder.with_client_auth_cert(client_certs, client_key)?
} else {
builder.with_no_client_auth()
};
builder.with_client_auth_cert(client_certs, client_key)?
} else {
builder.with_no_client_auth()
};

Ok(TLSConfig {
client_config
})
Ok(TLSConfig { client_config })
}
}

Expand Down Expand Up @@ -160,9 +148,7 @@ impl Client {
}

pub fn build(mut self) -> Self {
let mut agent = AgentBuilder::new()
.timeout_connect(Duration::from_secs(10))
.timeout(Duration::from_secs(30));
let mut agent = AgentBuilder::new().timeout_connect(Duration::from_secs(10)).timeout(Duration::from_secs(30));

if let Some(tls_config) = &self.tls_config {
agent = agent.tls_config(Arc::new(tls_config.client_config.clone()));
Expand All @@ -172,12 +158,7 @@ impl Client {
self
}

pub fn request(
&self,
method: &str,
path: &str,
data: Option<Map<String, Value>>
) -> Result<HttpResponse, RvError> {
pub fn request(&self, method: &str, path: &str, data: Option<Map<String, Value>>) -> Result<HttpResponse, RvError> {
let url = if path.starts_with("/") {
format!("{}{}", self.address, path)
} else {
Expand All @@ -192,11 +173,7 @@ impl Client {
req = req.set("X-RustyVault-Token", &self.token);
}

let mut ret = HttpResponse {
method: method.to_string(),
url: url,
..Default::default()
};
let mut ret = HttpResponse { method: method.to_string(), url, ..Default::default() };

let response_result = if let Some(send_data) = data { req.send_json(send_data) } else { req.call() };

Expand Down Expand Up @@ -236,18 +213,15 @@ impl Client {
self.request("GET", path, None)
}

pub fn request_write(&self, path: &str, data: Option<Map<String, Value>>,
) -> Result<HttpResponse, RvError> {
pub fn request_write(&self, path: &str, data: Option<Map<String, Value>>) -> Result<HttpResponse, RvError> {
self.request("POST", path, data)
}

pub fn request_put(&self, path: &str, data: Option<Map<String, Value>>,
) -> Result<HttpResponse, RvError> {
pub fn request_put(&self, path: &str, data: Option<Map<String, Value>>) -> Result<HttpResponse, RvError> {
self.request("PUT", path, data)
}

pub fn request_delete(&self, path: &str, data: Option<Map<String, Value>>,
) -> Result<HttpResponse, RvError> {
pub fn request_delete(&self, path: &str, data: Option<Map<String, Value>>) -> Result<HttpResponse, RvError> {
self.request("DELETE", path, data)
}
}
9 changes: 2 additions & 7 deletions src/api/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use derive_more::Deref;
use serde_json::{Map, Value};

use super::{Client, HttpResponse};

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

#[derive(Deref)]
pub struct Logical<'a> {
Expand All @@ -15,9 +12,7 @@ pub struct Logical<'a> {

impl Client {
pub fn logical(&self) -> Logical {
Logical {
client: self
}
Logical { client: self }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use serde_json::Value;

pub mod auth;
pub mod client;
pub mod sys;
pub mod logical;
pub mod secret;
pub mod auth;
pub mod sys;

pub use client::Client;

Expand Down
3 changes: 2 additions & 1 deletion src/api/secret.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;

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

Expand Down Expand Up @@ -38,4 +39,4 @@ pub struct SecretAuth {
pub lease_duration: u32,
#[serde(default)]
pub renewable: bool,
}
}
16 changes: 4 additions & 12 deletions src/api/sys.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use derive_more::Deref;
use serde_json::json;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};

use super::{secret::SecretAuth, Client, HttpResponse};

use crate::{
errors::RvError,
http::sys::InitRequest,
};

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::{errors::RvError, http::sys::InitRequest};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Secret {
Expand Down Expand Up @@ -61,9 +55,7 @@ pub struct Sys<'a> {

impl Client {
pub fn sys(&self) -> Sys {
Sys {
client: self
}
Sys { client: self }
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/cli/command/auth_enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ impl CommandExecutor for Enable {
match sys.enable_auth(&auth_input) {
Ok(ret) => match ret.response_status {
200 | 204 => {
println!(
"Success! Enabled {} auth method at: {}",
self.method,
auth_input.path
);
println!("Success! Enabled {} auth method at: {}", self.method, auth_input.path);
}
_ => ret.print_debug_info(),
},
Expand Down
18 changes: 12 additions & 6 deletions src/cli/command/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ use derive_more::Deref;
use ureq::json;

use crate::{
api::sys::MountOutput, cli::command::{self, format::table_data_add_header, CommandExecutor}, errors::RvError
api::sys::MountOutput,
cli::command::{self, format::table_data_add_header, CommandExecutor},
errors::RvError,
};

#[derive(Parser, Deref)]
#[command(author, version, about = r#"Lists the enabled auth methods on the RustyVault server. This command also outputs
#[command(
author,
version,
about = r#"Lists the enabled auth methods on the RustyVault server. This command also outputs
information about the method including configuration and human-friendly descriptions.
A TTL of "system" indicates that the system default is in use.
Expand All @@ -17,7 +22,8 @@ List all enabled auth methods:
List all enabled auth methods with detailed output:
$ rvault auth list -detailed"#)]
$ rvault auth list -detailed"#
)]
pub struct List {
#[deref]
#[command(flatten, next_help_heading = "HTTP Options")]
Expand Down Expand Up @@ -50,8 +56,8 @@ impl CommandExecutor for List {
&mount_output.logical_type,
&mount_output.accessor,
&mount_output.description,
&mount_output.plugin_version]
));
&mount_output.plugin_version
]));
}

let data = if self.output.is_format_table() {
Expand All @@ -71,4 +77,4 @@ impl CommandExecutor for List {
}
Ok(())
}
}
}
10 changes: 2 additions & 8 deletions src/cli/command/auth_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
command::{self, CommandExecutor},
util,
},
modules::auth::AUTH_ROUTER_PREFIX,
errors::RvError,
modules::auth::AUTH_ROUTER_PREFIX,
};

#[derive(Parser, Deref)]
Expand All @@ -34,13 +34,7 @@ where ns1 and ns2 are child namespaces of the current namespace:
$ rvault auth move ns1/approle/ ns2/generic/"#
)]
pub struct Move {
#[arg(
index = 1,
required = true,
next_line_help = false,
value_name = "SOURCE",
help = r#"The path of source."#
)]
#[arg(index = 1, required = true, next_line_help = false, value_name = "SOURCE", help = r#"The path of source."#)]
source: String,

#[arg(
Expand Down
11 changes: 6 additions & 5 deletions src/cli/command/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use clap::Parser;
use derive_more::Deref;

use crate::{cli::command::{self, CommandExecutor}, errors::RvError, rv_error_string};
use crate::{
cli::command::{self, CommandExecutor},
errors::RvError,
rv_error_string,
};

#[derive(Parser, Deref)]
#[command(
Expand All @@ -15,10 +19,7 @@ Remove data in the status secret backend:
$ vault delete secret/my-secret"#
)]
pub struct Delete {
#[arg(
next_line_help = false,
value_name = "PATH",
)]
#[arg(next_line_help = false, value_name = "PATH")]
path: String,

#[deref]
Expand Down
10 changes: 5 additions & 5 deletions src/cli/command/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use clap::Parser;
use derive_more::Deref;

use crate::{errors::RvError, cli::command::{self, CommandExecutor}};
use crate::{
cli::command::{self, CommandExecutor},
errors::RvError,
};

#[derive(Parser, Deref)]
#[command(
Expand All @@ -18,10 +21,7 @@ For a full list of examples and paths, please see the documentation that corresp
to the secret engine in use. Not all engines support listing."#
)]
pub struct List {
#[arg(
next_line_help = false,
value_name = "PATH",
)]
#[arg(next_line_help = false, value_name = "PATH")]
path: String,

#[deref]
Expand Down
Loading

0 comments on commit 718a88d

Please sign in to comment.