Commit 88faaeef authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Move UUID calculator class

Version 5 UUID calculation is not something specific to Vulnerabilities
so it's better to make the calculator class more generic.
parent 8611fb40
......@@ -176,7 +176,7 @@ module Gitlab
name = uuid_v5_name_components.values.join('-')
Gitlab::Vulnerabilities::CalculateFindingUUID.call(name)
Gitlab::UUID.v5(name)
end
end
end
......
......@@ -165,8 +165,8 @@ RSpec.describe Gitlab::Ci::Parsers::Security::Common do
let(:finding_uuids) { report.findings.map(&:uuid) }
let(:uuid_1_components) { "dependency_scanning-4ff8184cd18485b6e85d5b101e341b12eacd1b3b-33dc9f32c77dde16d39c69d3f78f27ca3114a7c5-#{pipeline.project_id}" }
let(:uuid_2_components) { "dependency_scanning-d55f9e66e79882ae63af9fd55cc822ab75307e31-33dc9f32c77dde16d39c69d3f78f27ca3114a7c5-#{pipeline.project_id}" }
let(:uuid_1) { Gitlab::Vulnerabilities::CalculateFindingUUID.call(uuid_1_components) }
let(:uuid_2) { Gitlab::Vulnerabilities::CalculateFindingUUID.call(uuid_2_components) }
let(:uuid_1) { Gitlab::UUID.v5(uuid_1_components) }
let(:uuid_2) { Gitlab::UUID.v5(uuid_2_components) }
let(:expected_uuids) { [uuid_1, uuid_2, nil] }
it 'sets the UUIDv5 for findings', :aggregate_failures do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Vulnerabilities::CalculateFindingUUID do
let_it_be(:value) { "GitLab" }
subject { described_class.call(value) }
before do
# This is necessary to clear memoization for testing different environments
described_class.instance_variable_set(:@namespace_id, nil)
end
context 'in development' do
let_it_be(:development_proper_uuid) { "5b593e54-90f5-504b-8805-5394a4d14b94" }
before do
allow(Rails).to receive(:env).and_return(:development)
end
it { is_expected.to eq(development_proper_uuid) }
end
context 'in test' do
let_it_be(:test_proper_uuid) { "5b593e54-90f5-504b-8805-5394a4d14b94" }
it { is_expected.to eq(test_proper_uuid) }
end
context 'in staging' do
let_it_be(:staging_proper_uuid) { "dd190b37-7754-5c7c-80a0-85621a5823ad" }
before do
allow(Rails).to receive(:env).and_return(:staging)
end
it { is_expected.to eq(staging_proper_uuid) }
end
context 'in production' do
let_it_be(:production_proper_uuid) { "4961388b-9d8e-5da0-a499-3ef5da58daf0" }
before do
allow(Rails).to receive(:env).and_return(:production)
end
it { is_expected.to eq(production_proper_uuid) }
end
end
# frozen_string_literal: true
module Gitlab
module Vulnerabilities
class CalculateFindingUUID
FINDING_NAMESPACES_IDS = {
development: "a143e9e2-41b3-47bc-9a19-081d089229f4",
test: "a143e9e2-41b3-47bc-9a19-081d089229f4",
staging: "a6930898-a1b2-4365-ab18-12aa474d9b26",
production: "58dc0f06-936c-43b3-93bb-71693f1b6570"
}.freeze
class UUID
NAMESPACE_IDS = {
development: "a143e9e2-41b3-47bc-9a19-081d089229f4",
test: "a143e9e2-41b3-47bc-9a19-081d089229f4",
staging: "a6930898-a1b2-4365-ab18-12aa474d9b26",
production: "58dc0f06-936c-43b3-93bb-71693f1b6570"
}.freeze
NAMESPACE_REGEX = /(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})/.freeze
PACK_PATTERN = "NnnnnN".freeze
NAMESPACE_REGEX = /(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})/.freeze
PACK_PATTERN = "NnnnnN".freeze
def self.call(value)
Digest::UUID.uuid_v5(namespace_id, value)
class << self
def v5(name, namespace_id: default_namespace_id)
Digest::UUID.uuid_v5(namespace_id, name)
end
def self.namespace_id
@namespace_id ||= begin
namespace_uuid = FINDING_NAMESPACES_IDS.fetch(Rails.env.to_sym)
private
def default_namespace_id
@default_namespace_id ||= begin
namespace_uuid = NAMESPACE_IDS.fetch(Rails.env.to_sym)
# Digest::UUID is broken when using a UUID as a namespace_id
# https://github.com/rails/rails/issues/37681#issue-520718028
namespace_uuid.scan(NAMESPACE_REGEX).flatten.map { |s| s.to_i(16) }.pack(PACK_PATTERN)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::UUID do
let_it_be(:name) { "GitLab" }
describe '.v5' do
subject { described_class.v5(name) }
before do
# This is necessary to clear memoization for testing different environments
described_class.instance_variable_set(:@default_namespace_id, nil)
end
context 'in development' do
let_it_be(:development_proper_uuid) { "5b593e54-90f5-504b-8805-5394a4d14b94" }
before do
allow(Rails).to receive(:env).and_return(:development)
end
it { is_expected.to eq(development_proper_uuid) }
end
context 'in test' do
let_it_be(:test_proper_uuid) { "5b593e54-90f5-504b-8805-5394a4d14b94" }
it { is_expected.to eq(test_proper_uuid) }
end
context 'in staging' do
let_it_be(:staging_proper_uuid) { "dd190b37-7754-5c7c-80a0-85621a5823ad" }
before do
allow(Rails).to receive(:env).and_return(:staging)
end
it { is_expected.to eq(staging_proper_uuid) }
end
context 'in production' do
let_it_be(:production_proper_uuid) { "4961388b-9d8e-5da0-a499-3ef5da58daf0" }
before do
allow(Rails).to receive(:env).and_return(:production)
end
it { is_expected.to eq(production_proper_uuid) }
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