2020-10-02 20:08:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AlertManagement
|
|
|
|
class HttpIntegration < ApplicationRecord
|
2020-10-28 20:08:36 -04:00
|
|
|
include ::Gitlab::Routing
|
2020-10-20 23:08:45 -04:00
|
|
|
LEGACY_IDENTIFIER = 'legacy'
|
|
|
|
DEFAULT_NAME_SLUG = 'http-endpoint'
|
|
|
|
|
2020-10-02 20:08:46 -04:00
|
|
|
belongs_to :project, inverse_of: :alert_management_http_integrations
|
|
|
|
|
|
|
|
attr_encrypted :token,
|
|
|
|
mode: :per_attribute_iv,
|
2021-04-22 17:09:53 -04:00
|
|
|
key: Settings.attr_encrypted_db_key_base_32,
|
2020-10-02 20:08:46 -04:00
|
|
|
algorithm: 'aes-256-gcm'
|
|
|
|
|
2020-10-20 23:08:45 -04:00
|
|
|
default_value_for(:endpoint_identifier, allows_nil: false) { SecureRandom.hex(8) }
|
|
|
|
default_value_for(:token) { generate_token }
|
|
|
|
|
2020-10-02 20:08:46 -04:00
|
|
|
validates :project, presence: true
|
|
|
|
validates :active, inclusion: { in: [true, false] }
|
2020-10-20 23:08:45 -04:00
|
|
|
validates :token, presence: true, format: { with: /\A\h{32}\z/ }
|
2020-10-02 20:08:46 -04:00
|
|
|
validates :name, presence: true, length: { maximum: 255 }
|
2020-10-20 23:08:45 -04:00
|
|
|
validates :endpoint_identifier, presence: true, length: { maximum: 255 }, format: { with: /\A[A-Za-z0-9]+\z/ }
|
2020-10-02 20:08:46 -04:00
|
|
|
validates :endpoint_identifier, uniqueness: { scope: [:project_id, :active] }, if: :active?
|
2020-12-16 07:09:53 -05:00
|
|
|
validates :payload_attribute_mapping, json_schema: { filename: 'http_integration_payload_attribute_mapping' }
|
2020-10-02 20:08:46 -04:00
|
|
|
|
|
|
|
before_validation :prevent_token_assignment
|
2020-10-20 23:08:45 -04:00
|
|
|
before_validation :prevent_endpoint_identifier_assignment
|
2020-10-02 20:08:46 -04:00
|
|
|
before_validation :ensure_token
|
2021-03-12 04:09:06 -05:00
|
|
|
before_validation :ensure_payload_example_not_nil
|
2020-10-02 20:08:46 -04:00
|
|
|
|
2020-10-20 23:08:45 -04:00
|
|
|
scope :for_endpoint_identifier, -> (endpoint_identifier) { where(endpoint_identifier: endpoint_identifier) }
|
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
scope :ordered_by_id, -> { order(:id) }
|
|
|
|
|
|
|
|
def url
|
2020-10-28 20:08:36 -04:00
|
|
|
return project_alerts_notify_url(project, format: :json) if legacy?
|
2020-10-20 23:08:45 -04:00
|
|
|
|
2020-10-28 20:08:36 -04:00
|
|
|
project_alert_http_integration_url(project, name_slug, endpoint_identifier, format: :json)
|
2020-10-20 23:08:45 -04:00
|
|
|
end
|
|
|
|
|
2020-10-02 20:08:46 -04:00
|
|
|
private
|
|
|
|
|
2020-10-20 23:08:45 -04:00
|
|
|
def self.generate_token
|
|
|
|
SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_slug
|
|
|
|
(name && Gitlab::Utils.slugify(name)) || DEFAULT_NAME_SLUG
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy?
|
|
|
|
endpoint_identifier == LEGACY_IDENTIFIER
|
|
|
|
end
|
|
|
|
|
|
|
|
# Blank token assignment triggers token reset
|
2020-10-02 20:08:46 -04:00
|
|
|
def prevent_token_assignment
|
|
|
|
if token.present? && token_changed?
|
|
|
|
self.token = nil
|
|
|
|
self.encrypted_token = encrypted_token_was
|
|
|
|
self.encrypted_token_iv = encrypted_token_iv_was
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_token
|
2020-10-20 23:08:45 -04:00
|
|
|
self.token = self.class.generate_token if token.blank?
|
2020-10-02 20:08:46 -04:00
|
|
|
end
|
|
|
|
|
2020-10-20 23:08:45 -04:00
|
|
|
def prevent_endpoint_identifier_assignment
|
|
|
|
if endpoint_identifier_changed? && endpoint_identifier_was.present?
|
|
|
|
self.endpoint_identifier = endpoint_identifier_was
|
|
|
|
end
|
2020-10-02 20:08:46 -04:00
|
|
|
end
|
2021-03-12 04:09:06 -05:00
|
|
|
|
|
|
|
def ensure_payload_example_not_nil
|
|
|
|
self.payload_example ||= {}
|
|
|
|
end
|
2020-10-02 20:08:46 -04:00
|
|
|
end
|
|
|
|
end
|