2018-11-09 18:39:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-14 09:51:34 +00:00
|
|
|
module Gitlab
|
|
|
|
module Gpg
|
2020-02-07 00:09:12 +00:00
|
|
|
class Commit < Gitlab::SignedCommit
|
2017-06-15 12:18:00 +00:00
|
|
|
def update_signature!(cached_signature)
|
|
|
|
using_keychain do |gpg_key|
|
2018-07-02 10:43:06 +00:00
|
|
|
cached_signature.update!(attributes(gpg_key))
|
2018-12-01 07:20:00 +00:00
|
|
|
@signature = cached_signature
|
2017-06-15 12:18:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 11:37:03 +00:00
|
|
|
private
|
|
|
|
|
2022-07-11 21:08:18 +00:00
|
|
|
def signature_class
|
|
|
|
CommitSignatures::GpgSignature
|
2019-10-17 12:07:33 +00:00
|
|
|
end
|
|
|
|
|
2017-06-15 11:37:03 +00:00
|
|
|
def using_keychain
|
2017-06-14 09:51:34 +00:00
|
|
|
Gitlab::Gpg.using_tmp_keychain do
|
2019-06-17 13:33:39 +00:00
|
|
|
# first we need to get the fingerprint from the signature to query the gpg
|
|
|
|
# key belonging to the fingerprint.
|
2017-06-14 09:51:34 +00:00
|
|
|
# This way we can add the key to the temporary keychain and extract
|
|
|
|
# the proper signature.
|
2019-06-17 13:33:39 +00:00
|
|
|
# NOTE: the invoked method is #fingerprint but versions of GnuPG
|
|
|
|
# prior to 2.2.13 return 16 characters (the format used by keyid)
|
|
|
|
# instead of 40.
|
2018-12-01 07:20:00 +00:00
|
|
|
fingerprint = verified_signature&.fingerprint
|
|
|
|
|
|
|
|
break unless fingerprint
|
|
|
|
|
|
|
|
gpg_key = find_gpg_key(fingerprint)
|
2017-06-14 09:51:34 +00:00
|
|
|
|
|
|
|
if gpg_key
|
|
|
|
Gitlab::Gpg::CurrentKeyChain.add(gpg_key.key)
|
2021-11-10 18:12:35 +00:00
|
|
|
clear_memoization(:gpg_signatures)
|
2017-06-14 09:51:34 +00:00
|
|
|
end
|
|
|
|
|
2017-06-15 11:37:03 +00:00
|
|
|
yield gpg_key
|
2017-06-14 09:51:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verified_signature
|
2021-11-10 18:12:35 +00:00
|
|
|
gpg_signatures.first
|
2017-06-14 09:51:34 +00:00
|
|
|
end
|
|
|
|
|
2017-08-15 11:22:55 +00:00
|
|
|
def create_cached_signature!
|
|
|
|
using_keychain do |gpg_key|
|
2019-02-05 17:22:25 +00:00
|
|
|
attributes = attributes(gpg_key)
|
2021-12-13 12:12:59 +00:00
|
|
|
break CommitSignatures::GpgSignature.new(attributes) if Gitlab::Database.read_only?
|
2019-02-05 17:22:25 +00:00
|
|
|
|
2021-12-13 12:12:59 +00:00
|
|
|
CommitSignatures::GpgSignature.safe_create!(attributes)
|
2017-08-15 11:22:55 +00:00
|
|
|
end
|
2017-07-13 13:22:15 +00:00
|
|
|
end
|
|
|
|
|
2021-11-10 18:12:35 +00:00
|
|
|
def gpg_signatures
|
|
|
|
strong_memoize(:gpg_signatures) do
|
|
|
|
signatures = []
|
|
|
|
|
|
|
|
GPGME::Crypto.new.verify(signature_text, signed_text: signed_text) do |verified_signature|
|
|
|
|
signatures << verified_signature
|
|
|
|
end
|
|
|
|
|
|
|
|
signatures
|
|
|
|
rescue GPGME::Error
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def multiple_signatures?
|
|
|
|
gpg_signatures.size > 1
|
|
|
|
end
|
|
|
|
|
2017-07-13 13:22:15 +00:00
|
|
|
def attributes(gpg_key)
|
|
|
|
user_infos = user_infos(gpg_key)
|
2017-08-24 12:21:30 +00:00
|
|
|
verification_status = verification_status(gpg_key)
|
2017-07-13 13:22:15 +00:00
|
|
|
|
|
|
|
{
|
2017-08-24 12:21:26 +00:00
|
|
|
commit_sha: @commit.sha,
|
|
|
|
project: @commit.project,
|
2017-06-14 09:51:34 +00:00
|
|
|
gpg_key: gpg_key,
|
2018-12-01 07:20:00 +00:00
|
|
|
gpg_key_primary_keyid: gpg_key&.keyid || verified_signature&.fingerprint,
|
2017-07-13 13:22:15 +00:00
|
|
|
gpg_key_user_name: user_infos[:name],
|
|
|
|
gpg_key_user_email: user_infos[:email],
|
2017-08-24 12:21:30 +00:00
|
|
|
verification_status: verification_status
|
2017-07-13 13:22:15 +00:00
|
|
|
}
|
2017-06-14 09:51:34 +00:00
|
|
|
end
|
2017-06-15 12:18:00 +00:00
|
|
|
|
2017-08-24 12:21:30 +00:00
|
|
|
def verification_status(gpg_key)
|
2022-01-13 09:15:32 +00:00
|
|
|
return :multiple_signatures if multiple_signatures?
|
2017-08-24 12:22:19 +00:00
|
|
|
return :unknown_key unless gpg_key
|
|
|
|
return :unverified_key unless gpg_key.verified?
|
2018-12-01 07:20:00 +00:00
|
|
|
return :unverified unless verified_signature&.valid?
|
2017-08-24 12:21:46 +00:00
|
|
|
|
|
|
|
if gpg_key.verified_and_belongs_to_email?(@commit.committer_email)
|
2017-08-24 12:22:19 +00:00
|
|
|
:verified
|
2017-08-24 12:21:46 +00:00
|
|
|
elsif gpg_key.user.all_emails.include?(@commit.committer_email)
|
2017-08-24 12:22:19 +00:00
|
|
|
:same_user_different_email
|
2017-08-24 12:21:30 +00:00
|
|
|
else
|
2017-08-24 12:22:19 +00:00
|
|
|
:other_user
|
2017-08-24 12:21:30 +00:00
|
|
|
end
|
2017-06-15 12:18:00 +00:00
|
|
|
end
|
2017-07-13 13:22:15 +00:00
|
|
|
|
|
|
|
def user_infos(gpg_key)
|
|
|
|
gpg_key&.verified_user_infos&.first || gpg_key&.user_infos&.first || {}
|
|
|
|
end
|
2017-09-28 00:45:19 +00:00
|
|
|
|
2019-06-17 13:33:39 +00:00
|
|
|
def find_gpg_key(fingerprint)
|
|
|
|
if fingerprint.length > 16
|
|
|
|
GpgKey.find_by_fingerprint(fingerprint) || GpgKeySubkey.find_by_fingerprint(fingerprint)
|
|
|
|
else
|
|
|
|
GpgKey.find_by_primary_keyid(fingerprint) || GpgKeySubkey.find_by_keyid(fingerprint)
|
|
|
|
end
|
2017-09-28 00:45:19 +00:00
|
|
|
end
|
2017-06-14 09:51:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|