1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Subcommands of the `tmkms` command-line application

mod keygen;
mod start;
mod version;
#[cfg(feature = "yubihsm")]
mod yubihsm;
#[cfg(feature = "yubihsm")]
pub use self::yubihsm::YubihsmCommand;
#[cfg(feature = "ledgertm")]
mod ledger;
#[cfg(feature = "ledgertm")]
pub use self::ledger::LedgerCommand;

pub use self::{keygen::KeygenCommand, start::StartCommand, version::VersionCommand};
use crate::config::{KmsConfig, CONFIG_ENV_VAR, CONFIG_FILE_NAME};
use abscissa::{Command, Configurable, Help, Runnable};
use std::{env, path::PathBuf};

/// Subcommands of the KMS command-line application
#[derive(Command, Debug, Options, Runnable)]
pub enum KmsCommand {
    /// `help` subcommand
    #[options(help = "show help for a command")]
    Help(Help<Self>),

    /// `keygen` subcommand
    #[options(help = "generate a new software signing key")]
    Keygen(KeygenCommand),

    /// `start` subcommand
    #[options(help = "start the KMS application")]
    Start(StartCommand),

    /// `version` subcommand
    #[options(help = "display version information")]
    Version(VersionCommand),

    /// `yubihsm` subcommand
    #[cfg(feature = "yubihsm")]
    #[options(help = "subcommands for YubiHSM2")]
    Yubihsm(YubihsmCommand),

    /// `ledgertm` subcommand
    #[cfg(feature = "ledgertm")]
    #[options(help = "subcommands for Ledger")]
    Ledger(LedgerCommand),
}

impl KmsCommand {
    /// Are we configured for verbose logging?
    pub fn verbose(&self) -> bool {
        match self {
            KmsCommand::Start(run) => run.verbose,
            #[cfg(feature = "yubihsm")]
            KmsCommand::Yubihsm(yubihsm) => yubihsm.verbose(),
            _ => false,
        }
    }
}

impl Configurable<KmsConfig> for KmsCommand {
    /// Get the path to the configuration file, either from selected subcommand
    /// or the default
    fn config_path(&self) -> Option<PathBuf> {
        let config = match self {
            KmsCommand::Start(run) => run.config.as_ref(),
            #[cfg(feature = "yubihsm")]
            KmsCommand::Yubihsm(yubihsm) => yubihsm.config_path(),
            #[cfg(feature = "ledgertm")]
            KmsCommand::Ledger(ledger) => ledger.config_path(),
            _ => return None,
        };

        let path = PathBuf::from(
            config
                .cloned()
                .or_else(|| env::var(CONFIG_ENV_VAR).ok())
                .unwrap_or_else(|| CONFIG_FILE_NAME.to_owned()),
        );

        Some(path)
    }
}