Commit 4c93874c authored by Markus Koller's avatar Markus Koller

Merge branch 'philipcunningham-rename-dast-site-validation-find-or-create-service' into 'master'

Move DAST validation service to AppSec namespace

See merge request gitlab-org/gitlab!70903
parents f6e83b81 f3eba884
...@@ -38,7 +38,7 @@ module Mutations ...@@ -38,7 +38,7 @@ module Mutations
dast_site_token = dast_site_token_id.find dast_site_token = dast_site_token_id.find
response = ::DastSiteValidations::FindOrCreateService.new( response = ::AppSec::Dast::SiteValidations::FindOrCreateService.new(
container: project, container: project,
current_user: current_user, current_user: current_user,
params: { params: {
......
# frozen_string_literal: true
module AppSec
module Dast
module SiteValidations
class FindOrCreateService < BaseContainerService
def execute
return ServiceResponse.error(message: 'Insufficient permissions') unless allowed?
dast_site_validation = existing_successful_validation || create_validation!
return ServiceResponse.error(message: 'Site does not exist for profile') unless dast_site_validation.dast_site
associate_dast_site!(dast_site_validation)
return ServiceResponse.success(payload: dast_site_validation) if dast_site_validation.passed?
perform_runner_validation(dast_site_validation)
rescue ActiveRecord::RecordInvalid => err
ServiceResponse.error(message: err.record.errors.full_messages)
rescue KeyError => err
ServiceResponse.error(message: err.message.capitalize)
end
private
def allowed?
can?(current_user, :create_on_demand_dast_scan, container) &&
dast_site_token.project == container
end
def dast_site_token
@dast_site_token ||= params.fetch(:dast_site_token)
end
def url_path
@url_path ||= params.fetch(:url_path)
end
def validation_strategy
@validation_strategy ||= params.fetch(:validation_strategy)
end
def existing_successful_validation
@existing_successful_validation ||= find_latest_successful_dast_site_validation
end
def url_base
@url_base ||= DastSiteValidation.get_normalized_url_base(dast_site_token.url)
end
def associate_dast_site!(dast_site_validation)
dast_site_validation.dast_site.update!(dast_site_validation_id: dast_site_validation.id)
end
def find_latest_successful_dast_site_validation
DastSiteValidationsFinder.new(
project_id: container.id,
state: :passed,
url_base: url_base
).execute.first
end
def create_validation!
DastSiteValidation.create!(
dast_site_token: dast_site_token,
url_path: url_path,
validation_strategy: validation_strategy
)
end
def perform_runner_validation(dast_site_validation)
AppSec::Dast::SiteValidations::RunnerService.new(
project: dast_site_validation.project,
current_user: current_user,
params: { dast_site_validation: dast_site_validation }
).execute
end
end
end
end
end
# frozen_string_literal: true
module DastSiteValidations
class FindOrCreateService < BaseContainerService
def execute
return ServiceResponse.error(message: 'Insufficient permissions') unless allowed?
dast_site_validation = existing_successful_validation || create_validation!
return ServiceResponse.error(message: 'Site does not exist for profile') unless dast_site_validation.dast_site
associate_dast_site!(dast_site_validation)
return ServiceResponse.success(payload: dast_site_validation) if dast_site_validation.passed?
perform_runner_validation(dast_site_validation)
rescue ActiveRecord::RecordInvalid => err
ServiceResponse.error(message: err.record.errors.full_messages)
rescue KeyError => err
ServiceResponse.error(message: err.message.capitalize)
end
private
def allowed?
can?(current_user, :create_on_demand_dast_scan, container) &&
dast_site_token.project == container
end
def dast_site_token
@dast_site_token ||= params.fetch(:dast_site_token)
end
def url_path
@url_path ||= params.fetch(:url_path)
end
def validation_strategy
@validation_strategy ||= params.fetch(:validation_strategy)
end
def existing_successful_validation
@existing_successful_validation ||= find_latest_successful_dast_site_validation
end
def url_base
@url_base ||= DastSiteValidation.get_normalized_url_base(dast_site_token.url)
end
def associate_dast_site!(dast_site_validation)
dast_site_validation.dast_site.update!(dast_site_validation_id: dast_site_validation.id)
end
def find_latest_successful_dast_site_validation
DastSiteValidationsFinder.new(
project_id: container.id,
state: :passed,
url_base: url_base
).execute.first
end
def create_validation!
DastSiteValidation.create!(
dast_site_token: dast_site_token,
url_path: url_path,
validation_strategy: validation_strategy
)
end
def perform_runner_validation(dast_site_validation)
AppSec::Dast::SiteValidations::RunnerService.new(
project: dast_site_validation.project,
current_user: current_user,
params: { dast_site_validation: dast_site_validation }
).execute
end
end
end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe DastSiteValidations::FindOrCreateService do RSpec.describe AppSec::Dast::SiteValidations::FindOrCreateService do
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:developer) { create(:user, developer_projects: [project]) } let_it_be(:developer) { create(:user, developer_projects: [project]) }
let_it_be(:dast_site) { create(:dast_site, project: project) } let_it_be(:dast_site) { create(:dast_site, project: project) }
......
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