Skip to content

Conversation

@kozmikyak
Copy link

…rough GPGKEY env variable

An issue was created asking for the ability to specify a GPG key to use from the keychain, as it grabs the first. There was an -e option added at some point allowing one to choose the proper GPG key from the keychain by email address; however, if a user has multiple keys that match the same Email address, this will still grab the first key found.

Added an option --fingerprint (or -f) to search for the first key matching the given key fingerprint. The user supplied fingerprint is regex-matched at the END of the fingerprint string stored in the key object, as many users use shortened fingerprint strings.

In addition, if the GPGKEY environment variable (used by other GPG tools) is set to a fingerprint, that is used as the default value of the --fingerprint (-f) option.

I am not an expert ruby coder, but this seemed to work reasonably well without disabling the -e option, and appears to integrate well with other GPG tools that assume use of GPGKEY env variable. I am entirely open to requests for revision, or to other people making better changes.

This is a partial answer to issue #17. It does not follow the setting in the gpg config file, but does follow the env variable used to do the same thing. Ideally both would be supported, but this allows for flexibility to bypass such defaults when desired.

existing_private_keys.detect do |k|
# Match user-supplied fingerprint at END of key's fingerprint string,
# as users often use a shortened fingerprint.
/#{fingerprint}$/.match(k.fingerprint)
Copy link
Owner

Choose a reason for hiding this comment

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

@kozmikyak What is GPGKEY usually set to? I suspect you want to detect k.fingerprint.ends_with(fingerprint) instead of converting it to a regex

# it takes priority as it is more specific. Must handle either or both
# search parameters being nil.
#
if ! fingerprint.nil?
Copy link
Owner

Choose a reason for hiding this comment

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

minor style: in ruby no space between ! and the argument: if !fingerprint.nil?

@ConradIrwin
Copy link
Owner

Thanks for this! I like the idea a lot, though I worry it may lead to some confusing interactions. If the user has set -f or GPGKEY, and the key doesn't exist, dotgpg init will create a new key that won't match the fingerprint, and running dotgpg init again with the same arguments will create another new key, etc. etc.

To fix this I suggest just making dotgpg fail noisily if --fingerprint is passed, or if GPGKEY is set, and there is no matching key.

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.

3 participants