Commit 16991649 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Check alerts service token properly

Fixes https://gitlab.com/gitlab-org/gitlab/issues/14792

Check the token generated by alerts service
instead of checking with a hard-coded value
parent 248475cd
......@@ -5,11 +5,6 @@ module Projects
class NotifyService < BaseService
include Gitlab::Utils::StrongMemoize
# Prevents users to use WIP feature on private GitLab instances
# by enabling 'generic_alert_endpoint' feature manually.
# TODO: https://gitlab.com/gitlab-org/gitlab/issues/14792
DEV_TOKEN = :development_token
def execute(token)
return forbidden unless alerts_service_activated?
return unauthorized unless valid_token?(token)
......@@ -23,6 +18,8 @@ module Projects
private
delegate :alerts_service, to: :project
def generic_alert_endpoint_enabled?
Feature.enabled?(:generic_alert_endpoint, project)
end
......@@ -34,7 +31,7 @@ module Projects
def alerts_service_activated?
incident_management_available? &&
generic_alert_endpoint_enabled? &&
project.alerts_service.try(:active?)
alerts_service.try(:active?)
end
def process_incident_issues
......@@ -47,7 +44,7 @@ module Projects
end
def valid_token?(token)
token == DEV_TOKEN
token == alerts_service.token
end
def bad_request
......
......@@ -36,7 +36,7 @@ describe Projects::Alerting::NotifyService do
end
describe '#execute' do
let(:token) { :development_token }
let(:token) { 'invalid-token' }
let(:starts_at) { Time.now.change(usec: 0) }
let(:service) { described_class.new(project, nil, payload) }
let(:payload_raw) do
......@@ -63,6 +63,8 @@ describe Projects::Alerting::NotifyService do
let!(:alerts_service) { create(:alerts_service, project: project) }
context 'with valid token' do
let(:token) { alerts_service.token }
context 'with a valid payload' do
it_behaves_like 'processes incident issues', 1
end
......@@ -79,8 +81,6 @@ describe Projects::Alerting::NotifyService do
end
context 'with invalid token' do
let(:token) { 'invalid-token' }
it_behaves_like 'does not process incident issues', http_status: 401
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