debian-mirror-gitlab/lib/gitlab/gpg/commit.rb

139 lines
4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module Gpg
class Commit
2018-03-27 19:54:05 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
def initialize(commit)
@commit = commit
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
repo = commit.project.repository.raw_repository
2018-03-27 19:54:05 +05:30
@signature_data = Gitlab::Git::Commit.extract_signature_lazily(repo, commit.sha || commit.id)
end
def signature_text
strong_memoize(:signature_text) do
@signature_data&.itself && @signature_data[0]
end
end
def signed_text
strong_memoize(:signed_text) do
@signature_data&.itself && @signature_data[1]
end
2017-09-10 17:25:29 +05:30
end
def has_signature?
2018-03-27 19:54:05 +05:30
!!(signature_text && signed_text)
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def signature
return unless has_signature?
return @signature if @signature
2018-03-17 18:26:18 +05:30
cached_signature = GpgSignature.find_by(commit_sha: @commit.sha)
2017-09-10 17:25:29 +05:30
return @signature = cached_signature if cached_signature.present?
@signature = create_cached_signature!
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def update_signature!(cached_signature)
using_keychain do |gpg_key|
2018-11-18 11:00:15 +05:30
cached_signature.update!(attributes(gpg_key))
2019-02-15 15:39:39 +05:30
@signature = cached_signature
2017-09-10 17:25:29 +05:30
end
end
private
def using_keychain
Gitlab::Gpg.using_tmp_keychain do
# first we need to get the keyid from the signature to query the gpg
# key belonging to the keyid.
# This way we can add the key to the temporary keychain and extract
# the proper signature.
2018-03-17 18:26:18 +05:30
# NOTE: the invoked method is #fingerprint but it's only returning
# 16 characters (the format used by keyid) instead of 40.
2019-02-15 15:39:39 +05:30
fingerprint = verified_signature&.fingerprint
break unless fingerprint
gpg_key = find_gpg_key(fingerprint)
2017-09-10 17:25:29 +05:30
if gpg_key
Gitlab::Gpg::CurrentKeyChain.add(gpg_key.key)
2019-02-15 15:39:39 +05:30
clear_memoization(:verified_signature)
2017-09-10 17:25:29 +05:30
end
yield gpg_key
end
end
def verified_signature
2019-02-15 15:39:39 +05:30
strong_memoize(:verified_signature) { gpgme_signature }
end
def gpgme_signature
GPGME::Crypto.new.verify(signature_text, signed_text: signed_text) do |verified_signature|
# Return the first signature for now: https://gitlab.com/gitlab-org/gitlab-ce/issues/54932
2017-09-10 17:25:29 +05:30
break verified_signature
end
2019-02-15 15:39:39 +05:30
rescue GPGME::Error
nil
2017-09-10 17:25:29 +05:30
end
def create_cached_signature!
using_keychain do |gpg_key|
2019-03-02 22:35:43 +05:30
attributes = attributes(gpg_key)
break GpgSignature.new(attributes) if Gitlab::Database.read_only?
GpgSignature.safe_create!(attributes)
2017-09-10 17:25:29 +05:30
end
end
def attributes(gpg_key)
user_infos = user_infos(gpg_key)
2018-03-17 18:26:18 +05:30
verification_status = verification_status(gpg_key)
2017-09-10 17:25:29 +05:30
{
2018-03-17 18:26:18 +05:30
commit_sha: @commit.sha,
project: @commit.project,
2017-09-10 17:25:29 +05:30
gpg_key: gpg_key,
2019-02-15 15:39:39 +05:30
gpg_key_primary_keyid: gpg_key&.keyid || verified_signature&.fingerprint,
2017-09-10 17:25:29 +05:30
gpg_key_user_name: user_infos[:name],
gpg_key_user_email: user_infos[:email],
2018-03-17 18:26:18 +05:30
verification_status: verification_status
2017-09-10 17:25:29 +05:30
}
end
2018-03-17 18:26:18 +05:30
def verification_status(gpg_key)
return :unknown_key unless gpg_key
return :unverified_key unless gpg_key.verified?
2019-02-15 15:39:39 +05:30
return :unverified unless verified_signature&.valid?
2018-03-17 18:26:18 +05:30
if gpg_key.verified_and_belongs_to_email?(@commit.committer_email)
:verified
elsif gpg_key.user.all_emails.include?(@commit.committer_email)
:same_user_different_email
else
:other_user
end
2017-09-10 17:25:29 +05:30
end
def user_infos(gpg_key)
gpg_key&.verified_user_infos&.first || gpg_key&.user_infos&.first || {}
end
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def find_gpg_key(keyid)
GpgKey.find_by(primary_keyid: keyid) || GpgKeySubkey.find_by(keyid: keyid)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
end
end
end