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:
committed by
GitHub
parent
de51b0cc46
commit
1228d16439
+2
-2
@@ -473,7 +473,7 @@ pub fn generate(
|
||||
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.";
|
||||
|
||||
let setting_roca: settings::BoolValue;
|
||||
let setting_roca: settings::ConfigValue;
|
||||
|
||||
match algorithm {
|
||||
AlgorithmId::Rsa1024 | AlgorithmId::Rsa2048 => {
|
||||
@@ -481,7 +481,7 @@ pub fn generate(
|
||||
&& (yubikey.version.minor < 3
|
||||
|| 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 {
|
||||
settings::Source::User => {
|
||||
|
||||
+42
-44
@@ -53,45 +53,36 @@ pub enum Source {
|
||||
Default,
|
||||
}
|
||||
|
||||
impl Default for Source {
|
||||
fn default() -> Self {
|
||||
Self::Default
|
||||
}
|
||||
}
|
||||
|
||||
/// Setting booleans
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct BoolValue {
|
||||
pub struct ConfigValue {
|
||||
/// Boolean value
|
||||
pub value: bool,
|
||||
|
||||
/// Source of the configuration setting (user/admin/default)
|
||||
/// Source of the configuration setting (user, admin, or default)
|
||||
pub source: Source,
|
||||
}
|
||||
|
||||
impl BoolValue {
|
||||
/// Get a [`BoolValue`] value
|
||||
pub fn get(key: &str, def: bool) -> Self {
|
||||
let mut setting = get_setting_from_file(key);
|
||||
|
||||
if setting.source == Source::Default {
|
||||
setting = get_setting_from_env(key);
|
||||
}
|
||||
|
||||
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,
|
||||
impl ConfigValue {
|
||||
/// Get a [`BoolValue`] value by name.
|
||||
pub fn get(key: &str, default: bool) -> Self {
|
||||
Self::from_file(key)
|
||||
.or_else(|| Self::from_env(key))
|
||||
.unwrap_or(Self {
|
||||
value: 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() {
|
||||
let line = match line {
|
||||
Ok(line) => line,
|
||||
@@ -113,26 +104,33 @@ fn get_setting_from_file(key: &str) -> BoolValue {
|
||||
};
|
||||
|
||||
if name == key {
|
||||
setting.source = Source::Admin;
|
||||
setting.value = value == "1" || value == "true";
|
||||
break;
|
||||
return Some(ConfigValue {
|
||||
source: Source::Admin,
|
||||
value: value == "1" || value == "true",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setting
|
||||
None
|
||||
}
|
||||
|
||||
/// 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",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a setting boolean from an environment variable
|
||||
fn get_setting_from_env(key: &str) -> BoolValue {
|
||||
let mut setting: BoolValue = BoolValue {
|
||||
impl Default for ConfigValue {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
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";
|
||||
source: Source::default(),
|
||||
}
|
||||
}
|
||||
|
||||
setting
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user