Commit f7fda715 authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Dmytro Zaporozhets (DZ)

Process prometheus-formatted alerts via HTTP integrations

parent 77e068e2
...@@ -17,10 +17,10 @@ module Projects ...@@ -17,10 +17,10 @@ module Projects
SUPPORTED_VERSION = '4' SUPPORTED_VERSION = '4'
def execute(token, _integration = nil) def execute(token, integration = nil)
return bad_request unless valid_payload_size? return bad_request unless valid_payload_size?
return unprocessable_entity unless self.class.processable?(params) return unprocessable_entity unless self.class.processable?(params)
return unauthorized unless valid_alert_manager_token?(token) return unauthorized unless valid_alert_manager_token?(token, integration)
process_prometheus_alerts process_prometheus_alerts
...@@ -53,9 +53,9 @@ module Projects ...@@ -53,9 +53,9 @@ module Projects
params['alerts'] params['alerts']
end end
def valid_alert_manager_token?(token) def valid_alert_manager_token?(token, integration)
valid_for_manual?(token) || valid_for_manual?(token) ||
valid_for_alerts_endpoint?(token) || valid_for_alerts_endpoint?(token, integration) ||
valid_for_managed?(token) valid_for_managed?(token)
end end
...@@ -70,11 +70,10 @@ module Projects ...@@ -70,11 +70,10 @@ module Projects
end end
end end
def valid_for_alerts_endpoint?(token) def valid_for_alerts_endpoint?(token, integration)
return false unless project.alerts_service_activated? return false unless integration&.active?
# Here we are enforcing the existence of the token compare_token(token, integration.token)
compare_token(token, project.alerts_service.token)
end end
def valid_for_managed?(token) def valid_for_managed?(token)
......
---
title: Handle prometheus-formatted alert notifications through HTTP integrations
merge_request: 49268
author:
type: fixed
...@@ -11,6 +11,10 @@ FactoryBot.define do ...@@ -11,6 +11,10 @@ FactoryBot.define do
active { false } active { false }
end end
trait :active do
active { true }
end
trait :legacy do trait :legacy do
endpoint_identifier { 'legacy' } endpoint_identifier { 'legacy' }
end end
......
...@@ -138,10 +138,10 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -138,10 +138,10 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end end
end end
context 'with generic alerts integration' do context 'with HTTP integration' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:alerts_service, :token, :result) do where(:active, :token, :result) do
:active | :valid | :success :active | :valid | :success
:active | :invalid | :failure :active | :invalid | :failure
:active | nil | :failure :active | nil | :failure
...@@ -150,15 +150,12 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do ...@@ -150,15 +150,12 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end end
with_them do with_them do
let(:valid) { project.alerts_service.token } let(:valid) { integration.token }
let(:invalid) { 'invalid token' } let(:invalid) { 'invalid token' }
let(:token_input) { public_send(token) if token } let(:token_input) { public_send(token) if token }
let(:integration) { create(:alert_management_http_integration, active, project: project) if active }
before do let(:subject) { service.execute(token_input, integration) }
if alerts_service
create(:alerts_service, alerts_service, project: project)
end
end
case result = params[:result] case result = params[:result]
when :success when :success
......
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