Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@srfilipek
Copy link
Contributor

Overview

This change allows users to use pivit while multiple YubiKeys are attached, or just want specificity when interacting with a YubiKey. This also tweaks the contributing guide make command.

Errors when:

  • no cards are found
  • multiple cards are found and the serial is not specified
  • one or more cards are found and the serial does not match any

Successfully returns a card when:

  • only one card is found and the serial is not specified
  • one or more cards are found and the serial matches

Note that this also applies if there are other potential PIV sources attached, such as a smart card reader.

Changes

  • Create a new YubikeyHandleWithSerial() function.
  • Make YubikeyHandle() call the new function with backward-compatible behavior.
  • Update runCommand() to read the serial from the environment and use the new function.
  • Tweak the contributing guide.

Testing

Behavior when one YubiKey is attached:

~ $ pivit
specify --help, --sign, --verify, --import, --generate, --reset or --print
~ $ PIVIT_YK_SERIAL= pivit
specify --help, --sign, --verify, --import, --generate, --reset or --print
~ $ PIVIT_YK_SERIAL=1234 pivit
failed to open yubikey: no smart card found with serial number 1234
~ $ PIVIT_YK_SERIAL=10180478 pivit
specify --help, --sign, --verify, --import, --generate, --reset or --print
~ $ PIVIT_YK_SERIAL=asdf pivit
failed to open yubikey: invalid serial number format: strconv.ParseUint: parsing "asdf": invalid syntax

No YubiKeys:

~ $ pivit
failed to open yubikey: no smart card found
~ $ PIVIT_YK_SERIAL=asdf pivit
failed to open yubikey: no smart card found

Multiple yubikeys:

~ $ pivit
failed to open yubikey: multiple smart cards found but no serial specified
~ $ PIVIT_YK_SERIAL=10180478 pivit --print
f605a5b5f56cf0d622334b8d84c2eebaf4ffe2a7
-----BEGIN CERTIFICATE-----
...
~ $ PIVIT_YK_SERIAL=13077792 pivit --print
b59aff995edf77effa418ecfae23efa1445791e1
-----BEGIN CERTIFICATE-----
...
~ $ PIVIT_YK_SERIAL=1234 pivit
failed to open yubikey: no smart card found with serial number 1234

Also tweak the contributing guide make command.

Yubikey discovery behavior below.

Errors when:
- no cards are found
- multiple cards are found and the serial is not specified
- one or more cards are found and the serial does not match any

Successfully returns a card when:
- only one card is found and the serial is not specified
- one or more cards are found and the serial matches

Note that this also applies if there are other potential PIV sources
attached, such as a smart card reader.
Copy link
Collaborator

@yoavamit yoavamit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple and clean!

Please update the README as well

continue
}

// Get serial number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supernit: please remove these comments. This code is simple enough that it doesn't need them.
From a readability perspective - I think it only hurts.

return nil, fmt.Errorf("invalid serial number format: %v", err)
}

// Serial specified - try to find matching card
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@srfilipek srfilipek requested a review from yoavamit April 17, 2025 17:27
Copy link
Collaborator

@yoavamit yoavamit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@codecov
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

Attention: Patch coverage is 0% with 26 lines in your changes missing coverage. Please review.

Project coverage is 57.72%. Comparing base (a278606) to head (44c5e4a).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
pkg/pivit/pivit.go 0.00% 26 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #61      +/-   ##
==========================================
- Coverage   59.15%   57.72%   -1.44%     
==========================================
  Files          10       10              
  Lines         928      951      +23     
==========================================
  Hits          549      549              
- Misses        312      335      +23     
  Partials       67       67              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yoavamit yoavamit merged commit f769868 into cashapp:main Apr 17, 2025
4 of 6 checks passed
@srfilipek srfilipek deleted the sfilipek/yubikey-serial branch April 17, 2025 18:54
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