key.rb 3.14 KB
Newer Older
1 2
# frozen_string_literal: true

miks's avatar
miks committed
3 4
require 'digest/md5'

gitlabhq's avatar
gitlabhq committed
5
class Key < ActiveRecord::Base
6
  include AfterCommitQueue
7
  include Sortable
8

gitlabhq's avatar
gitlabhq committed
9 10
  belongs_to :user

11
  before_validation :generate_fingerprint
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
12

13 14 15
  validates :title,
    presence: true,
    length: { maximum: 255 }
16

17 18 19 20
  validates :key,
    presence: true,
    length: { maximum: 5000 },
    format: { with: /\A(ssh|ecdsa)-.*\Z/ }
21

22 23 24
  validates :fingerprint,
    uniqueness: true,
    presence: { message: 'cannot be generated' }
gitlabhq's avatar
gitlabhq committed
25

26 27 28
  validate :key_meets_restrictions

  # EE-only
29 30
  scope :ldap, -> { where(type: 'LDAPKey') }

31
  delegate :name, :email, to: :user, prefix: true
miks's avatar
miks committed
32

33
  after_commit :add_to_shell, on: :create
34
  after_create :post_create_hook
35
  after_create :refresh_user_cache
36
  after_commit :remove_from_shell, on: :destroy
37
  after_destroy :post_destroy_hook
38
  after_destroy :refresh_user_cache
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
39

40 41 42 43
  def self.regular_keys
    where(type: ['Key', nil])
  end

44
  def key=(value)
Rubén Dávila's avatar
Rubén Dávila committed
45 46
    write_attribute(:key, value.present? ? Gitlab::SSHPublicKey.sanitize(value) : nil)

47
    @public_key = nil
miks's avatar
miks committed
48 49
  end

50
  def publishable_key
51 52 53
    # Strip out the keys comment so we don't leak email addresses
    # Replace with simple ident of user_name (hostname)
    self.key.split[0..1].push("#{self.user_name} (#{Gitlab.config.gitlab.host})").join(' ')
54 55
  end

56
  # projects that has this key
gitlabhq's avatar
gitlabhq committed
57
  def projects
58
    user.authorized_projects
gitlabhq's avatar
gitlabhq committed
59
  end
60

61
  def shell_id
62
    "key-#{id}"
63
  end
64

65
  # rubocop: disable CodeReuse/ServiceClass
66
  def update_last_used_at
67
    Keys::LastUsedService.new(self).execute
68
  end
69
  # rubocop: enable CodeReuse/ServiceClass
70

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
71 72 73 74 75 76 77 78
  def add_to_shell
    GitlabShellWorker.perform_async(
      :add_key,
      shell_id,
      key
    )
  end

79
  # rubocop: disable CodeReuse/ServiceClass
80 81 82
  def post_create_hook
    SystemHooksService.new.execute_hooks_for(self, :create)
  end
83
  # rubocop: enable CodeReuse/ServiceClass
84

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
85 86 87 88
  def remove_from_shell
    GitlabShellWorker.perform_async(
      :remove_key,
      shell_id,
89
      key
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
90 91 92
    )
  end

93
  # rubocop: disable CodeReuse/ServiceClass
94 95 96 97 98
  def refresh_user_cache
    return unless user

    Users::KeysCountService.new(user).refresh_cache
  end
99
  # rubocop: enable CodeReuse/ServiceClass
100

101
  # rubocop: disable CodeReuse/ServiceClass
102 103 104
  def post_destroy_hook
    SystemHooksService.new.execute_hooks_for(self, :destroy)
  end
105
  # rubocop: enable CodeReuse/ServiceClass
106

107 108 109 110
  def public_key
    @public_key ||= Gitlab::SSHPublicKey.new(key)
  end

111 112
  private

Steven Burgart's avatar
Steven Burgart committed
113
  def generate_fingerprint
114
    self.fingerprint = nil
115

Rubén Dávila's avatar
Rubén Dávila committed
116
    return unless public_key.valid?
117

118 119 120 121
    self.fingerprint = public_key.fingerprint
  end

  def key_meets_restrictions
122
    restriction = Gitlab::CurrentSettings.key_restriction_for(public_key.type)
123 124 125 126 127 128 129 130 131 132

    if restriction == ApplicationSetting::FORBIDDEN_KEY_VALUE
      errors.add(:key, forbidden_key_type_message)
    elsif public_key.bits < restriction
      errors.add(:key, "must be at least #{restriction} bits")
    end
  end

  def forbidden_key_type_message
    allowed_types =
133
      Gitlab::CurrentSettings
134 135 136 137 138
        .allowed_key_types
        .map(&:upcase)
        .to_sentence(last_word_connector: ', or ', two_words_connector: ' or ')

    "type is forbidden. Must be #{allowed_types}"
139
  end
gitlabhq's avatar
gitlabhq committed
140
end
141 142

Key.prepend(EE::Key)