Commit f59791a4 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'refactor-vulnerabiltiy-parser' into 'master'

Refactor vulnerability template classes

See merge request gitlab-org/gitlab!44063
parents d667c0b4 f5f6fdd4
...@@ -9,19 +9,20 @@ module Gitlab ...@@ -9,19 +9,20 @@ module Gitlab
@data = data.deep_symbolize_keys @data = data.deep_symbolize_keys
end end
# Ensure mandatory properties are defined # Passthrough properties
%i[ %i[
title
description
severity severity
confidence confidence
solution solution
identifiers identifiers
links links
].each do |method_name| remediations
define_method(method_name) do target_branch
raise NotImplementedError ].each { |method_name| define_method(method_name) { @data[method_name] } }
end
# Ensure mandatory properties are defined
%i[title description].each do |method_name|
define_method(method_name) { raise NotImplementedError }
end end
end end
end end
......
...@@ -3,20 +3,6 @@ ...@@ -3,20 +3,6 @@
module Gitlab module Gitlab
module Vulnerabilities module Vulnerabilities
class ContainerScanningVulnerability < BaseVulnerability class ContainerScanningVulnerability < BaseVulnerability
# Passthrough properties
%i[
confidence
severity
identifiers
links
remediations
target_branch
].each do |method_name|
define_method(method_name) do
@data[method_name]
end
end
def title def title
format_data( format_data(
:name => :name =>
......
...@@ -4,20 +4,6 @@ module Gitlab ...@@ -4,20 +4,6 @@ module Gitlab
module Vulnerabilities module Vulnerabilities
class StandardVulnerability < BaseVulnerability class StandardVulnerability < BaseVulnerability
include RequestAwareEntity include RequestAwareEntity
# Passthrough properties
%i[
severity
confidence
solution
identifiers
links
remediations
target_branch
].each do |method_name|
define_method(method_name) do
@data[method_name]
end
end
def title def title
@data[:title].presence || @data[:name] @data[:title].presence || @data[:name]
......
...@@ -3,25 +3,49 @@ ...@@ -3,25 +3,49 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Vulnerabilities::BaseVulnerability do RSpec.describe Gitlab::Vulnerabilities::BaseVulnerability do
let(:vulnerability) do let(:data) do
described_class.new( {
title: 'title', title: 'title',
description: 'desc', description: 'desc',
severity: 'high', severity: 'high',
confidence: 'low', confidence: 'low',
solution: 'fix', identifiers: [
identifiers: '42', {
links: 'link' type: 'CVE',
) name: 'CVE-2017-15650',
value: 'CVE-2017-15650',
url: 'https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-15650'
}
],
links: [{ name: 'Awesome-security blog post', url: 'https;//example.com/blog-post' }],
location: { file: 'main.rb', start_line: 14, blob_path: '/bar/foo/main.rb#14' },
solution: 'upgrade dependencies'
}
end end
where(:getter) do let(:vulnerability) { described_class.new(data) }
%w(title description severity confidence solution identifiers links)
end where(:getter) { %w[title description] }
with_them do with_them do
it 'raises an error' do it 'raises an error' do
expect { vulnerability.public_send(getter) }.to raise_error(NotImplementedError) expect { vulnerability.public_send(getter) }.to raise_error(NotImplementedError)
end end
end end
describe 'getters' do
where(:getter) { %i[severity confidence solution identifiers links remediations target_branch] }
let(:with_nil) { described_class.new({}) }
with_them do
it 'returns right value' do
expect(vulnerability.public_send(getter)).to eq(data[getter])
end
it 'returns nil value' do
expect(with_nil.public_send(getter)).to eq(nil)
end
end
end
end end
...@@ -18,20 +18,6 @@ RSpec.describe Gitlab::Vulnerabilities::ContainerScanningVulnerability do ...@@ -18,20 +18,6 @@ RSpec.describe Gitlab::Vulnerabilities::ContainerScanningVulnerability do
}.with_indifferent_access }.with_indifferent_access
end end
describe 'getters' do
let(:vulnerability) { described_class.new(data) }
where(:getter) do
%i[severity confidence identifiers links target_branch remediations]
end
with_them do
it 'returns right value' do
expect(vulnerability.public_send(getter)).to eq(data[getter])
end
end
end
describe '#title' do describe '#title' do
subject { described_class.new(data).title } subject { described_class.new(data).title }
......
...@@ -6,129 +6,20 @@ RSpec.describe Gitlab::Vulnerabilities::StandardVulnerability do ...@@ -6,129 +6,20 @@ RSpec.describe Gitlab::Vulnerabilities::StandardVulnerability do
let(:title) { 'Predictable pseudorandom number generator' } let(:title) { 'Predictable pseudorandom number generator' }
let(:name) { 'Predictable pseudorandom number generator (from name)' } let(:name) { 'Predictable pseudorandom number generator (from name)' }
let(:description) { 'Description of Predictable pseudorandom number generator' } let(:description) { 'Description of Predictable pseudorandom number generator' }
let(:severity) { 'Low' }
let(:confidence) { 'High' }
let(:solution) { 'Please do something!' } let(:solution) { 'Please do something!' }
let(:file) { 'subdir/src/main/java/com/gitlab/security_products/tests/App.java' } let(:file) { 'subdir/src/main/java/com/gitlab/security_products/tests/App.java' }
let(:line) { 15 } let(:line) { 15 }
let(:blob_path) { "bar/foo/-/blob/sha/#{file}#L#{line}" } let(:blob_path) { "bar/foo/-/blob/sha/#{file}#L#{line}" }
let(:location) do let(:location) do
{ file: file, start_line: line, blob_path: "/#{blob_path}" } { file: file, start_line: line, blob_path: "/#{blob_path}" }
end end
let(:identifiers) do
[{
type: 'CVE',
name: 'CVE-2017-15650',
value: 'CVE-2017-15650',
url: 'https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-15650'
}]
end
let(:links) do
[
{ name: 'Awesome-security blog post', url: 'https;//example.com/blog-post' },
{ url: 'https://example.com/another-link' }
]
end
it 'inherits from Gitlab::Vulnerabilities::BaseVulnerability' do it 'inherits from Gitlab::Vulnerabilities::BaseVulnerability' do
vulnerability = described_class.new(foo: 'bar') vulnerability = described_class.new(foo: 'bar')
expect(vulnerability).to be_kind_of(Gitlab::Vulnerabilities::BaseVulnerability) expect(vulnerability).to be_kind_of(Gitlab::Vulnerabilities::BaseVulnerability)
end end
describe '#severity' do
context 'when severity is present' do
it 'returns severity' do
vulnerability = described_class.new(severity: severity)
expect(vulnerability.severity).to eq severity
end
end
context 'when severity is not set' do
it 'returns nil' do
vulnerability = described_class.new(foo: 'bar')
expect(vulnerability.severity).to be_nil
end
end
end
describe '#confidence' do
context 'when confidence is present' do
it 'returns confidence' do
vulnerability = described_class.new(confidence: confidence)
expect(vulnerability.confidence).to eq confidence
end
end
context 'when confidence is not set' do
it 'returns nil' do
vulnerability = described_class.new(foo: 'bar')
expect(vulnerability.confidence).to be_nil
end
end
end
describe '#solution' do
context 'when solution is present' do
it 'returns solution' do
vulnerability = described_class.new(solution: solution)
expect(vulnerability.solution).to eq solution
end
end
context 'when solution is not set' do
it 'returns nil' do
vulnerability = described_class.new(foo: 'bar')
expect(vulnerability.solution).to be_nil
end
end
end
describe '#identifiers' do
context 'when identifiers is present' do
it 'returns identifiers' do
vulnerability = described_class.new(identifiers: identifiers)
expect(vulnerability.identifiers).to eq identifiers
end
end
context 'when identifiers is not set' do
it 'returns nil' do
vulnerability = described_class.new(foo: 'bar')
expect(vulnerability.identifiers).to be_nil
end
end
end
describe '#links' do
context 'when links is present' do
it 'returns links' do
vulnerability = described_class.new(links: links)
expect(vulnerability.links).to eq links
end
end
context 'when links is not set' do
it 'returns nil' do
vulnerability = described_class.new(foo: 'bar')
expect(vulnerability.links).to be_nil
end
end
end
describe '#title' do describe '#title' do
context 'when title is present' do context 'when title is present' do
it 'returns title' do it 'returns title' 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