debian-mirror-gitlab/spec/lib/gitlab/ci/jwt_v2_spec.rb

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

117 lines
3.4 KiB
Ruby
Raw Normal View History

2022-03-02 08:16:31 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-07-09 08:55:56 +05:30
RSpec.describe Gitlab::Ci::JwtV2, feature_category: :continuous_integration do
2022-03-02 08:16:31 +05:30
let(:namespace) { build_stubbed(:namespace) }
let(:project) { build_stubbed(:project, namespace: namespace) }
2023-06-20 00:43:36 +05:30
let(:user) do
build_stubbed(
:user,
identities: [build_stubbed(:identity, extern_uid: '1', provider: 'github')]
)
end
2022-03-02 08:16:31 +05:30
let(:pipeline) { build_stubbed(:ci_pipeline, ref: 'auto-deploy-2020-03-19') }
2023-07-09 08:55:56 +05:30
let(:runner) { build_stubbed(:ci_runner) }
2022-11-25 23:54:43 +05:30
let(:aud) { described_class::DEFAULT_AUD }
2022-03-02 08:16:31 +05:30
let(:build) do
build_stubbed(
:ci_build,
project: project,
user: user,
2023-07-09 08:55:56 +05:30
pipeline: pipeline,
runner: runner
2022-03-02 08:16:31 +05:30
)
end
2022-11-25 23:54:43 +05:30
subject(:ci_job_jwt_v2) { described_class.new(build, ttl: 30, aud: aud) }
2022-03-02 08:16:31 +05:30
it { is_expected.to be_a Gitlab::Ci::Jwt }
describe '#payload' do
subject(:payload) { ci_job_jwt_v2.payload }
it 'has correct values for the standard JWT attributes' do
aggregate_failures do
expect(payload[:iss]).to eq(Settings.gitlab.base_url)
expect(payload[:aud]).to eq(Settings.gitlab.base_url)
expect(payload[:sub]).to eq("project_path:#{project.full_path}:ref_type:branch:ref:#{pipeline.source_ref}")
end
end
2022-11-25 23:54:43 +05:30
2023-06-20 00:43:36 +05:30
it 'includes user identities when enabled' do
expect(user).to receive(:pass_user_identities_to_ci_jwt).and_return(true)
identities = payload[:user_identities].map { |identity| identity.slice(:extern_uid, :provider) }
expect(identities).to eq([{ extern_uid: '1', provider: 'github' }])
end
it 'does not include user identities when disabled' do
expect(user).to receive(:pass_user_identities_to_ci_jwt).and_return(false)
expect(payload).not_to include(:user_identities)
end
2022-11-25 23:54:43 +05:30
context 'when given an aud' do
let(:aud) { 'AWS' }
it 'uses that aud in the payload' do
expect(payload[:aud]).to eq('AWS')
end
end
2023-07-09 08:55:56 +05:30
describe 'custom claims' do
describe 'runner_id' do
it 'is the ID of the runner executing the job' do
expect(payload[:runner_id]).to eq(runner.id)
end
context 'when build is not associated with a runner' do
let(:runner) { nil }
it 'is nil' do
expect(payload[:runner_id]).to be_nil
end
end
end
describe 'runner_environment' do
context 'when runner is gitlab-hosted' do
before do
allow(runner).to receive(:gitlab_hosted?).and_return(true)
end
it "is #{described_class::GITLAB_HOSTED_RUNNER}" do
expect(payload[:runner_environment]).to eq(described_class::GITLAB_HOSTED_RUNNER)
end
end
context 'when runner is self-hosted' do
before do
allow(runner).to receive(:gitlab_hosted?).and_return(false)
end
it "is #{described_class::SELF_HOSTED_RUNNER}" do
expect(payload[:runner_environment]).to eq(described_class::SELF_HOSTED_RUNNER)
end
end
context 'when build is not associated with a runner' do
let(:runner) { nil }
it 'is nil' do
expect(payload[:runner_environment]).to be_nil
end
end
end
describe 'sha' do
it 'is the commit revision the project is built for' do
expect(payload[:sha]).to eq(pipeline.sha)
end
end
end
2022-03-02 08:16:31 +05:30
end
end