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 => {
+63 -65
View File
@@ -53,86 +53,84 @@ 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,
} source: Source::Default,
})
if setting.source == Source::Default {
setting.value = def;
}
setting
} }
}
/// Get a boolean config value /// Get a boolean config value from the provided config file
fn get_setting_from_file(key: &str) -> BoolValue { fn from_file(key: &str) -> Option<Self> {
let mut setting: BoolValue = BoolValue { if let Ok(file) = File::open(DEFAULT_CONFIG_FILE) {
value: false, for line in BufReader::new(file).lines() {
source: Source::Default, let line = match line {
}; Ok(line) => line,
_ => continue,
};
let file = match File::open(DEFAULT_CONFIG_FILE) { if line.starts_with('#') || line.starts_with('\r') || line.starts_with('\n') {
Ok(f) => f, continue;
Err(_) => return setting, }
};
for line in BufReader::new(file).lines() { let (name, value) = {
let line = match line { let mut parts = line.splitn(1, '=');
Ok(line) => line, let name = parts.next();
_ => continue, let value = parts.next();
}; match (name, value, parts.next()) {
(Some(name), Some(value), None) => (name.trim(), value.trim()),
_ => continue,
}
};
if line.starts_with('#') || line.starts_with('\r') || line.starts_with('\n') { if name == key {
continue; return Some(ConfigValue {
} source: Source::Admin,
value: value == "1" || value == "true",
let (name, value) = { });
let mut parts = line.splitn(1, '='); }
let name = parts.next();
let value = parts.next();
match (name, value, parts.next()) {
(Some(name), Some(value), None) => (name.trim(), value.trim()),
_ => continue,
} }
}; }
if name == key { None
setting.source = Source::Admin; }
setting.value = value == "1" || value == "true";
break; /// Get a setting boolean from an environment variable
fn from_env(key: &str) -> Option<Self> {
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,
source: Source::default(),
} }
} }
setting
}
/// Get a setting boolean from an environment variable
fn get_setting_from_env(key: &str) -> BoolValue {
let mut setting: BoolValue = BoolValue {
value: false,
source: Source::Default,
};
if let Ok(value) = env::var(format!("YUBIKEY_PIV_{}", key)) {
setting.source = Source::User;
setting.value = value == "1" || value == "true";
}
setting
} }