Class: Key

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

Direct Known Subclasses

DeployKey

Constant Summary

Constants included from Cells::Claimable

Cells::Claimable::CLAIMS_BUCKET_TYPE, Cells::Claimable::CLAIMS_SOURCE_TYPE, Cells::Claimable::CLAIMS_SUBJECT_TYPE, Cells::Claimable::MissingPrimaryKeyError

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, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.regular_key_typesObject



81
82
83
# File 'app/models/key.rb', line 81

def self.regular_key_types
  [nil, 'Key']
end

.regular_keysObject



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

def self.regular_keys
  where(type: regular_key_types)
end

Instance Method Details

#add_to_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass



117
118
119
120
121
# File 'app/models/key.rb', line 117

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



107
108
109
# File 'app/models/key.rb', line 107

def can_delete?
  true
end

#ensure_sha256_fingerprint!Object



153
154
155
156
157
# File 'app/models/key.rb', line 153

def ensure_sha256_fingerprint!
  return if self.fingerprint_sha256

  save if generate_fingerprint
end

#key=(value) ⇒ Object



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

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

  @public_key = nil
end

#post_create_hookObject

rubocop: disable CodeReuse/ServiceClass



124
125
126
# File 'app/models/key.rb', line 124

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

#post_destroy_hookObject

rubocop: disable CodeReuse/ServiceClass



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

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

#projectsObject

projects that has this key



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

def projects
  user.authorized_projects
end

#public_keyObject

rubocop: enable CodeReuse/ServiceClass



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

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

#publishable_keyObject



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

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



163
164
165
# File 'app/models/key.rb', line 163

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

#refresh_user_cacheObject

rubocop: disable CodeReuse/ServiceClass



136
137
138
139
140
# File 'app/models/key.rb', line 136

def refresh_user_cache
  return unless user

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

#regular_key?Boolean



171
172
173
# File 'app/models/key.rb', line 171

def regular_key?
  type.in?(self.class.regular_key_types)
end

#remove_from_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass



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

def remove_from_authorized_keys
  return unless Gitlab::CurrentSettings.authorized_keys_enabled?

  AuthorizedKeysWorker.perform_async('remove_key', shell_id)
end

#shell_idObject



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

def shell_id
  "key-#{id}"
end

#signing?Boolean



159
160
161
# File 'app/models/key.rb', line 159

def signing?
  super || auth_and_signing?
end

#to_referenceObject



167
168
169
# File 'app/models/key.rb', line 167

def to_reference
  fingerprint
end

#update_last_used_atObject

rubocop: disable CodeReuse/ServiceClass



112
113
114
# File 'app/models/key.rb', line 112

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