Commit 91e92374 authored by ap4y's avatar ap4y

Add edit action to the threat monitoring controller

This commit introduces a new empty edit page for the new policy
editor. Actual page content will be implemented using an existing vue
component in a separate commit.
parent 118c8e69
...@@ -3,11 +3,27 @@ ...@@ -3,11 +3,27 @@
module Projects module Projects
class ThreatMonitoringController < Projects::ApplicationController class ThreatMonitoringController < Projects::ApplicationController
before_action :authorize_read_threat_monitoring! before_action :authorize_read_threat_monitoring!
before_action :verify_network_policy_editor_flag!, only: :new before_action :verify_network_policy_editor_flag!, only: [:new, :edit]
before_action do before_action do
push_frontend_feature_flag(:network_policy_editor, project) push_frontend_feature_flag(:network_policy_editor, project)
end end
def edit
environment = project.environments.find(params[:environment_id])
@policy_name = params[:id]
response = NetworkPolicies::FindResourceService.new(
resource_name: @policy_name,
environment: environment,
kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND
).execute
if response.success?
@policy = response.payload
else
render_404
end
end
private private
def verify_network_policy_editor_flag! def verify_network_policy_editor_flag!
......
- add_to_breadcrumbs s_("ThreatMonitoring|Threat Monitoring"), project_threat_monitoring_path(@project)
- breadcrumb_title @policy_name
- page_title s_("NetworkPolicies|Policy editor")
#js-policy-builder-app{ data: { network_policies_endpoint: project_security_network_policies_path(@project),
environments_endpoint: project_environments_path(@project),
threat_monitoring_path: project_threat_monitoring_path(@project),
policy: @policy.to_json,
} }
...@@ -34,7 +34,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -34,7 +34,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
resources :subscriptions, only: [:create, :destroy] resources :subscriptions, only: [:create, :destroy]
resource :threat_monitoring, only: [:show], controller: :threat_monitoring do resource :threat_monitoring, only: [:show], controller: :threat_monitoring do
resources :policies, only: [:new], controller: :threat_monitoring resources :policies, only: [:new, :edit], controller: :threat_monitoring
end end
resources :protected_environments, only: [:create, :update, :destroy], constraints: { id: /\d+/ } do resources :protected_environments, only: [:create, :update, :destroy], constraints: { id: /\d+/ } do
......
...@@ -100,7 +100,7 @@ RSpec.describe Projects::ThreatMonitoringController do ...@@ -100,7 +100,7 @@ RSpec.describe Projects::ThreatMonitoringController do
stub_feature_flags(network_policy_editor: true) stub_feature_flags(network_policy_editor: true)
end end
it 'renders the show template' do it 'renders the new template' do
subject subject
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
...@@ -149,4 +149,125 @@ RSpec.describe Projects::ThreatMonitoringController do ...@@ -149,4 +149,125 @@ RSpec.describe Projects::ThreatMonitoringController do
end end
end end
end end
describe 'GET edit' do
subject do
get :edit, params: { namespace_id: project.namespace, project_id: project, id: 'policy', environment_id: environment_id }
end
let_it_be(:environment) { create(:environment, :with_review_app, project: project) }
let(:environment_id) { environment.id }
context 'with authorized user' do
before do
project.add_developer(user)
sign_in(user)
end
context 'when feature is available' do
before do
stub_licensed_features(threat_monitoring: true)
end
context 'and feature flag is disabled' do
before do
stub_feature_flags(network_policy_editor: false)
end
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'and feature flag is enabled' do
let(:service) { instance_double('NetworkPolicies::FindResourceService', execute: ServiceResponse.success(payload: policy)) }
let(:policy) do
Gitlab::Kubernetes::CiliumNetworkPolicy.new(
name: 'policy',
namespace: 'another',
selector: { matchLabels: { role: 'db' } },
ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }]
)
end
before do
stub_feature_flags(network_policy_editor: true)
allow(NetworkPolicies::FindResourceService).to(
receive(:new)
.with(resource_name: 'policy', environment: environment, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND)
.and_return(service)
)
end
it 'renders the new template' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:edit)
end
context 'when environment is missing' do
let(:environment_id) { 'missing' }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when service failed' do
let(:service) { instance_double('NetworkPolicies::FindResourceService', execute: ServiceResponse.error(message: 'error')) }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
context 'when feature is not available' do
before do
stub_licensed_features(threat_monitoring: false)
end
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'with unauthorized user' do
before do
sign_in(user)
end
context 'when feature is available' do
before do
stub_licensed_features(threat_monitoring: true)
end
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'with anonymous user' do
it 'returns 302' do
subject
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(new_user_session_path)
end
end
end
end end
...@@ -15,6 +15,7 @@ RSpec.describe NetworkPolicies::FindResourceService do ...@@ -15,6 +15,7 @@ RSpec.describe NetworkPolicies::FindResourceService do
ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }] ingress: [{ from: [{ namespaceSelector: { matchLabels: { project: 'myproject' } } }] }]
) )
end end
let(:kind) { Gitlab::Kubernetes::NetworkPolicy::KIND } let(:kind) { Gitlab::Kubernetes::NetworkPolicy::KIND }
describe '#execute' do describe '#execute' do
......
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