2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class DeployToken < ApplicationRecord
|
2018-03-19 16:11:12 +00:00
|
|
|
include Expirable
|
|
|
|
include TokenAuthenticatable
|
2018-07-27 09:37:19 +00:00
|
|
|
include PolicyActor
|
2018-09-27 07:22:24 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-07-12 20:49:47 +00:00
|
|
|
add_authentication_token_field :token, encrypted: :optional
|
2018-03-19 16:11:12 +00:00
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
AVAILABLE_SCOPES = %i(read_repository read_registry).freeze
|
2019-08-31 19:57:00 +00:00
|
|
|
GITLAB_DEPLOY_TOKEN_NAME = 'gitlab-deploy-token'
|
2018-04-06 16:23:45 +00:00
|
|
|
|
2018-04-06 19:48:17 +00:00
|
|
|
default_value_for(:expires_at) { Forever.date }
|
2018-03-19 16:11:12 +00:00
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
has_many :project_deploy_tokens, inverse_of: :deploy_token
|
2018-04-18 13:41:42 +00:00
|
|
|
has_many :projects, through: :project_deploy_tokens
|
2018-03-19 16:11:12 +00:00
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
validate :ensure_at_least_one_scope
|
2019-07-02 18:56:48 +00:00
|
|
|
validates :username,
|
|
|
|
length: { maximum: 255 },
|
|
|
|
allow_nil: true,
|
|
|
|
format: {
|
|
|
|
with: /\A[a-zA-Z0-9\.\+_-]+\z/,
|
|
|
|
message: "can contain only letters, digits, '_', '-', '+', and '.'"
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:11:12 +00:00
|
|
|
before_save :ensure_token
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
accepts_nested_attributes_for :project_deploy_tokens
|
|
|
|
|
2018-04-06 14:30:21 +00:00
|
|
|
scope :active, -> { where("revoked = false AND expires_at >= NOW()") }
|
2018-03-19 16:11:12 +00:00
|
|
|
|
2018-04-20 15:05:16 +00:00
|
|
|
def self.gitlab_deploy_token
|
|
|
|
active.find_by(name: GITLAB_DEPLOY_TOKEN_NAME)
|
|
|
|
end
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
def revoke!
|
|
|
|
update!(revoked: true)
|
2018-03-19 16:11:12 +00:00
|
|
|
end
|
2018-03-29 22:56:35 +00:00
|
|
|
|
|
|
|
def active?
|
2018-08-02 17:43:36 +00:00
|
|
|
!revoked && !expired?
|
2018-03-29 22:56:35 +00:00
|
|
|
end
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
def scopes
|
2018-04-06 14:30:21 +00:00
|
|
|
AVAILABLE_SCOPES.select { |token_scope| read_attribute(token_scope) }
|
2018-04-05 17:22:34 +00:00
|
|
|
end
|
|
|
|
|
2018-03-29 22:56:35 +00:00
|
|
|
def username
|
2019-07-02 18:56:48 +00:00
|
|
|
super || default_username
|
2018-04-05 13:49:18 +00:00
|
|
|
end
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
def has_access_to?(requested_project)
|
2018-04-10 07:31:30 +00:00
|
|
|
active? && project == requested_project
|
2018-04-05 17:22:34 +00:00
|
|
|
end
|
|
|
|
|
2018-04-06 14:30:21 +00:00
|
|
|
# This is temporal. Currently we limit DeployToken
|
|
|
|
# to a single project, later we're going to extend
|
|
|
|
# that to be for multiple projects and namespaces.
|
2018-04-05 17:22:34 +00:00
|
|
|
def project
|
2018-09-27 07:22:24 +00:00
|
|
|
strong_memoize(:project) do
|
|
|
|
projects.first
|
|
|
|
end
|
2018-04-05 17:22:34 +00:00
|
|
|
end
|
|
|
|
|
2018-04-06 19:48:17 +00:00
|
|
|
def expires_at
|
|
|
|
expires_at = read_attribute(:expires_at)
|
|
|
|
expires_at != Forever.date ? expires_at : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def expires_at=(value)
|
|
|
|
write_attribute(:expires_at, value.presence || Forever.date)
|
|
|
|
end
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
private
|
|
|
|
|
2018-08-02 17:43:36 +00:00
|
|
|
def expired?
|
|
|
|
return false unless expires_at
|
|
|
|
|
|
|
|
expires_at < Date.today
|
|
|
|
end
|
|
|
|
|
2018-04-05 17:22:34 +00:00
|
|
|
def ensure_at_least_one_scope
|
|
|
|
errors.add(:base, "Scopes can't be blank") unless read_repository || read_registry
|
2018-03-29 22:56:35 +00:00
|
|
|
end
|
2019-07-02 18:56:48 +00:00
|
|
|
|
|
|
|
def default_username
|
|
|
|
"gitlab+deploy-token-#{id}" if persisted?
|
|
|
|
end
|
2018-03-19 16:11:12 +00:00
|
|
|
end
|