Commit f5a3bca6 authored by Brian Williams's avatar Brian Williams

Use coerce_with to downcase inputs

parent ce4e4d0f
...@@ -30,26 +30,18 @@ module Vulnerabilities ...@@ -30,26 +30,18 @@ module Vulnerabilities
GENERIC_REPORT_TYPE GENERIC_REPORT_TYPE
end end
def sanitize_enums(vulnerability_hash) def initialize_vulnerability(vulnerability_hash)
vulnerability_hash attributes = vulnerability_hash
.slice(*%i[ .slice(*%i[
description description
state state
severity severity
confidence confidence
])
.transform_values(&:downcase)
end
def initialize_vulnerability(vulnerability_hash)
attributes = vulnerability_hash
.slice(*%i[
detected_at detected_at
confirmed_at confirmed_at
resolved_at resolved_at
dismissed_at dismissed_at
]) ])
.merge(sanitize_enums(vulnerability_hash))
.merge( .merge(
project: @project, project: @project,
author: @author, author: @author,
......
...@@ -72,7 +72,6 @@ module Vulnerabilities ...@@ -72,7 +72,6 @@ module Vulnerabilities
def initialize_vulnerability(vulnerability_hash) def initialize_vulnerability(vulnerability_hash)
vulnerability_hash[:state] = :detected vulnerability_hash[:state] = :detected
vulnerability_hash[:title] = vulnerability_hash[:name]
super(vulnerability_hash) super(vulnerability_hash)
end end
end end
......
...@@ -62,8 +62,8 @@ module EE ...@@ -62,8 +62,8 @@ 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 do requires :location, type: Hash do
requires :image, type: String requires :image, type: String
......
...@@ -232,8 +232,8 @@ RSpec.describe API::Internal::Kubernetes do ...@@ -232,8 +232,8 @@ 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', image: 'index.docker.io/library/nginx:latest',
kubernetes_resource: { kubernetes_resource: {
......
...@@ -252,27 +252,6 @@ RSpec.describe Vulnerabilities::ManuallyCreateService do ...@@ -252,27 +252,6 @@ RSpec.describe Vulnerabilities::ManuallyCreateService do
expect(subject.message).to match(/confirmed_at can only be set/) expect(subject.message).to match(/confirmed_at can only be set/)
end end
end end
context 'with capitalized enum fields' do
let(:params) do
{
vulnerability: {
name: "Test vulnerability",
state: "Detected",
severity: "Unknown",
confidence: "Unknown",
identifiers: [identifier_attributes],
scanner: scanner_attributes
}
}
end
it 'does not raise an exception' do
expect { subject }.not_to raise_error
expect(subject.success?).to be_truthy
end
end
end end
context 'with invalid parameters' do context 'with invalid parameters' do
......
...@@ -6,16 +6,14 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do ...@@ -6,16 +6,14 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do
let(:agent) { create(:cluster_agent) } let(:agent) { create(:cluster_agent) }
let(:project) { agent.project } let(:project) { agent.project }
let(:user) { agent.created_by_user } let(:user) { agent.created_by_user }
let(:severity) { 'high' }
let(:confidence) { 'unknown' }
let(:params) do let(:params) do
{ {
vulnerability: { vulnerability: {
name: 'CVE-123-4567 in libc', name: 'CVE-123-4567 in libc',
message: 'Vulnerability message', message: 'Vulnerability message',
description: 'Vulnerability description', description: 'Vulnerability description',
severity: severity, severity: 'high',
confidence: confidence, confidence: 'unknown',
location: { location: {
kubernetes_resource: { kubernetes_resource: {
namespace: 'production', namespace: 'production',
...@@ -75,16 +73,6 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do ...@@ -75,16 +73,6 @@ RSpec.describe Vulnerabilities::StarboardVulnerabilityCreateService do
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)) expect(scanner.vendor).to eq(params.dig(:scanner, :vendor, :name))
end end
context 'with capitalized enums' do
let(:severity) { 'High' }
let(:confidence) { 'Unknown' }
it 'does not raise exception' do
expect { subject }.not_to raise_error
expect(subject.success?).to be_truthy
end
end
end end
context 'with feature disabled' do context 'with feature disabled' 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