2012-02-07 16:56:53 -05:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2011-10-08 17:36:38 -04:00
|
|
|
class Key < ActiveRecord::Base
|
2017-11-29 10:30:17 -05:00
|
|
|
include AfterCommitQueue
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2013-06-24 13:07:21 -04:00
|
|
|
|
2011-10-08 17:36:38 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-11-15 14:16:45 -05:00
|
|
|
before_validation :generate_fingerprint
|
2012-10-08 20:10:04 -04:00
|
|
|
|
2016-12-02 07:54:57 -05:00
|
|
|
validates :title,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 255 }
|
2017-08-21 06:30:03 -04:00
|
|
|
|
2016-12-02 07:54:57 -05:00
|
|
|
validates :key,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 5000 },
|
|
|
|
format: { with: /\A(ssh|ecdsa)-.*\Z/ }
|
2017-08-21 06:30:03 -04:00
|
|
|
|
2016-12-02 07:54:57 -05:00
|
|
|
validates :fingerprint,
|
|
|
|
uniqueness: true,
|
|
|
|
presence: { message: 'cannot be generated' }
|
2011-10-08 17:36:38 -04:00
|
|
|
|
2017-08-25 09:08:48 -04:00
|
|
|
validate :key_meets_restrictions
|
2017-08-21 06:30:03 -04:00
|
|
|
|
2012-08-10 18:07:50 -04:00
|
|
|
delegate :name, :email, to: :user, prefix: true
|
2012-02-07 16:56:53 -05:00
|
|
|
|
2017-06-01 17:36:04 -04:00
|
|
|
after_commit :add_to_shell, on: :create
|
2014-08-18 10:46:46 -04:00
|
|
|
after_create :post_create_hook
|
2017-11-15 09:47:10 -05:00
|
|
|
after_create :refresh_user_cache
|
2017-06-01 17:36:04 -04:00
|
|
|
after_commit :remove_from_shell, on: :destroy
|
2014-08-18 10:46:46 -04:00
|
|
|
after_destroy :post_destroy_hook
|
2017-11-15 09:47:10 -05:00
|
|
|
after_destroy :refresh_user_cache
|
2014-04-02 14:13:05 -04:00
|
|
|
|
2016-11-15 14:16:45 -05:00
|
|
|
def key=(value)
|
2018-02-01 17:00:16 -05:00
|
|
|
write_attribute(:key, value.present? ? Gitlab::SSHPublicKey.sanitize(value) : nil)
|
|
|
|
|
2017-10-03 13:31:16 -04:00
|
|
|
@public_key = nil
|
2012-02-07 17:32:20 -05:00
|
|
|
end
|
|
|
|
|
2015-06-19 13:17:34 -04:00
|
|
|
def publishable_key
|
2016-08-02 01:56:23 -04:00
|
|
|
# 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(' ')
|
2015-06-19 13:17:34 -04:00
|
|
|
end
|
|
|
|
|
2012-06-07 08:44:57 -04:00
|
|
|
# projects that has this key
|
2011-10-08 17:36:38 -04:00
|
|
|
def projects
|
2013-05-06 05:26:36 -04:00
|
|
|
user.authorized_projects
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|
2012-08-28 17:04:06 -04:00
|
|
|
|
2013-02-05 04:12:15 -05:00
|
|
|
def shell_id
|
2013-05-06 05:26:36 -04:00
|
|
|
"key-#{id}"
|
2013-02-04 08:07:56 -05:00
|
|
|
end
|
2013-06-24 13:07:21 -04:00
|
|
|
|
2016-12-21 09:59:54 -05:00
|
|
|
def update_last_used_at
|
2017-09-20 08:21:15 -04:00
|
|
|
Keys::LastUsedService.new(self).execute
|
2016-12-21 09:59:54 -05:00
|
|
|
end
|
|
|
|
|
2014-04-02 14:13:05 -04:00
|
|
|
def add_to_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:add_key,
|
|
|
|
shell_id,
|
|
|
|
key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-08-18 10:46:46 -04:00
|
|
|
def post_create_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
2014-04-02 14:13:05 -04:00
|
|
|
def remove_from_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:remove_key,
|
|
|
|
shell_id,
|
2017-05-03 07:27:17 -04:00
|
|
|
key
|
2014-04-02 14:13:05 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-11-15 09:47:10 -05:00
|
|
|
def refresh_user_cache
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
Users::KeysCountService.new(user).refresh_cache
|
|
|
|
end
|
|
|
|
|
2014-08-18 10:46:46 -04:00
|
|
|
def post_destroy_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
def public_key
|
|
|
|
@public_key ||= Gitlab::SSHPublicKey.new(key)
|
|
|
|
end
|
|
|
|
|
2013-06-24 13:07:21 -04:00
|
|
|
private
|
|
|
|
|
2015-01-18 10:29:37 -05:00
|
|
|
def generate_fingerprint
|
2013-07-17 09:16:34 -04:00
|
|
|
self.fingerprint = nil
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2018-02-01 17:00:16 -05:00
|
|
|
return unless public_key.valid?
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
self.fingerprint = public_key.fingerprint
|
|
|
|
end
|
|
|
|
|
2017-08-25 09:08:48 -04:00
|
|
|
def key_meets_restrictions
|
2018-02-02 13:39:55 -05:00
|
|
|
restriction = Gitlab::CurrentSettings.key_restriction_for(public_key.type)
|
2017-08-25 09:08:48 -04:00
|
|
|
|
|
|
|
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")
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-25 09:08:48 -04:00
|
|
|
def forbidden_key_type_message
|
|
|
|
allowed_types =
|
2018-02-02 13:39:55 -05:00
|
|
|
Gitlab::CurrentSettings
|
2017-08-21 06:30:03 -04:00
|
|
|
.allowed_key_types
|
|
|
|
.map(&:upcase)
|
|
|
|
.to_sentence(last_word_connector: ', or ', two_words_connector: ' or ')
|
|
|
|
|
2017-08-25 09:08:48 -04:00
|
|
|
"type is forbidden. Must be #{allowed_types}"
|
2013-06-24 13:07:21 -04:00
|
|
|
end
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|