debian-mirror-gitlab/spec/requests/api/internal/base_spec.rb

1631 lines
53 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe API::Internal::Base, feature_category: :system_access do
2022-10-11 01:57:18 +05:30
include GitlabShellHelpers
2021-01-03 14:25:43 +05:30
include APIInternalBaseHelpers
2020-04-22 19:07:51 +05:30
let_it_be(:user, reload: true) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :repository, :wiki_repo) }
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
let_it_be(:project_snippet) { create(:project_snippet, :repository, author: user, project: project) }
2021-09-30 23:02:18 +05:30
2014-09-02 18:07:02 +05:30
let(:key) { create(:key, user: user) }
2017-08-17 22:00:37 +05:30
let(:secret_token) { Gitlab::Shell.secret_token }
2018-03-17 18:26:18 +05:30
let(:gl_repository) { "project-#{project.id}" }
let(:reference_counter) { double('ReferenceCounter') }
2020-04-08 14:13:33 +05:30
let(:snippet_changes) { "#{TestEnv::BRANCH_SHA['snippet/single-file']} #{TestEnv::BRANCH_SHA['snippet/edit-file']} refs/heads/snippet/edit-file" }
2017-09-10 17:25:29 +05:30
describe "GET /internal/check" do
2022-10-11 01:57:18 +05:30
def perform_request(headers: gitlab_shell_internal_api_request_header)
get api("/internal/check"), headers: headers
end
2014-09-02 18:07:02 +05:30
it do
2018-03-17 18:26:18 +05:30
expect_any_instance_of(Redis).to receive(:ping).and_return('PONG')
2022-10-11 01:57:18 +05:30
perform_request
2014-09-02 18:07:02 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2015-04-26 12:48:37 +05:30
expect(json_response['api_version']).to eq(API::API.version)
2018-03-17 18:26:18 +05:30
expect(json_response['redis']).to be(true)
end
it 'returns false for field `redis` when redis is unavailable' do
expect_any_instance_of(Redis).to receive(:ping).and_raise(Errno::ENOENT)
2022-10-11 01:57:18 +05:30
perform_request
2018-03-17 18:26:18 +05:30
expect(json_response['redis']).to be(false)
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
context 'authenticating' do
2022-10-11 01:57:18 +05:30
it 'authenticates using a jwt token in a header' do
perform_request
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-07-07 11:18:12 +05:30
end
2022-10-11 01:57:18 +05:30
it 'returns 401 when jwt token is expired' do
headers = gitlab_shell_internal_api_request_header
travel_to(2.minutes.since) do
perform_request(headers: headers)
end
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns 401 when jwt issuer is not Gitlab-Shell' do
perform_request(headers: gitlab_shell_internal_api_request_header(issuer: "gitlab-workhorse"))
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2019-07-07 11:18:12 +05:30
end
2022-10-11 01:57:18 +05:30
it 'returns 401 when jwt token is not provided, even if plain secret is provided' do
perform_request(headers: { API::Helpers::GITLAB_SHARED_SECRET_HEADER => Base64.encode64(secret_token) })
expect(response).to have_gitlab_http_status(:unauthorized)
end
2019-07-07 11:18:12 +05:30
end
2015-04-26 12:48:37 +05:30
end
2021-01-03 14:25:43 +05:30
describe 'GET /internal/two_factor_recovery_codes' do
let(:key_id) { key.id }
2016-09-29 09:46:39 +05:30
2021-01-03 14:25:43 +05:30
subject do
2016-09-29 09:46:39 +05:30
post api('/internal/two_factor_recovery_codes'),
2022-10-11 01:57:18 +05:30
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
2021-01-03 14:25:43 +05:30
end
2016-09-29 09:46:39 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'actor key validations'
context 'key is a deploy key' do
let(:key_id) { create(:deploy_key).id }
it 'returns an error message' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq('Deploy keys cannot be used to retrieve recovery codes')
end
2016-09-29 09:46:39 +05:30
end
context 'when two-factor is enabled' do
it 'returns new recovery codes when the user exists' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
allow_any_instance_of(User)
.to receive(:generate_otp_backup_codes!).and_return(%w(119135e5a3ebce8e 34bd7b74adbc8861))
2021-01-03 14:25:43 +05:30
subject
2016-09-29 09:46:39 +05:30
expect(json_response['success']).to be_truthy
expect(json_response['recovery_codes']).to match_array(%w(119135e5a3ebce8e 34bd7b74adbc8861))
end
end
context 'when two-factor is not enabled' do
it 'returns an error message' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(false)
2021-01-03 14:25:43 +05:30
subject
2016-09-29 09:46:39 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['recovery_codes']).to be_nil
end
end
end
2020-10-24 23:57:45 +05:30
describe 'POST /internal/personal_access_token' do
2021-01-03 14:25:43 +05:30
let(:key_id) { key.id }
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
subject do
2020-10-24 23:57:45 +05:30
post api('/internal/personal_access_token'),
2022-10-11 01:57:18 +05:30
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
end
2021-01-03 14:25:43 +05:30
it_behaves_like 'actor key validations'
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
context 'key is a deploy key' do
let(:key_id) { create(:deploy_key).id }
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
it 'returns an error message' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq('Deploy keys cannot be used to create personal access tokens')
end
2020-10-24 23:57:45 +05:30
end
it 'returns an error message when given an non existent user' do
post api('/internal/personal_access_token'),
2022-10-11 01:57:18 +05:30
params: { user_id: 0 },
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("Could not find the given user")
end
it 'returns an error message when no name parameter is received' do
post api('/internal/personal_access_token'),
2022-10-11 01:57:18 +05:30
params: { key_id: key.id },
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("No token name specified")
end
it 'returns an error message when no scopes parameter is received' do
post api('/internal/personal_access_token'),
2022-10-11 01:57:18 +05:30
params: { key_id: key.id, name: 'newtoken' },
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("No token scopes specified")
end
it 'returns an error message when expires_at contains an invalid date' do
post api('/internal/personal_access_token'),
params: {
2022-10-11 01:57:18 +05:30
key_id: key.id,
2020-10-24 23:57:45 +05:30
name: 'newtoken',
scopes: ['api'],
expires_at: 'invalid-date'
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("Invalid token expiry date: 'invalid-date'")
end
it 'returns an error message when it receives an invalid scope' do
post api('/internal/personal_access_token'),
params: {
2022-10-11 01:57:18 +05:30
key_id: key.id,
2020-10-24 23:57:45 +05:30
name: 'newtoken',
scopes: %w(read_api badscope read_repository)
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_falsey
expect(json_response['message']).to match(/\AInvalid scope: 'badscope'. Valid scopes are: /)
end
it 'returns a token without expiry when the expires_at parameter is missing' do
2021-02-22 17:27:13 +05:30
token_size = (PersonalAccessToken.token_prefix || '').size + 20
2020-10-24 23:57:45 +05:30
post api('/internal/personal_access_token'),
params: {
2022-10-11 01:57:18 +05:30
key_id: key.id,
2020-10-24 23:57:45 +05:30
name: 'newtoken',
scopes: %w(read_api read_repository)
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_truthy
2021-02-22 17:27:13 +05:30
expect(json_response['token']).to match(/\A\S{#{token_size}}\z/)
2020-10-24 23:57:45 +05:30
expect(json_response['scopes']).to match_array(%w(read_api read_repository))
expect(json_response['expires_at']).to be_nil
end
it 'returns a token with expiry when it receives a valid expires_at parameter' do
2021-02-22 17:27:13 +05:30
token_size = (PersonalAccessToken.token_prefix || '').size + 20
2020-10-24 23:57:45 +05:30
post api('/internal/personal_access_token'),
params: {
2022-10-11 01:57:18 +05:30
key_id: key.id,
2020-10-24 23:57:45 +05:30
name: 'newtoken',
scopes: %w(read_api read_repository),
expires_at: '9001-11-17'
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2020-10-24 23:57:45 +05:30
expect(json_response['success']).to be_truthy
2021-02-22 17:27:13 +05:30
expect(json_response['token']).to match(/\A\S{#{token_size}}\z/)
2020-10-24 23:57:45 +05:30
expect(json_response['scopes']).to match_array(%w(read_api read_repository))
expect(json_response['expires_at']).to eq('9001-11-17')
end
end
2016-09-29 09:46:39 +05:30
describe "POST /internal/lfs_authenticate" do
before do
2021-01-29 00:20:46 +05:30
stub_lfs_setting(enabled: true)
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2016-09-29 09:46:39 +05:30
end
context 'user key' do
it 'returns the correct information about the key' do
2018-11-18 11:00:15 +05:30
lfs_auth_key(key.id, project)
2016-09-29 09:46:39 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-29 09:46:39 +05:30
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
2019-07-07 11:18:12 +05:30
expect(json_response['expires_in']).to eq(Gitlab::LfsToken::DEFAULT_EXPIRE_TIME)
2019-02-15 15:39:39 +05:30
expect(Gitlab::LfsToken.new(key).token_valid?(json_response['lfs_token'])).to be_truthy
2016-09-29 09:46:39 +05:30
end
2018-11-18 11:00:15 +05:30
it 'returns the correct information about the user' do
lfs_auth_user(user.id, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-11-18 11:00:15 +05:30
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
2019-02-15 15:39:39 +05:30
expect(Gitlab::LfsToken.new(user).token_valid?(json_response['lfs_token'])).to be_truthy
2018-11-18 11:00:15 +05:30
end
it 'returns a 404 when no key or user is provided' do
lfs_auth_project(project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-11-18 11:00:15 +05:30
end
2016-09-29 09:46:39 +05:30
it 'returns a 404 when the wrong key is provided' do
2020-04-22 19:07:51 +05:30
lfs_auth_key(non_existing_record_id, project)
2018-11-18 11:00:15 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-11-18 11:00:15 +05:30
end
it 'returns a 404 when the wrong user is provided' do
2020-04-22 19:07:51 +05:30
lfs_auth_user(non_existing_record_id, project)
2016-09-29 09:46:39 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-29 09:46:39 +05:30
end
2021-01-29 00:20:46 +05:30
it 'returns a 404 when LFS is disabled on the project' do
project.update!(lfs_enabled: false)
lfs_auth_user(user.id, project)
expect(response).to have_gitlab_http_status(:not_found)
end
context 'other repository types' do
it 'returns the correct information for a project wiki' do
wiki = create(:project_wiki, project: project)
lfs_auth_user(user.id, wiki)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(wiki.http_url_to_repo)
expect(json_response['expires_in']).to eq(Gitlab::LfsToken::DEFAULT_EXPIRE_TIME)
expect(Gitlab::LfsToken.new(user).token_valid?(json_response['lfs_token'])).to be_truthy
end
it 'returns a 404 when the container does not support LFS' do
snippet = create(:project_snippet)
lfs_auth_user(user.id, snippet)
expect(response).to have_gitlab_http_status(:not_found)
end
end
2016-09-29 09:46:39 +05:30
end
context 'deploy key' do
let(:key) { create(:deploy_key) }
it 'returns the correct information about the key' do
2018-11-18 11:00:15 +05:30
lfs_auth_key(key.id, project)
2016-09-29 09:46:39 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-29 09:46:39 +05:30
expect(json_response['username']).to eq("lfs+deploy-key-#{key.id}")
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
2019-02-15 15:39:39 +05:30
expect(Gitlab::LfsToken.new(key).token_valid?(json_response['lfs_token'])).to be_truthy
2016-09-29 09:46:39 +05:30
end
end
end
2014-09-02 18:07:02 +05:30
describe "GET /internal/discover" do
2018-11-18 11:00:15 +05:30
it "finds a user by key id" do
2022-10-11 01:57:18 +05:30
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
2014-09-02 18:07:02 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(json_response['name']).to eq(user.name)
2014-09-02 18:07:02 +05:30
end
2018-11-18 11:00:15 +05:30
2023-03-04 22:38:38 +05:30
context 'when signing key is passed' do
it 'does not authenticate user' do
key.signing!
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
expect(json_response).to be_nil
end
end
context 'when auth-only key is passed' do
it 'authenticates user' do
key.auth!
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq(user.name)
end
end
2018-11-18 11:00:15 +05:30
it "finds a user by username" do
2022-10-11 01:57:18 +05:30
get(api("/internal/discover"), params: { username: user.username }, headers: gitlab_shell_internal_api_request_header)
2018-11-18 11:00:15 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-11-18 11:00:15 +05:30
expect(json_response['name']).to eq(user.name)
end
2019-07-07 11:18:12 +05:30
it 'responds successfully when a user is not found' do
2022-10-11 01:57:18 +05:30
get(api('/internal/discover'), params: { username: 'noone' }, headers: gitlab_shell_internal_api_request_header)
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-01-01 13:55:28 +05:30
expect(response.body).to eq('null')
end
it 'response successfully when passing invalid params' do
2022-10-11 01:57:18 +05:30
get(api('/internal/discover'), params: { nothing: 'to find a user' }, headers: gitlab_shell_internal_api_request_header)
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-07-07 11:18:12 +05:30
expect(response.body).to eq('null')
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
describe "GET /internal/authorized_keys" do
2021-09-04 01:27:46 +05:30
context "using an existing key" do
2018-03-17 18:26:18 +05:30
it "finds the key" do
2022-10-11 01:57:18 +05:30
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
2018-03-17 18:26:18 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-06-23 00:09:42 +05:30
expect(json_response['id']).to eq(key.id)
expect(json_response['key'].split[1]).to eq(key.key.split[1])
end
2023-03-04 22:38:38 +05:30
context 'when signing key is passed' do
it 'does not return the key' do
key.signing!
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['id']).to be_nil
end
end
context 'when auth-only key is passed' do
it 'authenticates user' do
key.auth!
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq(key.id)
expect(json_response['key'].split[1]).to eq(key.key.split[1])
end
end
2020-06-23 00:09:42 +05:30
it 'exposes the comment of the key as a simple identifier of username + hostname' do
2022-10-11 01:57:18 +05:30
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
2020-06-23 00:09:42 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['key']).to include("#{key.user_name} (#{Gitlab.config.gitlab.host})")
2018-03-17 18:26:18 +05:30
end
end
2021-09-04 01:27:46 +05:30
it "returns 404 with a partial key" do
2022-10-11 01:57:18 +05:30
get(api('/internal/authorized_keys'), params: { key: key.key.split[1][0...-3] }, headers: gitlab_shell_internal_api_request_header)
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
2021-09-04 01:27:46 +05:30
it "returns 404 with an not valid base64 string" do
2022-10-11 01:57:18 +05:30
get(api('/internal/authorized_keys'), params: { key: "whatever!" }, headers: gitlab_shell_internal_api_request_header)
2020-06-23 00:09:42 +05:30
2021-09-04 01:27:46 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
end
2022-03-02 08:16:31 +05:30
describe "POST /internal/allowed", :clean_gitlab_redis_shared_state, :clean_gitlab_redis_rate_limiting do
shared_examples 'rate limited request' do
let(:action) { 'git-upload-pack' }
let(:actor) { key }
2022-08-27 11:52:29 +05:30
let(:rate_limiter) { double(:rate_limiter, ip: "127.0.0.1", trusted_ip?: false) }
before do
allow(::Gitlab::Auth::IpRateLimiter).to receive(:new).with("127.0.0.1").and_return(rate_limiter)
end
2022-03-02 08:16:31 +05:30
it 'is throttled by rate limiter' do
allow(::Gitlab::ApplicationRateLimiter).to receive(:threshold).and_return(1)
2022-08-27 11:52:29 +05:30
2022-03-02 08:16:31 +05:30
expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(:gitlab_shell_operation, scope: [action, project.full_path, actor]).twice.and_call_original
2022-08-27 11:52:29 +05:30
expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(:gitlab_shell_operation, scope: [action, project.full_path, "127.0.0.1"]).and_call_original
2022-03-02 08:16:31 +05:30
request
expect(response).to have_gitlab_http_status(:ok)
request
expect(response).to have_gitlab_http_status(:too_many_requests)
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
end
2023-03-17 16:20:25 +05:30
it 'is not throttled by rate limiter' do
expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)
2022-08-27 11:52:29 +05:30
2023-03-17 16:20:25 +05:30
subject
2022-08-27 11:52:29 +05:30
end
context 'when the IP is in a trusted range' do
let(:rate_limiter) { double(:rate_limiter, ip: "127.0.0.1", trusted_ip?: true) }
it 'is not throttled by rate limiter' do
expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)
subject
end
end
2022-03-02 08:16:31 +05:30
end
2014-09-02 18:07:02 +05:30
context "access granted" do
2020-04-08 14:13:33 +05:30
let(:env) { {} }
2018-03-17 18:26:18 +05:30
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
before do
project.add_developer(user)
2017-08-17 22:00:37 +05:30
end
2020-04-08 14:13:33 +05:30
shared_examples 'sets hook env' do
context 'with env passed as a JSON' do
let(:obj_dir_relative) { './objects' }
let(:alt_obj_dirs_relative) { ['./alt-objects-1', './alt-objects-2'] }
let(:env) do
{
GIT_OBJECT_DIRECTORY_RELATIVE: obj_dir_relative,
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: alt_obj_dirs_relative
}
end
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
it 'sets env in RequestStore' do
expect(Gitlab::Git::HookEnv).to receive(:set).with(gl_repository, env.stringify_keys)
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
subject
2018-05-09 12:01:36 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
context "git push with project.wiki" do
2020-04-08 14:13:33 +05:30
subject { push(key, project.wiki, env: env.to_json) }
2016-06-02 11:05:42 +05:30
it 'responds with success' do
2020-04-08 14:13:33 +05:30
subject
2016-08-24 12:49:21 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-08-24 12:49:21 +05:30
expect(json_response["status"]).to be_truthy
2019-07-07 11:18:12 +05:30
expect(json_response["gl_project_path"]).to eq(project.wiki.full_path)
2017-08-17 22:00:37 +05:30
expect(json_response["gl_repository"]).to eq("wiki-#{project.id}")
2020-10-24 23:57:45 +05:30
expect(json_response["gl_key_type"]).to eq("key")
expect(json_response["gl_key_id"]).to eq(key.id)
2023-05-27 22:25:52 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2016-08-24 12:49:21 +05:30
end
2020-04-08 14:13:33 +05:30
it_behaves_like 'sets hook env' do
2021-01-03 14:25:43 +05:30
let(:gl_repository) { Gitlab::GlRepository::WIKI.identifier_for_container(project.wiki) }
2020-04-08 14:13:33 +05:30
end
2016-08-24 12:49:21 +05:30
end
2016-06-02 11:05:42 +05:30
2016-08-24 12:49:21 +05:30
context "git pull with project.wiki" do
it 'responds with success' do
pull(key, project.wiki)
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-06-02 11:05:42 +05:30
expect(json_response["status"]).to be_truthy
2019-07-07 11:18:12 +05:30
expect(json_response["gl_project_path"]).to eq(project.wiki.full_path)
2017-08-17 22:00:37 +05:30
expect(json_response["gl_repository"]).to eq("wiki-#{project.id}")
2018-11-18 11:00:15 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2016-06-02 11:05:42 +05:30
end
end
2020-04-22 19:07:51 +05:30
shared_examples 'snippet success' do
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to be_truthy
end
end
2020-04-08 14:13:33 +05:30
context 'git push with personal snippet' do
subject { push(key, personal_snippet, env: env.to_json, changes: snippet_changes) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(personal_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{personal_snippet.id}")
2023-05-27 22:25:52 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2020-04-08 14:13:33 +05:30
end
it_behaves_like 'sets hook env' do
let(:gl_repository) { Gitlab::GlRepository::SNIPPET.identifier_for_container(personal_snippet) }
end
end
context 'git pull with personal snippet' do
2020-04-22 19:07:51 +05:30
subject { pull(key, personal_snippet) }
2020-04-08 14:13:33 +05:30
it 'responds with success' do
2020-04-22 19:07:51 +05:30
subject
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(personal_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{personal_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
context 'git push with project snippet' do
subject { push(key, project_snippet, env: env.to_json, changes: snippet_changes) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{project_snippet.id}")
2023-05-27 22:25:52 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2020-04-08 14:13:33 +05:30
end
it_behaves_like 'sets hook env' do
let(:gl_repository) { Gitlab::GlRepository::SNIPPET.identifier_for_container(project_snippet) }
end
end
context 'git pull with project snippet' do
it 'responds with success' do
pull(key, project_snippet)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{project_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
2014-09-02 18:07:02 +05:30
context "git pull" do
2021-03-11 19:13:27 +05:30
context "with a feature flag enabled globally" do
before do
stub_feature_flags(gitaly_mep_mep: true)
end
it "has the correct payload" do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
expect(json_response["gl_project_path"]).to eq(project.full_path)
expect(json_response["gitaly"]).not_to be_nil
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'true')
expect(user.reload.last_activity_on).to eql(Date.today)
end
2022-03-02 08:16:31 +05:30
it_behaves_like 'rate limited request' do
def request
pull(key, project)
end
end
context 'when user_id is passed' do
it_behaves_like 'rate limited request' do
let(:actor) { user }
def request
post(
api("/internal/allowed"),
params: {
user_id: user.id,
project: full_path_for(project),
gl_repository: gl_repository_for(project),
action: 'git-upload-pack',
protocol: 'ssh'
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2022-03-02 08:16:31 +05:30
)
end
2023-04-23 21:23:45 +05:30
it "updates user's activity data" do
expect(::Users::ActivityService).to receive(:new).with(author: user, namespace: project.namespace, project: project)
request
end
2022-03-02 08:16:31 +05:30
end
end
2020-03-13 15:44:24 +05:30
end
2021-03-11 19:13:27 +05:30
context "with a feature flag enabled for a project" do
before do
stub_feature_flags(gitaly_mep_mep: project)
end
2017-09-10 17:25:29 +05:30
2021-03-11 19:13:27 +05:30
it "has the flag set to true for that project" do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'true')
end
it "has the flag set to false for other projects" do
other_project = create(:project, :public, :repository)
pull(key, other_project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["gl_repository"]).to eq("project-#{other_project.id}")
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'false')
end
2014-09-02 18:07:02 +05:30
end
end
context "git push" do
2018-03-27 19:54:05 +05:30
context 'project as namespace/project' do
it do
2017-09-10 17:25:29 +05:30
push(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-09-10 17:25:29 +05:30
expect(json_response["status"]).to be_truthy
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
2019-07-07 11:18:12 +05:30
expect(json_response["gl_project_path"]).to eq(project.full_path)
2020-10-24 23:57:45 +05:30
expect(json_response["gl_key_type"]).to eq("key")
expect(json_response["gl_key_id"]).to eq(key.id)
2017-09-10 17:25:29 +05:30
expect(json_response["gitaly"]).not_to be_nil
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
2023-05-27 22:25:52 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2017-09-10 17:25:29 +05:30
end
2022-03-02 08:16:31 +05:30
it_behaves_like 'rate limited request' do
let(:action) { 'git-receive-pack' }
def request
push(key, project)
end
end
2017-08-17 22:00:37 +05:30
end
2018-11-20 20:47:30 +05:30
context 'when receive_max_input_size has been updated' do
2019-12-21 20:55:43 +05:30
before do
2018-11-20 20:47:30 +05:30
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { 1 }
2019-12-21 20:55:43 +05:30
end
2018-11-20 20:47:30 +05:30
2020-05-24 23:13:21 +05:30
it 'returns maxInputSize and partial clone git config' do
2018-11-20 20:47:30 +05:30
push(key, project)
expect(json_response["git_config_options"]).to be_present
2020-05-24 23:13:21 +05:30
expect(json_response["git_config_options"]).to include("receive.maxInputSize=1048576")
2019-12-21 20:55:43 +05:30
expect(json_response["git_config_options"]).to include("uploadpack.allowFilter=true")
expect(json_response["git_config_options"]).to include("uploadpack.allowAnySHA1InWant=true")
end
2018-11-20 20:47:30 +05:30
end
context 'when receive_max_input_size is empty' do
2020-05-24 23:13:21 +05:30
before do
2018-11-20 20:47:30 +05:30
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { nil }
2020-05-24 23:13:21 +05:30
end
2018-11-20 20:47:30 +05:30
2020-05-24 23:13:21 +05:30
it 'returns partial clone git config' do
2018-11-20 20:47:30 +05:30
push(key, project)
2020-05-24 23:13:21 +05:30
expect(json_response["git_config_options"]).to be_present
expect(json_response["git_config_options"]).to include("uploadpack.allowFilter=true")
expect(json_response["git_config_options"]).to include("uploadpack.allowAnySHA1InWant=true")
end
2018-11-20 20:47:30 +05:30
end
2014-09-02 18:07:02 +05:30
end
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
context 'with Project' do
2021-11-18 22:05:49 +05:30
it_behaves_like 'storing arguments in the application context for the API' do
2021-04-29 21:17:54 +05:30
let(:expected_params) { { user: key.user.username, project: project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
2020-04-08 14:13:33 +05:30
subject { push(key, project) }
end
end
context 'with PersonalSnippet' do
2021-11-18 22:05:49 +05:30
it_behaves_like 'storing arguments in the application context for the API' do
2021-04-29 21:17:54 +05:30
let(:expected_params) { { user: key.user.username, caller_id: "POST /api/:version/internal/allowed" } }
2020-04-08 14:13:33 +05:30
subject { push(key, personal_snippet) }
end
end
context 'with ProjectSnippet' do
2021-11-18 22:05:49 +05:30
it_behaves_like 'storing arguments in the application context for the API' do
2021-04-29 21:17:54 +05:30
let(:expected_params) { { user: key.user.username, project: project_snippet.project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
subject { push(key, project_snippet) }
end
2020-03-13 15:44:24 +05:30
end
2014-09-02 18:07:02 +05:30
end
context "access denied" do
before do
2018-03-17 18:26:18 +05:30
project.add_guest(user)
2014-09-02 18:07:02 +05:30
end
context "git pull" do
it do
pull(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2018-11-18 11:00:15 +05:30
expect(user.reload.last_activity_on).to be_nil
2014-09-02 18:07:02 +05:30
end
end
context "git push" do
it do
push(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2018-11-18 11:00:15 +05:30
expect(user.reload.last_activity_on).to be_nil
2014-09-02 18:07:02 +05:30
end
end
end
2022-04-04 11:22:00 +05:30
context 'with a pending membership' do
let_it_be(:project) { create(:project, :repository) }
before_all do
create(:project_member, :awaiting, :developer, source: project, user: user)
end
it 'returns not found for git pull' do
pull(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
it 'returns not found for git push' do
push(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
2018-11-20 20:47:30 +05:30
context "custom action" do
let(:access_checker) { double(Gitlab::GitAccess) }
let(:payload) do
{
'action' => 'geo_proxy_to_primary',
'data' => {
2020-04-22 19:07:51 +05:30
'api_endpoints' => %w{geo/proxy_git_ssh/info_refs_receive_pack geo/proxy_git_ssh/receive_pack},
2018-11-20 20:47:30 +05:30
'gl_username' => 'testuser',
'primary_repo' => 'http://localhost:3000/testuser/repo.git'
}
}
end
2020-10-24 23:57:45 +05:30
2019-12-26 22:10:19 +05:30
let(:console_messages) { ['informational message'] }
let(:custom_action_result) { Gitlab::GitAccessResult::CustomAction.new(payload, console_messages) }
2018-11-20 20:47:30 +05:30
before do
project.add_guest(user)
expect(Gitlab::GitAccess).to receive(:new).with(
key,
project,
'ssh',
{
authentication_abilities: [:read_project, :download_code, :push_code],
2021-02-22 17:27:13 +05:30
repository_path: "#{project.full_path}.git",
2018-11-20 20:47:30 +05:30
redirected_path: nil
}
).and_return(access_checker)
expect(access_checker).to receive(:check).with(
'git-receive-pack',
'd14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/master'
).and_return(custom_action_result)
end
context "git push" do
it do
push(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:multiple_choices)
2018-11-20 20:47:30 +05:30
expect(json_response['status']).to be_truthy
expect(json_response['payload']).to eql(payload)
2019-12-26 22:10:19 +05:30
expect(json_response['gl_console_messages']).to eql(console_messages)
2023-05-27 22:25:52 +05:30
expect(user.reload.last_activity_on).to eql(Date.today)
2018-11-20 20:47:30 +05:30
end
end
end
2019-07-07 11:18:12 +05:30
context "console message" do
before do
project.add_developer(user)
end
2020-04-08 14:13:33 +05:30
context 'git pull' do
context 'with a key that has expired' do
2022-07-16 23:28:13 +05:30
let(:key) { create(:key, :expired, user: user) }
2020-04-08 14:13:33 +05:30
2022-07-16 23:28:13 +05:30
it 'includes the `key expired` message in the response and fails' do
2019-07-07 11:18:12 +05:30
pull(key, project)
2022-07-16 23:28:13 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['message']).to eq('Your SSH key has expired.')
2019-07-07 11:18:12 +05:30
end
end
2020-04-08 14:13:33 +05:30
context 'with a key that will expire in the next 7 days' do
let(:key) { create(:key, user: user, expires_at: 2.days.from_now) }
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
it 'includes the `key expiring soon` message in the response' do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['gl_console_messages']).to eq(['INFO: Your SSH key is expiring soon. Please generate a new key.'])
end
end
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
context 'with a key that has no expiry' do
it 'does not include any message in the response' do
2019-07-07 11:18:12 +05:30
pull(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-04-08 14:13:33 +05:30
expect(json_response['gl_console_messages']).to eq([])
2019-07-07 11:18:12 +05:30
end
end
end
end
2014-09-02 18:07:02 +05:30
context "blocked user" do
2017-09-10 17:25:29 +05:30
let(:personal_project) { create(:project, namespace: user.namespace) }
2014-09-02 18:07:02 +05:30
before do
user.block
end
context "git pull" do
it do
pull(key, personal_project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2018-11-18 11:00:15 +05:30
expect(user.reload.last_activity_on).to be_nil
2014-09-02 18:07:02 +05:30
end
end
context "git push" do
it do
push(key, personal_project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2018-11-18 11:00:15 +05:30
expect(user.reload.last_activity_on).to be_nil
2018-12-13 13:39:08 +05:30
end
end
end
context 'request times out' do
context 'git push' do
it 'responds with a gateway timeout' do
personal_project = create(:project, namespace: user.namespace)
expect_next_instance_of(Gitlab::GitAccess) do |access|
expect(access).to receive(:check).and_raise(Gitlab::GitAccess::TimeoutError, "Foo")
end
push(key, personal_project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:service_unavailable)
2018-12-13 13:39:08 +05:30
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq("Foo")
expect(user.reload.last_activity_on).to be_nil
2014-09-02 18:07:02 +05:30
end
end
end
context "archived project" do
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2018-11-18 11:00:15 +05:30
::Projects::UpdateService.new(project, user, archived: true).execute
2014-09-02 18:07:02 +05:30
end
context "git pull" do
it do
pull(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_truthy
2014-09-02 18:07:02 +05:30
end
end
context "git push" do
it do
push(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2014-09-02 18:07:02 +05:30
end
end
end
context "deploy key" do
let(:key) { create(:deploy_key) }
context "added to project" do
before do
key.projects << project
end
it do
archive(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_truthy
2018-05-09 12:01:36 +05:30
expect(json_response["gitaly"]).not_to be_nil
2020-10-24 23:57:45 +05:30
expect(json_response["gl_key_type"]).to eq("deploy_key")
expect(json_response["gl_key_id"]).to eq(key.id)
2018-05-09 12:01:36 +05:30
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
2014-09-02 18:07:02 +05:30
end
2022-03-02 08:16:31 +05:30
it_behaves_like 'rate limited request' do
let(:action) { 'git-upload-archive' }
def request
archive(key, project)
end
end
2014-09-02 18:07:02 +05:30
end
context "not added to project" do
it do
archive(key, project)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
2014-09-02 18:07:02 +05:30
end
end
end
2015-04-26 12:48:37 +05:30
context 'project does not exist' do
2020-04-25 10:58:03 +05:30
context 'git pull' do
it 'returns a 200 response with status: false' do
2021-04-29 21:17:54 +05:30
project.destroy!
2018-03-27 19:54:05 +05:30
2020-04-25 10:58:03 +05:30
pull(key, project)
2015-04-26 12:48:37 +05:30
2020-04-25 10:58:03 +05:30
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
it 'returns a 200 response when using a project path that does not exist' do
post(
api("/internal/allowed"),
params: {
key_id: key.id,
project: 'project/does-not-exist.git',
action: 'git-upload-pack',
protocol: 'ssh'
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2020-04-25 10:58:03 +05:30
)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
2020-04-25 10:58:03 +05:30
context 'git push' do
before do
2022-11-25 23:54:43 +05:30
allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(120)
2020-04-25 10:58:03 +05:30
end
2019-07-07 11:18:12 +05:30
2020-04-25 10:58:03 +05:30
subject { push_with_path(key, full_path: path, changes: '_any') }
context 'from a user/group namespace' do
let!(:path) { "#{user.namespace.path}/notexist.git" }
it 'creates the project' do
expect do
subject
end.to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to be_truthy
end
end
context 'from the personal snippet path' do
let!(:path) { 'snippets/notexist.git' }
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'from a project path' do
context 'from an non existent project path' do
let!(:path) { "#{user.namespace.path}/notexist/snippets/notexist.git" }
it 'does not create project' do
expect do
subject
end.not_to change { Project.count }
expect(response).to have_gitlab_http_status(:not_found)
end
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'from an existent project path' do
let!(:path) { "#{project.full_path}/notexist/snippets/notexist.git" }
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2019-07-07 11:18:12 +05:30
end
2015-04-26 12:48:37 +05:30
end
context 'user does not exist' do
it do
2021-12-11 22:18:48 +05:30
pull(double('key', id: 0), project)
2015-04-26 12:48:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-04-26 12:48:37 +05:30
expect(json_response["status"]).to be_falsey
end
end
2016-08-24 12:49:21 +05:30
context 'ssh access has been disabled' do
before do
2017-08-17 22:00:37 +05:30
stub_application_setting(enabled_git_access_protocol: 'http')
2016-08-24 12:49:21 +05:30
end
it 'rejects the SSH push' do
push(key, project)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2016-08-24 12:49:21 +05:30
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over SSH is not allowed'
end
it 'rejects the SSH pull' do
pull(key, project)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2016-08-24 12:49:21 +05:30
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over SSH is not allowed'
end
end
context 'http access has been disabled' do
before do
2017-08-17 22:00:37 +05:30
stub_application_setting(enabled_git_access_protocol: 'ssh')
2016-08-24 12:49:21 +05:30
end
it 'rejects the HTTP push' do
push(key, project, 'http')
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2016-08-24 12:49:21 +05:30
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over HTTP is not allowed'
end
it 'rejects the HTTP pull' do
pull(key, project, 'http')
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2016-08-24 12:49:21 +05:30
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over HTTP is not allowed'
end
end
context 'web actions are always allowed' do
it 'allows WEB push' do
2017-08-17 22:00:37 +05:30
stub_application_setting(enabled_git_access_protocol: 'ssh')
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2016-08-24 12:49:21 +05:30
push(key, project, 'web')
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-08-24 12:49:21 +05:30
expect(json_response['status']).to be_truthy
end
end
2017-09-10 17:25:29 +05:30
context 'the project path was changed' do
2018-03-27 19:54:05 +05:30
let(:project) { create(:project, :repository, :legacy_storage) }
2018-03-17 18:26:18 +05:30
let!(:repository) { project.repository }
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2017-09-10 17:25:29 +05:30
project.path = 'new_path'
project.save!
end
it 'rejects the push' do
2018-03-17 18:26:18 +05:30
push(key, project)
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
expect(json_response['status']).to be_falsy
2017-09-10 17:25:29 +05:30
end
it 'rejects the SSH pull' do
2018-03-17 18:26:18 +05:30
pull(key, project)
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
expect(json_response['status']).to be_falsy
2017-09-10 17:25:29 +05:30
end
end
2020-05-24 23:13:21 +05:30
context 'for design repositories' do
let(:gl_repository) { Gitlab::GlRepository::DESIGN.identifier_for_container(project) }
it 'does not allow access' do
post(api('/internal/allowed'),
params: {
key_id: key.id,
project: project.full_path,
gl_repository: gl_repository,
protocol: 'ssh'
2022-10-11 01:57:18 +05:30
}, headers: gitlab_shell_internal_api_request_header
)
2020-05-24 23:13:21 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
2021-03-11 19:13:27 +05:30
context 'admin mode' do
shared_examples 'pushes succeed for ssh and http' do
it 'accepts the SSH push' do
push(key, project)
expect(response).to have_gitlab_http_status(:ok)
end
it 'accepts the HTTP push' do
push(key, project, 'http')
expect(response).to have_gitlab_http_status(:ok)
end
end
shared_examples 'pushes fail for ssh and http' do
it 'rejects the SSH push' do
push(key, project)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'rejects the HTTP push' do
push(key, project, 'http')
expect(response).to have_gitlab_http_status(:not_found)
end
end
2021-04-29 21:17:54 +05:30
context 'application setting :admin_mode is enabled' do
2021-03-11 19:13:27 +05:30
context 'with an admin user' do
let(:user) { create(:admin) }
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes succeed for ssh and http'
end
end
context 'with a regular user' do
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes fail for ssh and http'
end
end
end
2021-04-29 21:17:54 +05:30
context 'application setting :admin_mode is disabled' do
2021-03-11 19:13:27 +05:30
before do
2021-04-29 21:17:54 +05:30
stub_application_setting(admin_mode: false)
2021-03-11 19:13:27 +05:30
end
context 'with an admin user' do
let(:user) { create(:admin) }
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes succeed for ssh and http'
end
end
context 'with a regular user' do
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes fail for ssh and http'
end
end
end
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
describe 'POST /internal/post_receive', :clean_gitlab_redis_shared_state do
let(:identifier) { 'key-123' }
2020-03-13 15:44:24 +05:30
let(:branch_name) { 'feature' }
let(:push_options) { ['ci.skip', 'another push option'] }
2018-03-17 18:26:18 +05:30
let(:valid_params) do
{
gl_repository: gl_repository,
identifier: identifier,
2019-02-15 15:39:39 +05:30
changes: changes,
push_options: push_options
2018-03-17 18:26:18 +05:30
}
end
let(:changes) do
2019-07-07 11:18:12 +05:30
"#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{branch_name}"
2018-03-17 18:26:18 +05:30
end
2022-10-11 01:57:18 +05:30
subject { post api('/internal/post_receive'), params: valid_params, headers: gitlab_shell_internal_api_request_header }
2019-02-15 15:39:39 +05:30
2018-03-17 18:26:18 +05:30
before do
project.add_developer(user)
allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(user)
end
2021-09-30 23:02:18 +05:30
shared_examples 'runs post-receive hooks' do
let(:gl_repository) { container.repository.gl_repository }
let(:messages) { [] }
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
it 'executes PostReceiveService' do
2020-04-08 14:13:33 +05:30
subject
2019-12-04 20:38:33 +05:30
2021-09-30 23:02:18 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-04-08 14:13:33 +05:30
expect(json_response).to eq({
2021-09-30 23:02:18 +05:30
'messages' => messages,
2020-04-08 14:13:33 +05:30
'reference_counter_decreased' => true
})
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
it 'tries to notify that the container has moved' do
expect(Gitlab::Checks::ContainerMoved).to receive(:fetch_message).with(user, container.repository)
subject
end
2021-11-18 22:05:49 +05:30
it_behaves_like 'storing arguments in the application context for the API' do
2021-09-30 23:02:18 +05:30
let(:expected_params) { expected_context }
2020-04-08 14:13:33 +05:30
end
2019-12-21 20:55:43 +05:30
end
2021-09-30 23:02:18 +05:30
context 'with Project' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { project }
let(:expected_context) { { user: user.username, project: project.full_path } }
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
let(:messages) do
[
{
'message' => <<~MESSAGE.strip,
To create a merge request for #{branch_name}, visit:
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/new?merge_request%5Bsource_branch%5D=#{branch_name}
MESSAGE
'type' => 'basic'
}
]
end
2020-04-08 14:13:33 +05:30
end
2021-09-30 23:02:18 +05:30
end
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
context 'with PersonalSnippet' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { personal_snippet }
let(:expected_context) { { user: key.user.username } }
2020-04-08 14:13:33 +05:30
end
end
context 'with ProjectSnippet' do
2021-09-30 23:02:18 +05:30
it_behaves_like 'runs post-receive hooks' do
let(:container) { project_snippet }
let(:expected_context) { { user: key.user.username, project: project_snippet.project.full_path } }
2020-04-08 14:13:33 +05:30
end
2021-09-30 23:02:18 +05:30
end
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
context 'with ProjectWiki' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { project.wiki }
let(:expected_context) { { user: key.user.username, project: project.full_path } }
2020-04-08 14:13:33 +05:30
end
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
context 'with an orphaned write deploy key' do
it 'does not try to notify that project moved' do
allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(nil)
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
expect(Gitlab::Checks::ContainerMoved).not_to receive(:fetch_message)
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
subject
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when container is nil' do
let(:gl_repository) { 'project-foo' }
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
it 'does not try to notify that project moved' do
allow(Gitlab::GlRepository).to receive(:parse).and_return([nil, nil, Gitlab::GlRepository::PROJECT])
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
expect(Gitlab::Checks::ContainerMoved).not_to receive(:fetch_message)
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
subject
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-17 18:26:18 +05:30
end
end
end
describe 'POST /internal/pre_receive' do
let(:valid_params) do
2022-10-11 01:57:18 +05:30
{ gl_repository: gl_repository }
2018-03-17 18:26:18 +05:30
end
it 'decreases the reference counter and returns the result' do
expect(Gitlab::ReferenceCounter).to receive(:new).with(gl_repository)
.and_return(reference_counter)
expect(reference_counter).to receive(:increase).and_return(true)
2022-10-11 01:57:18 +05:30
post api("/internal/pre_receive"), params: valid_params, headers: gitlab_shell_internal_api_request_header
2018-03-17 18:26:18 +05:30
expect(json_response['reference_counter_increased']).to be(true)
end
end
2021-01-03 14:25:43 +05:30
describe 'POST /internal/two_factor_config' do
let(:key_id) { key.id }
before do
stub_feature_flags(two_factor_for_cli: true)
2017-08-17 22:00:37 +05:30
end
2016-09-13 17:45:13 +05:30
2021-01-03 14:25:43 +05:30
subject do
post api('/internal/two_factor_config'),
2022-10-11 01:57:18 +05:30
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
2020-04-08 14:13:33 +05:30
end
2021-01-03 14:25:43 +05:30
it_behaves_like 'actor key validations'
2017-09-10 17:25:29 +05:30
2021-01-03 14:25:43 +05:30
context 'when the key is a deploy key' do
let(:key) { create(:deploy_key) }
2020-04-25 10:58:03 +05:30
2021-01-03 14:25:43 +05:30
it 'does not required two factor' do
subject
2020-04-08 14:13:33 +05:30
2021-01-03 14:25:43 +05:30
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_falsey
end
end
2018-11-08 19:23:39 +05:30
2021-01-03 14:25:43 +05:30
context 'when two-factor is enabled' do
it 'returns user two factor config' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
subject
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_truthy
end
end
context 'when two-factor is not enabled' do
it 'returns an error message' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(false)
subject
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_falsey
end
end
context 'two_factor_for_cli feature is disabled' do
before do
stub_feature_flags(two_factor_for_cli: false)
end
context 'when two-factor is enabled for the user' do
it 'returns user two factor config' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
subject
expect(json_response['success']).to be_falsey
end
end
end
2017-09-10 17:25:29 +05:30
end
2022-07-16 23:28:13 +05:30
describe 'POST /internal/two_factor_manual_otp_check' do
let(:key_id) { key.id }
2022-08-27 11:52:29 +05:30
let(:otp) { '123456' }
2022-07-16 23:28:13 +05:30
subject do
post api('/internal/two_factor_manual_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2022-07-16 23:28:13 +05:30
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq 'Feature is not available'
end
end
describe 'POST /internal/two_factor_push_otp_check' do
let(:key_id) { key.id }
2022-08-27 11:52:29 +05:30
let(:otp) { '123456' }
2022-07-16 23:28:13 +05:30
subject do
post api('/internal/two_factor_push_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2022-07-16 23:28:13 +05:30
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq 'Feature is not available'
end
end
describe 'POST /internal/two_factor_manual_otp_check' do
let(:key_id) { key.id }
2022-08-27 11:52:29 +05:30
let(:otp) { '123456' }
2022-07-16 23:28:13 +05:30
subject do
post api('/internal/two_factor_manual_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2022-07-16 23:28:13 +05:30
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
end
end
describe 'POST /internal/two_factor_push_otp_check' do
let(:key_id) { key.id }
2022-08-27 11:52:29 +05:30
let(:otp) { '123456' }
2022-07-16 23:28:13 +05:30
subject do
post api('/internal/two_factor_push_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
2022-10-11 01:57:18 +05:30
},
headers: gitlab_shell_internal_api_request_header
2022-07-16 23:28:13 +05:30
end
2021-03-11 19:13:27 +05:30
it 'is not available' do
subject
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
expect(json_response['success']).to be_falsey
2021-01-03 14:25:43 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
2018-11-18 11:00:15 +05:30
def lfs_auth_project(project)
post(
api("/internal/lfs_authenticate"),
2022-10-11 01:57:18 +05:30
params: { project: project.full_path },
headers: gitlab_shell_internal_api_request_header
2018-11-18 11:00:15 +05:30
)
end
def lfs_auth_key(key_id, project)
2016-09-29 09:46:39 +05:30
post(
api("/internal/lfs_authenticate"),
2022-10-11 01:57:18 +05:30
params: { key_id: key_id, project: project.full_path },
headers: gitlab_shell_internal_api_request_header
2016-09-29 09:46:39 +05:30
)
end
2018-11-18 11:00:15 +05:30
def lfs_auth_user(user_id, project)
post(
api("/internal/lfs_authenticate"),
2022-10-11 01:57:18 +05:30
params: { user_id: user_id, project: project.full_path },
headers: gitlab_shell_internal_api_request_header
2018-11-18 11:00:15 +05:30
)
end
2014-09-02 18:07:02 +05:30
end