Commit 1e87d6d4 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'bwill/vulnerability-report-parity' into 'master'

Make starboard_vulnerability API ingest the same data structures as security reports

See merge request gitlab-org/gitlab!71066
parents f2053ebd f5a3bca6
...@@ -97,6 +97,7 @@ module Vulnerabilities ...@@ -97,6 +97,7 @@ module Vulnerabilities
Vulnerabilities::Scanner.find_or_initialize_by(name: name) do |s| Vulnerabilities::Scanner.find_or_initialize_by(name: name) do |s|
s.project = @project s.project = @project
s.external_id = scanner_hash[:id] s.external_id = scanner_hash[:id]
s.vendor = scanner_hash.dig(:vendor, :name)
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -18,9 +18,7 @@ module Vulnerabilities ...@@ -18,9 +18,7 @@ module Vulnerabilities
raise Gitlab::Access::AccessDeniedError unless authorized? raise Gitlab::Access::AccessDeniedError unless authorized?
vulnerability_hash = @params[:vulnerability] vulnerability_hash = @params[:vulnerability]
vulnerability_hash[:state] = :detected
vulnerability = initialize_vulnerability(vulnerability_hash) vulnerability = initialize_vulnerability(vulnerability_hash)
vulnerability.title = vulnerability_hash[:name]&.truncate(::Issuable::TITLE_LENGTH_MAX)
identifiers = initialize_identifiers(@params.dig(:vulnerability, :identifiers)) identifiers = initialize_identifiers(@params.dig(:vulnerability, :identifiers))
scanner = initialize_scanner(@params[:scanner]) scanner = initialize_scanner(@params[:scanner])
finding = initialize_finding( finding = initialize_finding(
...@@ -71,5 +69,10 @@ module Vulnerabilities ...@@ -71,5 +69,10 @@ module Vulnerabilities
Digest::SHA1.hexdigest(fingerprint_data) Digest::SHA1.hexdigest(fingerprint_data)
end end
def initialize_vulnerability(vulnerability_hash)
vulnerability_hash[:state] = :detected
super(vulnerability_hash)
end
end end
end end
...@@ -62,9 +62,31 @@ module EE ...@@ -62,9 +62,31 @@ module EE
params do params do
requires :vulnerability, type: Hash, desc: 'Vulnerability details matching the `vulnerability` object on the security report schema' do requires :vulnerability, type: Hash, desc: 'Vulnerability details matching the `vulnerability` object on the security report schema' do
requires :name, type: String requires :name, type: String
requires :severity, type: String requires :severity, type: String, coerce_with: ->(s) { s.downcase }
requires :confidence, type: String requires :confidence, type: String, coerce_with: ->(c) { c.downcase }
requires :location, type: Hash
requires :location, type: Hash do
requires :image, type: String
requires :dependency, type: Hash do
requires :package, type: Hash do
requires :name, type: String
end
optional :version, type: String
end
requires :kubernetes_resource, type: Hash do
requires :namespace, type: String
requires :name, type: String
requires :kind, type: String
requires :container_name, type: String
requires :agent_id, type: String
end
optional :operating_system, type: String
end
requires :identifiers, type: Array do requires :identifiers, type: Array do
requires :type, type: String requires :type, type: String
requires :name, type: String requires :name, type: String
...@@ -77,11 +99,13 @@ module EE ...@@ -77,11 +99,13 @@ module EE
optional :solution, type: String optional :solution, type: String
optional :links, type: Array optional :links, type: Array
end end
requires :scanner, type: Hash, desc: 'Scanner details matching the `.scan.scanner` field on the security report schema' do requires :scanner, type: Hash, desc: 'Scanner details matching the `.scan.scanner` field on the security report schema' do
requires :id, type: String requires :id, type: String
requires :name, type: String requires :name, type: String
requires :vendor, type: Hash do
optional :vendor, type: String requires :name, type: String
end
end end
end end
......
...@@ -232,14 +232,22 @@ RSpec.describe API::Internal::Kubernetes do ...@@ -232,14 +232,22 @@ RSpec.describe API::Internal::Kubernetes do
{ {
vulnerability: { vulnerability: {
name: 'CVE-123-4567 in libc', name: 'CVE-123-4567 in libc',
severity: 'high', severity: 'High',
confidence: 'unknown', confidence: 'Unknown',
location: { location: {
image: 'index.docker.io/library/nginx:latest',
kubernetes_resource: { kubernetes_resource: {
namespace: 'production', namespace: 'production',
kind: 'deployment', kind: 'deployment',
name: 'nginx', name: 'nginx-ingress',
container: 'nginx' container_name: 'nginx',
agent_id: '1'
},
dependency: {
package: {
name: 'libc'
},
version: 'v1.2.3'
} }
}, },
identifiers: [ identifiers: [
...@@ -253,7 +261,9 @@ RSpec.describe API::Internal::Kubernetes do ...@@ -253,7 +261,9 @@ RSpec.describe API::Internal::Kubernetes do
scanner: { scanner: {
id: 'starboard_trivy', id: 'starboard_trivy',
name: 'Trivy (via Starboard Operator)', name: 'Trivy (via Starboard Operator)',
vendor: 'GitLab' vendor: {
name: 'GitLab'
}
} }
} }
end end
...@@ -276,6 +286,18 @@ RSpec.describe API::Internal::Kubernetes do ...@@ -276,6 +286,18 @@ RSpec.describe API::Internal::Kubernetes do
end end
end end
context 'when required parameters are missing' do
where(:missing_param) { %i[vulnerability scanner] }
with_them do
it 'returns bad request' do
send_request(params: payload.delete(missing_param))
expect(response).to have_gitlab_http_status(:bad_request)
end
end
end
context 'when feature is not available' do context 'when feature is not available' do
before do before do
stub_licensed_features(security_dashboard: false) stub_licensed_features(security_dashboard: false)
......
...@@ -33,7 +33,9 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do ...@@ -33,7 +33,9 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do
scanner: { scanner: {
id: 'starboard_trivy', id: 'starboard_trivy',
name: 'Trivy (via Starboard Operator)', name: 'Trivy (via Starboard Operator)',
vendor: 'GitLab' vendor: {
name: 'GitLab'
}
} }
} }
end end
...@@ -69,6 +71,7 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do ...@@ -69,6 +71,7 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do
scanner = finding.scanner scanner = finding.scanner
expect(scanner.external_id).to eq(params.dig(:scanner, :id)) expect(scanner.external_id).to eq(params.dig(:scanner, :id))
expect(scanner.name).to eq(params.dig(:scanner, :name)) expect(scanner.name).to eq(params.dig(:scanner, :name))
expect(scanner.vendor).to eq(params.dig(:scanner, :vendor, :name))
end end
end end
......
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