gitlab-org--gitlab-foss/app/models/project_services/prometheus_service.rb
Reuben Pereira 50591efc75 Check validity of prometheus_service before query
Check validity before querying so that if the dns entry for the api_url
has been changed to something invalid after the model was saved and
checked for validity, it will not query. This is to solve a toctou
(time of check to time of use) issue.
2019-03-04 18:36:43 +00:00

95 lines
2 KiB
Ruby

# frozen_string_literal: true
class PrometheusService < MonitoringService
include PrometheusAdapter
# Access to prometheus is directly through the API
prop_accessor :api_url
boolean_accessor :manual_configuration
with_options presence: true, if: :manual_configuration? do
validates :api_url, public_url: true
end
before_save :synchronize_service_state
after_save :clear_reactive_cache!
def initialize_properties
if properties.nil?
self.properties = {}
end
end
def show_active_box?
false
end
def editable?
manual_configuration? || !prometheus_available?
end
def title
'Prometheus'
end
def description
s_('PrometheusService|Time-series monitoring service')
end
def self.to_param
'prometheus'
end
def fields
return [] unless editable?
[
{
type: 'checkbox',
name: 'manual_configuration',
title: s_('PrometheusService|Active'),
required: true
},
{
type: 'text',
name: 'api_url',
title: 'API URL',
placeholder: s_('PrometheusService|Prometheus API Base URL, like http://prometheus.example.com/'),
required: true
}
]
end
# Check we can connect to the Prometheus API
def test(*args)
Gitlab::PrometheusClient.new(prometheus_client).ping
{ success: true, result: 'Checked API endpoint' }
rescue Gitlab::PrometheusClient::Error => err
{ success: false, result: err }
end
def prometheus_client
RestClient::Resource.new(api_url, max_redirects: 0) if should_return_client?
end
def prometheus_available?
return false if template?
return false unless project
project.clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
end
private
def should_return_client?
api_url && manual_configuration? && active? && valid?
end
def synchronize_service_state
self.active = prometheus_available? || manual_configuration?
true
end
end