Skip to content

Commit

Permalink
Pass on resolved USB path to extensions
Browse files Browse the repository at this point in the history
To avoid reimplementing the device selection logic in extensions, we
introduce a new environment variable NITROCLI_RESOLVED_USB_PATH that is
set to the USB path of the single matching Nitrokey device. If no device
matches, or if there are multiple matching devices, the variable
is not set.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Apr 12, 2021
1 parent 3d4ec0a commit 899fa2d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------
- Enabled usage of empty PWS slot fields
- Added `NITROCLI_RESOLVED_USB_PATH` environment variable to be used by
extensions
- Allowed entering of `base32` encoded strings containing spaces
- Fixed pinentry dialog highlighting some messages incorrectly as errors
- Bumped `nitrokey` dependency to `0.9.0`
Expand Down
10 changes: 8 additions & 2 deletions doc/nitrocli.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH NITROCLI 1 2020-10-11
.TH NITROCLI 1 2021-04-14
.SH NAME
nitrocli \- access Nitrokey devices
.SH SYNOPSIS
Expand Down Expand Up @@ -427,12 +427,18 @@ The program conveys basic configuration information to any extension being
started this way. Specifically, it will set each environment variable as
described in the Configuration subsection of the Environment section above, if
the corresponding \fBnitrocli\fR program configuration was set. In addition, the
following variable will be set unconditionally:
following variables will be set:
.TP
.B NITROCLI_BINARY
The absolute path to the \fBnitrocli\fR binary through which the extension was
invoked. This path may be used to recursively invoke \fBnitrocli\fR to implement
certain functionality.
.TP
.B NITROCLI_RESOLVED_USB_PATH
The USB path of the device that \fBnitrocli\fR would connect to based on the
\fB\-\-model\fR, \fB\-\-serial-number\fR, and \fB\-\-usb-path\fR options.
If there is no matching Nitrokey device, or if multiple devices match the
options, the environment variable is not set.

.P
All other variables present in the environment will be passed through to the
Expand Down
Binary file modified doc/nitrocli.1.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,10 @@ pub fn extension(ctx: &mut Context<'_>, args: Vec<ffi::OsString>) -> anyhow::Res
// a cargo test context.
let mut cmd = process::Command::new(&ext_path);

if let Ok(device_info) = find_device(&ctx.config) {
let _ = cmd.env(crate::NITROCLI_RESOLVED_USB_PATH, device_info.path);
}

if let Some(model) = ctx.config.model {
let _ = cmd.env(crate::NITROCLI_MODEL, model.to_string());
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use structopt::clap::SubCommand;
use structopt::StructOpt;

const NITROCLI_BINARY: &str = "NITROCLI_BINARY";
const NITROCLI_RESOLVED_USB_PATH: &str = "NITROCLI_RESOLVED_USB_PATH";
const NITROCLI_MODEL: &str = "NITROCLI_MODEL";
const NITROCLI_USB_PATH: &str = "NITROCLI_USB_PATH";
const NITROCLI_VERBOSITY: &str = "NITROCLI_VERBOSITY";
Expand Down

0 comments on commit 899fa2d

Please sign in to comment.