debian-mirror-gitlab/spec/lib/gitlab/auth/request_authenticator_spec.rb

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

436 lines
14 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Auth::RequestAuthenticator do
2021-11-18 22:05:49 +05:30
include DependencyProxyHelpers
2018-03-17 18:26:18 +05:30
let(:env) do
{
'rack.input' => '',
'REQUEST_METHOD' => 'GET'
}
end
2020-10-24 23:57:45 +05:30
2018-03-17 18:26:18 +05:30
let(:request) { ActionDispatch::Request.new(env) }
subject { described_class.new(request) }
describe '#user' do
2021-11-18 22:05:49 +05:30
let_it_be(:sessionless_user) { build(:user) }
let_it_be(:session_user) { build(:user) }
2018-03-17 18:26:18 +05:30
it 'returns sessionless user first' do
2022-04-04 11:22:00 +05:30
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:find_sessionless_user).and_return(sessionless_user)
allow(instance).to receive(:find_user_from_warden).and_return(session_user)
end
2018-03-17 18:26:18 +05:30
2018-11-29 20:51:05 +05:30
expect(subject.user([:api])).to eq sessionless_user
2018-03-17 18:26:18 +05:30
end
it 'returns session user if no sessionless user found' do
allow_any_instance_of(described_class).to receive(:find_user_from_warden).and_return(session_user)
2018-11-29 20:51:05 +05:30
expect(subject.user([:api])).to eq session_user
2018-03-17 18:26:18 +05:30
end
it 'returns nil if no user found' do
2018-11-29 20:51:05 +05:30
expect(subject.user([:api])).to be_blank
2018-03-17 18:26:18 +05:30
end
it 'bubbles up exceptions' do
allow_any_instance_of(described_class).to receive(:find_user_from_warden).and_raise(Gitlab::Auth::UnauthorizedError)
end
end
2022-05-07 20:08:51 +05:30
describe '#can_sign_in_bot?' do
context 'the user is nil' do
it { is_expected.not_to be_can_sign_in_bot(nil) }
end
context 'the user is a bot, but for a web request' do
let(:user) { build(:user, :project_bot) }
it { is_expected.not_to be_can_sign_in_bot(user) }
end
context 'the user is a regular user, for an API request' do
let(:user) { build(:user) }
before do
env['SCRIPT_NAME'] = '/api/some_resource'
end
it { is_expected.not_to be_can_sign_in_bot(user) }
end
context 'the user is a project bot, for an API request' do
let(:user) { build(:user, :project_bot) }
before do
env['SCRIPT_NAME'] = '/api/some_resource'
end
it { is_expected.to be_can_sign_in_bot(user) }
end
end
2022-05-03 16:02:30 +05:30
describe '#find_authenticated_requester' do
let_it_be(:api_user) { create(:user) }
let_it_be(:deploy_token_user) { create(:user) }
it 'returns the deploy token if it exists' do
allow_next_instance_of(described_class) do |authenticator|
expect(authenticator).to receive(:deploy_token_from_request).and_return(deploy_token_user)
allow(authenticator).to receive(:user).and_return(nil)
end
expect(subject.find_authenticated_requester([:api])).to eq deploy_token_user
end
it 'returns the user id if it exists' do
allow_next_instance_of(described_class) do |authenticator|
allow(authenticator).to receive(:deploy_token_from_request).and_return(deploy_token_user)
expect(authenticator).to receive(:user).and_return(api_user)
end
expect(subject.find_authenticated_requester([:api])).to eq api_user
end
it 'rerturns nil if no match is found' do
allow_next_instance_of(described_class) do |authenticator|
expect(authenticator).to receive(:deploy_token_from_request).and_return(nil)
expect(authenticator).to receive(:user).and_return(nil)
end
expect(subject.find_authenticated_requester([:api])).to eq nil
end
end
2018-03-17 18:26:18 +05:30
describe '#find_sessionless_user' do
2021-11-18 22:05:49 +05:30
let_it_be(:dependency_proxy_user) { build(:user) }
let_it_be(:access_token_user) { build(:user) }
let_it_be(:feed_token_user) { build(:user) }
let_it_be(:static_object_token_user) { build(:user) }
let_it_be(:job_token_user) { build(:user) }
let_it_be(:lfs_token_user) { build(:user) }
let_it_be(:basic_auth_access_token_user) { build(:user) }
let_it_be(:basic_auth_password_user) { build(:user) }
it 'returns dependency_proxy user first' do
allow_any_instance_of(described_class).to receive(:find_user_from_dependency_proxy_token)
.and_return(dependency_proxy_user)
allow_any_instance_of(described_class).to receive(:find_user_from_web_access_token).and_return(access_token_user)
expect(subject.find_sessionless_user(:api)).to eq dependency_proxy_user
end
it 'returns access_token user if no dependency_proxy user found' do
2021-03-08 18:12:59 +05:30
allow_any_instance_of(described_class).to receive(:find_user_from_web_access_token)
.with(anything, scopes: [:api, :read_api])
.and_return(access_token_user)
2018-11-08 19:23:39 +05:30
allow_any_instance_of(described_class).to receive(:find_user_from_feed_token).and_return(feed_token_user)
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
expect(subject.find_sessionless_user(:api)).to eq access_token_user
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
it 'returns feed_token user if no access_token user found' do
allow_any_instance_of(described_class).to receive(:find_user_from_feed_token).and_return(feed_token_user)
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
expect(subject.find_sessionless_user(:api)).to eq feed_token_user
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
it 'returns static_object_token user if no feed_token user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_static_object_token)
.and_return(static_object_token_user)
2021-02-22 17:27:13 +05:30
expect(subject.find_sessionless_user(:api)).to eq static_object_token_user
2020-03-13 15:44:24 +05:30
end
it 'returns job_token user if no static_object_token user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_job_token)
.and_return(job_token_user)
2021-02-22 17:27:13 +05:30
expect(subject.find_sessionless_user(:api)).to eq job_token_user
2020-03-13 15:44:24 +05:30
end
2021-09-30 23:02:18 +05:30
it 'returns nil even if basic_auth_access_token is available' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_personal_access_token)
.and_return(basic_auth_access_token_user)
expect(subject.find_sessionless_user(:api)).to be_nil
end
it 'returns nil even if find_user_from_lfs_token is available' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_lfs_token)
.and_return(lfs_token_user)
expect(subject.find_sessionless_user(:api)).to be_nil
end
2018-03-17 18:26:18 +05:30
it 'returns nil if no user found' do
2021-09-30 23:02:18 +05:30
expect(subject.find_sessionless_user(:api)).to be_nil
end
context 'in an API request' do
before do
env['SCRIPT_NAME'] = '/api/v4/projects'
end
it 'returns basic_auth_access_token user if no job_token_user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_personal_access_token)
.and_return(basic_auth_access_token_user)
expect(subject.find_sessionless_user(:api)).to eq basic_auth_access_token_user
end
end
context 'in a Git request' do
before do
env['SCRIPT_NAME'] = '/group/project.git/info/refs'
end
it 'returns lfs_token user if no job_token user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_lfs_token)
.and_return(lfs_token_user)
expect(subject.find_sessionless_user(nil)).to eq lfs_token_user
end
it 'returns basic_auth_access_token user if no lfs_token user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_personal_access_token)
.and_return(basic_auth_access_token_user)
expect(subject.find_sessionless_user(nil)).to eq basic_auth_access_token_user
end
it 'returns basic_auth_access_password user if no basic_auth_access_token user found' do
allow_any_instance_of(described_class)
.to receive(:find_user_from_basic_auth_password)
.and_return(basic_auth_password_user)
expect(subject.find_sessionless_user(nil)).to eq basic_auth_password_user
end
it 'returns nil if no user found' do
expect(subject.find_sessionless_user(nil)).to be_blank
end
2018-03-17 18:26:18 +05:30
end
it 'rescue Gitlab::Auth::AuthenticationError exceptions' do
2018-11-29 20:51:05 +05:30
allow_any_instance_of(described_class).to receive(:find_user_from_web_access_token).and_raise(Gitlab::Auth::UnauthorizedError)
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
expect(subject.find_sessionless_user(:api)).to be_blank
end
2021-11-18 22:05:49 +05:30
context 'dependency proxy' do
let_it_be(:dependency_proxy_user) { create(:user) }
let(:token) { build_jwt(dependency_proxy_user).encoded }
let(:authenticator) { described_class.new(request) }
subject { authenticator.find_sessionless_user(:api) }
before do
env['SCRIPT_NAME'] = accessed_path
env['HTTP_AUTHORIZATION'] = "Bearer #{token}"
end
shared_examples 'identifying dependency proxy urls properly with' do |user_type|
context 'with pulling a manifest' do
let(:accessed_path) { '/v2/group1/dependency_proxy/containers/alpine/manifests/latest' }
it { is_expected.to eq(dependency_proxy_user) } if user_type == :user
it { is_expected.to eq(nil) } if user_type == :no_user
end
context 'with pulling a blob' do
let(:accessed_path) { '/v2/group1/dependency_proxy/containers/alpine/blobs/sha256:a0d0a0d46f8b52473982a3c466318f479767577551a53ffc9074c9fa7035982e' }
it { is_expected.to eq(dependency_proxy_user) } if user_type == :user
it { is_expected.to eq(nil) } if user_type == :no_user
end
context 'with any other path' do
let(:accessed_path) { '/foo/bar' }
it { is_expected.to eq(nil) }
end
end
context 'with a user' do
it_behaves_like 'identifying dependency proxy urls properly with', :user
context 'with an invalid id' do
let(:token) { build_jwt { |jwt| jwt['user_id'] = 'this_is_not_a_user' } }
it_behaves_like 'identifying dependency proxy urls properly with', :no_user
end
end
context 'with a deploy token' do
let_it_be(:dependency_proxy_user) { create(:deploy_token) }
it_behaves_like 'identifying dependency proxy urls properly with', :no_user
end
context 'with no jwt token' do
let(:token) { nil }
it_behaves_like 'identifying dependency proxy urls properly with', :no_user
end
context 'with an expired jwt token' do
let(:token) { build_jwt(dependency_proxy_user).encoded }
let(:accessed_path) { '/v2/group1/dependency_proxy/containers/alpine/manifests/latest' }
it 'returns nil' do
travel_to(Time.zone.now + Auth::DependencyProxyAuthenticationService.token_expire_at + 1.minute) do
expect(subject).to eq(nil)
end
end
end
end
2021-02-22 17:27:13 +05:30
end
describe '#find_personal_access_token_from_http_basic_auth' do
let_it_be(:personal_access_token) { create(:personal_access_token) }
let_it_be(:user) { personal_access_token.user }
before do
allow(subject).to receive(:has_basic_credentials?).and_return(true)
allow(subject).to receive(:user_name_and_password).and_return([user.username, personal_access_token.token])
end
context 'with API requests' do
before do
env['SCRIPT_NAME'] = '/api/endpoint'
end
it 'tries to find the user' do
expect(subject.user([:api])).to eq user
end
it 'returns nil if the token is revoked' do
personal_access_token.revoke!
expect(subject.user([:api])).to be_blank
end
it 'returns nil if the token does not have API scope' do
personal_access_token.update!(scopes: ['read_registry'])
expect(subject.user([:api])).to be_blank
end
end
context 'without API requests' do
before do
env['SCRIPT_NAME'] = '/web/endpoint'
end
it 'does not search for job users' do
expect(PersonalAccessToken).not_to receive(:find_by_token)
expect(subject.user([:api])).to be_nil
end
2018-03-17 18:26:18 +05:30
end
end
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
describe '#find_user_from_job_token' do
2021-11-18 22:05:49 +05:30
let_it_be(:user) { build(:user) }
let_it_be(:job) { build(:ci_build, user: user, status: :running) }
2020-03-13 15:44:24 +05:30
before do
env[Gitlab::Auth::AuthFinders::JOB_TOKEN_HEADER] = 'token'
end
context 'with API requests' do
before do
env['SCRIPT_NAME'] = '/api/endpoint'
2020-09-03 11:15:55 +05:30
expect(::Ci::Build).to receive(:find_by_token).with('token').and_return(job)
2020-03-13 15:44:24 +05:30
end
it 'tries to find the user' do
expect(subject.find_sessionless_user([:api])).to eq user
end
2020-09-03 11:15:55 +05:30
it 'returns nil if the job is not running' do
job.status = :success
expect(subject.find_sessionless_user([:api])).to be_blank
end
2020-03-13 15:44:24 +05:30
end
context 'without API requests' do
before do
env['SCRIPT_NAME'] = '/web/endpoint'
end
it 'does not search for job users' do
expect(::Ci::Build).not_to receive(:find_by_token)
expect(subject.find_sessionless_user([:api])).to be_nil
end
end
end
2020-01-01 13:55:28 +05:30
describe '#runner' do
2021-11-18 22:05:49 +05:30
let_it_be(:runner) { build(:ci_runner) }
2020-01-01 13:55:28 +05:30
it 'returns the runner using #find_runner_from_token' do
expect_any_instance_of(described_class)
.to receive(:find_runner_from_token)
.and_return(runner)
expect(subject.runner).to eq runner
end
it 'returns nil if no runner is found' do
expect(subject.runner).to be_blank
end
it 'rescue Gitlab::Auth::AuthenticationError exceptions' do
expect_any_instance_of(described_class)
.to receive(:find_runner_from_token)
.and_raise(Gitlab::Auth::UnauthorizedError)
expect(subject.runner).to be_blank
end
end
2021-09-30 23:02:18 +05:30
describe '#route_authentication_setting' do
using RSpec::Parameterized::TableSyntax
2022-05-03 16:02:30 +05:30
where(:script_name, :expected_job_token_allowed, :expected_basic_auth_personal_access_token, :expected_deploy_token_allowed) do
'/api/endpoint' | true | true | true
'/namespace/project.git' | false | true | true
'/web/endpoint' | false | false | false
2021-09-30 23:02:18 +05:30
end
with_them do
before do
env['SCRIPT_NAME'] = script_name
end
it 'returns correct settings' do
expect(subject.send(:route_authentication_setting)).to eql({
job_token_allowed: expected_job_token_allowed,
2022-05-03 16:02:30 +05:30
basic_auth_personal_access_token: expected_basic_auth_personal_access_token,
deploy_token_allowed: expected_deploy_token_allowed
2021-09-30 23:02:18 +05:30
})
end
end
end
2018-03-17 18:26:18 +05:30
end