|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe Gitlab::BackgroundMigration::BackfillIdentifierNamesOfVulnerabilityReads, feature_category: :vulnerability_management do |
| 6 | + let(:namespaces) { table(:namespaces) } |
| 7 | + let(:projects) { table(:projects) } |
| 8 | + let(:users) { table(:users) } |
| 9 | + let(:scanners) { table(:vulnerability_scanners) } |
| 10 | + let(:vulnerabilities) { table(:vulnerabilities) } |
| 11 | + let(:vulnerability_reads) { table(:vulnerability_reads) } |
| 12 | + let(:vulnerability_findings) { table(:vulnerability_occurrences) } |
| 13 | + let(:vulnerability_occurrence_identifiers) { table(:vulnerability_occurrence_identifiers) } |
| 14 | + let(:vulnerability_identifiers) { table(:vulnerability_identifiers) } |
| 15 | + |
| 16 | + let(:namespace) { namespaces.create!(name: 'user', path: 'user') } |
| 17 | + let(:project) { projects.create!(namespace_id: namespace.id, project_namespace_id: namespace.id) } |
| 18 | + let(:user) { users.create!(username: 'john_doe', email: '[email protected]', projects_limit: 10) } |
| 19 | + let(:scanner) { scanners.create!(project_id: project.id, external_id: 'external_id', name: 'Test Scanner') } |
| 20 | + |
| 21 | + shared_context 'with vulnerability data' do |
| 22 | + let(:identifier_1) do |
| 23 | + create_identifier(external_id: 'A03:2021', external_type: 'owasp', name: 'A03:2021 - Injection') |
| 24 | + end |
| 25 | + |
| 26 | + let(:identifier_2) { create_identifier(external_id: 'CVE-2021-1234', external_type: 'cve', name: 'CVE-2021-1234') } |
| 27 | + let(:identifier_3) { create_identifier(external_id: '79', external_type: 'cwe', name: 'CWE-79') } |
| 28 | + |
| 29 | + let(:finding_1) { create_finding(primary_identifier_id: identifier_1.id) } |
| 30 | + let(:finding_2) { create_finding(primary_identifier_id: identifier_2.id) } |
| 31 | + let(:finding_3) { create_finding(primary_identifier_id: identifier_3.id) } |
| 32 | + |
| 33 | + let(:vulnerability_1) { create_vulnerability(title: 'vulnerability 1', finding_id: finding_1.id) } |
| 34 | + let(:vulnerability_2) { create_vulnerability(title: 'vulnerability 2', finding_id: finding_2.id) } |
| 35 | + let(:vulnerability_3) { create_vulnerability(title: 'vulnerability 3', finding_id: finding_3.id) } |
| 36 | + |
| 37 | + let!(:vulnerability_read_1) { create_vulnerability_read(vulnerability_id: vulnerability_1.id) } |
| 38 | + let!(:vulnerability_read_2) { create_vulnerability_read(vulnerability_id: vulnerability_2.id) } |
| 39 | + let!(:vulnerability_read_3) { create_vulnerability_read(vulnerability_id: vulnerability_3.id) } |
| 40 | + |
| 41 | + before do |
| 42 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_1.id, identifier_id: identifier_1.id) |
| 43 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_2.id, identifier_id: identifier_2.id) |
| 44 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_3.id, identifier_id: identifier_3.id) |
| 45 | + |
| 46 | + finding_1.update!(vulnerability_id: vulnerability_1.id) |
| 47 | + finding_2.update!(vulnerability_id: vulnerability_2.id) |
| 48 | + finding_3.update!(vulnerability_id: vulnerability_3.id) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe '#perform' do |
| 53 | + subject(:perform_migration) do |
| 54 | + described_class.new( |
| 55 | + start_id: vulnerability_reads.first.id, |
| 56 | + end_id: vulnerability_reads.last.id, |
| 57 | + batch_table: :vulnerability_reads, |
| 58 | + batch_column: :id, |
| 59 | + sub_batch_size: vulnerability_reads.count, |
| 60 | + pause_ms: 0, |
| 61 | + connection: ActiveRecord::Base.connection |
| 62 | + ).perform |
| 63 | + end |
| 64 | + |
| 65 | + context 'with vulnerability data' do |
| 66 | + include_context 'with vulnerability data' |
| 67 | + |
| 68 | + it 'updates identifier_names for vulnerability_reads' do |
| 69 | + expect { perform_migration } |
| 70 | + .to change { vulnerability_read_1.reload.identifier_names } |
| 71 | + .from([]).to(array_including(identifier_1.name)) |
| 72 | + .and change { vulnerability_read_2.reload.identifier_names } |
| 73 | + .from([]).to(array_including(identifier_2.name)) |
| 74 | + .and change { vulnerability_read_3.reload.identifier_names } |
| 75 | + .from([]).to(array_including(identifier_3.name)) |
| 76 | + end |
| 77 | + |
| 78 | + it 'updates identifier_names with correct aggregation' do |
| 79 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_1.id, identifier_id: identifier_2.id) |
| 80 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_2.id, identifier_id: identifier_3.id) |
| 81 | + |
| 82 | + perform_migration |
| 83 | + |
| 84 | + expect(vulnerability_read_1.reload.identifier_names).to contain_exactly(identifier_1.name, |
| 85 | + identifier_2.name) |
| 86 | + expect(vulnerability_read_2.reload.identifier_names).to contain_exactly(identifier_2.name, |
| 87 | + identifier_3.name) |
| 88 | + expect(vulnerability_read_3.reload.identifier_names).to contain_exactly(identifier_3.name) |
| 89 | + end |
| 90 | + |
| 91 | + it 'sorts identifier_names' do |
| 92 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_1.id, identifier_id: identifier_3.id) |
| 93 | + create_vulnerability_occurrence_identifier(occurrence_id: finding_1.id, identifier_id: identifier_2.id) |
| 94 | + |
| 95 | + perform_migration |
| 96 | + |
| 97 | + expect(vulnerability_read_1.reload.identifier_names).to eq([identifier_1.name, |
| 98 | + identifier_2.name, identifier_3.name]) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'with no matching identifiers' do |
| 103 | + include_context 'with vulnerability data' do |
| 104 | + before do |
| 105 | + vulnerability_occurrence_identifiers.delete_all |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + it 'does not update identifier_names' do |
| 110 | + perform_migration |
| 111 | + |
| 112 | + expect(vulnerability_reads.where.not(identifier_names: []).count).to eq(0) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + private |
| 118 | + |
| 119 | + def create_vulnerability(overrides = {}) |
| 120 | + vulnerabilities.create!({ |
| 121 | + project_id: project.id, |
| 122 | + author_id: user.id, |
| 123 | + title: 'test', |
| 124 | + severity: 1, |
| 125 | + confidence: 1, |
| 126 | + report_type: 1 |
| 127 | + }.merge(overrides)) |
| 128 | + end |
| 129 | + |
| 130 | + def create_vulnerability_read(overrides = {}) |
| 131 | + vulnerability_reads.create!({ |
| 132 | + project_id: project.id, |
| 133 | + vulnerability_id: 1, |
| 134 | + scanner_id: scanner.id, |
| 135 | + severity: 1, |
| 136 | + report_type: 1, |
| 137 | + state: 1, |
| 138 | + uuid: SecureRandom.uuid |
| 139 | + }.merge(overrides)) |
| 140 | + end |
| 141 | + |
| 142 | + def create_finding(overrides = {}) |
| 143 | + vulnerability_findings.create!({ |
| 144 | + project_id: project.id, |
| 145 | + scanner_id: scanner.id, |
| 146 | + severity: 5, # medium |
| 147 | + confidence: 2, # unknown, |
| 148 | + report_type: 99, # generic |
| 149 | + primary_identifier_id: create_identifier.id, |
| 150 | + project_fingerprint: SecureRandom.hex(20), |
| 151 | + location_fingerprint: SecureRandom.hex(20), |
| 152 | + uuid: SecureRandom.uuid, |
| 153 | + name: "CVE-2018-1234", |
| 154 | + raw_metadata: "{}", |
| 155 | + metadata_version: "test:1.0" |
| 156 | + }.merge(overrides)) |
| 157 | + end |
| 158 | + |
| 159 | + def create_identifier(overrides = {}) |
| 160 | + vulnerability_identifiers.create!({ |
| 161 | + project_id: project.id, |
| 162 | + external_id: "CVE-2018-1234", |
| 163 | + external_type: "CVE", |
| 164 | + name: "CVE-2018-1234", |
| 165 | + fingerprint: SecureRandom.hex(20) |
| 166 | + }.merge(overrides)) |
| 167 | + end |
| 168 | + |
| 169 | + def create_vulnerability_occurrence_identifier(overrides = {}) |
| 170 | + vulnerability_occurrence_identifiers.create!({ |
| 171 | + created_at: Time.now.utc, |
| 172 | + updated_at: Time.now.utc, |
| 173 | + occurrence_id: nil, |
| 174 | + identifier_id: nil |
| 175 | + }.merge(overrides)) |
| 176 | + end |
| 177 | +end |
0 commit comments