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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# frozen_string_literal: true
module Gitlab
module Ci
class JwtV2 < Jwt
DEFAULT_AUD = Settings.gitlab.base_url
GITLAB_HOSTED_RUNNER = 'gitlab-hosted'
SELF_HOSTED_RUNNER = 'self-hosted'
def self.for_build(build, aud: DEFAULT_AUD)
new(build, ttl: build.metadata_timeout, aud: aud).encoded
end
def initialize(build, ttl:, aud:)
super(build, ttl: ttl)
@aud = aud
end
private
attr_reader :aud
def reserved_claims
super.merge({
iss: Settings.gitlab.base_url,
sub: "project_path:#{project.full_path}:ref_type:#{ref_type}:ref:#{source_ref}",
aud: aud,
user_identities: user_identities
}.compact)
end
def user_identities
return unless user&.pass_user_identities_to_ci_jwt
user.identities.map do |identity|
{
provider: identity.provider.to_s,
extern_uid: identity.extern_uid.to_s
}
end
end
def custom_claims
additional_claims = {
runner_id: runner&.id,
runner_environment: runner_environment,
sha: pipeline.sha
}
if Feature.enabled?(:ci_jwt_v2_ref_uri_claim, pipeline.project)
additional_claims[:ci_config_ref_uri] = ci_config_ref_uri
end
super.merge(additional_claims)
end
def ci_config_ref_uri
project_config = Gitlab::Ci::ProjectConfig.new(
project: project,
sha: pipeline.sha,
pipeline_source: pipeline.source&.to_sym,
pipeline_source_bridge: pipeline.source_bridge
)
return unless project_config&.source == :repository_source
"#{project_config.url}@#{pipeline.source_ref_path}"
# Errors are rescued to mitigate risk. This can be removed if no errors are observed.
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/117923#note_1387660746 for context.
rescue StandardError => e
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e, pipeline_id: pipeline.id)
nil
end
def runner_environment
return unless runner
runner.gitlab_hosted? ? GITLAB_HOSTED_RUNNER : SELF_HOSTED_RUNNER
end
end
end
end
|