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::io::{stdout, IsTerminal};
use std::path::PathBuf; use std::path::PathBuf;
use crate::key; use crate::key;
use crate::key::KeyLocation; use crate::key::KeyPolicy;
use std::ffi::{CString, c_char, c_void}; use std::ffi::{CString, c_char, c_void};
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
@ -136,14 +136,14 @@ pub struct Cli {
#[arg(short = 'f', long)] #[arg(short = 'f', long)]
key_file: Option<PathBuf>, key_file: Option<PathBuf>,
/// Where the password would be loaded from. /// Password policy to use in case of encrypted filesystem.
/// ///
/// Possible values are: /// Possible values are:
/// "fail" - don't ask for password, fail if filesystem is encrypted; /// "fail" - don't ask for password, fail if filesystem is encrypted;
/// "wait" - wait for password to become available before mounting; /// "wait" - wait for password to become available before mounting;
/// "ask" - prompt the user for password; /// "ask" - prompt the user for password;
#[arg(short, long, default_value = "ask", verbatim_doc_comment)] #[arg(short = 'k', long = "key_location", default_value = "ask", verbatim_doc_comment)]
key_location: KeyLocation, key_policy: KeyPolicy,
/// Device, or UUID=\<UUID\> /// Device, or UUID=\<UUID\>
dev: String, 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 decryption by key_file was unsuccesful, prompt for password (or follow key_policy)
if fallback_to_prepare_key { 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; use anyhow::anyhow;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum KeyLocation { pub enum KeyPolicy {
None, None,
Fail, Fail,
Wait, Wait,
Ask, Ask,
} }
impl std::str::FromStr for KeyLocation { impl std::str::FromStr for KeyPolicy {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> { fn from_str(s: &str) -> anyhow::Result<Self> {
match s { match s {
""|"none" => Ok(KeyLocation::None), ""|"none" => Ok(KeyPolicy::None),
"fail" => Ok(KeyLocation::Fail), "fail" => Ok(KeyPolicy::Fail),
"wait" => Ok(KeyLocation::Wait), "wait" => Ok(KeyPolicy::Wait),
"ask" => Ok(KeyLocation::Ask), "ask" => Ok(KeyPolicy::Ask),
_ => Err(anyhow!("invalid password option")), _ => Err(anyhow!("invalid password option")),
} }
} }
} }
impl clap::ValueEnum for KeyLocation { impl clap::ValueEnum for KeyPolicy {
fn value_variants<'a>() -> &'a [Self] { fn value_variants<'a>() -> &'a [Self] {
&[ &[
KeyLocation::None, KeyPolicy::None,
KeyLocation::Fail, KeyPolicy::Fail,
KeyLocation::Wait, KeyPolicy::Wait,
KeyLocation::Ask, 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 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
KeyLocation::None => write!(f, "None"), KeyPolicy::None => write!(f, "None"),
KeyLocation::Fail => write!(f, "Fail"), KeyPolicy::Fail => write!(f, "Fail"),
KeyLocation::Wait => write!(f, "Wait"), KeyPolicy::Wait => write!(f, "Wait"),
KeyLocation::Ask => write!(f, "Ask"), 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) decrypt_master_key(sb, pass)
} }
pub fn prepare_key(sb: &bch_sb_handle, password: KeyLocation) -> anyhow::Result<()> { pub fn prepare_key(sb: &bch_sb_handle, password: KeyPolicy) -> anyhow::Result<()> {
info!("checking if key exists for filesystem {}", sb.sb().uuid()); info!("Attempting to decrypt master key for filesystem {}, using key policy {}", sb.sb().uuid(), password_policy);
match password { match password {
KeyLocation::Fail => Err(anyhow!("no key available")), KeyPolicy::Fail => Err(anyhow!("no key available")),
KeyLocation::Wait => Ok(wait_for_key(&sb.sb().uuid())?), KeyPolicy::Wait => Ok(wait_for_key(&sb.sb().uuid())?),
KeyLocation::Ask => ask_for_key(sb), KeyPolicy::Ask => ask_for_key(sb),
_ => Err(anyhow!("no keyoption specified for locked filesystem")), _ => Err(anyhow!("no keyoption specified for locked filesystem")),
} }
} }