Rename KeyLocation to more appropriate KeyPolicy

Also key_location to key_policy

Improve help description key policy

Signed-off-by: Roland Vet <RlndVt@protonmail.com>
This commit is contained in:
Roland Vet 2024-02-20 20:46:42 +01:00
parent ceb259b2a3
commit 37c0ae2e79
2 changed files with 26 additions and 26 deletions

View File

@ -5,7 +5,7 @@ use uuid::Uuid;
use std::io::{stdout, IsTerminal};
use std::path::PathBuf;
use crate::key;
use crate::key::KeyLocation;
use crate::key::KeyPolicy;
use std::ffi::{CString, c_char, c_void};
use std::os::unix::ffi::OsStrExt;
@ -136,14 +136,14 @@ pub struct Cli {
#[arg(short = 'f', long)]
key_file: Option<PathBuf>,
/// Where the password would be loaded from.
/// Password policy to use in case of encrypted filesystem.
///
/// Possible values are:
/// "fail" - don't ask for password, fail if filesystem is encrypted;
/// "wait" - wait for password to become available before mounting;
/// "ask" - prompt the user for password;
#[arg(short, long, default_value = "ask", verbatim_doc_comment)]
key_location: KeyLocation,
#[arg(short = 'k', long = "key_location", default_value = "ask", verbatim_doc_comment)]
key_policy: KeyPolicy,
/// Device, or UUID=\<UUID\>
dev: String,
@ -227,7 +227,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
};
// If decryption by key_file was unsuccesful, prompt for password (or follow key_policy)
if fallback_to_prepare_key {
key::prepare_key(&block_devices_to_mount[0], opt.key_location)?;
key::prepare_key(&block_devices_to_mount[0], opt.key_policy)?;
};
}

View File

@ -7,33 +7,33 @@ use crate::c_str;
use anyhow::anyhow;
#[derive(Clone, Debug)]
pub enum KeyLocation {
pub enum KeyPolicy {
None,
Fail,
Wait,
Ask,
}
impl std::str::FromStr for KeyLocation {
impl std::str::FromStr for KeyPolicy {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> {
match s {
""|"none" => Ok(KeyLocation::None),
"fail" => Ok(KeyLocation::Fail),
"wait" => Ok(KeyLocation::Wait),
"ask" => Ok(KeyLocation::Ask),
""|"none" => Ok(KeyPolicy::None),
"fail" => Ok(KeyPolicy::Fail),
"wait" => Ok(KeyPolicy::Wait),
"ask" => Ok(KeyPolicy::Ask),
_ => Err(anyhow!("invalid password option")),
}
}
}
impl clap::ValueEnum for KeyLocation {
impl clap::ValueEnum for KeyPolicy {
fn value_variants<'a>() -> &'a [Self] {
&[
KeyLocation::None,
KeyLocation::Fail,
KeyLocation::Wait,
KeyLocation::Ask,
KeyPolicy::None,
KeyPolicy::Fail,
KeyPolicy::Wait,
KeyPolicy::Ask,
]
}
@ -47,13 +47,13 @@ impl clap::ValueEnum for KeyLocation {
}
}
impl fmt::Display for KeyLocation {
impl fmt::Display for KeyPolicy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
KeyLocation::None => write!(f, "None"),
KeyLocation::Fail => write!(f, "Fail"),
KeyLocation::Wait => write!(f, "Wait"),
KeyLocation::Ask => write!(f, "Ask"),
KeyPolicy::None => write!(f, "None"),
KeyPolicy::Fail => write!(f, "Fail"),
KeyPolicy::Wait => write!(f, "Wait"),
KeyPolicy::Ask => write!(f, "Ask"),
}
}
}
@ -160,12 +160,12 @@ pub fn read_from_key_file(sb: &bch_sb_handle, key_file: &std::path::Path) -> any
decrypt_master_key(sb, pass)
}
pub fn prepare_key(sb: &bch_sb_handle, password: KeyLocation) -> anyhow::Result<()> {
info!("checking if key exists for filesystem {}", sb.sb().uuid());
pub fn prepare_key(sb: &bch_sb_handle, password: KeyPolicy) -> anyhow::Result<()> {
info!("Attempting to decrypt master key for filesystem {}, using key policy {}", sb.sb().uuid(), password_policy);
match password {
KeyLocation::Fail => Err(anyhow!("no key available")),
KeyLocation::Wait => Ok(wait_for_key(&sb.sb().uuid())?),
KeyLocation::Ask => ask_for_key(sb),
KeyPolicy::Fail => Err(anyhow!("no key available")),
KeyPolicy::Wait => Ok(wait_for_key(&sb.sb().uuid())?),
KeyPolicy::Ask => ask_for_key(sb),
_ => Err(anyhow!("no keyoption specified for locked filesystem")),
}
}