aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-25 20:33:57 +0100
committerDaniel Mueller <deso@posteo.net>2020-09-07 10:14:21 -0700
commit6917be7ab3c5a9d47866a45855c836a9cc6f86ff (patch)
treeca75486d09c9656e1bbf8ac7c68b5807483cef01 /src/config.rs
parent4d25d79f18cd2c5627c46727b425c745c78cf942 (diff)
downloadnitrocli-6917be7ab3c5a9d47866a45855c836a9cc6f86ff.tar.gz
nitrocli-6917be7ab3c5a9d47866a45855c836a9cc6f86ff.tar.bz2
Add --serial-number option
This patch adds the --serial-number option that allows the user to filter the attached Nitrokey devices by serial number. As the Nitrokey Storage does not include its serial number in the USB device descriptor and as we don't want to connect to it just to query the serial number, this option only works for Nitrokey Storage devices.
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/config.rs b/src/config.rs
index aceda38..6f0cd17 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -5,6 +5,9 @@
use std::fs;
use std::path;
+use std::str::FromStr as _;
+
+use serde::de::Error as _;
use crate::args;
@@ -20,10 +23,14 @@ const CONFIG_FILE: &str = "config.toml";
/// The configuration for nitrocli, usually read from configuration
/// files and environment variables.
-#[derive(Clone, Copy, Debug, Default, PartialEq, merge::Merge, serde::Deserialize)]
+#[derive(Clone, Debug, Default, PartialEq, merge::Merge, serde::Deserialize)]
pub struct Config {
/// The model to connect to.
pub model: Option<args::DeviceModel>,
+ /// The serial numbers of the device to connect to.
+ #[merge(strategy = merge::vec::overwrite_empty)]
+ #[serde(default, deserialize_with = "deserialize_serial_number_vec")]
+ pub serial_numbers: Vec<nitrokey::SerialNumber>,
/// Whether to bypass the cache for all secrets or not.
#[merge(strategy = merge::bool::overwrite_false)]
#[serde(default)]
@@ -34,6 +41,18 @@ pub struct Config {
pub verbosity: u8,
}
+fn deserialize_serial_number_vec<'de, D>(d: D) -> Result<Vec<nitrokey::SerialNumber>, D::Error>
+where
+ D: serde::Deserializer<'de>,
+{
+ let strings: Vec<String> = serde::Deserialize::deserialize(d).map_err(D::Error::custom)?;
+ let result: Result<Vec<_>, _> = strings
+ .iter()
+ .map(|s| nitrokey::SerialNumber::from_str(s))
+ .collect();
+ result.map_err(D::Error::custom)
+}
+
impl Config {
pub fn load() -> anyhow::Result<Self> {
use merge::Merge as _;
@@ -51,6 +70,10 @@ impl Config {
if args.model.is_some() {
self.model = args.model;
}
+ if !args.serial_numbers.is_empty() {
+ // TODO: Don't clone.
+ self.serial_numbers = args.serial_numbers.clone();
+ }
if args.no_cache {
self.no_cache = true;
}