Skip to content

Commit

Permalink
Reject command-line if it contains invalid unlock method
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 14, 2024
1 parent 1f0fe7b commit 9694a2b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
20 changes: 9 additions & 11 deletions src/bin/stratis-min/stratis-min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use serde_json::{json, Map, Value};

use stratisd::{
engine::{
EncryptionInfo, KeyDescription, Name, PoolIdentifier, PoolUuid, UnlockMethod,
CLEVIS_TANG_TRUST_URL,
EncryptionInfo, KeyDescription, Name, PoolIdentifier, PoolUuid, CLEVIS_TANG_TRUST_URL,
},
jsonrpc::client::{filesystem, key, pool, report},
stratis::{StratisError, VERSION},
stratis::{UnlockMethod, VERSION},
};

fn parse_args() -> Command {
Expand Down Expand Up @@ -41,7 +40,12 @@ fn parse_args() -> Command {
Command::new("start")
.arg(Arg::new("id").required(true))
.arg(Arg::new("name").long("name").num_args(0))
.arg(Arg::new("unlock_method").long("unlock-method").num_args(1))
.arg(
Arg::new("unlock_method")
.long("unlock-method")
.num_args(1)
.value_parser(clap::value_parser!(UnlockMethod)),
)
.arg(
Arg::new("prompt")
.long("prompt")
Expand Down Expand Up @@ -235,13 +239,7 @@ fn main() -> Result<(), String> {
.expect("required"),
)?)
};
let unlock_method =
match args.get_one::<String>("unlock_method").map(|s| s.as_str()) {
Some(um) => Some(UnlockMethod::try_from(um).map_err(|_| {
StratisError::Msg(format!("{um} is an invalid unlock method"))
})?),
None => None,
};
let unlock_method = args.get_one::<UnlockMethod>("unlock_method").copied();
let prompt = args.get_flag("prompt");
pool::pool_start(id, unlock_method, prompt)?;
Ok(())
Expand Down
15 changes: 13 additions & 2 deletions src/engine/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
use libudev::EventType;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum_macros::{self, EnumString, FromRepr};
use strum_macros::{self, EnumString, FromRepr, IntoStaticStr, VariantArray};
use uuid::Uuid;

pub use crate::engine::{
Expand Down Expand Up @@ -131,7 +131,18 @@ impl Display for StratisUuid {
}

/// Use Clevis or keyring to unlock LUKS volume.
#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, Debug, EnumString)]
#[derive(
Serialize,
Deserialize,
Clone,
Copy,
Eq,
PartialEq,
Debug,
EnumString,
VariantArray,
IntoStaticStr,
)]
#[strum(serialize_all = "snake_case")]
pub enum UnlockMethod {
Clevis,
Expand Down
20 changes: 20 additions & 0 deletions src/stratis/command_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use clap::{builder::PossibleValue, ValueEnum};

use strum::VariantArray;

pub use crate::engine::UnlockMethod;

impl ValueEnum for UnlockMethod {
fn value_variants<'a>() -> &'a [UnlockMethod] {
UnlockMethod::VARIANTS
}

fn to_possible_value(&self) -> Option<PossibleValue> {
let value: &'static str = self.into();
Some(PossibleValue::new(value))
}
}
2 changes: 2 additions & 0 deletions src/stratis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

pub use self::{
command_line::UnlockMethod,
errors::{StratisError, StratisResult},
run::run,
stratis::VERSION,
};

mod command_line;
mod dm;
mod errors;
mod ipc_support;
Expand Down
7 changes: 1 addition & 6 deletions tests/stratis_min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,7 @@ fn test_stratis_min_pool_start_invalid_unlock_method() {
.arg("--name")
.arg("pn")
.arg("--unlock-method=bogus");
cmd.assert()
.failure()
.code(1)
.stderr(predicate::str::contains(
"bogus is an invalid unlock method",
));
cmd.assert().failure().code(2);
}

#[test]
Expand Down

0 comments on commit 9694a2b

Please sign in to comment.