Commit 70e46340 authored by Robert Speicher's avatar Robert Speicher

Merge branch '235822-maven-group-token' into 'master'

Fix deploy tokens to respond to #projects

See merge request gitlab-org/gitlab!43628
parents 0d589b25 d8e731f4
...@@ -78,6 +78,20 @@ class DeployToken < ApplicationRecord ...@@ -78,6 +78,20 @@ class DeployToken < ApplicationRecord
end end
end end
def group
strong_memoize(:group) do
groups.first
end
end
def accessible_projects
if project_type?
projects
elsif group_type?
group.all_projects
end
end
def holder def holder
strong_memoize(:holder) do strong_memoize(:holder) do
if project_type? if project_type?
......
...@@ -604,7 +604,7 @@ class Project < ApplicationRecord ...@@ -604,7 +604,7 @@ class Project < ApplicationRecord
return public_to_user unless user return public_to_user unless user
if user.is_a?(DeployToken) if user.is_a?(DeployToken)
user.projects user.accessible_projects
else else
where('EXISTS (?) OR projects.visibility_level IN (?)', where('EXISTS (?) OR projects.visibility_level IN (?)',
user.authorizations_for_projects(min_access_level: min_access_level), user.authorizations_for_projects(min_access_level: min_access_level),
......
---
title: Fix group deploy tokens to return all projects and work with the Maven group
endpoint
merge_request: 43628
author:
type: fixed
...@@ -353,4 +353,29 @@ RSpec.describe DeployToken do ...@@ -353,4 +353,29 @@ RSpec.describe DeployToken do
end end
end end
end end
describe '#accessible_projects' do
subject { deploy_token.accessible_projects }
context 'when a deploy token is associated to a project' do
let_it_be(:deploy_token) { create(:deploy_token, :project) }
it 'returns only projects directly associated with the token' do
expect(deploy_token).to receive(:projects)
subject
end
end
context 'when a deploy token is associated to a group' do
let_it_be(:group) { create(:group) }
let_it_be(:deploy_token) { create(:deploy_token, :group, groups: [group]) }
it 'returns all projects from the group' do
expect(group).to receive(:all_projects)
subject
end
end
end
end end
...@@ -15,10 +15,13 @@ RSpec.describe API::MavenPackages do ...@@ -15,10 +15,13 @@ RSpec.describe API::MavenPackages do
let_it_be(:job, reload: true) { create(:ci_build, user: user, status: :running) } let_it_be(:job, reload: true) { create(:ci_build, user: user, status: :running) }
let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) } let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) }
let_it_be(:project_deploy_token) { create(:project_deploy_token, deploy_token: deploy_token, project: project) } let_it_be(:project_deploy_token) { create(:project_deploy_token, deploy_token: deploy_token, project: project) }
let_it_be(:deploy_token_for_group) { create(:deploy_token, :group, read_package_registry: true, write_package_registry: true) }
let_it_be(:group_deploy_token) { create(:group_deploy_token, deploy_token: deploy_token_for_group, group: group) }
let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') } let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') }
let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } } let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } }
let(:headers_with_token) { headers.merge('Private-Token' => personal_access_token.token) } let(:headers_with_token) { headers.merge('Private-Token' => personal_access_token.token) }
let(:group_deploy_token_headers) { { Gitlab::Auth::AuthFinders::DEPLOY_TOKEN_HEADER => deploy_token_for_group.token } }
let(:headers_with_deploy_token) do let(:headers_with_deploy_token) do
headers.merge( headers.merge(
...@@ -342,6 +345,17 @@ RSpec.describe API::MavenPackages do ...@@ -342,6 +345,17 @@ RSpec.describe API::MavenPackages do
it_behaves_like 'downloads with a job token' it_behaves_like 'downloads with a job token'
it_behaves_like 'downloads with a deploy token' it_behaves_like 'downloads with a deploy token'
context 'with group deploy token' do
subject { download_file_with_token(package_file.file_name, {}, group_deploy_token_headers) }
it 'returns the file' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response.media_type).to eq('application/octet-stream')
end
end
end end
def download_file(file_name, params = {}, request_headers = headers) def download_file(file_name, params = {}, request_headers = headers)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment