debian-mirror-gitlab/app/models/ci/job_token/scope.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.6 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
2023-03-04 22:38:38 +05:30
# This model represents the scope of access for a CI_JOB_TOKEN.
2021-09-04 01:27:46 +05:30
#
2023-03-04 22:38:38 +05:30
# A scope is initialized with a project.
#
# Projects can be added to the scope by adding ScopeLinks to
# create an allowlist of projects in either access direction (inbound, outbound).
#
# Currently, projects in the outbound allowlist can be accessed via the token
# in the source project.
#
# TODO(Issue #346298) Projects in the inbound allowlist can use their token to access
# the source project.
#
# CI_JOB_TOKEN should be considered untrusted without these features enabled.
2021-09-04 01:27:46 +05:30
#
module Ci
module JobToken
class Scope
2023-03-04 22:38:38 +05:30
attr_reader :current_project
2021-09-04 01:27:46 +05:30
2023-03-04 22:38:38 +05:30
def initialize(current_project)
@current_project = current_project
2021-09-04 01:27:46 +05:30
end
2023-03-04 22:38:38 +05:30
def allows?(accessed_project)
self_referential?(accessed_project) || outbound_allows?(accessed_project)
end
2021-09-04 01:27:46 +05:30
2023-03-04 22:38:38 +05:30
def outbound_projects
outbound_allowlist.projects
2021-09-04 01:27:46 +05:30
end
2023-03-04 22:38:38 +05:30
# Deprecated: use outbound_projects, TODO(Issue #346298) remove references to all_project
2021-09-04 01:27:46 +05:30
def all_projects
2023-03-04 22:38:38 +05:30
outbound_projects
2021-09-04 01:27:46 +05:30
end
2021-11-18 22:05:49 +05:30
private
2023-03-04 22:38:38 +05:30
def outbound_allows?(accessed_project)
# if the setting is disabled any project is considered to be in scope.
return true unless @current_project.ci_outbound_job_token_scope_enabled?
outbound_allowlist.includes?(accessed_project)
end
def outbound_allowlist
Ci::JobToken::Allowlist.new(@current_project, direction: :outbound)
2021-11-18 22:05:49 +05:30
end
2022-10-11 01:57:18 +05:30
2023-03-04 22:38:38 +05:30
def self_referential?(accessed_project)
@current_project.id == accessed_project.id
2022-10-11 01:57:18 +05:30
end
2021-09-04 01:27:46 +05:30
end
end
end