Print a warning when an env variable is being overriden by the config file, and reorganize the main file a bit.

Modified the JWT key generation, now it should also show the output of OpenSSL in the logs.
This commit is contained in:
Daniel García 2019-02-20 20:59:37 +01:00
parent 5ee04e31e5
commit b50c27b619
No known key found for this signature in database
GPG Key ID: FC8A7D14C3CD543A
2 changed files with 86 additions and 90 deletions

View File

@ -62,12 +62,25 @@ macro_rules! make_config {
/// Merges the values of both builders into a new builder. /// Merges the values of both builders into a new builder.
/// If both have the same element, `other` wins. /// If both have the same element, `other` wins.
fn merge(&self, other: &Self) -> Self { fn merge(&self, other: &Self) -> Self {
let mut overrides = Vec::new();
let mut builder = self.clone(); let mut builder = self.clone();
$($( $($(
if let v @Some(_) = &other.$name { if let v @Some(_) = &other.$name {
builder.$name = v.clone(); builder.$name = v.clone();
if self.$name.is_some() {
overrides.push(stringify!($name).to_uppercase());
}
} }
)+)+ )+)+
if !overrides.is_empty() {
// We can't use warn! here because logging isn't setup yet.
println!("[WARNING] The following environment variables are being overriden by the config file,");
println!("[WARNING] please use the admin panel to make changes to them:");
println!("[WARNING] {}\n", overrides.join(", "));
}
builder builder
} }

View File

@ -20,8 +20,6 @@ extern crate derive_more;
#[macro_use] #[macro_use]
extern crate num_derive; extern crate num_derive;
use rocket::{fairing::AdHoc, Rocket};
use std::{ use std::{
path::Path, path::Path,
process::{exit, Command}, process::{exit, Command},
@ -40,56 +38,9 @@ mod util;
pub use config::CONFIG; pub use config::CONFIG;
pub use error::{Error, MapResult}; pub use error::{Error, MapResult};
fn launch_rocket() {
// Create Rocket object, this stores current log level and sets it's own
let rocket = rocket::ignite();
// If we aren't logging the mounts, we force the logging level down
if !CONFIG.log_mounts() {
log::set_max_level(log::LevelFilter::Warn);
}
let rocket = rocket
.mount("/", api::web_routes())
.mount("/api", api::core_routes())
.mount("/admin", api::admin_routes())
.mount("/identity", api::identity_routes())
.mount("/icons", api::icons_routes())
.mount("/notifications", api::notifications_routes());
// Force the level up for the fairings, managed state and lauch
if !CONFIG.log_mounts() {
log::set_max_level(log::LevelFilter::max());
}
let rocket = rocket
.manage(db::init_pool())
.manage(api::start_notification_server())
.attach(util::AppHeaders())
.attach(AdHoc::on_launch("Launch Info", launch_info));
// Launch and print error if there is one
// The launch will restore the original logging level
error!("Launch error {:#?}", rocket.launch());
}
// Embed the migrations from the migrations folder into the application
// This way, the program automatically migrates the database to the latest version
// https://docs.rs/diesel_migrations/*/diesel_migrations/macro.embed_migrations.html
#[allow(unused_imports)]
mod migrations {
embed_migrations!();
pub fn run_migrations() {
// Make sure the database is up to date (create if it doesn't exist, or run the migrations)
let connection = crate::db::get_connection().expect("Can't conect to DB");
use std::io::stdout;
embedded_migrations::run_with_output(&connection, &mut stdout()).expect("Can't run migrations");
}
}
fn main() { fn main() {
launch_info();
if CONFIG.extended_logging() { if CONFIG.extended_logging() {
init_logging().ok(); init_logging().ok();
} }
@ -102,6 +53,21 @@ fn main() {
launch_rocket(); launch_rocket();
} }
fn launch_info() {
println!("/--------------------------------------------------------------------\\");
println!("| Starting Bitwarden_RS |");
if let Some(version) = option_env!("GIT_VERSION") {
println!("|{:^68}|", format!("Version {}", version));
}
println!("|--------------------------------------------------------------------|");
println!("| This is an *unofficial* Bitwarden implementation, DO NOT use the |");
println!("| official channels to report bugs/features, regardless of client. |");
println!("| Report URL: https://github.com/dani-garcia/bitwarden_rs/issues/new |");
println!("\\--------------------------------------------------------------------/\n");
}
fn init_logging() -> Result<(), fern::InitError> { fn init_logging() -> Result<(), fern::InitError> {
let mut logger = fern::Dispatch::new() let mut logger = fern::Dispatch::new()
.format(|out, message, record| { .format(|out, message, record| {
@ -182,49 +148,36 @@ fn check_rsa_keys() {
if !util::file_exists(&CONFIG.private_rsa_key()) || !util::file_exists(&CONFIG.public_rsa_key()) { if !util::file_exists(&CONFIG.private_rsa_key()) || !util::file_exists(&CONFIG.public_rsa_key()) {
info!("JWT keys don't exist, checking if OpenSSL is available..."); info!("JWT keys don't exist, checking if OpenSSL is available...");
Command::new("openssl").arg("version").output().unwrap_or_else(|_| { Command::new("openssl").arg("version").status().unwrap_or_else(|_| {
info!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH"); info!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH");
exit(1); exit(1);
}); });
info!("OpenSSL detected, creating keys..."); info!("OpenSSL detected, creating keys...");
let key = CONFIG.rsa_key_filename();
let pem = format!("{}.pem", key);
let priv_der = format!("{}.der", key);
let pub_der = format!("{}.pub.der", key);
let mut success = Command::new("openssl") let mut success = Command::new("openssl")
.arg("genrsa") .args(&["genrsa", "-out", &pem])
.arg("-out") .status()
.arg(&CONFIG.private_rsa_key_pem())
.output()
.expect("Failed to create private pem file") .expect("Failed to create private pem file")
.status
.success(); .success();
success &= Command::new("openssl") success &= Command::new("openssl")
.arg("rsa") .args(&["rsa", "-in", &pem, "-outform", "DER", "-out", &priv_der])
.arg("-in") .status()
.arg(&CONFIG.private_rsa_key_pem())
.arg("-outform")
.arg("DER")
.arg("-out")
.arg(&CONFIG.private_rsa_key())
.output()
.expect("Failed to create private der file") .expect("Failed to create private der file")
.status
.success(); .success();
success &= Command::new("openssl") success &= Command::new("openssl")
.arg("rsa") .args(&["rsa", "-in", &priv_der, "-inform", "DER"])
.arg("-in") .args(&["-RSAPublicKey_out", "-outform", "DER", "-out", &pub_der])
.arg(&CONFIG.private_rsa_key()) .status()
.arg("-inform")
.arg("DER")
.arg("-RSAPublicKey_out")
.arg("-outform")
.arg("DER")
.arg("-out")
.arg(&CONFIG.public_rsa_key())
.output()
.expect("Failed to create public der file") .expect("Failed to create public der file")
.status
.success(); .success();
if success { if success {
@ -249,20 +202,50 @@ fn check_web_vault() {
} }
} }
fn launch_info(_: &Rocket) { // Embed the migrations from the migrations folder into the application
// Remove the target to keep the message more centered // This way, the program automatically migrates the database to the latest version
macro_rules! w {( $l:literal $(,$e:expr)* ) => {warn!(target: "", $l, $($e),* )}} // https://docs.rs/diesel_migrations/*/diesel_migrations/macro.embed_migrations.html
#[allow(unused_imports)]
mod migrations {
embed_migrations!();
w!("/--------------------------------------------------------------------\\"); pub fn run_migrations() {
w!("| Starting Bitwarden_RS |"); // Make sure the database is up to date (create if it doesn't exist, or run the migrations)
let connection = crate::db::get_connection().expect("Can't conect to DB");
if let Some(version) = option_env!("GIT_VERSION") { use std::io::stdout;
w!("|{:^68}|", format!("Version {}", version)); embedded_migrations::run_with_output(&connection, &mut stdout()).expect("Can't run migrations");
}
}
fn launch_rocket() {
// Create Rocket object, this stores current log level and sets it's own
let rocket = rocket::ignite();
// If we aren't logging the mounts, we force the logging level down
if !CONFIG.log_mounts() {
log::set_max_level(log::LevelFilter::Warn);
} }
w!("|--------------------------------------------------------------------|"); let rocket = rocket
w!("| This is an *unofficial* Bitwarden implementation, DO NOT use the |"); .mount("/", api::web_routes())
w!("| official channels to report bugs/features, regardless of client. |"); .mount("/api", api::core_routes())
w!("| Report URL: https://github.com/dani-garcia/bitwarden_rs/issues/new |"); .mount("/admin", api::admin_routes())
w!("\\--------------------------------------------------------------------/"); .mount("/identity", api::identity_routes())
.mount("/icons", api::icons_routes())
.mount("/notifications", api::notifications_routes());
// Force the level up for the fairings, managed state and lauch
if !CONFIG.log_mounts() {
log::set_max_level(log::LevelFilter::max());
}
let rocket = rocket
.manage(db::init_pool())
.manage(api::start_notification_server())
.attach(util::AppHeaders());
// Launch and print error if there is one
// The launch will restore the original logging level
error!("Launch error {:#?}", rocket.launch());
} }