2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-05 12:22:25 -05:00
|
|
|
class GpgSignature < ApplicationRecord
|
2017-07-20 09:44:15 -04:00
|
|
|
include ShaAttribute
|
|
|
|
|
|
|
|
sha_attribute :commit_sha
|
|
|
|
sha_attribute :gpg_key_primary_keyid
|
|
|
|
|
2017-08-24 08:21:30 -04:00
|
|
|
enum verification_status: {
|
|
|
|
unverified: 0,
|
|
|
|
verified: 1,
|
2017-08-24 08:21:42 -04:00
|
|
|
same_user_different_email: 2,
|
|
|
|
other_user: 3,
|
|
|
|
unverified_key: 4,
|
|
|
|
unknown_key: 5
|
2017-08-24 08:21:30 -04:00
|
|
|
}
|
|
|
|
|
2017-06-14 03:17:34 -04:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :gpg_key
|
2017-09-27 20:45:19 -04:00
|
|
|
belongs_to :gpg_key_subkey
|
2017-06-14 03:17:34 -04:00
|
|
|
|
|
|
|
validates :commit_sha, presence: true
|
2017-07-25 15:20:48 -04:00
|
|
|
validates :project_id, presence: true
|
2017-06-15 06:43:04 -04:00
|
|
|
validates :gpg_key_primary_keyid, presence: true
|
2017-07-06 04:17:09 -04:00
|
|
|
|
2017-10-04 19:44:49 -04:00
|
|
|
def self.with_key_and_subkeys(gpg_key)
|
2017-10-05 11:17:18 -04:00
|
|
|
subkey_ids = gpg_key.subkeys.pluck(:id)
|
2017-10-04 19:44:49 -04:00
|
|
|
|
|
|
|
where(
|
2017-10-05 11:17:18 -04:00
|
|
|
arel_table[:gpg_key_id].eq(gpg_key.id).or(
|
|
|
|
arel_table[:gpg_key_subkey_id].in(subkey_ids)
|
2017-10-04 19:44:49 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-02-05 12:22:25 -05:00
|
|
|
def self.safe_create!(attributes)
|
|
|
|
create_with(attributes)
|
|
|
|
.safe_find_or_create_by!(commit_sha: attributes[:commit_sha])
|
|
|
|
end
|
|
|
|
|
2019-03-28 10:59:24 -04:00
|
|
|
# Find commits that are lacking a signature in the database at present
|
|
|
|
def self.unsigned_commit_shas(commit_shas)
|
|
|
|
return [] if commit_shas.empty?
|
|
|
|
|
|
|
|
signed = GpgSignature.where(commit_sha: commit_shas).pluck(:commit_sha)
|
|
|
|
|
|
|
|
commit_shas - signed
|
|
|
|
end
|
|
|
|
|
2017-09-27 20:45:19 -04:00
|
|
|
def gpg_key=(model)
|
|
|
|
case model
|
2017-10-04 11:34:50 -04:00
|
|
|
when GpgKey
|
|
|
|
super
|
|
|
|
when GpgKeySubkey
|
|
|
|
self.gpg_key_subkey = model
|
|
|
|
when NilClass
|
|
|
|
super
|
|
|
|
self.gpg_key_subkey = nil
|
2017-09-27 20:45:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gpg_key
|
|
|
|
if gpg_key_id
|
|
|
|
super
|
|
|
|
elsif gpg_key_subkey_id
|
|
|
|
gpg_key_subkey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-25 10:23:52 -04:00
|
|
|
def gpg_key_primary_keyid
|
|
|
|
super&.upcase
|
|
|
|
end
|
|
|
|
|
2017-07-06 04:17:09 -04:00
|
|
|
def commit
|
|
|
|
project.commit(commit_sha)
|
|
|
|
end
|
2017-08-15 07:22:55 -04:00
|
|
|
|
|
|
|
def gpg_commit
|
2017-10-07 11:47:53 -04:00
|
|
|
return unless commit
|
|
|
|
|
2017-08-24 08:21:26 -04:00
|
|
|
Gitlab::Gpg::Commit.new(commit)
|
2017-08-15 07:22:55 -04:00
|
|
|
end
|
2017-06-14 03:17:34 -04:00
|
|
|
end
|