2020-03-25 14:08:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Prometheus
|
|
|
|
module Alerts
|
2020-12-16 13:10:10 -05:00
|
|
|
class NotifyService
|
2020-03-25 14:08:10 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-07-13 17:09:24 -04:00
|
|
|
include ::IncidentManagement::Settings
|
2020-03-25 14:08:10 -04:00
|
|
|
|
2020-06-09 14:08:28 -04:00
|
|
|
# This set of keys identifies a payload as a valid Prometheus
|
|
|
|
# payload and thus processable by this service. See also
|
|
|
|
# https://prometheus.io/docs/alerting/configuration/#webhook_config
|
|
|
|
REQUIRED_PAYLOAD_KEYS = %w[
|
|
|
|
version groupKey status receiver groupLabels commonLabels
|
|
|
|
commonAnnotations externalURL alerts
|
|
|
|
].to_set.freeze
|
|
|
|
|
|
|
|
SUPPORTED_VERSION = '4'
|
|
|
|
|
2020-12-16 13:10:10 -05:00
|
|
|
def initialize(project, payload)
|
|
|
|
@project = project
|
|
|
|
@payload = payload
|
|
|
|
end
|
|
|
|
|
2020-12-08 07:09:53 -05:00
|
|
|
def execute(token, integration = nil)
|
2020-04-07 11:09:30 -04:00
|
|
|
return bad_request unless valid_payload_size?
|
2020-12-16 13:10:10 -05:00
|
|
|
return unprocessable_entity unless self.class.processable?(payload)
|
2020-12-08 07:09:53 -05:00
|
|
|
return unauthorized unless valid_alert_manager_token?(token, integration)
|
2020-03-25 14:08:10 -04:00
|
|
|
|
2020-05-07 11:09:29 -04:00
|
|
|
process_prometheus_alerts
|
2020-03-25 14:08:10 -04:00
|
|
|
|
2020-04-07 11:09:30 -04:00
|
|
|
ServiceResponse.success
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
|
2020-12-16 13:10:10 -05:00
|
|
|
def self.processable?(payload)
|
2020-06-09 14:08:28 -04:00
|
|
|
# Workaround for https://gitlab.com/gitlab-org/gitlab/-/issues/220496
|
2020-12-16 13:10:10 -05:00
|
|
|
return false unless payload
|
2020-06-09 14:08:28 -04:00
|
|
|
|
2020-12-16 13:10:10 -05:00
|
|
|
REQUIRED_PAYLOAD_KEYS.subset?(payload.keys.to_set) &&
|
|
|
|
payload['version'] == SUPPORTED_VERSION
|
2020-06-09 14:08:28 -04:00
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
private
|
|
|
|
|
2020-12-16 13:10:10 -05:00
|
|
|
attr_reader :project, :payload
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def valid_payload_size?
|
2020-12-16 13:10:10 -05:00
|
|
|
Gitlab::Utils::DeepSize.new(payload).valid?
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def firings
|
|
|
|
@firings ||= alerts_by_status('firing')
|
|
|
|
end
|
|
|
|
|
|
|
|
def alerts_by_status(status)
|
|
|
|
alerts.select { |alert| alert['status'] == status }
|
|
|
|
end
|
|
|
|
|
|
|
|
def alerts
|
2020-12-16 13:10:10 -05:00
|
|
|
payload['alerts']
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
|
2020-12-08 07:09:53 -05:00
|
|
|
def valid_alert_manager_token?(token, integration)
|
2020-06-09 14:08:28 -04:00
|
|
|
valid_for_manual?(token) ||
|
2020-12-08 07:09:53 -05:00
|
|
|
valid_for_alerts_endpoint?(token, integration) ||
|
2020-06-09 14:08:28 -04:00
|
|
|
valid_for_managed?(token)
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_for_manual?(token)
|
|
|
|
prometheus = project.find_or_initialize_service('prometheus')
|
|
|
|
return false unless prometheus.manual_configuration?
|
|
|
|
|
|
|
|
if setting = project.alerting_setting
|
|
|
|
compare_token(token, setting.token)
|
|
|
|
else
|
|
|
|
token.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-08 07:09:53 -05:00
|
|
|
def valid_for_alerts_endpoint?(token, integration)
|
|
|
|
return false unless integration&.active?
|
2020-06-09 14:08:28 -04:00
|
|
|
|
2020-12-08 07:09:53 -05:00
|
|
|
compare_token(token, integration.token)
|
2020-06-09 14:08:28 -04:00
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def valid_for_managed?(token)
|
|
|
|
prometheus_application = available_prometheus_application(project)
|
|
|
|
return false unless prometheus_application
|
|
|
|
|
|
|
|
if token
|
|
|
|
compare_token(token, prometheus_application.alert_manager_token)
|
|
|
|
else
|
|
|
|
prometheus_application.alert_manager_token.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_prometheus_application(project)
|
|
|
|
alert_id = gitlab_alert_id
|
|
|
|
return unless alert_id
|
|
|
|
|
|
|
|
alert = find_alert(project, alert_id)
|
|
|
|
return unless alert
|
|
|
|
|
|
|
|
cluster = alert.environment.deployment_platform&.cluster
|
|
|
|
return unless cluster&.enabled?
|
|
|
|
return unless cluster.application_prometheus_available?
|
|
|
|
|
|
|
|
cluster.application_prometheus
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_alert(project, metric)
|
|
|
|
Projects::Prometheus::AlertsFinder
|
|
|
|
.new(project: project, metric: metric)
|
|
|
|
.execute
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_alert_id
|
|
|
|
alerts&.first&.dig('labels', 'gitlab_alert_id')
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_token(expected, actual)
|
|
|
|
return unless expected && actual
|
|
|
|
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(expected, actual)
|
|
|
|
end
|
|
|
|
|
2020-05-07 11:09:29 -04:00
|
|
|
def process_prometheus_alerts
|
|
|
|
alerts.each do |alert|
|
|
|
|
AlertManagement::ProcessPrometheusAlertService
|
2020-12-16 13:10:10 -05:00
|
|
|
.new(project, alert.to_h)
|
2020-05-07 11:09:29 -04:00
|
|
|
.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 11:09:30 -04:00
|
|
|
def bad_request
|
|
|
|
ServiceResponse.error(message: 'Bad Request', http_status: :bad_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unauthorized
|
|
|
|
ServiceResponse.error(message: 'Unauthorized', http_status: :unauthorized)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unprocessable_entity
|
|
|
|
ServiceResponse.error(message: 'Unprocessable Entity', http_status: :unprocessable_entity)
|
|
|
|
end
|
2020-03-25 14:08:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|