88 lines
2.7 KiB
Rust
88 lines
2.7 KiB
Rust
use clap::{App, AppSettings, ArgMatches};
|
|
use rust_util::util_clap::{Command, CommandError};
|
|
use rust_util::{failure_and_exit, information, success, warning};
|
|
|
|
mod db;
|
|
mod proc;
|
|
mod jose;
|
|
mod cli;
|
|
mod serve;
|
|
mod serve_common;
|
|
mod serve_status;
|
|
mod serve_init;
|
|
mod serve_encrypt_decrypt;
|
|
mod serve_read_write;
|
|
#[cfg(feature = "yubikey")]
|
|
mod yubikey_hmac;
|
|
#[cfg(feature = "yubikey")]
|
|
mod yubikey_init_master_key;
|
|
mod serve_datakey;
|
|
mod serve_log;
|
|
|
|
pub struct DefaultCommandImpl;
|
|
|
|
impl DefaultCommandImpl {
|
|
pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|
app
|
|
}
|
|
pub fn run(_arg_matches: &ArgMatches) -> CommandError {
|
|
information!("Local mini KMS cli, use --help for help");
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
if let Err(e) = inner_main() {
|
|
failure_and_exit!("Run local-mini-kms error: {}", e);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "harden_process")]
|
|
fn harden_process() {
|
|
let ignore_harden_process_error = std::env::var("IGNORE_HARDEN_PROCESS_ERROR")
|
|
.map(|v| &v == "true").unwrap_or_else(|_| false);
|
|
match secmem_proc::harden_process() {
|
|
Err(e) => if ignore_harden_process_error {
|
|
warning!("Harden local-mini-kms failed: {}", e);
|
|
} else {
|
|
failure_and_exit!("Harden local-mini-kms failed: {}", e);
|
|
}
|
|
Ok(_) => success!("Harden local-mini-kms success"),
|
|
}
|
|
}
|
|
|
|
fn inner_main() -> CommandError {
|
|
let commands: Vec<Box<dyn Command>> = vec![
|
|
Box::new(cli::CommandImpl),
|
|
Box::new(serve::CommandImpl),
|
|
#[cfg(feature = "yubikey")]
|
|
Box::new(yubikey_init_master_key::CommandImpl),
|
|
];
|
|
let mut features: Vec<String> = vec![];
|
|
#[allow(clippy::vec_init_then_push)]
|
|
{
|
|
#[cfg(feature = "yubikey")]
|
|
features.push("yubikey".to_string());
|
|
#[cfg(feature = "harden_process")]
|
|
features.push("harden_process".to_string());
|
|
}
|
|
let long_about = format!("Local mini KMS, features: [{}]", features.join(", "));
|
|
let mut app = App::new(env!("CARGO_PKG_NAME"))
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
.about(env!("CARGO_PKG_DESCRIPTION"))
|
|
.long_about(long_about.as_str())
|
|
.setting(AppSettings::ColoredHelp);
|
|
app = DefaultCommandImpl::process_command(app);
|
|
for command in &commands {
|
|
app = app.subcommand(command.subcommand());
|
|
}
|
|
let matches = app.get_matches();
|
|
for command in &commands {
|
|
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
|
|
#[cfg(feature = "harden_process")]
|
|
if command.name() == "serve" { harden_process(); }
|
|
return command.run(&matches, sub_cmd_matches);
|
|
}
|
|
}
|
|
DefaultCommandImpl::run(&matches)
|
|
} |