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

  belongs_to :user
5
  has_many :gpg_signatures, dependent: :nullify
Alexis Reigel's avatar
Alexis Reigel committed
6

7
8
  validates :user, presence: true

Alexis Reigel's avatar
Alexis Reigel committed
9
10
11
12
  validates :key,
    presence: true,
    uniqueness: true,
    format: {
13
14
      with: /\A#{KEY_PREFIX}((?!#{KEY_PREFIX}).)+\Z/m,
      message: "is invalid. A valid public GPG key begins with '#{KEY_PREFIX}'"
Alexis Reigel's avatar
Alexis Reigel committed
15
16
    }

17
18
19
20
21
22
23
  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) }

24
25
26
27
28
29
30
31
  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
32
33
  after_commit :update_invalid_gpg_signatures, on: :create
  after_commit :notify_user, on: :create
Alexis Reigel's avatar
Alexis Reigel committed
34
35
36
37
38
39

  def key=(value)
    value.strip! unless value.blank?
    write_attribute(:key, value)
  end

Alexis Reigel's avatar
Alexis Reigel committed
40
  def emails
41
    @emails ||= Gitlab::Gpg.emails_from_key(key)
Alexis Reigel's avatar
Alexis Reigel committed
42
43
  end

44
45
  def emails_with_verified_status
    emails.map do |email|
Alexis Reigel's avatar
Alexis Reigel committed
46
47
      [
        email,
Alexis Reigel's avatar
Alexis Reigel committed
48
        email == user.email
Alexis Reigel's avatar
Alexis Reigel committed
49
      ]
Alexis Reigel's avatar
Alexis Reigel committed
50
    end.to_h
Alexis Reigel's avatar
Alexis Reigel committed
51
52
  end

53
54
55
56
  def verified?
    emails_with_verified_status.any? { |_email, verified| verified }
  end

57
  def update_invalid_gpg_signatures
58
    InvalidGpgSignatureUpdateWorker.perform_async(self.id)
59
60
  end

Alexis Reigel's avatar
Alexis Reigel committed
61
62
63
64
65
66
67
68
69
70
71
  def revoke
    GpgSignature.where(gpg_key: self, valid_signature: true).find_each do |gpg_signature|
      gpg_signature.update_attributes!(
        gpg_key: nil,
        valid_signature: false
      )
    end

    destroy
  end

Alexis Reigel's avatar
Alexis Reigel committed
72
73
74
75
76
  private

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

80
81
82
83
84
85
  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

86
  def notify_user
87
    NotificationService.new.new_gpg_key(self)
88
  end
Alexis Reigel's avatar
Alexis Reigel committed
89
end