2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-22 11:20:42 -05:00
|
|
|
module Gitlab
|
|
|
|
module Gpg
|
|
|
|
extend self
|
|
|
|
|
2019-11-02 23:06:20 -04:00
|
|
|
CleanupError = Class.new(StandardError)
|
2019-11-25 16:06:28 -05:00
|
|
|
BG_CLEANUP_RUNTIME_S = 10
|
2020-01-23 07:08:38 -05:00
|
|
|
FG_CLEANUP_RUNTIME_S = 1
|
2019-11-02 23:06:20 -04:00
|
|
|
|
2017-08-10 14:33:24 -04:00
|
|
|
MUTEX = Mutex.new
|
|
|
|
|
2017-06-13 08:26:42 -04:00
|
|
|
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)
|
2017-06-13 07:46:43 -04:00
|
|
|
using_tmp_keychain do
|
2017-07-06 05:55:56 -04:00
|
|
|
CurrentKeyChain.fingerprints_from_key(key)
|
|
|
|
end
|
|
|
|
end
|
2017-06-13 07:46:43 -04:00
|
|
|
|
2017-07-06 05:55:56 -04:00
|
|
|
def primary_keyids_from_key(key)
|
|
|
|
using_tmp_keychain do
|
|
|
|
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
|
2017-06-13 07:46:43 -04:00
|
|
|
|
|
|
|
GPGME::Key.find(:public, fingerprints).map { |raw_key| raw_key.primary_subkey.keyid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 20:42:23 -04:00
|
|
|
def subkeys_from_key(key)
|
|
|
|
using_tmp_keychain do
|
2017-09-29 18:55:36 -04:00
|
|
|
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
|
|
|
|
raw_keys = GPGME::Key.find(:public, fingerprints)
|
2017-09-26 20:42:23 -04:00
|
|
|
|
2017-09-29 18:55:36 -04:00
|
|
|
raw_keys.each_with_object({}) do |raw_key, grouped_subkeys|
|
2017-09-26 20:42:23 -04:00
|
|
|
primary_subkey_id = raw_key.primary_subkey.keyid
|
|
|
|
|
2021-12-14 07:13:33 -05:00
|
|
|
grouped_subkeys[primary_subkey_id] = raw_key.subkeys[1..].map do |s|
|
2017-09-29 18:55:36 -04:00
|
|
|
{ keyid: s.keyid, fingerprint: s.fingerprint }
|
2017-09-26 20:42:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-13 09:22:15 -04:00
|
|
|
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
|
|
|
|
2017-07-13 09:22:15 -04:00
|
|
|
GPGME::Key.find(:public, fingerprints).flat_map do |raw_key|
|
2018-06-05 17:39:44 -04:00
|
|
|
raw_key.uids.each_with_object([]) do |uid, arr|
|
|
|
|
name = uid.name.force_encoding('UTF-8')
|
|
|
|
email = uid.email.force_encoding('UTF-8')
|
|
|
|
arr << { name: name, email: email.downcase } if name.valid_encoding? && email.valid_encoding?
|
|
|
|
end
|
2017-07-13 09:22:15 -04:00
|
|
|
end
|
2017-02-24 14:07:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-10 14:33:24 -04:00
|
|
|
# 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
|
2018-12-15 04:06:56 -05:00
|
|
|
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
2018-07-28 09:47:26 -04:00
|
|
|
MUTEX.synchronize do
|
|
|
|
optimistic_using_tmp_keychain(&block)
|
|
|
|
end
|
2017-08-10 14:33:24 -04:00
|
|
|
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-09-13 07:31:37 -04:00
|
|
|
tmp_dir = Dir.mktmpdir
|
|
|
|
GPGME::Engine.home_dir = tmp_dir
|
2019-11-02 23:06:20 -04:00
|
|
|
tmp_keychains_created.increment
|
|
|
|
|
2017-09-13 07:31:37 -04:00
|
|
|
yield
|
2017-08-14 06:38:08 -04:00
|
|
|
ensure
|
2019-11-02 23:06:20 -04:00
|
|
|
GPGME::Engine.home_dir = previous_dir
|
|
|
|
|
|
|
|
begin
|
|
|
|
cleanup_tmp_dir(tmp_dir)
|
|
|
|
rescue CleanupError => e
|
2019-11-25 16:06:28 -05:00
|
|
|
folder_contents = Dir.children(tmp_dir)
|
2019-11-02 23:06:20 -04:00
|
|
|
# This means we left a GPG-agent process hanging. Logging the problem in
|
|
|
|
# sentry will make this more visible.
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e,
|
2019-11-02 23:06:20 -04:00
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab/issues/20918',
|
2019-12-13 07:07:41 -05:00
|
|
|
tmp_dir: tmp_dir, contents: folder_contents)
|
2019-11-02 23:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
tmp_keychains_removed.increment unless File.exist?(tmp_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_tmp_dir(tmp_dir)
|
|
|
|
# Retry when removing the tmp directory failed, as we may run into a
|
2017-09-13 07:31:37 -04:00
|
|
|
# race condition:
|
|
|
|
# The `gpg-agent` agent process may clean up some files as well while
|
|
|
|
# `FileUtils.remove_entry` is iterating the directory and removing all
|
|
|
|
# its contained files and directories recursively, which could raise an
|
|
|
|
# error.
|
2019-11-02 23:06:20 -04:00
|
|
|
# Failing to remove the tmp directory could leave the `gpg-agent` process
|
|
|
|
# running forever.
|
2020-01-23 07:08:38 -05:00
|
|
|
#
|
|
|
|
# 15 tries will never complete within the maximum time with exponential
|
|
|
|
# backoff. So our limit is the runtime, not the number of tries.
|
|
|
|
Retriable.retriable(max_elapsed_time: cleanup_time, base_interval: 0.1, tries: 15) do
|
2019-11-02 23:06:20 -04:00
|
|
|
FileUtils.remove_entry(tmp_dir) if File.exist?(tmp_dir)
|
|
|
|
end
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2019-11-02 23:06:20 -04:00
|
|
|
raise CleanupError, e
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_time
|
2019-12-22 04:07:51 -05:00
|
|
|
Gitlab::Runtime.sidekiq? ? BG_CLEANUP_RUNTIME_S : FG_CLEANUP_RUNTIME_S
|
2019-11-02 23:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_keychains_created
|
2020-11-25 04:09:14 -05:00
|
|
|
Gitlab::Metrics.counter(:gpg_tmp_keychains_created_total, 'The number of temporary GPG keychains created')
|
2019-11-02 23:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_keychains_removed
|
2020-11-25 04:09:14 -05:00
|
|
|
Gitlab::Metrics.counter(:gpg_tmp_keychains_removed_total, 'The number of temporary GPG keychains removed')
|
2017-02-22 11:20:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|