diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2017-11-14 10:54:30 +0000 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2017-11-14 10:54:30 +0000 |
commit | 6b01821b0d7c7c624ab86936a7cadb82b3603630 (patch) | |
tree | 22027af01434dddcf507a48a9e12d05b86ff4953 | |
parent | 6b9b516007c8dda88f33e9603a6880e3fc3ff103 (diff) | |
parent | 8029c92e1c81e4c9ab55704bff82cca5ff893a03 (diff) | |
download | gitlab-ce-6b01821b0d7c7c624ab86936a7cadb82b3603630.tar.gz |
Merge branch 'fix/sm/31771-do-not-allow-jobs-to-be-erased-new' into 'master'
Do not allow jobs to be erased
Closes #31771
See merge request gitlab-org/gitlab-ce!15216
-rw-r--r-- | app/controllers/projects/jobs_controller.rb | 7 | ||||
-rw-r--r-- | app/models/ci/build.rb | 4 | ||||
-rw-r--r-- | app/policies/ci/build_policy.rb | 11 | ||||
-rw-r--r-- | app/serializers/build_details_entity.rb | 2 | ||||
-rw-r--r-- | app/views/projects/jobs/show.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/fix-sm-31771-do-not-allow-jobs-to-be-erased-new.yml | 5 | ||||
-rw-r--r-- | doc/user/permissions.md | 2 | ||||
-rw-r--r-- | lib/api/jobs.rb | 2 | ||||
-rw-r--r-- | lib/api/v3/builds.rb | 2 | ||||
-rw-r--r-- | spec/controllers/projects/jobs_controller_spec.rb | 25 | ||||
-rw-r--r-- | spec/models/ci/build_spec.rb | 17 | ||||
-rw-r--r-- | spec/policies/ci/build_policy_spec.rb | 77 | ||||
-rw-r--r-- | spec/requests/api/jobs_spec.rb | 21 | ||||
-rw-r--r-- | spec/requests/api/v3/builds_spec.rb | 2 |
14 files changed, 172 insertions, 7 deletions
diff --git a/app/controllers/projects/jobs_controller.rb b/app/controllers/projects/jobs_controller.rb index 1b985ea9763..1c4c09c772f 100644 --- a/app/controllers/projects/jobs_controller.rb +++ b/app/controllers/projects/jobs_controller.rb @@ -4,7 +4,8 @@ class Projects::JobsController < Projects::ApplicationController before_action :authorize_read_build!, only: [:index, :show, :status, :raw, :trace] before_action :authorize_update_build!, - except: [:index, :show, :status, :raw, :trace, :cancel_all] + except: [:index, :show, :status, :raw, :trace, :cancel_all, :erase] + before_action :authorize_erase_build!, only: [:erase] layout 'project' @@ -131,6 +132,10 @@ class Projects::JobsController < Projects::ApplicationController return access_denied! unless can?(current_user, :update_build, build) end + def authorize_erase_build! + return access_denied! unless can?(current_user, :erase_build, build) + end + def build @build ||= project.builds.find(params[:id]) .present(current_user: current_user) diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 6ca46ae89c1..1b2b0d17910 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -192,6 +192,10 @@ module Ci project.build_timeout end + def triggered_by?(current_user) + user == current_user + end + # A slugified version of the build ref, suitable for inclusion in URLs and # domain names. Rules: # diff --git a/app/policies/ci/build_policy.rb b/app/policies/ci/build_policy.rb index 984e5482288..1ab391a5a9d 100644 --- a/app/policies/ci/build_policy.rb +++ b/app/policies/ci/build_policy.rb @@ -10,6 +10,15 @@ module Ci end end - rule { protected_ref }.prevent :update_build + condition(:owner_of_job) do + can?(:developer_access) && @subject.triggered_by?(@user) + end + + rule { protected_ref }.policy do + prevent :update_build + prevent :erase_build + end + + rule { can?(:master_access) | owner_of_job }.enable :erase_build end end diff --git a/app/serializers/build_details_entity.rb b/app/serializers/build_details_entity.rb index 8c89eea607f..69d46f5ec14 100644 --- a/app/serializers/build_details_entity.rb +++ b/app/serializers/build_details_entity.rb @@ -6,7 +6,7 @@ class BuildDetailsEntity < JobEntity expose :pipeline, using: PipelineEntity expose :erased_by, if: -> (*) { build.erased? }, using: UserEntity - expose :erase_path, if: -> (*) { build.erasable? && can?(current_user, :update_build, project) } do |build| + expose :erase_path, if: -> (*) { build.erasable? && can?(current_user, :erase_build, build) } do |build| erase_project_job_path(project, build) end diff --git a/app/views/projects/jobs/show.html.haml b/app/views/projects/jobs/show.html.haml index ce0e3872240..2abd2c9e652 100644 --- a/app/views/projects/jobs/show.html.haml +++ b/app/views/projects/jobs/show.html.haml @@ -71,7 +71,7 @@ class: 'js-raw-link-controller has-tooltip controllers-buttons' do = icon('file-text-o') - - if can?(current_user, :update_build, @project) && @build.erasable? + - if @build.erasable? && can?(current_user, :erase_build, @build) = link_to erase_project_job_path(@project, @build), method: :post, data: { confirm: 'Are you sure you want to erase this build?', placement: 'top', container: 'body' }, diff --git a/changelogs/unreleased/fix-sm-31771-do-not-allow-jobs-to-be-erased-new.yml b/changelogs/unreleased/fix-sm-31771-do-not-allow-jobs-to-be-erased-new.yml new file mode 100644 index 00000000000..198116f34aa --- /dev/null +++ b/changelogs/unreleased/fix-sm-31771-do-not-allow-jobs-to-be-erased-new.yml @@ -0,0 +1,5 @@ +--- +title: Only owner or master can erase jobs +merge_request: 15216 +author: +type: changed diff --git a/doc/user/permissions.md b/doc/user/permissions.md index c03700a3501..b9532bf897f 100644 --- a/doc/user/permissions.md +++ b/doc/user/permissions.md @@ -197,6 +197,7 @@ instance and project. In addition, all admins can use the admin interface under |---------------------------------------|-----------------|-------------|----------|--------| | See commits and jobs | ✓ | ✓ | ✓ | ✓ | | Retry or cancel job | | ✓ | ✓ | ✓ | +| Erase job artifacts and trace | | ✓ [^7] | ✓ | ✓ | | Remove project | | | ✓ | ✓ | | Create project | | | ✓ | ✓ | | Change project configuration | | | ✓ | ✓ | @@ -261,5 +262,6 @@ only. [^4]: Not allowed for Guest, Reporter, Developer, Master, or Owner [^5]: Only if user is not external one. [^6]: Only if user is a member of the project. +[^7]: Only if the build was triggered by the user [ce-18994]: https://gitlab.com/gitlab-org/gitlab-ce/issues/18994 [new-mod]: project/new_ci_build_permissions_model.md diff --git a/lib/api/jobs.rb b/lib/api/jobs.rb index 3c1c412ba42..a116ab3c9bd 100644 --- a/lib/api/jobs.rb +++ b/lib/api/jobs.rb @@ -136,7 +136,7 @@ module API authorize_update_builds! build = find_build!(params[:job_id]) - authorize!(:update_build, build) + authorize!(:erase_build, build) return forbidden!('Job is not erasable!') unless build.erasable? build.erase(erased_by: current_user) diff --git a/lib/api/v3/builds.rb b/lib/api/v3/builds.rb index f493fd7c7ec..fa0bef39602 100644 --- a/lib/api/v3/builds.rb +++ b/lib/api/v3/builds.rb @@ -169,7 +169,7 @@ module API authorize_update_builds! build = get_build!(params[:build_id]) - authorize!(:update_build, build) + authorize!(:erase_build, build) return forbidden!('Build is not erasable!') unless build.erasable? build.erase(erased_by: current_user) diff --git a/spec/controllers/projects/jobs_controller_spec.rb b/spec/controllers/projects/jobs_controller_spec.rb index f9688949a19..7490f8fefce 100644 --- a/spec/controllers/projects/jobs_controller_spec.rb +++ b/spec/controllers/projects/jobs_controller_spec.rb @@ -371,8 +371,10 @@ describe Projects::JobsController do end describe 'POST erase' do + let(:role) { :master } + before do - project.add_developer(user) + project.team << [user, role] sign_in(user) post_erase @@ -404,6 +406,27 @@ describe Projects::JobsController do end end + context 'when user is developer' do + let(:role) { :developer } + let(:job) { create(:ci_build, :erasable, :trace, pipeline: pipeline, user: triggered_by) } + + context 'when triggered by same user' do + let(:triggered_by) { user } + + it 'has successful status' do + expect(response).to have_gitlab_http_status(:found) + end + end + + context 'when triggered by different user' do + let(:triggered_by) { create(:user) } + + it 'does not have successful status' do + expect(response).not_to have_gitlab_http_status(:found) + end + end + end + def post_erase post :erase, namespace_id: project.namespace, project_id: project, diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 5ed2e1ca99a..1795ee8e9a4 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -270,6 +270,23 @@ describe Ci::Build do end end + describe '#triggered_by?' do + subject { build.triggered_by?(user) } + + context 'when user is owner' do + let(:build) { create(:ci_build, pipeline: pipeline, user: user) } + + it { is_expected.to be_truthy } + end + + context 'when user is not owner' do + let(:another_user) { create(:user) } + let(:build) { create(:ci_build, pipeline: pipeline, user: another_user) } + + it { is_expected.to be_falsy } + end + end + describe '#detailed_status' do it 'returns a detailed status' do expect(build.detailed_status(user)) diff --git a/spec/policies/ci/build_policy_spec.rb b/spec/policies/ci/build_policy_spec.rb index 8e1bc3d1543..298a9d16425 100644 --- a/spec/policies/ci/build_policy_spec.rb +++ b/spec/policies/ci/build_policy_spec.rb @@ -150,5 +150,82 @@ describe Ci::BuildPolicy do end end end + + describe 'rules for erase build' do + let(:project) { create(:project, :repository) } + let(:build) { create(:ci_build, pipeline: pipeline, ref: 'some-ref', user: owner) } + + context 'when a developer erases a build' do + before do + project.add_developer(user) + end + + context 'when developers can push to the branch' do + before do + create(:protected_branch, :developers_can_push, + name: build.ref, project: project) + end + + context 'when the build was created by the developer' do + let(:owner) { user } + + it { expect(policy).to be_allowed :erase_build } + end + + context 'when the build was created by the other' do + let(:owner) { create(:user) } + + it { expect(policy).to be_disallowed :erase_build } + end + end + + context 'when no one can push or merge to the branch' do + let(:owner) { user } + + before do + create(:protected_branch, :no_one_can_push, :no_one_can_merge, + name: build.ref, project: project) + end + + it { expect(policy).to be_disallowed :erase_build } + end + end + + context 'when a master erases a build' do + before do + project.add_master(user) + end + + context 'when masters can push to the branch' do + before do + create(:protected_branch, :masters_can_push, + name: build.ref, project: project) + end + + context 'when the build was created by the master' do + let(:owner) { user } + + it { expect(policy).to be_allowed :erase_build } + end + + context 'when the build was created by the other' do + let(:owner) { create(:user) } + + it { expect(policy).to be_allowed :erase_build } + end + end + + context 'when no one can push or merge to the branch' do + let(:owner) { user } + + before do + create(:protected_branch, :no_one_can_push, :no_one_can_merge, + name: build.ref, project: project) + end + + it { expect(policy).to be_disallowed :erase_build } + end + end + end end end diff --git a/spec/requests/api/jobs_spec.rb b/spec/requests/api/jobs_spec.rb index 1765907c1b4..2a83213e87a 100644 --- a/spec/requests/api/jobs_spec.rb +++ b/spec/requests/api/jobs_spec.rb @@ -500,7 +500,11 @@ describe API::Jobs do end describe 'POST /projects/:id/jobs/:job_id/erase' do + let(:role) { :master } + before do + project.team << [user, role] + post api("/projects/#{project.id}/jobs/#{job.id}/erase", user) end @@ -529,6 +533,23 @@ describe API::Jobs do expect(response).to have_gitlab_http_status(403) end end + + context 'when a developer erases a build' do + let(:role) { :developer } + let(:job) { create(:ci_build, :trace, :artifacts, :success, project: project, pipeline: pipeline, user: owner) } + + context 'when the build was created by the developer' do + let(:owner) { user } + + it { expect(response).to have_gitlab_http_status(201) } + end + + context 'when the build was created by the other' do + let(:owner) { create(:user) } + + it { expect(response).to have_gitlab_http_status(403) } + end + end end describe 'POST /projects/:id/jobs/:job_id/artifacts/keep' do diff --git a/spec/requests/api/v3/builds_spec.rb b/spec/requests/api/v3/builds_spec.rb index 3f58b7ef384..a73bb456b52 100644 --- a/spec/requests/api/v3/builds_spec.rb +++ b/spec/requests/api/v3/builds_spec.rb @@ -408,6 +408,8 @@ describe API::V3::Builds do describe 'POST /projects/:id/builds/:build_id/erase' do before do + project.add_master(user) + post v3_api("/projects/#{project.id}/builds/#{build.id}/erase", user) end |