gpg_key.rb 2.65 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1
2
class GpgKey < ActiveRecord::Base
  KEY_PREFIX = '-----BEGIN PGP PUBLIC KEY BLOCK-----'.freeze
Alexis Reigel's avatar
Alexis Reigel committed
3
  KEY_SUFFIX = '-----END PGP PUBLIC KEY BLOCK-----'.freeze
Alexis Reigel's avatar
Alexis Reigel committed
4

5
6
7
8
9
  include ShaAttribute

  sha_attribute :primary_keyid
  sha_attribute :fingerprint

Alexis Reigel's avatar
Alexis Reigel committed
10
  belongs_to :user
11
  has_many :gpg_signatures
Alexis Reigel's avatar
Alexis Reigel committed
12

13
14
  validates :user, presence: true

Alexis Reigel's avatar
Alexis Reigel committed
15
16
17
18
  validates :key,
    presence: true,
    uniqueness: true,
    format: {
Alexis Reigel's avatar
Alexis Reigel committed
19
20
      with: /\A#{KEY_PREFIX}((?!#{KEY_PREFIX})(?!#{KEY_SUFFIX}).)+#{KEY_SUFFIX}\Z/m,
      message: "is invalid. A valid public GPG key begins with '#{KEY_PREFIX}' and ends with '#{KEY_SUFFIX}'"
Alexis Reigel's avatar
Alexis Reigel committed
21
22
    }

23
24
25
26
27
28
29
  validates :fingerprint,
    presence: true,
    uniqueness: true,
    # only validate when the `key` is valid, as we don't want the user to show
    # the error about the fingerprint
    unless: -> { errors.has_key?(:key) }

30
31
32
33
34
35
36
37
  validates :primary_keyid,
    presence: true,
    uniqueness: true,
    # only validate when the `key` is valid, as we don't want the user to show
    # the error about the fingerprint
    unless: -> { errors.has_key?(:key) }

  before_validation :extract_fingerprint, :extract_primary_keyid
38
39
  after_commit :update_invalid_gpg_signatures, on: :create
  after_commit :notify_user, on: :create
Alexis Reigel's avatar
Alexis Reigel committed
40

41
42
43
44
45
46
47
48
  def primary_keyid
    super&.upcase
  end

  def fingerprint
    super&.upcase
  end

Alexis Reigel's avatar
Alexis Reigel committed
49
  def key=(value)
Alexis Reigel's avatar
Alexis Reigel committed
50
    super(value&.strip)
Alexis Reigel's avatar
Alexis Reigel committed
51
52
  end

53
54
55
56
57
58
  def user_infos
    @user_infos ||= Gitlab::Gpg.user_infos_from_key(key)
  end

  def verified_user_infos
    user_infos.select do |user_info|
59
      user.verified_email?(user_info[:email])
60
    end
61
62
  end

Alexis Reigel's avatar
Alexis Reigel committed
63
  def emails_with_verified_status
64
    user_infos.map do |user_info|
Alexis Reigel's avatar
Alexis Reigel committed
65
      [
66
        user_info[:email],
67
        user.verified_email?(user_info[:email])
Alexis Reigel's avatar
Alexis Reigel committed
68
      ]
Alexis Reigel's avatar
Alexis Reigel committed
69
    end.to_h
Alexis Reigel's avatar
Alexis Reigel committed
70
71
  end

72
  def verified?
73
    emails_with_verified_status.values.any?
74
75
  end

76
  def verified_and_belongs_to_email?(email)
77
    emails_with_verified_status.fetch(email, false)
78
79
  end

80
  def update_invalid_gpg_signatures
81
    InvalidGpgSignatureUpdateWorker.perform_async(self.id)
82
83
  end

Alexis Reigel's avatar
Alexis Reigel committed
84
  def revoke
85
86
87
88
89
    GpgSignature.where(gpg_key: self, valid_signature: true).update_all(
      gpg_key_id: nil,
      valid_signature: false,
      updated_at: Time.zone.now
    )
Alexis Reigel's avatar
Alexis Reigel committed
90
91
92
93

    destroy
  end

Alexis Reigel's avatar
Alexis Reigel committed
94
95
96
97
98
  private

  def extract_fingerprint
    # we can assume that the result only contains one item as the validation
    # only allows one key
99
    self.fingerprint = Gitlab::Gpg.fingerprints_from_key(key).first
Alexis Reigel's avatar
Alexis Reigel committed
100
  end
101

102
103
104
105
106
107
  def extract_primary_keyid
    # we can assume that the result only contains one item as the validation
    # only allows one key
    self.primary_keyid = Gitlab::Gpg.primary_keyids_from_key(key).first
  end

108
  def notify_user
109
    NotificationService.new.new_gpg_key(self)
110
  end
Alexis Reigel's avatar
Alexis Reigel committed
111
end