Skip to content

Commit 16ceca8

Browse files
committed
Cleanup command line options for auth / share input.
Not sure this is the "right" way since we're duplicating a structure, just w/ different clap stuff ... it works.
1 parent 78695dd commit 16ceca8

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use oks::{
2626
},
2727
hsm::Hsm,
2828
secret_reader::{
29-
self, PasswordReader, SecretInputArg, StdioPasswordReader,
29+
self, AuthInputArg, PasswordReader, ShareInputArg, StdioPasswordReader,
3030
},
3131
secret_writer::{self, SecretOutputArg},
3232
util,
@@ -75,7 +75,7 @@ struct Args {
7575
enum Command {
7676
Ca {
7777
#[clap(flatten)]
78-
auth_method: SecretInputArg,
78+
auth_method: AuthInputArg,
7979

8080
#[command(subcommand)]
8181
command: CaCommand,
@@ -159,7 +159,7 @@ enum HsmCommand {
159159
/// Generate keys in YubiHSM from specification.
160160
Generate {
161161
#[clap(flatten)]
162-
auth_method: SecretInputArg,
162+
auth_method: AuthInputArg,
163163

164164
#[clap(long, env, default_value = "input")]
165165
key_spec: PathBuf,
@@ -185,7 +185,7 @@ enum HsmCommand {
185185
backups: PathBuf,
186186

187187
#[clap(flatten)]
188-
share_method: SecretInputArg,
188+
share_method: ShareInputArg,
189189

190190
#[clap(long, env, default_value = "input/verifier.json")]
191191
verifier: PathBuf,
@@ -194,7 +194,7 @@ enum HsmCommand {
194194
/// Get serial number from YubiHSM and dump to console.
195195
SerialNumber {
196196
#[clap(flatten)]
197-
auth_method: SecretInputArg,
197+
auth_method: AuthInputArg,
198198
},
199199
}
200200

@@ -239,7 +239,7 @@ fn get_auth_id(auth_id: Option<Id>, command: &HsmCommand) -> Id {
239239
/// the user with a password prompt.
240240
fn get_passwd(
241241
auth_id: Option<Id>,
242-
auth_method: &SecretInputArg,
242+
auth_method: &AuthInputArg,
243243
command: &HsmCommand,
244244
) -> Result<Zeroizing<String>> {
245245
let passwd = match env::var(ENV_PASSWORD).ok() {

src/secret_reader.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ pub enum SecretInput {
2828
Stdio,
2929
}
3030

31-
#[derive(Args, Clone, Debug, Default, PartialEq)]
32-
pub struct SecretInputArg {
33-
#[clap(long, env)]
34-
auth_method: SecretInput,
35-
36-
#[clap(long, env)]
37-
auth_dev: Option<PathBuf>,
38-
}
39-
4031
impl From<SecretInput> for ArgPredicate {
4132
fn from(val: SecretInput) -> Self {
4233
let rep = match val {
@@ -58,20 +49,29 @@ impl From<SecretInput> for &str {
5849
}
5950
}
6051

52+
#[derive(Args, Clone, Debug, Default, PartialEq)]
53+
pub struct AuthInputArg {
54+
#[clap(long = "auth-method", env)]
55+
method: SecretInput,
56+
57+
#[clap(long = "auth-device", env)]
58+
device: Option<PathBuf>,
59+
}
60+
6161
pub trait PasswordReader {
6262
fn read(&mut self, prompt: &str) -> Result<Zeroizing<String>>;
6363
}
6464

6565
pub fn get_passwd_reader(
66-
input: &SecretInputArg,
66+
input: &AuthInputArg,
6767
) -> Result<Box<dyn PasswordReader>> {
68-
Ok(match input.auth_method {
68+
Ok(match input.method {
6969
SecretInput::Cdr => {
70-
let cdr = CdReader::new(input.auth_dev.as_ref());
70+
let cdr = CdReader::new(input.device.as_ref());
7171
Box::new(CdrPasswordReader::new(cdr))
7272
}
7373
SecretInput::Iso => {
74-
Box::new(IsoPasswordReader::new(input.auth_dev.as_ref())?)
74+
Box::new(IsoPasswordReader::new(input.device.as_ref())?)
7575
}
7676
SecretInput::Stdio => Box::new(StdioPasswordReader {}),
7777
})
@@ -139,17 +139,26 @@ impl PasswordReader for CdrPasswordReader {
139139
}
140140
}
141141

142+
#[derive(Args, Clone, Debug, Default, PartialEq)]
143+
pub struct ShareInputArg {
144+
#[clap(long = "share-method", env)]
145+
method: SecretInput,
146+
147+
#[clap(long = "share-device", env)]
148+
device: Option<PathBuf>,
149+
}
150+
142151
pub fn get_share_reader(
143-
input: &SecretInputArg,
152+
input: &ShareInputArg,
144153
verifier: Verifier,
145154
) -> Result<Box<dyn Iterator<Item = Result<Zeroizing<Share>>>>> {
146-
Ok(match input.auth_method {
155+
Ok(match input.method {
147156
SecretInput::Cdr => {
148-
let cdr = CdReader::new(input.auth_dev.as_ref());
157+
let cdr = CdReader::new(input.device.as_ref());
149158
Box::new(CdrShareReader::new(cdr, verifier))
150159
}
151160
SecretInput::Iso => {
152-
Box::new(IsoShareReader::new(input.auth_dev.as_ref(), verifier)?)
161+
Box::new(IsoShareReader::new(input.device.as_ref(), verifier)?)
153162
}
154163
SecretInput::Stdio => Box::new(StdioShareReader::new(verifier)),
155164
})

0 commit comments

Comments
 (0)