gitlab-org--gitlab-foss/lib/gitlab/gpg.rb

81 lines
2 KiB
Ruby
Raw Normal View History

2017-02-22 11:20:42 -05:00
module Gitlab
module Gpg
extend self
MUTEX = Mutex.new
module CurrentKeyChain
extend self
def add(key)
GPGME::Key.import(key)
end
2017-07-06 05:55:56 -04:00
def fingerprints_from_key(key)
2017-02-22 11:20:42 -05:00
import = GPGME::Key.import(key)
return [] if import.imported == 0
import.imports.map(&:fingerprint)
end
end
2017-07-06 05:55:56 -04:00
def fingerprints_from_key(key)
using_tmp_keychain do
2017-07-06 05:55:56 -04:00
CurrentKeyChain.fingerprints_from_key(key)
end
end
2017-07-06 05:55:56 -04:00
def primary_keyids_from_key(key)
using_tmp_keychain do
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
GPGME::Key.find(:public, fingerprints).map { |raw_key| raw_key.primary_subkey.keyid }
end
end
def user_infos_from_key(key)
2017-02-24 14:07:57 -05:00
using_tmp_keychain do
2017-07-06 05:55:56 -04:00
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
2017-02-24 14:07:57 -05:00
GPGME::Key.find(:public, fingerprints).flat_map do |raw_key|
raw_key.uids.map { |uid| { name: uid.name, email: uid.email } }
end
2017-02-24 14:07:57 -05:00
end
end
# Allows thread safe switching of temporary keychain files
#
# 1. The current thread may use nesting of temporary keychain
# 2. Another thread needs to wait for the lock to be released
def using_tmp_keychain(&block)
if MUTEX.locked? && MUTEX.owned?
optimistic_using_tmp_keychain(&block)
else
MUTEX.synchronize do
optimistic_using_tmp_keychain(&block)
end
end
end
# 1. Returns the custom home directory if one has been set by calling
# `GPGME::Engine.home_dir=`
# 2. Returns the default home directory otherwise
def current_home_dir
GPGME::Engine.info.first.home_dir || GPGME::Engine.dirinfo('homedir')
end
private
def optimistic_using_tmp_keychain
2017-08-14 06:38:08 -04:00
previous_dir = current_home_dir
2017-02-22 11:20:42 -05:00
Dir.mktmpdir do |dir|
GPGME::Engine.home_dir = dir
2017-08-14 06:38:08 -04:00
yield
2017-02-22 11:20:42 -05:00
end
2017-08-14 06:38:08 -04:00
ensure
GPGME::Engine.home_dir = previous_dir
2017-02-22 11:20:42 -05:00
end
end
end