Commit bff2c004 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '210530-move-Prometheus-alert_manager_token-to-ce' into 'master'

Move Prometheus#alert_manager_token to CE

Closes #210530

See merge request gitlab-org/gitlab!27407
parents 105ebb1f 7ebe98ff
...@@ -17,6 +17,11 @@ module Clusters ...@@ -17,6 +17,11 @@ module Clusters
default_value_for :version, VERSION default_value_for :version, VERSION
attr_encrypted :alert_manager_token,
mode: :per_attribute_iv,
key: Settings.attr_encrypted_db_key_base_truncated,
algorithm: 'aes-256-gcm'
after_destroy do after_destroy do
run_after_commit do run_after_commit do
disable_prometheus_integration disable_prometheus_integration
...@@ -103,8 +108,18 @@ module Clusters ...@@ -103,8 +108,18 @@ module Clusters
false false
end end
def generate_alert_manager_token!
unless alert_manager_token.present?
update!(alert_manager_token: generate_token)
end
end
private private
def generate_token
SecureRandom.hex
end
def disable_prometheus_integration def disable_prometheus_integration
::Clusters::Applications::DeactivateServiceWorker ::Clusters::Applications::DeactivateServiceWorker
.perform_async(cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass .perform_async(cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass
......
...@@ -9,11 +9,6 @@ module EE ...@@ -9,11 +9,6 @@ module EE
extend ActiveSupport::Concern extend ActiveSupport::Concern
prepended do prepended do
attr_encrypted :alert_manager_token,
mode: :per_attribute_iv,
key: Settings.attr_encrypted_db_key_base_truncated,
algorithm: 'aes-256-gcm'
state_machine :status do state_machine :status do
after_transition any => :updating do |application| after_transition any => :updating do |application|
application.update(last_update_started_at: Time.now) application.update(last_update_started_at: Time.now)
...@@ -26,18 +21,6 @@ module EE ...@@ -26,18 +21,6 @@ module EE
last_update_started_at > timestamp && last_update_started_at > timestamp &&
!update_errored? !update_errored?
end end
def generate_alert_manager_token!
unless alert_manager_token.present?
update!(alert_manager_token: generate_token)
end
end
private
def generate_token
SecureRandom.hex
end
end end
end end
end end
......
...@@ -47,46 +47,4 @@ describe Clusters::Applications::Prometheus do ...@@ -47,46 +47,4 @@ describe Clusters::Applications::Prometheus do
end end
end end
end end
describe 'alert manager token' do
subject { create(:clusters_applications_prometheus) }
context 'when not set' do
it 'is empty by default' do
expect(subject.alert_manager_token).to be_nil
expect(subject.encrypted_alert_manager_token).to be_nil
expect(subject.encrypted_alert_manager_token_iv).to be_nil
end
describe '#generate_alert_manager_token!' do
it 'generates a token' do
subject.generate_alert_manager_token!
expect(subject.alert_manager_token).to match(/\A\h{32}\z/)
end
end
end
context 'when set' do
let(:token) { SecureRandom.hex }
before do
subject.update!(alert_manager_token: token)
end
it 'reads the token' do
expect(subject.alert_manager_token).to eq(token)
expect(subject.encrypted_alert_manager_token).not_to be_nil
expect(subject.encrypted_alert_manager_token_iv).not_to be_nil
end
describe '#generate_alert_manager_token!' do
it 'does not re-generate the token' do
subject.generate_alert_manager_token!
expect(subject.alert_manager_token).to eq(token)
end
end
end
end
end end
...@@ -330,4 +330,46 @@ describe Clusters::Applications::Prometheus do ...@@ -330,4 +330,46 @@ describe Clusters::Applications::Prometheus do
it { is_expected.to be_falsy } it { is_expected.to be_falsy }
end end
end end
describe 'alert manager token' do
subject { create(:clusters_applications_prometheus) }
context 'when not set' do
it 'is empty by default' do
expect(subject.alert_manager_token).to be_nil
expect(subject.encrypted_alert_manager_token).to be_nil
expect(subject.encrypted_alert_manager_token_iv).to be_nil
end
describe '#generate_alert_manager_token!' do
it 'generates a token' do
subject.generate_alert_manager_token!
expect(subject.alert_manager_token).to match(/\A\h{32}\z/)
end
end
end
context 'when set' do
let(:token) { SecureRandom.hex }
before do
subject.update!(alert_manager_token: token)
end
it 'reads the token' do
expect(subject.alert_manager_token).to eq(token)
expect(subject.encrypted_alert_manager_token).not_to be_nil
expect(subject.encrypted_alert_manager_token_iv).not_to be_nil
end
describe '#generate_alert_manager_token!' do
it 'does not re-generate the token' do
subject.generate_alert_manager_token!
expect(subject.alert_manager_token).to eq(token)
end
end
end
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