2012-02-07 16:56:53 -05:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2011-10-08 17:36:38 -04:00
|
|
|
class Key < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2013-06-24 13:07:21 -04:00
|
|
|
|
2017-01-20 05:42:46 -05:00
|
|
|
LAST_USED_AT_REFRESH_TIME = 1.day.to_i
|
|
|
|
|
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 }
|
|
|
|
validates :key,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 5000 },
|
|
|
|
format: { with: /\A(ssh|ecdsa)-.*\Z/ }
|
|
|
|
validates :fingerprint,
|
|
|
|
uniqueness: true,
|
|
|
|
presence: { message: 'cannot be generated' }
|
2011-10-08 17:36:38 -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
|
|
|
|
after_commit :notify_user, on: :create
|
2014-08-18 10:46:46 -04:00
|
|
|
after_create :post_create_hook
|
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
|
2014-04-02 14:13:05 -04:00
|
|
|
|
2016-11-15 14:16:45 -05:00
|
|
|
def key=(value)
|
2017-08-03 12:39:10 -04:00
|
|
|
value&.delete!("\n\r")
|
2016-11-15 14:16:45 -05:00
|
|
|
value.strip! unless value.blank?
|
|
|
|
write_attribute(:key, value)
|
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-01-20 05:42:46 -05: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 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
|
|
|
|
|
2014-08-18 10:46:46 -04:00
|
|
|
def post_destroy_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :destroy)
|
|
|
|
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
|
|
|
|
|
|
|
return unless self.key.present?
|
|
|
|
|
|
|
|
self.fingerprint = Gitlab::KeyFingerprint.new(self.key).fingerprint
|
2013-06-24 13:07:21 -04:00
|
|
|
end
|
2016-11-17 12:23:04 -05:00
|
|
|
|
|
|
|
def notify_user
|
2017-06-01 17:36:04 -04:00
|
|
|
NotificationService.new.new_key(self)
|
2016-11-17 12:23:04 -05:00
|
|
|
end
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|