Class: Key

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, Expirable, FromUnion, ShaAttribute, Sortable, Todoable
Defined in:
app/models/key.rb

Direct Known Subclasses

DeployKey, GroupDeployKey

Constant Summary

Constants included from Expirable

Expirable::DAYS_TO_EXPIRE

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Expirable

#expired?, #expires?, #expires_soon?

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.regular_keysObject



66
67
68
# File 'app/models/key.rb', line 66

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

Instance Method Details

#add_to_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass



102
103
104
105
106
# File 'app/models/key.rb', line 102

def add_to_authorized_keys
  return unless Gitlab::CurrentSettings.authorized_keys_enabled?

  AuthorizedKeysWorker.perform_async('add_key', shell_id, key)
end

#can_delete?Boolean

EE overrides this

Returns:

  • (Boolean)


92
93
94
# File 'app/models/key.rb', line 92

def can_delete?
  true
end

#ensure_sha256_fingerprint!Object



138
139
140
141
142
# File 'app/models/key.rb', line 138

def ensure_sha256_fingerprint!
  return if self.fingerprint_sha256

  save if generate_fingerprint
end

#key=(value) ⇒ Object



70
71
72
73
74
# File 'app/models/key.rb', line 70

def key=(value)
  write_attribute(:key, value.present? ? Gitlab::SSHPublicKey.sanitize(value) : nil)

  @public_key = nil
end

#post_create_hookObject

rubocop: disable CodeReuse/ServiceClass



109
110
111
# File 'app/models/key.rb', line 109

def post_create_hook
  SystemHooksService.new.execute_hooks_for(self, :create)
end

#post_destroy_hookObject

rubocop: disable CodeReuse/ServiceClass



129
130
131
# File 'app/models/key.rb', line 129

def post_destroy_hook
  SystemHooksService.new.execute_hooks_for(self, :destroy)
end

#projectsObject

projects that has this key



83
84
85
# File 'app/models/key.rb', line 83

def projects
  user.authorized_projects
end

#public_keyObject

rubocop: enable CodeReuse/ServiceClass



134
135
136
# File 'app/models/key.rb', line 134

def public_key
  @public_key ||= Gitlab::SSHPublicKey.new(key)
end

#publishable_keyObject



76
77
78
79
80
# File 'app/models/key.rb', line 76

def publishable_key
  # 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(' ')
end

#readable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/key.rb', line 148

def readable_by?(user)
  user_id == user.id
end

#refresh_user_cacheObject

rubocop: disable CodeReuse/ServiceClass



121
122
123
124
125
# File 'app/models/key.rb', line 121

def refresh_user_cache
  return unless user

  Users::KeysCountService.new(user).refresh_cache
end

#remove_from_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass



114
115
116
117
118
# File 'app/models/key.rb', line 114

def remove_from_authorized_keys
  return unless Gitlab::CurrentSettings.authorized_keys_enabled?

  AuthorizedKeysWorker.perform_async('remove_key', shell_id)
end

#shell_idObject



87
88
89
# File 'app/models/key.rb', line 87

def shell_id
  "key-#{id}"
end

#signing?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/key.rb', line 144

def signing?
  super || auth_and_signing?
end

#to_referenceObject



152
153
154
# File 'app/models/key.rb', line 152

def to_reference
  fingerprint
end

#update_last_used_atObject

rubocop: disable CodeReuse/ServiceClass



97
98
99
# File 'app/models/key.rb', line 97

def update_last_used_at
  Keys::LastUsedService.new(self).execute_async
end