Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make specifying an invalid unlock method a parser error #3719

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/bin/stratis-min/stratis-min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

use std::{error::Error, path::PathBuf};

use clap::{Arg, ArgAction, ArgGroup, Command};
use clap::{builder::PossibleValuesParser, Arg, ArgAction, ArgGroup, Command};
use serde_json::{json, Map, Value};
use strum::VariantNames;

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

fn parse_args() -> Command {
Expand Down Expand Up @@ -41,7 +42,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(PossibleValuesParser::new(UnlockMethod::VARIANTS)),
)
.arg(
Arg::new("prompt")
.long("prompt")
Expand Down Expand Up @@ -235,13 +241,9 @@ 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::<String>("unlock_method").map(|s| {
UnlockMethod::try_from(s.as_str()).expect("parser ensures valid string value")
});
let prompt = args.get_flag("prompt");
pool::pool_start(id, unlock_method, prompt)?;
Ok(())
Expand Down
4 changes: 2 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, VariantNames};
use uuid::Uuid;

pub use crate::engine::{
Expand Down Expand Up @@ -131,7 +131,7 @@ 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, VariantNames)]
#[strum(serialize_all = "snake_case")]
pub enum UnlockMethod {
Clevis,
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