2019-07-25 05:21:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-09-12 06:23:16 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe Gitlab::Auth, :use_clean_rails_memory_store_caching do
|
2020-02-20 18:08:51 +00:00
|
|
|
let_it_be(:project) { create(:project) }
|
2021-04-10 18:09:23 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
let(:auth_failure) { { actor: nil, project: nil, type: nil, authentication_abilities: nil } }
|
2016-04-29 16:58:55 +00:00
|
|
|
let(:gl_auth) { described_class }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-01-31 10:21:29 +00:00
|
|
|
describe 'constants' do
|
|
|
|
it 'API_SCOPES contains all scopes for API access' do
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject::API_SCOPES).to match_array %i[api read_user read_api]
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ADMIN_SCOPES contains all scopes for ADMIN access' do
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject::ADMIN_SCOPES).to match_array %i[sudo]
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'REPOSITORY_SCOPES contains all scopes for REPOSITORY access' do
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject::REPOSITORY_SCOPES).to match_array %i[read_repository write_repository]
|
2017-01-31 10:21:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'OPENID_SCOPES contains all scopes for OpenID Connect' do
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject::OPENID_SCOPES).to match_array [:openid]
|
2017-01-31 10:21:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'DEFAULT_SCOPES contains all default scopes' do
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject::DEFAULT_SCOPES).to match_array [:api]
|
2017-01-31 10:21:29 +00:00
|
|
|
end
|
|
|
|
|
2017-09-15 15:43:49 +00:00
|
|
|
it 'optional_scopes contains all non-default scopes' do
|
2017-08-04 13:17:20 +00:00
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject.optional_scopes).to match_array %i[read_user read_api read_repository write_repository read_registry write_registry sudo openid profile email]
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'available_scopes' do
|
|
|
|
it 'contains all non-default scopes' do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject.all_available_scopes).to match_array %i[api read_user read_api read_repository write_repository read_registry write_registry sudo]
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains for non-admin user all non-default scopes without ADMIN access' do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
user = create(:user, admin: false)
|
|
|
|
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject.available_scopes_for(user)).to match_array %i[api read_user read_api read_repository write_repository read_registry write_registry]
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains for admin user all non-default scopes with ADMIN access' do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
user = create(:user, admin: true)
|
|
|
|
|
2022-02-04 12:17:40 +00:00
|
|
|
expect(subject.available_scopes_for(user)).to match_array %i[api read_user read_api read_repository write_repository read_registry write_registry sudo]
|
2017-06-06 11:18:01 +00:00
|
|
|
end
|
|
|
|
|
2017-09-15 15:43:49 +00:00
|
|
|
context 'registry_scopes' do
|
2017-08-04 13:17:20 +00:00
|
|
|
context 'when registry is disabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is empty' do
|
2017-09-15 15:43:49 +00:00
|
|
|
expect(subject.registry_scopes).to eq []
|
2017-08-04 13:17:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when registry is enabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains all registry related scopes' do
|
2020-04-14 00:09:57 +00:00
|
|
|
expect(subject.registry_scopes).to eq %i[read_registry write_registry]
|
2017-08-04 13:17:20 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-31 10:21:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-13 13:38:25 +00:00
|
|
|
describe 'find_for_git_client' do
|
2019-12-03 21:06:23 +00:00
|
|
|
describe 'rate limiting' do
|
|
|
|
before do
|
|
|
|
stub_rack_attack_setting(enabled: true, ip_whitelist: [])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when IP is already banned' do
|
2022-07-26 18:11:45 +00:00
|
|
|
subject { gl_auth.find_for_git_client('username-does-not-matter', 'password-does-not-matter', project: nil, ip: 'ip') }
|
2019-12-03 21:06:23 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).to receive(:banned?).and_return(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an IpBlacklisted exception' do
|
|
|
|
expect { subject }.to raise_error(Gitlab::Auth::IpBlacklisted)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for CI registry user' do
|
|
|
|
let_it_be(:build) { create(:ci_build, :running) }
|
|
|
|
|
|
|
|
it 'skips rate limiting for successful auth' do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).not_to receive(:reset!)
|
|
|
|
end
|
|
|
|
|
|
|
|
gl_auth.find_for_git_client('gitlab-ci-token', build.token, project: build.project, ip: 'ip')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips rate limiting for failed auth' do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).not_to receive(:register_fail!)
|
|
|
|
end
|
|
|
|
|
|
|
|
gl_auth.find_for_git_client('gitlab-ci-token', 'wrong_token', project: build.project, ip: 'ip')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for other users' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'resets rate limit for successful auth' do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).to receive(:reset!)
|
|
|
|
end
|
|
|
|
|
|
|
|
gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip')
|
|
|
|
end
|
|
|
|
|
2020-01-08 06:08:13 +00:00
|
|
|
it 'rate limits a user by unique IPs' do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).to receive(:reset!)
|
|
|
|
end
|
2020-12-16 09:10:26 +00:00
|
|
|
expect(Gitlab::Auth::UniqueIpsLimiter).to receive(:limit_user!).twice.and_call_original
|
2020-01-08 06:08:13 +00:00
|
|
|
|
|
|
|
gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip')
|
|
|
|
end
|
|
|
|
|
2019-12-03 21:06:23 +00:00
|
|
|
it 'registers failure for failed auth' do
|
|
|
|
expect_next_instance_of(Gitlab::Auth::IpRateLimiter) do |rate_limiter|
|
|
|
|
expect(rate_limiter).to receive(:register_fail!)
|
|
|
|
end
|
|
|
|
|
|
|
|
gl_auth.find_for_git_client(user.username, 'wrong_password', project: nil, ip: 'ip')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-15 09:57:09 +00:00
|
|
|
context 'build token' do
|
2020-07-30 12:09:33 +00:00
|
|
|
subject { gl_auth.find_for_git_client(username, build.token, project: project, ip: 'ip') }
|
|
|
|
|
|
|
|
let(:username) { 'gitlab-ci-token' }
|
2016-09-15 09:57:09 +00:00
|
|
|
|
|
|
|
context 'for running build' do
|
2022-01-25 00:11:49 +00:00
|
|
|
let!(:group) { create(:group) }
|
|
|
|
let!(:project) { create(:project, group: group) }
|
|
|
|
let!(:build) { create(:ci_build, :running, project: project) }
|
2016-09-15 09:57:09 +00:00
|
|
|
|
|
|
|
it 'recognises user-less build' do
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(actor: nil, project: build.project, type: :ci, authentication_abilities: described_class.build_authentication_abilities)
|
2016-09-15 09:57:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognises user token' do
|
2022-01-31 18:18:10 +00:00
|
|
|
build.update!(user: create(:user))
|
2016-09-15 09:57:09 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(actor: build.user, project: build.project, type: :build, authentication_abilities: described_class.build_authentication_abilities)
|
2016-09-15 09:57:09 +00:00
|
|
|
end
|
2020-03-26 18:08:03 +00:00
|
|
|
|
2022-01-25 00:11:49 +00:00
|
|
|
it 'recognises project level bot access token' do
|
2022-01-31 18:18:10 +00:00
|
|
|
build.update!(user: create(:user, :project_bot))
|
2022-01-25 00:11:49 +00:00
|
|
|
project.add_maintainer(build.user)
|
|
|
|
|
|
|
|
expect(subject).to have_attributes(actor: build.user, project: build.project, type: :build, authentication_abilities: described_class.build_authentication_abilities)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognises group level bot access token' do
|
2022-01-31 18:18:10 +00:00
|
|
|
build.update!(user: create(:user, :project_bot))
|
2022-01-25 00:11:49 +00:00
|
|
|
group.add_maintainer(build.user)
|
|
|
|
|
|
|
|
expect(subject).to have_attributes(actor: build.user, project: build.project, type: :build, authentication_abilities: described_class.build_authentication_abilities)
|
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
it 'fails with blocked user token' do
|
2022-01-31 18:18:10 +00:00
|
|
|
build.update!(user: create(:user, :blocked))
|
2020-03-26 18:08:03 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(auth_failure)
|
2020-03-26 18:08:03 +00:00
|
|
|
end
|
2020-07-30 12:09:33 +00:00
|
|
|
|
|
|
|
context 'username is not gitlab-ci-token' do
|
|
|
|
let(:username) { 'another_username' }
|
|
|
|
|
|
|
|
it 'fails to authenticate' do
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(auth_failure)
|
2020-07-30 12:09:33 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-15 09:57:09 +00:00
|
|
|
end
|
|
|
|
|
2020-06-22 15:09:27 +00:00
|
|
|
(Ci::HasStatus::AVAILABLE_STATUSES - ['running']).each do |build_status|
|
2016-09-16 09:06:31 +00:00
|
|
|
context "for #{build_status} build" do
|
|
|
|
let!(:build) { create(:ci_build, status: build_status) }
|
|
|
|
let(:project) { build.project }
|
|
|
|
|
|
|
|
it 'denies authentication' do
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(auth_failure)
|
2016-09-16 09:06:31 +00:00
|
|
|
end
|
2016-09-15 09:57:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognizes other ci services' do
|
2021-06-16 00:10:15 +00:00
|
|
|
project.create_drone_ci_integration(active: true)
|
2022-01-31 18:18:10 +00:00
|
|
|
project.drone_ci_integration.update!(token: 'token', drone_url: generate(:url))
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client('drone-ci-token', 'token', project: project, ip: 'ip')).to have_attributes(actor: nil, project: project, type: :ci, authentication_abilities: described_class.build_authentication_abilities)
|
2016-04-29 16:58:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognizes master passwords' do
|
2022-07-26 18:11:45 +00:00
|
|
|
user = create(:user)
|
2016-04-29 16:58:55 +00:00
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip')).to have_attributes(actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities)
|
2016-04-29 16:58:55 +00:00
|
|
|
end
|
|
|
|
|
2017-02-17 12:24:32 +00:00
|
|
|
include_examples 'user login operation with unique ip limit' do
|
2022-07-26 18:11:45 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-02-17 11:52:27 +00:00
|
|
|
|
2017-02-17 12:24:32 +00:00
|
|
|
def operation
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip')).to have_attributes(actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities)
|
2017-02-17 11:52:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
context 'while using LFS authenticate' do
|
|
|
|
it 'recognizes user lfs tokens' do
|
|
|
|
user = create(:user)
|
|
|
|
token = Gitlab::LfsToken.new(user).token
|
2016-09-19 14:34:32 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client(user.username, token, project: nil, ip: 'ip')).to have_attributes(actor: user, project: nil, type: :lfs_token, authentication_abilities: described_class.read_write_project_authentication_abilities)
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
2016-09-19 14:34:32 +00:00
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
it 'recognizes deploy key lfs tokens' do
|
|
|
|
key = create(:deploy_key)
|
|
|
|
token = Gitlab::LfsToken.new(key).token
|
2016-09-19 14:34:32 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: nil, ip: 'ip')).to have_attributes(actor: key, project: nil, type: :lfs_deploy_token, authentication_abilities: described_class.read_only_authentication_abilities)
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
2016-09-19 14:34:32 +00:00
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
it 'does not try password auth before oauth' do
|
2016-11-22 09:13:37 +00:00
|
|
|
user = create(:user)
|
2017-01-24 17:12:49 +00:00
|
|
|
token = Gitlab::LfsToken.new(user).token
|
|
|
|
|
|
|
|
expect(gl_auth).not_to receive(:find_with_user_password)
|
2016-11-22 09:13:37 +00:00
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
gl_auth.find_for_git_client(user.username, token, project: nil, ip: 'ip')
|
|
|
|
end
|
2017-11-08 16:21:39 +00:00
|
|
|
|
|
|
|
it 'grants deploy key write permissions' do
|
2018-01-05 15:23:44 +00:00
|
|
|
key = create(:deploy_key)
|
|
|
|
create(:deploy_keys_project, :write_access, deploy_key: key, project: project)
|
2017-11-08 16:21:39 +00:00
|
|
|
token = Gitlab::LfsToken.new(key).token
|
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: project, ip: 'ip')).to have_attributes(actor: key, project: nil, type: :lfs_deploy_token, authentication_abilities: described_class.read_write_authentication_abilities)
|
2017-11-08 16:21:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not grant deploy key write permissions' do
|
2018-01-05 15:23:44 +00:00
|
|
|
key = create(:deploy_key)
|
2017-11-08 16:21:39 +00:00
|
|
|
token = Gitlab::LfsToken.new(key).token
|
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: project, ip: 'ip')).to have_attributes(actor: key, project: nil, type: :lfs_deploy_token, authentication_abilities: described_class.read_only_authentication_abilities)
|
2017-11-08 16:21:39 +00:00
|
|
|
end
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while using OAuth tokens as passwords' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:application) { Doorkeeper::Application.create!(name: 'MyApp', redirect_uri: 'https://app.com', owner: user) }
|
|
|
|
|
2020-03-28 00:07:51 +00:00
|
|
|
shared_examples 'an oauth failure' do
|
|
|
|
it 'fails' do
|
2021-11-19 09:13:48 +00:00
|
|
|
access_token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id, scopes: 'api')
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client("oauth2", access_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-03-28 00:07:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-19 09:13:48 +00:00
|
|
|
context 'with specified scopes' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:scopes, :abilities) do
|
|
|
|
'api' | described_class.full_authentication_abilities
|
|
|
|
'read_api' | described_class.read_only_authentication_abilities
|
|
|
|
'read_repository' | [:download_code]
|
|
|
|
'write_repository' | [:download_code, :push_code]
|
|
|
|
'read_user' | []
|
|
|
|
'sudo' | []
|
|
|
|
'openid' | []
|
|
|
|
'profile' | []
|
|
|
|
'email' | []
|
|
|
|
end
|
2016-11-22 09:13:37 +00:00
|
|
|
|
2021-11-19 09:13:48 +00:00
|
|
|
with_them do
|
|
|
|
it 'authenticates with correct abilities' do
|
|
|
|
access_token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id, scopes: scopes)
|
2016-11-22 09:13:37 +00:00
|
|
|
|
2021-11-19 09:13:48 +00:00
|
|
|
expect(gl_auth.find_for_git_client("oauth2", access_token.token, project: nil, ip: 'ip'))
|
|
|
|
.to have_attributes(actor: user, project: nil, type: :oauth, authentication_abilities: abilities)
|
|
|
|
end
|
|
|
|
end
|
2016-11-22 09:13:37 +00:00
|
|
|
end
|
2017-01-24 17:12:49 +00:00
|
|
|
|
|
|
|
it 'does not try password auth before oauth' do
|
2021-11-19 09:13:48 +00:00
|
|
|
access_token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id, scopes: 'api')
|
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
expect(gl_auth).not_to receive(:find_with_user_password)
|
|
|
|
|
2021-11-19 09:13:48 +00:00
|
|
|
gl_auth.find_for_git_client("oauth2", access_token.token, project: nil, ip: 'ip')
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
2020-03-26 18:08:03 +00:00
|
|
|
|
|
|
|
context 'blocked user' do
|
|
|
|
let(:user) { create(:user, :blocked) }
|
|
|
|
|
2020-03-28 00:07:51 +00:00
|
|
|
it_behaves_like 'an oauth failure'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'orphaned token' do
|
|
|
|
before do
|
2022-01-31 18:18:10 +00:00
|
|
|
user.destroy!
|
2020-03-26 18:08:03 +00:00
|
|
|
end
|
2020-03-28 00:07:51 +00:00
|
|
|
|
|
|
|
it_behaves_like 'an oauth failure'
|
2020-03-26 18:08:03 +00:00
|
|
|
end
|
2016-11-22 09:13:37 +00:00
|
|
|
end
|
|
|
|
|
2017-01-24 17:12:49 +00:00
|
|
|
context 'while using personal access tokens as passwords' do
|
|
|
|
it 'succeeds for personal access tokens with the `api` scope' do
|
2016-12-28 16:19:08 +00:00
|
|
|
personal_access_token = create(:personal_access_token, scopes: ['api'])
|
|
|
|
|
2019-09-28 00:06:20 +00:00
|
|
|
expect_results_with_abilities(personal_access_token, described_class.full_authentication_abilities)
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds for personal access tokens with the `read_repository` scope' do
|
|
|
|
personal_access_token = create(:personal_access_token, scopes: ['read_repository'])
|
|
|
|
|
|
|
|
expect_results_with_abilities(personal_access_token, [:download_code])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds for personal access tokens with the `write_repository` scope' do
|
|
|
|
personal_access_token = create(:personal_access_token, scopes: ['write_repository'])
|
|
|
|
|
|
|
|
expect_results_with_abilities(personal_access_token, [:download_code, :push_code])
|
2016-11-22 09:13:37 +00:00
|
|
|
end
|
|
|
|
|
2017-08-04 13:17:20 +00:00
|
|
|
context 'when registry is enabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds for personal access tokens with the `read_registry` scope' do
|
|
|
|
personal_access_token = create(:personal_access_token, scopes: ['read_registry'])
|
2017-05-31 13:55:12 +00:00
|
|
|
|
2019-04-15 13:05:55 +00:00
|
|
|
expect_results_with_abilities(personal_access_token, [:read_container_image])
|
2017-08-04 13:17:20 +00:00
|
|
|
end
|
2017-05-31 13:55:12 +00:00
|
|
|
end
|
|
|
|
|
2016-12-28 16:19:08 +00:00
|
|
|
it 'succeeds if it is an impersonation token' do
|
2017-03-01 16:59:03 +00:00
|
|
|
impersonation_token = create(:personal_access_token, :impersonation, scopes: ['api'])
|
2016-04-29 16:58:55 +00:00
|
|
|
|
2019-09-28 00:06:20 +00:00
|
|
|
expect_results_with_abilities(impersonation_token, described_class.full_authentication_abilities)
|
2016-11-22 09:13:37 +00:00
|
|
|
end
|
|
|
|
|
2021-08-03 21:09:39 +00:00
|
|
|
it 'fails if it is an impersonation token but impersonation is blocked' do
|
|
|
|
stub_config_setting(impersonation_enabled: false)
|
|
|
|
|
|
|
|
impersonation_token = create(:personal_access_token, :impersonation, scopes: ['api'])
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client('', impersonation_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2021-08-03 21:09:39 +00:00
|
|
|
end
|
|
|
|
|
2017-06-06 11:18:01 +00:00
|
|
|
it 'limits abilities based on scope' do
|
2017-11-23 11:41:29 +00:00
|
|
|
personal_access_token = create(:personal_access_token, scopes: %w[read_user sudo])
|
2016-04-29 16:58:55 +00:00
|
|
|
|
2019-04-15 13:05:55 +00:00
|
|
|
expect_results_with_abilities(personal_access_token, [])
|
2017-02-27 18:56:54 +00:00
|
|
|
end
|
2017-01-24 17:12:49 +00:00
|
|
|
|
2016-12-28 16:19:08 +00:00
|
|
|
it 'fails if password is nil' do
|
2019-04-15 13:05:55 +00:00
|
|
|
expect_results_with_abilities(nil, nil, false)
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
2020-03-26 18:08:03 +00:00
|
|
|
|
|
|
|
context 'when user is blocked' do
|
|
|
|
let(:user) { create(:user, :blocked) }
|
|
|
|
let(:personal_access_token) { create(:personal_access_token, scopes: ['read_registry'], user: user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if user is blocked' do
|
|
|
|
expect(gl_auth.find_for_git_client('', personal_access_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-03-26 18:08:03 +00:00
|
|
|
end
|
|
|
|
end
|
2020-09-16 12:10:15 +00:00
|
|
|
|
2021-07-20 09:08:43 +00:00
|
|
|
context 'when using a resource access token' do
|
|
|
|
shared_examples 'with a valid access token' do
|
2020-12-16 12:09:53 +00:00
|
|
|
it 'successfully authenticates the project bot' do
|
2021-07-20 09:08:43 +00:00
|
|
|
expect(gl_auth.find_for_git_client(project_bot_user.username, access_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(actor: project_bot_user, project: nil, type: :personal_access_token, authentication_abilities: described_class.full_authentication_abilities)
|
2020-09-16 12:10:15 +00:00
|
|
|
end
|
2021-01-07 12:10:24 +00:00
|
|
|
|
|
|
|
it 'successfully authenticates the project bot with a nil project' do
|
2021-07-20 09:08:43 +00:00
|
|
|
expect(gl_auth.find_for_git_client(project_bot_user.username, access_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(actor: project_bot_user, project: nil, type: :personal_access_token, authentication_abilities: described_class.full_authentication_abilities)
|
2021-01-07 12:10:24 +00:00
|
|
|
end
|
2020-09-16 12:10:15 +00:00
|
|
|
end
|
|
|
|
|
2021-07-20 09:08:43 +00:00
|
|
|
shared_examples 'with an invalid access token' do
|
|
|
|
it 'fails for a non-member' do
|
|
|
|
expect(gl_auth.find_for_git_client(project_bot_user.username, access_token.token, project: project, ip: 'ip'))
|
2021-09-30 18:11:31 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-12-16 12:09:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project bot user is blocked' do
|
|
|
|
before do
|
|
|
|
project_bot_user.block!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails for a blocked project bot' do
|
2021-07-20 09:08:43 +00:00
|
|
|
expect(gl_auth.find_for_git_client(project_bot_user.username, access_token.token, project: project, ip: 'ip'))
|
2021-09-30 18:11:31 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-12-16 12:09:53 +00:00
|
|
|
end
|
2020-09-16 12:10:15 +00:00
|
|
|
end
|
|
|
|
end
|
2021-07-20 09:08:43 +00:00
|
|
|
|
|
|
|
context 'when using a personal namespace project access token' do
|
|
|
|
let_it_be(:project_bot_user) { create(:user, :project_bot) }
|
|
|
|
let_it_be(:access_token) { create(:personal_access_token, user: project_bot_user) }
|
|
|
|
|
|
|
|
context 'when the token belongs to the project' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(project_bot_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with a valid access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with an invalid access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in a group namespace' do
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, group: group) }
|
|
|
|
|
|
|
|
context 'when using a project access token' do
|
|
|
|
let_it_be(:project_bot_user) { create(:user, :project_bot) }
|
|
|
|
let_it_be(:access_token) { create(:personal_access_token, user: project_bot_user) }
|
|
|
|
|
|
|
|
context 'when token user belongs to the project' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(project_bot_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with a valid access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with an invalid access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using a group access token' do
|
|
|
|
let_it_be(:project_bot_user) { create(:user, name: 'Group token bot', email: "group_#{group.id}_bot@example.com", username: "group_#{group.id}_bot", user_type: :project_bot) }
|
|
|
|
let_it_be(:access_token) { create(:personal_access_token, user: project_bot_user) }
|
|
|
|
|
|
|
|
context 'when the token belongs to the group' do
|
|
|
|
before do
|
|
|
|
group.add_maintainer(project_bot_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with a valid access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with an invalid access token'
|
2022-06-20 06:09:26 +00:00
|
|
|
|
|
|
|
context 'when the token belongs to a group via project share' do
|
|
|
|
let_it_be(:invited_group) { create(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
invited_group.add_maintainer(project_bot_user)
|
|
|
|
create(:project_group_link, group: invited_group, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'with a valid access token'
|
|
|
|
end
|
2021-07-20 09:08:43 +00:00
|
|
|
end
|
|
|
|
end
|
2020-09-16 12:10:15 +00:00
|
|
|
end
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while using regular user and password' do
|
2020-03-26 18:08:03 +00:00
|
|
|
it 'fails for a blocked user' do
|
|
|
|
user = create(
|
|
|
|
:user,
|
|
|
|
:blocked,
|
2022-07-26 18:11:45 +00:00
|
|
|
username: 'normal_user'
|
2020-03-26 18:08:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-03-26 18:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-09-30 18:11:31 +00:00
|
|
|
context 'when 2fa is enabled globally' do
|
|
|
|
let_it_be(:user) do
|
2022-07-26 18:11:45 +00:00
|
|
|
create(:user, username: 'normal_user', otp_grace_period_started_at: 1.day.ago)
|
2021-09-30 18:11:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(require_two_factor_authentication: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if grace period expired' do
|
|
|
|
stub_application_setting(two_factor_grace_period: 0)
|
|
|
|
|
|
|
|
expect { gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip') }
|
|
|
|
.to raise_error(Gitlab::Auth::MissingPersonalAccessTokenError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'goes through if grace period is not expired yet' do
|
|
|
|
stub_application_setting(two_factor_grace_period: 72)
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip'))
|
|
|
|
.to have_attributes(actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when 2fa is enabled personally' do
|
|
|
|
let(:user) do
|
2022-07-26 18:11:45 +00:00
|
|
|
create(:user, :two_factor, username: 'normal_user', otp_grace_period_started_at: 1.day.ago)
|
2021-09-30 18:11:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails' do
|
|
|
|
expect { gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip') }
|
|
|
|
.to raise_error(Gitlab::Auth::MissingPersonalAccessTokenError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-03 21:06:23 +00:00
|
|
|
it 'goes through lfs authentication' do
|
2017-01-24 17:12:49 +00:00
|
|
|
user = create(
|
|
|
|
:user,
|
2022-07-26 18:11:45 +00:00
|
|
|
username: 'normal_user'
|
2017-01-24 17:12:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities)
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 21:06:23 +00:00
|
|
|
it 'goes through oauth authentication when the username is oauth2' do
|
2017-01-24 17:12:49 +00:00
|
|
|
user = create(
|
|
|
|
:user,
|
2022-07-26 18:11:45 +00:00
|
|
|
username: 'oauth2'
|
2017-01-24 17:12:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(user.username, user.password, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities)
|
2017-01-24 17:12:49 +00:00
|
|
|
end
|
2016-04-29 16:58:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns double nil for invalid credentials' do
|
|
|
|
login = 'foo'
|
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(gl_auth.find_for_git_client(login, 'bar', project: nil, ip: 'ip')).to have_attributes(auth_failure)
|
2016-04-29 16:58:55 +00:00
|
|
|
end
|
2017-06-07 19:49:45 +00:00
|
|
|
|
|
|
|
it 'throws an error suggesting user create a PAT when internal auth is disabled' do
|
2017-11-23 13:16:14 +00:00
|
|
|
allow_any_instance_of(ApplicationSetting).to receive(:password_authentication_enabled_for_git?) { false }
|
2017-06-07 19:49:45 +00:00
|
|
|
|
2020-12-16 09:10:26 +00:00
|
|
|
expect { gl_auth.find_for_git_client('foo', 'bar', project: nil, ip: 'ip') }.to raise_error(Gitlab::Auth::MissingPersonalAccessTokenError)
|
2017-06-07 19:49:45 +00:00
|
|
|
end
|
2018-03-29 22:56:35 +00:00
|
|
|
|
|
|
|
context 'while using deploy tokens' do
|
2020-04-14 00:09:57 +00:00
|
|
|
shared_examples 'registry token scope' do
|
|
|
|
it 'fails when login is not valid' do
|
|
|
|
expect(gl_auth.find_for_git_client('random_login', deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when token is not valid' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, '123123', project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if token is nil' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, nil, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if token is not related to project' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, 'abcdef', project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if token has been revoked' do
|
|
|
|
deploy_token.revoke!
|
|
|
|
|
|
|
|
expect(deploy_token.revoked?).to be_truthy
|
|
|
|
expect(gl_auth.find_for_git_client('deploy-token', deploy_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-02 15:10:54 +00:00
|
|
|
shared_examples 'deploy token with disabled feature' do
|
2020-04-14 00:09:57 +00:00
|
|
|
context 'when registry disabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when login and token are valid' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, deploy_token.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
end
|
2020-09-02 15:10:54 +00:00
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
let(:project) { create(:project, :repository_disabled) }
|
|
|
|
|
|
|
|
it 'fails when login and token are valid' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-09-02 15:10:54 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
|
|
|
|
2019-07-17 20:08:58 +00:00
|
|
|
context 'when deploy token and user have the same username' do
|
|
|
|
let(:username) { 'normal_user' }
|
2022-07-26 18:11:45 +00:00
|
|
|
let(:user) { create(:user, username: username) }
|
2019-07-17 20:08:58 +00:00
|
|
|
let(:deploy_token) { create(:deploy_token, username: username, read_registry: false, projects: [project]) }
|
|
|
|
|
|
|
|
it 'succeeds for the token' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project, type: :deploy_token, authentication_abilities: [:download_code] }
|
2019-07-17 20:08:58 +00:00
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(username, deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds for the user' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: user, project: nil, type: :gitlab_or_ldap, authentication_abilities: described_class.full_authentication_abilities }
|
2019-07-17 20:08:58 +00:00
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_for_git_client(username, user.password, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deploy tokens have the same username' do
|
|
|
|
context 'and belong to the same project' do
|
|
|
|
let!(:read_registry) { create(:deploy_token, username: 'deployer', read_repository: false, projects: [project]) }
|
|
|
|
let!(:read_repository) { create(:deploy_token, username: read_registry.username, read_registry: false, projects: [project]) }
|
2021-08-17 18:10:35 +00:00
|
|
|
let(:auth_success) { { actor: read_repository, project: project, type: :deploy_token, authentication_abilities: [:download_code] } }
|
2019-07-17 20:08:58 +00:00
|
|
|
|
|
|
|
it 'succeeds for the right token' do
|
|
|
|
expect(gl_auth.find_for_git_client('deployer', read_repository.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails for the wrong token' do
|
|
|
|
expect(gl_auth.find_for_git_client('deployer', read_registry.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.not_to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and belong to different projects' do
|
2020-04-14 00:09:57 +00:00
|
|
|
let_it_be(:other_project) { create(:project) }
|
2021-04-10 18:09:23 +00:00
|
|
|
|
2019-09-28 00:06:20 +00:00
|
|
|
let!(:read_registry) { create(:deploy_token, username: 'deployer', read_repository: false, projects: [project]) }
|
2020-04-14 00:09:57 +00:00
|
|
|
let!(:read_repository) { create(:deploy_token, username: read_registry.username, read_registry: false, projects: [other_project]) }
|
2021-08-17 18:10:35 +00:00
|
|
|
let(:auth_success) { { actor: read_repository, project: other_project, type: :deploy_token, authentication_abilities: [:download_code] } }
|
2019-07-17 20:08:58 +00:00
|
|
|
|
|
|
|
it 'succeeds for the right token' do
|
2020-04-14 00:09:57 +00:00
|
|
|
expect(gl_auth.find_for_git_client('deployer', read_repository.token, project: other_project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails for the wrong token' do
|
2020-04-14 00:09:57 +00:00
|
|
|
expect(gl_auth.find_for_git_client('deployer', read_registry.token, project: other_project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.not_to have_attributes(auth_success)
|
2019-07-17 20:08:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-04 23:43:41 +00:00
|
|
|
context 'when the deploy token has read_repository as scope' do
|
2018-04-05 17:22:34 +00:00
|
|
|
let(:deploy_token) { create(:deploy_token, read_registry: false, projects: [project]) }
|
2018-04-06 03:02:13 +00:00
|
|
|
let(:login) { deploy_token.username }
|
2018-03-29 22:56:35 +00:00
|
|
|
|
2018-04-06 03:02:13 +00:00
|
|
|
it 'succeeds when login and token are valid' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project, type: :deploy_token, authentication_abilities: [:download_code] }
|
2018-03-29 22:56:35 +00:00
|
|
|
|
2018-04-06 03:02:13 +00:00
|
|
|
expect(gl_auth.find_for_git_client(login, deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
2018-03-29 22:56:35 +00:00
|
|
|
|
2019-07-02 18:56:48 +00:00
|
|
|
it 'succeeds when custom login and token are valid' do
|
|
|
|
deploy_token = create(:deploy_token, username: 'deployer', read_registry: false, projects: [project])
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project, type: :deploy_token, authentication_abilities: [:download_code] }
|
2019-07-02 18:56:48 +00:00
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client('deployer', deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2019-07-02 18:56:48 +00:00
|
|
|
end
|
|
|
|
|
2020-01-08 06:08:13 +00:00
|
|
|
it 'does not attempt to rate limit unique IPs for a deploy token' do
|
|
|
|
expect(Gitlab::Auth::UniqueIpsLimiter).not_to receive(:limit_user!)
|
|
|
|
|
|
|
|
gl_auth.find_for_git_client(login, deploy_token.token, project: project, ip: 'ip')
|
|
|
|
end
|
|
|
|
|
2018-04-06 03:02:13 +00:00
|
|
|
it 'fails when login is not valid' do
|
|
|
|
expect(gl_auth.find_for_git_client('random_login', deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2018-04-06 03:02:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when token is not valid' do
|
|
|
|
expect(gl_auth.find_for_git_client(login, '123123', project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2018-04-06 03:02:13 +00:00
|
|
|
end
|
|
|
|
|
2018-04-03 21:34:56 +00:00
|
|
|
it 'fails if token is nil' do
|
2018-04-06 03:02:13 +00:00
|
|
|
expect(gl_auth.find_for_git_client(login, nil, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
2018-03-29 22:56:35 +00:00
|
|
|
|
2018-04-03 21:34:56 +00:00
|
|
|
it 'fails if token is not related to project' do
|
2018-04-06 03:02:13 +00:00
|
|
|
another_deploy_token = create(:deploy_token)
|
2020-08-18 18:10:10 +00:00
|
|
|
expect(gl_auth.find_for_git_client(another_deploy_token.username, another_deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
2018-03-31 22:45:02 +00:00
|
|
|
|
2018-04-03 21:34:56 +00:00
|
|
|
it 'fails if token has been revoked' do
|
|
|
|
deploy_token.revoke!
|
2018-03-31 22:45:02 +00:00
|
|
|
|
2018-04-03 21:34:56 +00:00
|
|
|
expect(deploy_token.revoked?).to be_truthy
|
|
|
|
expect(gl_auth.find_for_git_client('deploy-token', deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2018-03-31 22:45:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 12:10:29 +00:00
|
|
|
context 'when the deploy token is of group type' do
|
|
|
|
let(:project_with_group) { create(:project, group: create(:group)) }
|
|
|
|
let(:deploy_token) { create(:deploy_token, :group, read_repository: true, groups: [project_with_group.group]) }
|
|
|
|
let(:login) { deploy_token.username }
|
|
|
|
|
|
|
|
subject { gl_auth.find_for_git_client(login, deploy_token.token, project: project_with_group, ip: 'ip') }
|
|
|
|
|
|
|
|
it 'succeeds when login and a group deploy token are valid' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project_with_group, type: :deploy_token, authentication_abilities: [:download_code, :read_container_image] }
|
2020-02-06 12:10:29 +00:00
|
|
|
|
2021-08-17 18:10:35 +00:00
|
|
|
expect(subject).to have_attributes(auth_success)
|
2020-02-06 12:10:29 +00:00
|
|
|
end
|
2020-08-18 18:10:10 +00:00
|
|
|
|
|
|
|
it 'fails if token is not related to group' do
|
|
|
|
another_deploy_token = create(:deploy_token, :group, read_repository: true)
|
|
|
|
|
|
|
|
expect(gl_auth.find_for_git_client(another_deploy_token.username, another_deploy_token.token, project: project_with_group, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_failure)
|
2020-08-18 18:10:10 +00:00
|
|
|
end
|
2020-02-06 12:10:29 +00:00
|
|
|
end
|
|
|
|
|
2018-04-03 21:34:56 +00:00
|
|
|
context 'when the deploy token has read_registry as a scope' do
|
2018-04-05 17:22:34 +00:00
|
|
|
let(:deploy_token) { create(:deploy_token, read_repository: false, projects: [project]) }
|
2018-04-06 03:02:13 +00:00
|
|
|
let(:login) { deploy_token.username }
|
2018-04-03 21:34:56 +00:00
|
|
|
|
|
|
|
context 'when registry enabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
2020-02-06 12:10:29 +00:00
|
|
|
it 'succeeds when login and a project token are valid' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project, type: :deploy_token, authentication_abilities: [:read_container_image] }
|
2018-04-03 21:34:56 +00:00
|
|
|
|
2020-02-06 12:10:29 +00:00
|
|
|
expect(gl_auth.find_for_git_client(login, deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
it_behaves_like 'registry token scope'
|
|
|
|
end
|
2018-04-06 03:02:13 +00:00
|
|
|
|
2020-09-02 15:10:54 +00:00
|
|
|
it_behaves_like 'deploy token with disabled feature'
|
2020-04-14 00:09:57 +00:00
|
|
|
end
|
2018-04-06 03:02:13 +00:00
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
context 'when the deploy token has write_registry as a scope' do
|
|
|
|
let_it_be(:deploy_token) { create(:deploy_token, write_registry: true, read_repository: false, read_registry: false, projects: [project]) }
|
|
|
|
let_it_be(:login) { deploy_token.username }
|
2018-04-03 21:34:56 +00:00
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
context 'when registry enabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
it 'succeeds when login and a project token are valid' do
|
2021-08-17 18:10:35 +00:00
|
|
|
auth_success = { actor: deploy_token, project: project, type: :deploy_token, authentication_abilities: [:create_container_image] }
|
2018-03-31 22:45:02 +00:00
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
expect(gl_auth.find_for_git_client(login, deploy_token.token, project: project, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(auth_success)
|
2018-04-03 21:34:56 +00:00
|
|
|
end
|
2018-03-31 22:45:02 +00:00
|
|
|
|
2020-04-14 00:09:57 +00:00
|
|
|
it_behaves_like 'registry token scope'
|
2018-03-31 22:45:02 +00:00
|
|
|
end
|
2020-04-14 00:09:57 +00:00
|
|
|
|
2020-09-02 15:10:54 +00:00
|
|
|
it_behaves_like 'deploy token with disabled feature'
|
2018-03-31 22:45:02 +00:00
|
|
|
end
|
2018-03-29 22:56:35 +00:00
|
|
|
end
|
2016-04-29 16:58:55 +00:00
|
|
|
end
|
|
|
|
|
2021-06-02 18:10:01 +00:00
|
|
|
describe '#build_access_token_check' do
|
|
|
|
subject { gl_auth.find_for_git_client('gitlab-ci-token', build.token, project: build.project, ip: '1.2.3.4') }
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
context 'for running build' do
|
|
|
|
let!(:build) { create(:ci_build, :running, user: user) }
|
|
|
|
|
|
|
|
it 'executes query using primary database' do
|
|
|
|
expect(Ci::Build).to receive(:find_by_token).with(build.token).and_wrap_original do |m, *args|
|
|
|
|
expect(::Gitlab::Database::LoadBalancing::Session.current.use_primary?).to eq(true)
|
|
|
|
m.call(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject).to be_a(Gitlab::Auth::Result)
|
|
|
|
expect(subject.actor).to eq(user)
|
|
|
|
expect(subject.project).to eq(build.project)
|
|
|
|
expect(subject.type).to eq(:build)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-10 12:51:16 +00:00
|
|
|
describe 'find_with_user_password' do
|
2022-07-26 18:11:45 +00:00
|
|
|
let!(:user) { create(:user, username: username) }
|
2019-01-16 12:09:29 +00:00
|
|
|
let(:username) { 'John' } # username isn't lowercase, test this
|
2012-09-12 06:23:16 +00:00
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "finds user by valid login/password" do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to eql user
|
2012-09-12 06:23:16 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'finds user by valid email/password with case-insensitive email' do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(user.email.upcase, user.password)).to eql user
|
2014-10-22 15:29:26 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'finds user by valid username/password with case-insensitive username' do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username.upcase, user.password)).to eql user
|
2014-10-22 15:29:26 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "does not find user with invalid password" do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, 'incorrect_password')).not_to eql user
|
2012-09-12 06:23:16 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "does not find user with invalid login" do
|
2022-07-26 18:11:45 +00:00
|
|
|
username = 'wrong'
|
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).not_to eql user
|
2013-03-11 06:44:45 +00:00
|
|
|
end
|
2014-09-08 11:26:25 +00:00
|
|
|
|
2017-02-17 12:24:32 +00:00
|
|
|
include_examples 'user login operation with unique ip limit' do
|
|
|
|
def operation
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to eq(user)
|
2017-02-17 12:24:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-10 00:06:44 +00:00
|
|
|
it 'finds the user in deactivated state' do
|
|
|
|
user.deactivate!
|
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to eql user
|
2019-10-10 00:06:44 +00:00
|
|
|
end
|
|
|
|
|
2017-01-18 10:23:25 +00:00
|
|
|
it "does not find user in blocked state" do
|
|
|
|
user.block
|
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).not_to eql user
|
2017-01-18 10:23:25 +00:00
|
|
|
end
|
|
|
|
|
2020-09-02 15:10:54 +00:00
|
|
|
it 'does not find user in locked state' do
|
|
|
|
user.lock_access!
|
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).not_to eql user
|
2020-09-02 15:10:54 +00:00
|
|
|
end
|
|
|
|
|
2017-01-18 10:23:25 +00:00
|
|
|
it "does not find user in ldap_blocked state" do
|
|
|
|
user.ldap_block
|
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).not_to eql user
|
2017-01-18 10:23:25 +00:00
|
|
|
end
|
|
|
|
|
2020-10-06 12:08:38 +00:00
|
|
|
it 'does not find user in blocked_pending_approval state' do
|
|
|
|
user.block_pending_approval
|
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).not_to eql user
|
2020-10-06 12:08:38 +00:00
|
|
|
end
|
|
|
|
|
2020-09-02 15:10:54 +00:00
|
|
|
context 'with increment_failed_attempts' do
|
|
|
|
wrong_password = 'incorrect_password'
|
|
|
|
|
|
|
|
it 'increments failed_attempts when true and password is incorrect' do
|
|
|
|
expect do
|
|
|
|
gl_auth.find_with_user_password(username, wrong_password, increment_failed_attempts: true)
|
|
|
|
user.reload
|
|
|
|
end.to change(user, :failed_attempts).from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets failed_attempts when true and password is correct' do
|
|
|
|
user.failed_attempts = 2
|
2022-01-31 18:18:10 +00:00
|
|
|
user.save!
|
2020-09-02 15:10:54 +00:00
|
|
|
|
|
|
|
expect do
|
2022-07-26 18:11:45 +00:00
|
|
|
gl_auth.find_with_user_password(username, user.password, increment_failed_attempts: true)
|
2020-09-02 15:10:54 +00:00
|
|
|
user.reload
|
|
|
|
end.to change(user, :failed_attempts).from(2).to(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment failed_attempts by default' do
|
|
|
|
expect do
|
|
|
|
gl_auth.find_with_user_password(username, wrong_password)
|
|
|
|
user.reload
|
|
|
|
end.not_to change(user, :failed_attempts)
|
|
|
|
end
|
|
|
|
|
2021-05-06 09:18:56 +00:00
|
|
|
context 'when the database is read-only' do
|
2020-09-02 15:10:54 +00:00
|
|
|
before do
|
2021-08-02 09:10:09 +00:00
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
2020-09-02 15:10:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment failed_attempts when true and password is incorrect' do
|
|
|
|
expect do
|
|
|
|
gl_auth.find_with_user_password(username, wrong_password, increment_failed_attempts: true)
|
|
|
|
user.reload
|
|
|
|
end.not_to change(user, :failed_attempts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset failed_attempts when true and password is correct' do
|
|
|
|
user.failed_attempts = 2
|
2022-01-31 18:18:10 +00:00
|
|
|
user.save!
|
2020-09-02 15:10:54 +00:00
|
|
|
|
|
|
|
expect do
|
2022-07-26 18:11:45 +00:00
|
|
|
gl_auth.find_with_user_password(username, user.password, increment_failed_attempts: true)
|
2020-09-02 15:10:54 +00:00
|
|
|
user.reload
|
|
|
|
end.not_to change(user, :failed_attempts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-08 11:26:25 +00:00
|
|
|
context "with ldap enabled" do
|
2015-05-21 21:49:06 +00:00
|
|
|
before do
|
2020-03-12 15:09:39 +00:00
|
|
|
allow(Gitlab::Auth::Ldap::Config).to receive(:enabled?).and_return(true)
|
2015-05-21 21:49:06 +00:00
|
|
|
end
|
2014-09-08 11:26:25 +00:00
|
|
|
|
|
|
|
it "tries to autheticate with db before ldap" do
|
2020-03-12 15:09:39 +00:00
|
|
|
expect(Gitlab::Auth::Ldap::Authentication).not_to receive(:login)
|
2014-09-08 11:26:25 +00:00
|
|
|
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to eq(user)
|
2018-03-26 09:23:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not find user by using ldap as fallback to for authentication" do
|
2020-03-12 15:09:39 +00:00
|
|
|
expect(Gitlab::Auth::Ldap::Authentication).to receive(:login).and_return(nil)
|
2018-03-26 09:23:54 +00:00
|
|
|
|
2022-03-31 21:08:16 +00:00
|
|
|
expect(gl_auth.find_with_user_password('ldap_user', 'password')).to be_nil
|
2014-09-08 11:26:25 +00:00
|
|
|
end
|
|
|
|
|
2018-03-26 09:23:54 +00:00
|
|
|
it "find new user by using ldap as fallback to for authentication" do
|
2020-03-12 15:09:39 +00:00
|
|
|
expect(Gitlab::Auth::Ldap::Authentication).to receive(:login).and_return(user)
|
2014-09-08 11:26:25 +00:00
|
|
|
|
2022-03-31 21:08:16 +00:00
|
|
|
expect(gl_auth.find_with_user_password('ldap_user', 'password')).to eq(user)
|
2014-09-08 11:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-23 13:16:14 +00:00
|
|
|
|
|
|
|
context "with password authentication disabled for Git" do
|
|
|
|
before do
|
|
|
|
stub_application_setting(password_authentication_enabled_for_git: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not find user by valid login/password" do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to be_nil
|
2017-11-23 13:16:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with ldap enabled" do
|
|
|
|
before do
|
2020-03-12 15:09:39 +00:00
|
|
|
allow(Gitlab::Auth::Ldap::Config).to receive(:enabled?).and_return(true)
|
2017-11-23 13:16:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not find non-ldap user by valid login/password" do
|
2022-07-26 18:11:45 +00:00
|
|
|
expect(gl_auth.find_with_user_password(username, user.password)).to be_nil
|
2017-11-23 13:16:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-12 06:23:16 +00:00
|
|
|
end
|
2016-09-15 09:57:09 +00:00
|
|
|
|
2020-05-05 21:09:42 +00:00
|
|
|
describe ".resource_bot_scopes" do
|
|
|
|
subject { described_class.resource_bot_scopes }
|
|
|
|
|
|
|
|
it { is_expected.to include(*described_class::API_SCOPES - [:read_user]) }
|
|
|
|
it { is_expected.to include(*described_class::REPOSITORY_SCOPES) }
|
|
|
|
it { is_expected.to include(*described_class.registry_scopes) }
|
|
|
|
end
|
|
|
|
|
2016-09-15 09:57:09 +00:00
|
|
|
private
|
|
|
|
|
2019-04-15 13:05:55 +00:00
|
|
|
def expect_results_with_abilities(personal_access_token, abilities, success = true)
|
|
|
|
expect(gl_auth.find_for_git_client('', personal_access_token&.token, project: nil, ip: 'ip'))
|
2021-08-17 18:10:35 +00:00
|
|
|
.to have_attributes(actor: personal_access_token&.user, project: nil, type: personal_access_token.nil? ? nil : :personal_access_token, authentication_abilities: abilities)
|
2019-04-15 13:05:55 +00:00
|
|
|
end
|
2012-09-12 06:23:16 +00:00
|
|
|
end
|