diff --git a/ee/app/controllers/projects/security/policies_controller.rb b/ee/app/controllers/projects/security/policies_controller.rb index 55a49af9d260309ea8eda708449702e3e04ba254..773cc20a1461048f40d9e7f4265ec8a7b18ec491 100644 --- a/ee/app/controllers/projects/security/policies_controller.rb +++ b/ee/app/controllers/projects/security/policies_controller.rb @@ -22,7 +22,7 @@ def assign result = ::Security::Orchestration::AssignService.new(project, nil, policy_project_id: policy_project_params[:policy_project_id]).execute if result.success? - flash[:notice] = _('Successfull') + flash[:notice] = _('Operation completed') else flash[:alert] = result.message end diff --git a/ee/app/models/security/orchestration_policy_configuration.rb b/ee/app/models/security/orchestration_policy_configuration.rb index a610a97165bbc80413eef8fbf35c47b596038581..0c4ab062927f9c9ad470c8bac6f842003488a3fe 100644 --- a/ee/app/models/security/orchestration_policy_configuration.rb +++ b/ee/app/models/security/orchestration_policy_configuration.rb @@ -14,7 +14,7 @@ class OrchestrationPolicyConfiguration < ApplicationRecord belongs_to :security_policy_management_project, class_name: 'Project', foreign_key: 'security_policy_management_project_id' validates :project, presence: true, uniqueness: true - validates :security_policy_management_project, presence: true, uniqueness: true + validates :security_policy_management_project, presence: true def enabled? ::Feature.enabled?(:security_orchestration_policies_configuration, project) diff --git a/ee/app/services/security/orchestration/assign_service.rb b/ee/app/services/security/orchestration/assign_service.rb index 387b7cb1c4ab69144a0b6b1f55844f47af66a224..35fcb735b876b2bf8b756cc22ecf1ff3153b6ffa 100644 --- a/ee/app/services/security/orchestration/assign_service.rb +++ b/ee/app/services/security/orchestration/assign_service.rb @@ -9,7 +9,7 @@ def execute return success if res rescue ActiveRecord::RecordNotFound => _ - error(_('Policy project doesn\'t exists')) + error(_('Policy project doesn\'t exist')) rescue ActiveRecord::RecordInvalid => _ error(_('Couldn\'t assign policy to project')) end @@ -17,6 +17,10 @@ def execute private def create_or_update_security_policy_configuration + if policy_project_id.blank? && has_existing_policy? + return unassign_policy_project + end + policy_project = Project.find(policy_project_id) if has_existing_policy? @@ -30,6 +34,10 @@ def create_or_update_security_policy_configuration end end + def unassign_policy_project + project.security_orchestration_policy_configuration.delete + end + def success ServiceResponse.success(payload: { policy_project: policy_project_id }) end diff --git a/ee/app/views/layouts/nav/sidebar/_project_security_link.html.haml b/ee/app/views/layouts/nav/sidebar/_project_security_link.html.haml index 3cb7312dfad0c675db250267d2277086035e64e4..9111ac98fc11bfce6bbf111bcb9ca1a76b091061 100644 --- a/ee/app/views/layouts/nav/sidebar/_project_security_link.html.haml +++ b/ee/app/views/layouts/nav/sidebar/_project_security_link.html.haml @@ -44,7 +44,7 @@ = link_to project_threat_monitoring_path(@project), title: _('Threat Monitoring') do %span= _('Threat Monitoring') - - if project_nav_tab?(:security_orchestration_policies) + - if project_nav_tab?(:security_orchestration_policies) && Feature.enabled?(:security_orchestration_policies_configuration, @project) = nav_link(controller: ['projects/security/policies']) do = link_to project_security_policy_path(@project), title: _('Scan Policies') do %span= _('Scan Policies') diff --git a/ee/spec/models/security/orchestration_policy_configuration_spec.rb b/ee/spec/models/security/orchestration_policy_configuration_spec.rb index c519e04564e766eed1774257403c5984f05355a2..d5850252574ed8685a5a6a63143007bdce31d18f 100644 --- a/ee/spec/models/security/orchestration_policy_configuration_spec.rb +++ b/ee/spec/models/security/orchestration_policy_configuration_spec.rb @@ -21,7 +21,6 @@ it { is_expected.to validate_presence_of(:security_policy_management_project) } it { is_expected.to validate_uniqueness_of(:project) } - it { is_expected.to validate_uniqueness_of(:security_policy_management_project) } end describe '#enabled?' do diff --git a/ee/spec/requests/projects/security/policies_controller_spec.rb b/ee/spec/requests/projects/security/policies_controller_spec.rb index c3ecbf7b637c61859f9c95e69a92541adf931e08..81a1de8b87936a460eb3f60ac679c2a46cbdcdd9 100644 --- a/ee/spec/requests/projects/security/policies_controller_spec.rb +++ b/ee/spec/requests/projects/security/policies_controller_spec.rb @@ -55,7 +55,7 @@ it 'returns error message for invalid input' do post assign_project_security_policy_url(project), params: { orchestration: { policy_project_id: nil } } - expect(flash[:alert]).to eq 'Policy project doesn\'t exists' + expect(flash[:alert]).to eq 'Policy project doesn\'t exist' end end end diff --git a/ee/spec/services/security/orchestration/assign_service_spec.rb b/ee/spec/services/security/orchestration/assign_service_spec.rb index e3d60351a693dd53f9e179da11ccac1733e42285..0f89d0be145d7dc7f1c2fed873885d695015b805 100644 --- a/ee/spec/services/security/orchestration/assign_service_spec.rb +++ b/ee/spec/services/security/orchestration/assign_service_spec.rb @@ -9,32 +9,50 @@ let_it_be(:new_policy_project) { create(:project) } describe '#execute' do - subject(:service) { described_class.new(project, nil, policy_project_id: policy_project.id).execute } + subject(:service) do + described_class.new(project, nil, policy_project_id: policy_project.id).execute + end + + before do + service + end it 'assigns policy project to project' do expect(service).to be_success - expect(project.security_orchestration_policy_configuration.security_policy_management_project_id).to eq(policy_project.id) + expect( + project.security_orchestration_policy_configuration.security_policy_management_project_id + ).to eq(policy_project.id) end it 'updates project with new policy project' do - service - - repeated_service = described_class.new(project, nil, policy_project_id: new_policy_project.id).execute + repeated_service = + described_class.new(project, nil, policy_project_id: new_policy_project.id).execute expect(repeated_service).to be_success - expect(project.security_orchestration_policy_configuration.security_policy_management_project_id).to eq(new_policy_project.id) + expect( + project.security_orchestration_policy_configuration.security_policy_management_project_id + ).to eq(new_policy_project.id) end - it 'returns error when same policy is assigned to different projects' do - service + it 'assigns same policy to different projects' do + repeated_service = + described_class.new(another_project, nil, policy_project_id: policy_project.id).execute + expect(repeated_service).to be_success + end - repeated_service = described_class.new(another_project, nil, policy_project_id: policy_project.id).execute - expect(repeated_service).to be_error + it 'unassigns project' do + expect { described_class.new(project, nil, policy_project_id: nil).execute }.to change { + project.reload.security_orchestration_policy_configuration + }.to(nil) end it 'returns error when db has problem' do dbl_error = double('ActiveRecord') - dbl = double('Security::OrchestrationPolicyConfiguration', security_orchestration_policy_configuration: dbl_error) + dbl = + double( + 'Security::OrchestrationPolicyConfiguration', + security_orchestration_policy_configuration: dbl_error + ) allow(dbl_error).to receive(:update!).and_raise(ActiveRecord::RecordInvalid) @@ -43,7 +61,8 @@ allow(instance).to receive(:project).and_return(dbl) end - repeated_service = described_class.new(project, nil, policy_project_id: new_policy_project.id).execute + repeated_service = + described_class.new(project, nil, policy_project_id: new_policy_project.id).execute expect(repeated_service).to be_error end diff --git a/locale/gitlab.pot b/locale/gitlab.pot index f03be45c5dd973a7cdc13e3a17fa518b11cddd04..283087ede9577d13f21191b278bc934f140c6153 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -21580,6 +21580,9 @@ msgstr "" msgid "Opens in a new window" msgstr "" +msgid "Operation completed" +msgstr "" + msgid "Operation failed. Check pod logs for %{pod_name} for more details." msgstr "" @@ -22951,7 +22954,7 @@ msgstr "" msgid "Point to any links you like: documentation, built binaries, or other related materials. These can be internal or external links from your GitLab instance. Duplicate URLs are not allowed." msgstr "" -msgid "Policy project doesn't exists" +msgid "Policy project doesn't exist" msgstr "" msgid "Popularity" @@ -29216,9 +29219,6 @@ msgstr "" msgid "Successful purchase image" msgstr "" -msgid "Successfull" -msgstr "" - msgid "Successfully activated" msgstr ""