Class: DeployToken

Inherits:
ApplicationRecord show all
Includes:
Expirable, Gitlab::Utils::StrongMemoize, PolicyActor, TokenAuthenticatable
Defined in:
app/models/deploy_token.rb

Constant Summary collapse

AVAILABLE_SCOPES =
%i(read_repository read_registry write_registry
read_package_registry write_package_registry).freeze
GITLAB_DEPLOY_TOKEN_NAME =
'gitlab-deploy-token'
REQUIRED_DEPENDENCY_PROXY_SCOPES =
%i[read_registry write_registry].freeze

Constants included from Expirable

Expirable::DAYS_TO_EXPIRE

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from PolicyActor

#access_locked?, #admin?, #alert_bot?, #automation_bot?, #blocked?, #can?, #can_create_group, #can_read_all_resources?, #confirmation_required_on_sign_in?, #external?, #from_ci_job_token?, #internal?, #password_expired_if_applicable?, #preferred_language, #required_terms_not_accepted?, #requires_ldap_check?, #security_bot?, #support_bot?, #try_obtain_ldap_lease

Methods included from Expirable

#expires?, #expires_soon?

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, 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 SensitiveSerializableHash

#serializable_hash

Class Method Details

.gitlab_deploy_tokenObject


51
52
53
# File 'app/models/deploy_token.rb', line 51

def self.gitlab_deploy_token
  active.find_by(name: GITLAB_DEPLOY_TOKEN_NAME)
end

Instance Method Details

#accessible_projectsObject


111
112
113
114
115
116
117
# File 'app/models/deploy_token.rb', line 111

def accessible_projects
  if project_type?
    projects
  elsif group_type?
    group.all_projects
  end
end

#active?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'app/models/deploy_token.rb', line 65

def active?
  !revoked && !expired?
end

#deactivated?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'app/models/deploy_token.rb', line 69

def deactivated?
  !active?
end

#expires_atObject


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

def expires_at
  expires_at = read_attribute(:expires_at)
  expires_at != Forever.date ? expires_at : nil
end

#expires_at=(value) ⇒ Object


138
139
140
# File 'app/models/deploy_token.rb', line 138

def expires_at=(value)
  write_attribute(:expires_at, value.presence || Forever.date)
end

#groupObject


105
106
107
108
109
# File 'app/models/deploy_token.rb', line 105

def group
  strong_memoize(:group) do
    groups.first
  end
end

#has_access_to?(requested_project) ⇒ Boolean

Returns:

  • (Boolean)

81
82
83
84
85
86
# File 'app/models/deploy_token.rb', line 81

def has_access_to?(requested_project)
  return false unless active?
  return false unless holder

  holder.has_access_to?(requested_project)
end

#has_access_to_group?(requested_group) ⇒ Boolean

Returns:

  • (Boolean)

88
89
90
91
92
93
94
# File 'app/models/deploy_token.rb', line 88

def has_access_to_group?(requested_group)
  return false unless active?
  return false unless group_type?
  return false unless holder

  holder.has_access_to_group?(requested_group)
end

#holderObject


119
120
121
122
123
124
125
126
127
# File 'app/models/deploy_token.rb', line 119

def holder
  strong_memoize(:holder) do
    if project_type?
      project_deploy_tokens.first
    elsif group_type?
      group_deploy_tokens.first
    end
  end
end

#impersonated?Boolean

Returns:

  • (Boolean)

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

def impersonated?
  false
end

#projectObject

This is temporal. Currently we limit DeployToken to a single project or group, later we're going to extend that to be for multiple projects and namespaces.


99
100
101
102
103
# File 'app/models/deploy_token.rb', line 99

def project
  strong_memoize(:project) do
    projects.first
  end
end

#revoke!Object


61
62
63
# File 'app/models/deploy_token.rb', line 61

def revoke!
  update!(revoked: true)
end

#scopesObject


73
74
75
# File 'app/models/deploy_token.rb', line 73

def scopes
  AVAILABLE_SCOPES.select { |token_scope| read_attribute(token_scope) }
end

#usernameObject


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

def username
  super || default_username
end

#valid_for_dependency_proxy?Boolean

Returns:

  • (Boolean)

55
56
57
58
59
# File 'app/models/deploy_token.rb', line 55

def valid_for_dependency_proxy?
  group_type? &&
    active? &&
    REQUIRED_DEPENDENCY_PROXY_SCOPES.all? { |scope| scope.in?(scopes) }
end