Rename settings::BoolValue => ConfigValue; refactor/cleanup (#272)

Renames the type used for storing a configuration setting.

Also changes the internal functions to use `Option<ConfigValue>` as the
return value, rather than comparing to a default value, which makes them
slightly more idiomatic.
This commit is contained in:
Tony Arcieri (iqlusion)
2021-07-11 14:53:54 -07:00
committed by GitHub
parent de51b0cc46
commit 1228d16439
2 changed files with 65 additions and 67 deletions
+2 -2
View File
@@ -473,7 +473,7 @@ pub fn generate(
const SZ_ROCA_BLOCK_ADMIN: &str = "was blocked due to an administrator configuration setting."; const SZ_ROCA_BLOCK_ADMIN: &str = "was blocked due to an administrator configuration setting.";
const SZ_ROCA_DEFAULT: &str = "was permitted by default, but is not recommended. The default behavior will change in a future Yubico release."; const SZ_ROCA_DEFAULT: &str = "was permitted by default, but is not recommended. The default behavior will change in a future Yubico release.";
let setting_roca: settings::BoolValue; let setting_roca: settings::ConfigValue;
match algorithm { match algorithm {
AlgorithmId::Rsa1024 | AlgorithmId::Rsa2048 => { AlgorithmId::Rsa1024 | AlgorithmId::Rsa2048 => {
@@ -481,7 +481,7 @@ pub fn generate(
&& (yubikey.version.minor < 3 && (yubikey.version.minor < 3
|| yubikey.version.minor == 3 && (yubikey.version.patch < 5)) || yubikey.version.minor == 3 && (yubikey.version.patch < 5))
{ {
setting_roca = settings::BoolValue::get(SZ_SETTING_ROCA, true); setting_roca = settings::ConfigValue::get(SZ_SETTING_ROCA, true);
let psz_msg = match setting_roca.source { let psz_msg = match setting_roca.source {
settings::Source::User => { settings::Source::User => {
+41 -43
View File
@@ -53,45 +53,36 @@ pub enum Source {
Default, Default,
} }
impl Default for Source {
fn default() -> Self {
Self::Default
}
}
/// Setting booleans /// Setting booleans
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct BoolValue { pub struct ConfigValue {
/// Boolean value /// Boolean value
pub value: bool, pub value: bool,
/// Source of the configuration setting (user/admin/default) /// Source of the configuration setting (user, admin, or default)
pub source: Source, pub source: Source,
} }
impl BoolValue { impl ConfigValue {
/// Get a [`BoolValue`] value /// Get a [`BoolValue`] value by name.
pub fn get(key: &str, def: bool) -> Self { pub fn get(key: &str, default: bool) -> Self {
let mut setting = get_setting_from_file(key); Self::from_file(key)
.or_else(|| Self::from_env(key))
if setting.source == Source::Default { .unwrap_or(Self {
setting = get_setting_from_env(key); value: default,
}
if setting.source == Source::Default {
setting.value = def;
}
setting
}
}
/// Get a boolean config value
fn get_setting_from_file(key: &str) -> BoolValue {
let mut setting: BoolValue = BoolValue {
value: false,
source: Source::Default, source: Source::Default,
}; })
}
let file = match File::open(DEFAULT_CONFIG_FILE) {
Ok(f) => f,
Err(_) => return setting,
};
/// Get a boolean config value from the provided config file
fn from_file(key: &str) -> Option<Self> {
if let Ok(file) = File::open(DEFAULT_CONFIG_FILE) {
for line in BufReader::new(file).lines() { for line in BufReader::new(file).lines() {
let line = match line { let line = match line {
Ok(line) => line, Ok(line) => line,
@@ -113,26 +104,33 @@ fn get_setting_from_file(key: &str) -> BoolValue {
}; };
if name == key { if name == key {
setting.source = Source::Admin; return Some(ConfigValue {
setting.value = value == "1" || value == "true"; source: Source::Admin,
break; value: value == "1" || value == "true",
});
}
} }
} }
setting None
} }
/// Get a setting boolean from an environment variable /// Get a setting boolean from an environment variable
fn get_setting_from_env(key: &str) -> BoolValue { fn from_env(key: &str) -> Option<Self> {
let mut setting: BoolValue = BoolValue { env::var(format!("YUBIKEY_PIV_{}", key))
.ok()
.map(|value| ConfigValue {
source: Source::User,
value: value == "1" || value == "true",
})
}
}
impl Default for ConfigValue {
fn default() -> Self {
Self {
value: false, value: false,
source: Source::Default, source: Source::default(),
}; }
if let Ok(value) = env::var(format!("YUBIKEY_PIV_{}", key)) {
setting.source = Source::User;
setting.value = value == "1" || value == "true";
} }
setting
} }