Skip to content

Commit

Permalink
feat(ch-app): Setup tracing as logger and replace rocket as logger; s…
Browse files Browse the repository at this point in the history
…etup config
  • Loading branch information
schoenenberg committed Aug 16, 2023
1 parent f2bf55f commit 356665a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 56 deletions.
2 changes: 2 additions & 0 deletions clearing-house-app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions clearing-house-app/logging-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ sha2 = "0.10.7"
generic-array = "0.14.7"
openssl = "0.10.56"
config = { version = "0.13.3", default-features = false, features = ["toml"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
1 change: 1 addition & 0 deletions clearing-house-app/logging-service/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
log_level = "DEBUG" # TRACE, DEBUG, INFO, WARN, ERROR
document_database_url= "mongodb://localhost:27017"
process_database_url= "mongodb://localhost:27017"
keyring_database_url= "mongodb://localhost:27017"
Expand Down
69 changes: 69 additions & 0 deletions clearing-house-app/logging-service/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#[derive(Debug, serde::Deserialize)]
pub(crate) struct CHConfig {
pub(crate) process_database_url: String,
pub(crate) keyring_database_url: String,
pub(crate) document_database_url: String,
pub(crate) clear_db: bool,
#[serde(default)]
pub(crate) log_level: Option<LogLevel>,
}

#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub(crate) enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

impl Into<tracing::Level> for LogLevel {
fn into(self) -> tracing::Level {
match self {
LogLevel::Trace => tracing::Level::TRACE,
LogLevel::Debug => tracing::Level::DEBUG,
LogLevel::Info => tracing::Level::INFO,
LogLevel::Warn => tracing::Level::WARN,
LogLevel::Error => tracing::Level::ERROR,
}
}
}

impl ToString for LogLevel {
fn to_string(&self) -> String {
match self {
LogLevel::Trace => String::from("TRACE"),
LogLevel::Debug => String::from("DEBUG"),
LogLevel::Info => String::from("INFO"),
LogLevel::Warn => String::from("WARN"),
LogLevel::Error => String::from("ERROR"),
}
}
}

pub(crate) fn read_config() -> CHConfig {
let conf = config::Config::builder()
.add_source(config::File::with_name("config.toml"))
.add_source(config::Environment::with_prefix("CH_APP_"))
.build()
.expect("Failure to read configuration! Exiting...");

let conf: CHConfig = conf.try_deserialize().expect("Failure to read configuration! Exiting...");
tracing::trace!(config = ?conf, "Config read");

conf
}

pub(crate) fn configure_logging(log_level: Option<LogLevel>) {
if std::env::var("RUST_LOG").is_err() {
if let Some(level) = log_level {
std::env::set_var("RUST_LOG", level.to_string());
}
}

// setup logging
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
}
30 changes: 15 additions & 15 deletions clearing-house-app/logging-service/src/db/key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ impl KeyStore {

/// Only one master key may exist in the database.
pub async fn store_master_key(&self, key: MasterKey) -> anyhow::Result<bool>{
debug!("Storing new master key...");
tracing::debug!("Storing new master key...");
let coll = self.database.collection::<MasterKey>(MONGO_COLL_MASTER_KEY);
debug!("... but first check if there's already one.");
tracing::debug!("... but first check if there's already one.");
let result= coll.find(None, None).await
.expect("Error retrieving the master keys")
.try_collect().await.unwrap_or_else(|_| vec![]);

if result.len() > 1{
error!("Master Key table corrupted!");
tracing::error!("Master Key table corrupted!");
exit(1);
}
if result.len() == 1{
error!("Master key already exists!");
tracing::error!("Master key already exists!");
Ok(false)
}
else{
Expand All @@ -50,7 +50,7 @@ impl KeyStore {
Ok(true)
},
Err(e) => {
error!("master key could not be stored: {:?}", &e);
tracing::error!("master key could not be stored: {:?}", &e);
panic!("master key could not be stored")
}
}
Expand All @@ -65,14 +65,14 @@ impl KeyStore {
.try_collect().await.unwrap_or_else(|_| vec![]);

if result.len() > 1{
error!("Master Key table corrupted!");
tracing::error!("Master Key table corrupted!");
exit(1);
}
if result.len() == 1{
Ok(result[0].clone())
}
else {
error!("Master Key missing!");
tracing::error!("Master Key missing!");
exit(1);
}
}
Expand All @@ -82,11 +82,11 @@ impl KeyStore {
let coll = self.database.collection::<DocumentType>(MONGO_COLL_DOC_TYPES);
match coll.insert_one(doc_type.clone(), None).await {
Ok(_r) => {
debug!("added new document type: {}", &_r.inserted_id);
tracing::debug!("added new document type: {}", &_r.inserted_id);
Ok(())
},
Err(e) => {
error!("failed to log document type {}", &doc_type.id);
tracing::error!("failed to log document type {}", &doc_type.id);
Err(Error::from(e))
}
}
Expand All @@ -111,7 +111,7 @@ impl KeyStore {
match result {
Some(_r) => Ok(true),
None => {
debug!("document type with id {} and pid {:?} does not exist!", &dt_id, &pid);
tracing::debug!("document type with id {} and pid {:?} does not exist!", &dt_id, &pid);
Ok(false)
}
}
Expand All @@ -126,11 +126,11 @@ impl KeyStore {

pub async fn get_document_type(&self, dt_id: &String) -> Result<Option<DocumentType>> {
let coll = self.database.collection::<DocumentType>(MONGO_COLL_DOC_TYPES);
debug!("get_document_type for dt_id: '{}'", dt_id);
tracing::debug!("get_document_type for dt_id: '{}'", dt_id);
match coll.find_one(Some(doc! { MONGO_ID: dt_id}), None).await{
Ok(result) => Ok(result),
Err(e) => {
error!("error while getting document type with id {}!", dt_id);
tracing::error!("error while getting document type with id {}!", dt_id);
Err(Error::from(e))
}
}
Expand All @@ -141,15 +141,15 @@ impl KeyStore {
match coll.replace_one(doc! { MONGO_ID: id}, doc_type, None).await{
Ok(r) => {
if r.matched_count != 1 || r.modified_count != 1{
warn!("while replacing doc type {} matched '{}' dts and modified '{}'", id, r.matched_count, r.modified_count);
tracing::warn!("while replacing doc type {} matched '{}' dts and modified '{}'", id, r.matched_count, r.modified_count);
}
else{
debug!("while replacing doc type {} matched '{}' dts and modified '{}'", id, r.matched_count, r.modified_count);
tracing::debug!("while replacing doc type {} matched '{}' dts and modified '{}'", id, r.matched_count, r.modified_count);
}
Ok(true)
},
Err(e) => {
error!("error while updating document type with id {}: {:#?}", id, e);
tracing::error!("error while updating document type with id {}: {:#?}", id, e);
Ok(false)
}
}
Expand Down
28 changes: 6 additions & 22 deletions clearing-house-app/logging-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![forbid(unsafe_code)]

#[macro_use]
extern crate rocket;
extern crate tracing;

use std::path::Path;
use core_lib::util::{add_service_config};
use rocket::fairing::AdHoc;
use tracing::subscriber;
use core_lib::constants::ENV_LOGGING_SERVICE_ID;
use db::config::doc_store::DatastoreConfigurator;
use db::config::keyring_store::KeyringDbConfigurator;
Expand All @@ -17,42 +18,25 @@ mod model;
mod services;
mod crypto;
mod ports;
mod config;

pub fn add_signing_key() -> AdHoc {
AdHoc::try_on_ignite("Adding Signing Key", |rocket| async {
let private_key_path = rocket.figment().extract_inner(SIGNING_KEY).unwrap_or(String::from("keys/private_key.der"));
if Path::new(&private_key_path).exists() {
Ok(rocket.manage(private_key_path))
} else {
error!("Signing key not found! Aborting startup! Please configure signing_key!");
tracing::error!("Signing key not found! Aborting startup! Please configure signing_key!");
return Err(rocket);
}
})
}

#[derive(Debug, serde::Deserialize)]
struct CHConfig {
process_database_url: String,
keyring_database_url: String,
document_database_url: String,
clear_db: bool,
}

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
// Read configuration
let conf = config::Config::builder()
.add_source(config::File::with_name("config.toml"))
.add_source(config::Environment::with_prefix("CH_APP_"))
.build()
.expect("Failure to read configuration! Exiting...");

// setup logging
// TODO: Setup tracing_subscriber

let conf: CHConfig = conf.try_deserialize().expect("Failure to read configuration! Exiting...");
println!("Config: {:?}", conf);

let conf = config::read_config();
config::configure_logging(conf.log_level);

let process_store =
ProcessStoreConfigurator::init_process_store(String::from(conf.process_database_url), conf.clear_db)
Expand Down
16 changes: 8 additions & 8 deletions clearing-house-app/logging-service/src/ports/doc_type_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rocket::serde::json::{json,Json};
use crate::services::keyring_service::KeyringService;
use crate::model::doc_type::DocumentType;

#[post("/", format = "json", data = "<doc_type>")]
#[rocket::post("/", format = "json", data = "<doc_type>")]
async fn create_doc_type(key_api: &State<KeyringService>, doc_type: Json<DocumentType>) -> ApiResponse {
match key_api.inner().create_doc_type(doc_type.into_inner()).await{
Ok(dt) => ApiResponse::SuccessCreate(json!(dt)),
Expand All @@ -18,7 +18,7 @@ async fn create_doc_type(key_api: &State<KeyringService>, doc_type: Json<Documen
}
}

#[post("/<id>", format = "json", data = "<doc_type>")]
#[rocket::post("/<id>", format = "json", data = "<doc_type>")]
async fn update_doc_type(key_api: &State<KeyringService>, id: String, doc_type: Json<DocumentType>) -> ApiResponse {
match key_api.inner().update_doc_type(id, doc_type.into_inner()).await{
Ok(id) => ApiResponse::SuccessOk(json!(id)),
Expand All @@ -29,12 +29,12 @@ async fn update_doc_type(key_api: &State<KeyringService>, id: String, doc_type:
}
}

#[delete("/<id>", format = "json")]
#[rocket::delete("/<id>", format = "json")]
async fn delete_default_doc_type(key_api: &State<KeyringService>, id: String) -> ApiResponse{
delete_doc_type(key_api, id, DEFAULT_PROCESS_ID.to_string()).await
}

#[delete("/<pid>/<id>", format = "json")]
#[rocket::delete("/<pid>/<id>", format = "json")]
async fn delete_doc_type(key_api: &State<KeyringService>, id: String, pid: String) -> ApiResponse{
match key_api.inner().delete_doc_type(id, pid).await{
Ok(id) => ApiResponse::SuccessOk(json!(id)),
Expand All @@ -45,12 +45,12 @@ async fn delete_doc_type(key_api: &State<KeyringService>, id: String, pid: Strin
}
}

#[get("/<id>", format = "json")]
#[rocket::get("/<id>", format = "json")]
async fn get_default_doc_type(key_api: &State<KeyringService>, id: String) -> ApiResponse {
get_doc_type(key_api, id, DEFAULT_PROCESS_ID.to_string()).await
}

#[get("/<pid>/<id>", format = "json")]
#[rocket::get("/<pid>/<id>", format = "json")]
async fn get_doc_type(key_api: &State<KeyringService>, id: String, pid: String) -> ApiResponse {
match key_api.inner().get_doc_type(id, pid).await{
Ok(dt) => {
Expand All @@ -66,7 +66,7 @@ async fn get_doc_type(key_api: &State<KeyringService>, id: String, pid: String)
}
}

#[get("/", format = "json")]
#[rocket::get("/", format = "json")]
async fn get_doc_types(key_api: &State<KeyringService>) -> ApiResponse {
match key_api.inner().get_doc_types().await{
Ok(dt) => ApiResponse::SuccessOk(json!(dt)),
Expand All @@ -80,7 +80,7 @@ async fn get_doc_types(key_api: &State<KeyringService>) -> ApiResponse {
pub fn mount_api() -> AdHoc {
AdHoc::on_ignite("Mounting Document Type API", |rocket| async {
rocket
.mount(ROCKET_DOC_TYPE_API, routes![create_doc_type,
.mount(ROCKET_DOC_TYPE_API, rocket::routes![create_doc_type,
update_doc_type, delete_default_doc_type, delete_doc_type,
get_default_doc_type, get_doc_type , get_doc_types])
})
Expand Down
22 changes: 11 additions & 11 deletions clearing-house-app/logging-service/src/ports/logging_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::model::ids::request::ClearingHouseMessage;
use crate::model::constants::{ROCKET_CLEARING_HOUSE_BASE_API, ROCKET_LOG_API, ROCKET_QUERY_API, ROCKET_PROCESS_API, ROCKET_PK_API};
use crate::services::logging_service::LoggingService;

#[post("/<pid>", format = "json", data = "<message>")]
#[rocket::post("/<pid>", format = "json", data = "<message>")]
async fn log(
ch_claims: ChClaims,
logging_api: &State<LoggingService>,
Expand All @@ -30,7 +30,7 @@ async fn log(
}
}

#[post("/<pid>", format = "json", data = "<message>")]
#[rocket::post("/<pid>", format = "json", data = "<message>")]
async fn create_process(
ch_claims: ChClaims,
logging_api: &State<LoggingService>,
Expand All @@ -46,17 +46,17 @@ async fn create_process(
}
}

#[post("/<_pid>", format = "json", rank = 50)]
#[rocket::post("/<_pid>", format = "json", rank = 50)]
async fn unauth(_pid: Option<String>) -> ApiResponse {
ApiResponse::Unauthorized(String::from("Token not valid!"))
}

#[post("/<_pid>/<_id>", format = "json", rank = 50)]
#[rocket::post("/<_pid>/<_id>", format = "json", rank = 50)]
async fn unauth_id(_pid: Option<String>, _id: Option<String>) -> ApiResponse {
ApiResponse::Unauthorized(String::from("Token not valid!"))
}

#[post("/<pid>?<page>&<size>&<sort>&<date_to>&<date_from>", format = "json", data = "<message>")]
#[rocket::post("/<pid>?<page>&<size>&<sort>&<date_to>&<date_from>", format = "json", data = "<message>")]
async fn query_pid(
ch_claims: ChClaims,
logging_api: &State<LoggingService>,
Expand All @@ -77,7 +77,7 @@ async fn query_pid(
}
}

#[post("/<pid>/<id>", format = "json", data = "<message>")]
#[rocket::post("/<pid>/<id>", format = "json", data = "<message>")]
async fn query_id(
ch_claims: ChClaims,
logging_api: &State<LoggingService>,
Expand All @@ -94,7 +94,7 @@ async fn query_id(
}
}

#[get("/.well-known/jwks.json", format = "json")]
#[rocket::get("/.well-known/jwks.json", format = "json")]
async fn get_public_sign_key(key_path: &State<String>) -> ApiResponse {
match get_jwks(key_path.as_str()) {
Some(jwks) => ApiResponse::SuccessOk(json!(jwks)),
Expand All @@ -105,10 +105,10 @@ async fn get_public_sign_key(key_path: &State<String>) -> ApiResponse {
pub fn mount_api() -> AdHoc {
AdHoc::on_ignite("Mounting Clearing House API", |rocket| async {
rocket
.mount(format!("{}{}", ROCKET_CLEARING_HOUSE_BASE_API, ROCKET_LOG_API).as_str(), routes![log, unauth])
.mount(format!("{}", ROCKET_PROCESS_API).as_str(), routes![create_process, unauth])
.mount(format!("{}{}", ROCKET_CLEARING_HOUSE_BASE_API, ROCKET_LOG_API).as_str(), rocket::routes![log, unauth])
.mount(format!("{}", ROCKET_PROCESS_API).as_str(), rocket::routes![create_process, unauth])
.mount(format!("{}{}", ROCKET_CLEARING_HOUSE_BASE_API, ROCKET_QUERY_API).as_str(),
routes![query_id, query_pid, unauth, unauth_id])
.mount(format!("{}", ROCKET_PK_API).as_str(), routes![get_public_sign_key])
rocket::routes![query_id, query_pid, unauth, unauth_id])
.mount(format!("{}", ROCKET_PK_API).as_str(), rocket::routes![get_public_sign_key])
})
}

0 comments on commit 356665a

Please sign in to comment.