Welcome to mirror list, hosted at ThFree Co, Russian Federation.

agent_token.rb « clusters « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2754db73b9c2be47b606ff4abdae0cf38ffed82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Clusters
  class AgentToken < ApplicationRecord
    TOKEN_PREFIX = "glagent-"

    include RedisCacheable
    include TokenAuthenticatable

    add_authentication_token_field :token,
      encrypted: :required,
      token_generator: -> { Devise.friendly_token(50) },
      format_with_prefix: :glagent_prefix
    cached_attr_reader :last_used_at

    self.table_name = 'cluster_agent_tokens'

    belongs_to :agent, class_name: 'Clusters::Agent', optional: false
    belongs_to :created_by_user, class_name: 'User', optional: true

    before_save :ensure_token

    validates :description, length: { maximum: 1024 }
    validates :name, presence: true, length: { maximum: 255 }

    scope :order_last_used_at_desc, -> { order(arel_table[:last_used_at].desc.nulls_last) }
    scope :with_status, -> (status) { where(status: status) }
    scope :active, -> { where(status: :active) }
    scope :connected, -> { active.where("last_used_at > ?", Clusters::Agent::INACTIVE_AFTER.ago) }

    enum status: {
      active: 0,
      revoked: 1
    }

    def revoke!
      update(status: :revoked)
    end

    def to_ability_name
      :cluster
    end

    def glagent_prefix
      TOKEN_PREFIX
    end
  end
end