Skip to content

Commit

Permalink
Enhancements to RvError and optimization of the logical field type.
Browse files Browse the repository at this point in the history
  • Loading branch information
wa5i committed Apr 7, 2024
1 parent 7ba7d14 commit 63e8bca
Show file tree
Hide file tree
Showing 19 changed files with 707 additions and 99 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ diesel = { version = "2.1.4", features = ["mysql", "r2d2"] }
r2d2 = "0.8.9"
r2d2-diesel = "1.0.0"
bcrypt = "0.15"
url = "2.5"
ureq = "2.9"
glob = "0.3"
serde_asn1_der = "0.8"
base64 = "0.22"

[target.'cfg(unix)'.dependencies]
daemonize = "0.5"
Expand Down
17 changes: 16 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub enum RvError {
ErrRequestClientTokenMissing,
#[error("Request field is not found.")]
ErrRequestFieldNotFound,
#[error("Request field is invalid.")]
ErrRequestFieldInvalid,
#[error("Handler is default.")]
ErrHandlerDefault,
#[error("Module kv data field is missing.")]
Expand Down Expand Up @@ -143,6 +145,8 @@ pub enum RvError {
ErrPkiInternal,
#[error("Credentail is invalid.")]
ErrCredentailInvalid,
#[error("Credentail is not config.")]
ErrCredentailNotConfig,
#[error("Some IO error happened, {:?}", .source)]
IO {
#[from]
Expand Down Expand Up @@ -208,11 +212,16 @@ pub enum RvError {
#[from]
source: bcrypt::BcryptError,
},
#[error("Some ureq error happened, {:?}", .source)]
UreqError {
#[from]
source: ureq::Error,
},
#[error("RwLock was poisoned (reading)")]
ErrRwLockReadPoison,
#[error("RwLock was poisoned (writing)")]
ErrRwLockWritePoison,

/// Database Errors Begin
///
#[error("Database type is not support now. Please try postgressql or mysql again.")]
Expand All @@ -234,6 +243,10 @@ pub enum RvError {

#[error(transparent)]
ErrOther(#[from] anyhow::Error),
#[error("Some error happend, response text: {0}")]
ErrResponse(String),
#[error("Some error happend, status: {0}, response text: {1}")]
ErrResponseStatus(u16, String),
#[error("Unknown error.")]
ErrUnknown,
}
Expand Down Expand Up @@ -278,6 +291,7 @@ impl PartialEq for RvError {
| (RvError::ErrRequestInvalid, RvError::ErrRequestInvalid)
| (RvError::ErrRequestClientTokenMissing, RvError::ErrRequestClientTokenMissing)
| (RvError::ErrRequestFieldNotFound, RvError::ErrRequestFieldNotFound)
| (RvError::ErrRequestFieldInvalid, RvError::ErrRequestFieldInvalid)
| (RvError::ErrHandlerDefault, RvError::ErrHandlerDefault)
| (RvError::ErrModuleKvDataFieldMissing, RvError::ErrModuleKvDataFieldMissing)
| (RvError::ErrRustDowncastFailed, RvError::ErrRustDowncastFailed)
Expand Down Expand Up @@ -311,6 +325,7 @@ impl PartialEq for RvError {
| (RvError::ErrPkiRoleNotFound, RvError::ErrPkiRoleNotFound)
| (RvError::ErrPkiInternal, RvError::ErrPkiInternal)
| (RvError::ErrCredentailInvalid, RvError::ErrCredentailInvalid)
| (RvError::ErrCredentailNotConfig, RvError::ErrCredentailNotConfig)
| (RvError::ErrUnknown, RvError::ErrUnknown) => true,
_ => false,
}
Expand Down
10 changes: 9 additions & 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, Response},
logical::{Operation, Connection as ReqConnection, Response},
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -55,8 +55,16 @@ async fn logical_request_handler(
let conn = req.conn_data::<Connection>().unwrap();
log::debug!("logical request, connection info: {:?}, method: {:?}, path: {:?}", conn, method, path);

let mut req_conn = ReqConnection::default();
req_conn.peer_addr = conn.peer.to_string();
if conn.tls.is_some() {
let tls_client_info = conn.tls.as_ref().unwrap();
req_conn.peer_tls_cert = tls_client_info.client_cert_chain.clone();
}

let mut r = request_auth(&req);
r.path = path.into_inner().clone();
r.connection = Some(req_conn);

match method {
Method::GET => {
Expand Down
13 changes: 11 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ pub fn init_service(cfg: &mut web::ServiceConfig) {
impl ResponseError for RvError {
// builds the actual response to send back when an error occurs
fn error_response(&self) -> HttpResponse {
let err_json = json!({ "error": self.to_string() });
HttpResponse::InternalServerError().json(err_json)
let mut status = StatusCode::INTERNAL_SERVER_ERROR;
let text: String;
if let RvError::ErrResponse(resp_text) = self {
text = resp_text.clone();
} else if let RvError::ErrResponseStatus(status_code, resp_text) = self {
status = StatusCode::from_u16(status_code.clone()).unwrap();
text = resp_text.clone();
} else {
text = self.to_string();
}
HttpResponse::build(status).json(json!({ "error": text }))
}
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[macro_use]
extern crate diesel;

pub mod cli;
Expand Down
2 changes: 2 additions & 0 deletions src/logical/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Auth {
pub client_token: String,
pub display_name: String,
pub policies: Vec<String>,
pub internal_data: HashMap<String, String>,
pub metadata: HashMap<String, String>,
}

Expand All @@ -23,6 +24,7 @@ impl Default for Auth {
client_token: String::new(),
display_name: String::new(),
policies: Vec::new(),
internal_data: HashMap::new(),
metadata: HashMap::new(),
}
}
Expand Down
Loading

0 comments on commit 63e8bca

Please sign in to comment.