debian-mirror-gitlab/app/models/grafana_integration.rb

53 lines
1.1 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
class GrafanaIntegration < ApplicationRecord
belongs_to :project
attr_encrypted :token,
mode: :per_attribute_iv,
algorithm: 'aes-256-gcm',
key: Settings.attr_encrypted_db_key_base_32
2020-02-01 01:16:34 +05:30
before_validation :check_token_changes
2019-12-21 20:55:43 +05:30
validates :grafana_url,
length: { maximum: 1024 },
addressable_url: { enforce_sanitization: true, ascii_only: true }
2020-02-01 01:16:34 +05:30
validates :encrypted_token, :project, presence: true
2019-12-21 20:55:43 +05:30
2019-12-26 22:10:19 +05:30
validates :enabled, inclusion: { in: [true, false] }
scope :enabled, -> { where(enabled: true) }
2019-12-21 20:55:43 +05:30
def client
2019-12-26 22:10:19 +05:30
return unless enabled?
2019-12-21 20:55:43 +05:30
@client ||= ::Grafana::Client.new(api_url: grafana_url.chomp('/'), token: token)
end
2020-02-01 01:16:34 +05:30
def masked_token
mask(encrypted_token)
end
def masked_token_was
mask(encrypted_token_was)
end
private
def token
decrypt(:token, encrypted_token)
end
def check_token_changes
return unless [encrypted_token_was, masked_token_was].include?(token)
clear_attribute_changes [:token, :encrypted_token, :encrypted_token_iv]
end
def mask(token)
token&.squish&.gsub(/./, '*')
end
2019-12-21 20:55:43 +05:30
end