Commit 97ffbc69 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'ci-platform-metrics-invalid-bucket' into 'master'

Updates CiPlatformMetrics to ignore invalids

See merge request gitlab-org/gitlab!42116
parents 1867bcac b1aa02a9
...@@ -14,19 +14,25 @@ class CiPlatformMetric < ApplicationRecord ...@@ -14,19 +14,25 @@ class CiPlatformMetric < ApplicationRecord
numericality: { only_integer: true, greater_than: 0 } numericality: { only_integer: true, greater_than: 0 }
CI_VARIABLE_KEY = 'AUTO_DEVOPS_PLATFORM_TARGET' CI_VARIABLE_KEY = 'AUTO_DEVOPS_PLATFORM_TARGET'
ALLOWED_TARGETS = %w[ECS FARGATE].freeze
def self.insert_auto_devops_platform_targets! def self.insert_auto_devops_platform_targets!
recorded_at = Time.zone.now
# This work can NOT be done in-database because value is encrypted. # This work can NOT be done in-database because value is encrypted.
# However, for 'AUTO_DEVOPS_PLATFORM_TARGET', these values are only # However, for 'AUTO_DEVOPS_PLATFORM_TARGET', these values are only
# encrypted as a matter of course, rather than as a need for secrecy. # encrypted as a matter of course, rather than as a need for secrecy.
# So this is not a security risk, but exposing other keys possibly could be. # So this is not a security risk, but exposing other keys possibly could be.
variables = Ci::Variable.by_key(CI_VARIABLE_KEY) variables = Ci::Variable.by_key(CI_VARIABLE_KEY)
recorded_at = Time.zone.now
counts = variables.group_by(&:value).map do |value, variables| counts = variables.group_by(&:value).map do |value, variables|
target = value.truncate(PLATFORM_TARGET_MAX_LENGTH, separator: '', omission: '') # While this value is, in theory, not secret. A user could accidentally
# put a secret in here so we need to make sure we filter invalid values.
next unless ALLOWED_TARGETS.include?(value)
count = variables.count count = variables.count
self.new(recorded_at: recorded_at, platform_target: target, count: count) self.new(recorded_at: recorded_at, platform_target: value, count: count)
end end.compact
bulk_insert!(counts, validate: true) bulk_insert!(counts, validate: true)
end end
......
---
title: Filter the values for deployment platform metrics
merge_request: 42116
author:
type: changed
...@@ -46,44 +46,45 @@ RSpec.describe CiPlatformMetric do ...@@ -46,44 +46,45 @@ RSpec.describe CiPlatformMetric do
it 'inserts platform target counts for that day' do it 'inserts platform target counts for that day' do
Timecop.freeze(today) do Timecop.freeze(today) do
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'aws') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'aws') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'fargate') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'fargate') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'fargate') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
described_class.insert_auto_devops_platform_targets! described_class.insert_auto_devops_platform_targets!
end end
Timecop.freeze(tomorrow) do Timecop.freeze(tomorrow) do
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'fargate') create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
described_class.insert_auto_devops_platform_targets! described_class.insert_auto_devops_platform_targets!
end end
expect(platform_target_counts_by_day).to eq({ expect(platform_target_counts_by_day).to eq({
today.to_date => { 'aws' => 2, 'fargate' => 3 }, today.to_date => { 'ECS' => 2, 'FARGATE' => 3 },
tomorrow.to_date => { 'aws' => 2, 'fargate' => 4 } tomorrow.to_date => { 'ECS' => 2, 'FARGATE' => 4 }
}) })
end end
end end
context 'when there are ci variable values too long for platform_target' do context 'when there are invalid ci variable values for platform_target' do
let(:today) { Time.zone.local(1982, 4, 24) } let(:today) { Time.zone.local(1982, 4, 24) }
it 'truncates those values' do it 'ignores those values' do
max = described_class::PLATFORM_TARGET_MAX_LENGTH
Timecop.freeze(today) do Timecop.freeze(today) do
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'F' * (max + 1)) create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FOO')
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'BAR')
described_class.insert_auto_devops_platform_targets! described_class.insert_auto_devops_platform_targets!
end end
expect(platform_target_counts_by_day).to eq({ expect(platform_target_counts_by_day).to eq({
today.to_date => { 'F' * max => 1 } today.to_date => { 'ECS' => 1 }
}) })
end end
end end
context 'when there are no platform target variables' do context 'when there are no platform target variables' do
it 'does not generate any new platform metrics' do it 'does not generate any new platform metrics' do
create(:ci_variable, key: 'KEY_WHATEVER', value: 'aws') create(:ci_variable, key: 'KEY_WHATEVER', value: 'ECS')
described_class.insert_auto_devops_platform_targets! described_class.insert_auto_devops_platform_targets!
expect(platform_target_counts_by_day).to eq({}) expect(platform_target_counts_by_day).to eq({})
......
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