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