Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor connection handling for multiple devices #119

Closed
wants to merge 5 commits into from

Conversation

robinkrahl
Copy link
Collaborator

This patch series refactors the connection handling into two new functions, find_device and connect, and improves the handling of multiple attached devices as discussed in issue #99: If mulitple attached devices match the filter set by the user, nitrocli now fails instead of just picking the first match. And the --serial-number option now allows the user to filter the devices by serial number. (We already have the --model option to filter by model.)

This means that we always list all attached Nitrokey devices before connecting. Still the overhead is very small as the list can be read from the sys file system without any communication with the Nitrokey device itself.

Unfortunately, the Nitrokey Storage sends a wrong serial number in the USB device descriptor that is used for listing the devices, see Nitrokey/nitrokey-storage-firmware#88. Therefore the new --serial-number option only works for Nitrokey Pro devices. Originally, I wanted to wait with this feature until this is fixed in the firmware, but I don’t think this will happen any time soon. (And we would still have to support devices with older firmware versions.)

As a next step, we could add support for multiple devices to the status command.

This patch introduces two new functions, find_device and connect, to
connect to a Nitrokey device.  find_device queries the attached Nitrokey
devices, applies the filters (currently only the --model option) and
returns the first match.  connect calls find_device and connects to the
returned device.

This refactoring allows us to add more device filters, for example a
--serial-number option, without code duplication.
Previously, we just applied our filter (if any) to all attached Nitrokey
devices and selected the first match when connection to a Nitrokey
device.  This may lead to unexpected behaviour if multiple devices are
attached.  This patch changes the find_device function to return an
error if multiple matching devices are found.
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.
@robinkrahl robinkrahl requested a review from d-e-s-o September 6, 2020 22:32
@d-e-s-o
Copy link
Owner

d-e-s-o commented Sep 7, 2020

Thanks for the pull request! Looks good! I've made a few cosmetic changes and merged it. But can we come up with a way to test this functionality better? E.g., one way could be to have a test function that accepts any model (runs on pro & storage), lists the available devices to get their serial number, and then invokes nitrocli with --serial-number for each to see whether we connect to the correct device. Something similar could be done to check whether we bail out properly when multiple devices are attached. Do you think that's feasible?

@d-e-s-o d-e-s-o closed this Sep 7, 2020
@d-e-s-o d-e-s-o self-assigned this Sep 7, 2020
@robinkrahl
Copy link
Collaborator Author

robinkrahl commented Sep 7, 2020 via email

@d-e-s-o
Copy link
Owner

d-e-s-o commented Sep 7, 2020

Oh, sorry about that :-| Didn't want too many review round trips. I'll merge your suggestions later today, but probably just as fixup! commits. Doesn't really matter to me in devel. Thanks for checking!

@robinkrahl robinkrahl deleted the serial-number branch September 10, 2020 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants