Commit 9feaf6a7 authored by Darby Frey's avatar Darby Frey

Updated permissions model for Secure Files

parent a185410f
...@@ -11,6 +11,6 @@ class Projects::Ci::SecureFilesController < Projects::ApplicationController ...@@ -11,6 +11,6 @@ class Projects::Ci::SecureFilesController < Projects::ApplicationController
private private
def check_can_collaborate! def check_can_collaborate!
render_404 unless can_collaborate_with_project?(project) render_404 unless can?(current_user, :read_secure_files, project)
end end
end end
...@@ -413,6 +413,7 @@ class ProjectPolicy < BasePolicy ...@@ -413,6 +413,7 @@ class ProjectPolicy < BasePolicy
enable :admin_feature_flag enable :admin_feature_flag
enable :admin_feature_flags_user_lists enable :admin_feature_flags_user_lists
enable :update_escalation_status enable :update_escalation_status
enable :read_secure_files
end end
rule { can?(:developer_access) & user_confirmed? }.policy do rule { can?(:developer_access) & user_confirmed? }.policy do
...@@ -462,6 +463,7 @@ class ProjectPolicy < BasePolicy ...@@ -462,6 +463,7 @@ class ProjectPolicy < BasePolicy
enable :register_project_runners enable :register_project_runners
enable :update_runners_registration_token enable :update_runners_registration_token
enable :admin_project_google_cloud enable :admin_project_google_cloud
enable :read_secure_files
enable :admin_secure_files enable :admin_secure_files
end end
......
...@@ -7,8 +7,8 @@ module API ...@@ -7,8 +7,8 @@ module API
before do before do
authenticate! authenticate!
authorize! :admin_secure_files, user_project
feature_flag_enabled? feature_flag_enabled?
authorize! :read_secure_files, user_project
end end
feature_category :pipeline_authoring feature_category :pipeline_authoring
...@@ -59,6 +59,10 @@ module API ...@@ -59,6 +59,10 @@ module API
optional :permissions, type: String, desc: 'The file permissions', default: 'read_only', values: %w[read_only read_write execute] optional :permissions, type: String, desc: 'The file permissions', default: 'read_only', values: %w[read_only read_write execute]
end end
before do
authorize! :admin_secure_files, user_project
end
route_setting :authentication, basic_auth_personal_access_token: true, job_token_allowed: true route_setting :authentication, basic_auth_personal_access_token: true, job_token_allowed: true
post ':id/secure_files' do post ':id/secure_files' do
secure_file = user_project.secure_files.new( secure_file = user_project.secure_files.new(
...@@ -78,6 +82,11 @@ module API ...@@ -78,6 +82,11 @@ module API
end end
desc 'Delete an individual Secure File' desc 'Delete an individual Secure File'
before do
authorize! :admin_secure_files, user_project
end
route_setting :authentication, basic_auth_personal_access_token: true, job_token_allowed: true route_setting :authentication, basic_auth_personal_access_token: true, job_token_allowed: true
delete ':id/secure_files/:secure_file_id' do delete ':id/secure_files/:secure_file_id' do
secure_file = user_project.secure_files.find(params[:secure_file_id]) secure_file = user_project.secure_files.find(params[:secure_file_id])
......
...@@ -10,9 +10,12 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -10,9 +10,12 @@ RSpec.describe API::Ci::SecureFiles do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:user2) { create(:user) } let_it_be(:user2) { create(:user) }
let_it_be(:user3) { create(:user) }
let_it_be(:user4) { create(:user) }
let_it_be(:project) { create(:project, creator_id: user.id) } let_it_be(:project) { create(:project, creator_id: user.id) }
let_it_be(:maintainer) { create(:project_member, :maintainer, user: user, project: project) } let_it_be(:maintainer) { create(:project_member, :maintainer, user: user, project: project) }
let_it_be(:developer) { create(:project_member, :developer, user: user2, project: project) } let_it_be(:developer) { create(:project_member, :developer, user: user2, project: project) }
let_it_be(:guest) { create(:project_member, :guest, user: user4, project: project) }
let_it_be(:secure_file) { create(:ci_secure_file, project: project) } let_it_be(:secure_file) { create(:ci_secure_file, project: project) }
describe 'GET /projects/:id/secure_files' do describe 'GET /projects/:id/secure_files' do
...@@ -33,7 +36,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -33,7 +36,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with proper permissions' do context 'authorized user with admin permissions' do
it 'returns project secure files' do it 'returns project secure files' do
get api("/projects/#{project.id}/secure_files", user) get api("/projects/#{project.id}/secure_files", user)
...@@ -42,14 +45,31 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -42,14 +45,31 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with invalid permissions' do context 'authorized user with read permissions' do
it 'does not return project secure files' do it 'does not return project secure files' do
get api("/projects/#{project.id}/secure_files", user2) get api("/projects/#{project.id}/secure_files", user2)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_a(Array)
end
end
context 'authorized user with guest permissions' do
it 'does not return project secure files' do
get api("/projects/#{project.id}/secure_files", user4)
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
end end
end end
context 'authorized user with no permissions' do
it 'does not return project secure files' do
get api("/projects/#{project.id}/secure_files", user3)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'unauthorized user' do context 'unauthorized user' do
it 'does not return project secure files' do it 'does not return project secure files' do
get api("/projects/#{project.id}/secure_files") get api("/projects/#{project.id}/secure_files")
...@@ -60,7 +80,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -60,7 +80,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
describe 'GET /projects/:id/secure_files/:secure_file_id' do describe 'GET /projects/:id/secure_files/:secure_file_id' do
context 'authorized user with proper permissions' do context 'authorized user with admin permissions' do
it 'returns project secure file details' do it 'returns project secure file details' do
get api("/projects/#{project.id}/secure_files/#{secure_file.id}", user) get api("/projects/#{project.id}/secure_files/#{secure_file.id}", user)
...@@ -76,11 +96,27 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -76,11 +96,27 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with invalid permissions' do context 'authorized user with read permissions' do
it 'does not return project secure file details' do it 'returns project secure file details' do
get api("/projects/#{project.id}/secure_files/#{secure_file.id}", user2) get api("/projects/#{project.id}/secure_files/#{secure_file.id}", user2)
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq(secure_file.name)
expect(json_response['permissions']).to eq(secure_file.permissions)
end
it 'responds with 404 Not Found if requesting non-existing secure file' do
get api("/projects/#{project.id}/secure_files/99999", user2)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'authorized user with no permissions' do
it 'does not return project secure file details' do
get api("/projects/#{project.id}/secure_files/#{secure_file.id}", user3)
expect(response).to have_gitlab_http_status(:not_found)
end end
end end
...@@ -94,7 +130,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -94,7 +130,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
describe 'GET /projects/:id/secure_files/:secure_file_id/download' do describe 'GET /projects/:id/secure_files/:secure_file_id/download' do
context 'authorized user with proper permissions' do context 'authorized user with admin permissions' do
it 'returns a secure file' do it 'returns a secure file' do
sample_file = fixture_file('ci_secure_files/upload-keystore.jks') sample_file = fixture_file('ci_secure_files/upload-keystore.jks')
secure_file.file = CarrierWaveStringFile.new(sample_file) secure_file.file = CarrierWaveStringFile.new(sample_file)
...@@ -113,11 +149,30 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -113,11 +149,30 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with invalid permissions' do context 'authorized user with read permissions' do
it 'does not return project secure file details' do it 'returns a secure file' do
sample_file = fixture_file('ci_secure_files/upload-keystore.jks')
secure_file.file = CarrierWaveStringFile.new(sample_file)
secure_file.save!
get api("/projects/#{project.id}/secure_files/#{secure_file.id}/download", user2) get api("/projects/#{project.id}/secure_files/#{secure_file.id}/download", user2)
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:ok)
expect(Base64.encode64(response.body)).to eq(Base64.encode64(sample_file))
end
it 'responds with 404 Not Found if requesting non-existing secure file' do
get api("/projects/#{project.id}/secure_files/99999/download", user2)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'authorized user with no permissions' do
it 'does not return project secure file details' do
get api("/projects/#{project.id}/secure_files/#{secure_file.id}/download", user3)
expect(response).to have_gitlab_http_status(:not_found)
end end
end end
...@@ -131,7 +186,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -131,7 +186,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
describe 'POST /projects/:id/secure_files' do describe 'POST /projects/:id/secure_files' do
context 'authorized user with proper permissions' do context 'authorized user with admin permissions' do
it 'creates a secure file' do it 'creates a secure file' do
params = { params = {
file: fixture_file_upload('spec/fixtures/ci_secure_files/upload-keystore.jks'), file: fixture_file_upload('spec/fixtures/ci_secure_files/upload-keystore.jks'),
...@@ -262,7 +317,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -262,7 +317,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with invalid permissions' do context 'authorized user with read permissions' do
it 'does not create a secure file' do it 'does not create a secure file' do
post api("/projects/#{project.id}/secure_files", user2) post api("/projects/#{project.id}/secure_files", user2)
...@@ -270,6 +325,14 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -270,6 +325,14 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with no permissions' do
it 'does not create a secure file' do
post api("/projects/#{project.id}/secure_files", user3)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'unauthorized user' do context 'unauthorized user' do
it 'does not create a secure file' do it 'does not create a secure file' do
post api("/projects/#{project.id}/secure_files") post api("/projects/#{project.id}/secure_files")
...@@ -280,7 +343,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -280,7 +343,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
describe 'DELETE /projects/:id/secure_files/:secure_file_id' do describe 'DELETE /projects/:id/secure_files/:secure_file_id' do
context 'authorized user with proper permissions' do context 'authorized user with admin permissions' do
it 'deletes the secure file' do it 'deletes the secure file' do
expect do expect do
delete api("/projects/#{project.id}/secure_files/#{secure_file.id}", user) delete api("/projects/#{project.id}/secure_files/#{secure_file.id}", user)
...@@ -296,7 +359,7 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -296,7 +359,7 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with invalid permissions' do context 'authorized user with read permissions' do
it 'does not delete the secure_file' do it 'does not delete the secure_file' do
delete api("/projects/#{project.id}/secure_files/#{secure_file.id}", user2) delete api("/projects/#{project.id}/secure_files/#{secure_file.id}", user2)
...@@ -304,6 +367,14 @@ RSpec.describe API::Ci::SecureFiles do ...@@ -304,6 +367,14 @@ RSpec.describe API::Ci::SecureFiles do
end end
end end
context 'authorized user with no permissions' do
it 'does not delete the secure_file' do
delete api("/projects/#{project.id}/secure_files/#{secure_file.id}", user3)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'unauthorized user' do context 'unauthorized user' do
it 'does not delete the secure_file' do it 'does not delete the secure_file' do
delete api("/projects/#{project.id}/secure_files/#{secure_file.id}") delete api("/projects/#{project.id}/secure_files/#{secure_file.id}")
......
...@@ -4,9 +4,11 @@ require 'spec_helper' ...@@ -4,9 +4,11 @@ require 'spec_helper'
RSpec.describe ::Ci::DestroySecureFileService do RSpec.describe ::Ci::DestroySecureFileService do
let_it_be(:maintainer_user) { create(:user) } let_it_be(:maintainer_user) { create(:user) }
let_it_be(:developer_user) { create(:user) }
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:secure_file) { create(:ci_secure_file, project: project) } let_it_be(:secure_file) { create(:ci_secure_file, project: project) }
let_it_be(:project_member) { create(:project_member, :maintainer, user: maintainer_user, project: project) } let_it_be(:project_member) { create(:project_member, :maintainer, user: maintainer_user, project: project) }
let_it_be(:project_member2) { create(:project_member, :developer, user: developer_user, project: project) }
subject { described_class.new(project, user).execute(secure_file) } subject { described_class.new(project, user).execute(secure_file) }
...@@ -20,8 +22,8 @@ RSpec.describe ::Ci::DestroySecureFileService do ...@@ -20,8 +22,8 @@ RSpec.describe ::Ci::DestroySecureFileService do
end end
end end
context 'user is not owner' do context 'user is a developer' do
let(:user) { create(:user) } let(:user) { developer_user }
it 'raises an exception' do it 'raises an exception' do
expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError) expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError)
......
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