diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-11 12:09:26 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-11 12:09:26 +0000 |
commit | c9687bdf58e9d4a9c3942f587bd4841f42e3b5de (patch) | |
tree | a60a2e20f152483be6a92bacdf10564bbc96c664 /lib | |
parent | 3f3e4bcc50a3280d03299c2c263eafd9c8e3bd7b (diff) | |
download | gitlab-ce-c9687bdf58e9d4a9c3942f587bd4841f42e3b5de.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/banzai/filter/inline_grafana_metrics_filter.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/templates/Managed-Cluster-Applications.gitlab-ci.yml | 3 | ||||
-rw-r--r-- | lib/gitlab/import_export/project/import_export.yml | 1 | ||||
-rw-r--r-- | lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb | 111 | ||||
-rw-r--r-- | lib/grafana/validator.rb | 96 |
5 files changed, 126 insertions, 87 deletions
diff --git a/lib/banzai/filter/inline_grafana_metrics_filter.rb b/lib/banzai/filter/inline_grafana_metrics_filter.rb index 60a16b164af..07bde9858e8 100644 --- a/lib/banzai/filter/inline_grafana_metrics_filter.rb +++ b/lib/banzai/filter/inline_grafana_metrics_filter.rb @@ -17,8 +17,6 @@ module Banzai def embed_params(node) query_params = Gitlab::Metrics::Dashboard::Url.parse_query(node['href']) - return unless query_params.include?(:panelId) - time_window = Grafana::TimeWindow.new(query_params[:from], query_params[:to]) url = url_with_window(node['href'], query_params, time_window.in_milliseconds) diff --git a/lib/gitlab/ci/templates/Managed-Cluster-Applications.gitlab-ci.yml b/lib/gitlab/ci/templates/Managed-Cluster-Applications.gitlab-ci.yml index ef77bbf5626..48458142f1c 100644 --- a/lib/gitlab/ci/templates/Managed-Cluster-Applications.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Managed-Cluster-Applications.gitlab-ci.yml @@ -1,6 +1,6 @@ apply: stage: deploy - image: "registry.gitlab.com/gitlab-org/cluster-integration/cluster-applications:v0.9.0" + image: "registry.gitlab.com/gitlab-org/cluster-integration/cluster-applications:v0.11.0" environment: name: production variables: @@ -15,6 +15,7 @@ apply: JUPYTERHUB_VALUES_FILE: $CI_PROJECT_DIR/.gitlab/managed-apps/jupyterhub/values.yaml PROMETHEUS_VALUES_FILE: $CI_PROJECT_DIR/.gitlab/managed-apps/prometheus/values.yaml ELASTIC_STACK_VALUES_FILE: $CI_PROJECT_DIR/.gitlab/managed-apps/elastic-stack/values.yaml + VAULT_VALUES_FILE: $CI_PROJECT_DIR/.gitlab/managed-apps/vault/values.yaml script: - gitlab-managed-apps /usr/local/share/gitlab-managed-apps/helmfile.yaml only: diff --git a/lib/gitlab/import_export/project/import_export.yml b/lib/gitlab/import_export/project/import_export.yml index b7b61df4cd6..aa6085de4f9 100644 --- a/lib/gitlab/import_export/project/import_export.yml +++ b/lib/gitlab/import_export/project/import_export.yml @@ -248,6 +248,7 @@ excluded_attributes: - :token_encrypted services: - :template + - :instance error_tracking_setting: - :encrypted_token - :encrypted_token_iv diff --git a/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb b/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb index ce75c54d014..c90c1e3f0bc 100644 --- a/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb +++ b/lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb @@ -13,12 +13,7 @@ module Gitlab # Reformats the specified panel in the Gitlab # dashboard-yml format def transform! - InputFormatValidator.new( - grafana_dashboard, - datasource, - panel, - query_params - ).validate! + validate_input! new_dashboard = formatted_dashboard @@ -28,6 +23,17 @@ module Gitlab private + def validate_input! + ::Grafana::Validator.new( + grafana_dashboard, + datasource, + panel, + query_params + ).validate! + rescue ::Grafana::Validator::Error => e + raise ::Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError, e.message + end + def formatted_dashboard { panel_groups: [{ panels: [formatted_panel] }] } end @@ -56,11 +62,25 @@ module Gitlab def panel strong_memoize(:panel) do grafana_dashboard[:dashboard][:panels].find do |panel| - panel[:id].to_s == query_params[:panelId] + query_params[:panelId] ? matching_panel?(panel) : valid_panel?(panel) end end end + # Determines whether a given panel is the one + # specified by the linked grafana url + def matching_panel?(panel) + panel[:id].to_s == query_params[:panelId] + end + + # Determines whether any given panel has the potenial + # to return valid results from grafana/prometheus + def valid_panel?(panel) + ::Grafana::Validator + .new(grafana_dashboard, datasource, panel, query_params) + .valid? + end + # Grafana url query parameters. Includes information # on which panel to select and time range. def query_params @@ -141,83 +161,6 @@ module Gitlab params[:grafana_url] end end - - class InputFormatValidator - include ::Gitlab::Metrics::Dashboard::Errors - - attr_reader :grafana_dashboard, :datasource, :panel, :query_params - - UNSUPPORTED_GRAFANA_GLOBAL_VARS = %w( - $__interval_ms - $__timeFilter - $__name - $timeFilter - $interval - ).freeze - - def initialize(grafana_dashboard, datasource, panel, query_params) - @grafana_dashboard = grafana_dashboard - @datasource = datasource - @panel = panel - @query_params = query_params - end - - def validate! - validate_query_params! - validate_datasource! - validate_panel_type! - validate_variable_definitions! - validate_global_variables! - end - - private - - def validate_datasource! - return if datasource[:access] == 'proxy' && datasource[:type] == 'prometheus' - - raise_error 'Only Prometheus datasources with proxy access in Grafana are supported.' - end - - def validate_query_params! - return if [:panelId, :from, :to].all? { |param| query_params.include?(param) } - - raise_error 'Grafana query parameters must include panelId, from, and to.' - end - - def validate_panel_type! - return if panel[:type] == 'graph' && panel[:lines] - - raise_error 'Panel type must be a line graph.' - end - - def validate_variable_definitions! - return unless grafana_dashboard[:dashboard][:templating] - - return if grafana_dashboard[:dashboard][:templating][:list].all? do |variable| - query_params[:"var-#{variable[:name]}"].present? - end - - raise_error 'All Grafana variables must be defined in the query parameters.' - end - - def validate_global_variables! - return unless panel_contains_unsupported_vars? - - raise_error 'Prometheus must not include' - end - - def panel_contains_unsupported_vars? - panel[:targets].any? do |target| - UNSUPPORTED_GRAFANA_GLOBAL_VARS.any? do |variable| - target[:expr].include?(variable) - end - end - end - - def raise_error(message) - raise DashboardProcessingError.new(message) - end - end end end end diff --git a/lib/grafana/validator.rb b/lib/grafana/validator.rb new file mode 100644 index 00000000000..760263f7ec9 --- /dev/null +++ b/lib/grafana/validator.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +# Performs checks on whether resources from Grafana can be handled +# We have certain restrictions on which formats we accept. +# Some are technical requirements, others are simplifications. +module Grafana + class Validator + Error = Class.new(StandardError) + + attr_reader :grafana_dashboard, :datasource, :panel, :query_params + + UNSUPPORTED_GRAFANA_GLOBAL_VARS = %w( + $__interval_ms + $__timeFilter + $__name + $timeFilter + $interval + ).freeze + + def initialize(grafana_dashboard, datasource, panel, query_params) + @grafana_dashboard = grafana_dashboard + @datasource = datasource + @panel = panel + @query_params = query_params + end + + def validate! + validate_query_params! + validate_panel_type! + validate_variable_definitions! + validate_global_variables! + validate_datasource! if datasource + end + + def valid? + validate! + + true + rescue ::Grafana::Validator::Error + false + end + + private + + # See defaults in Banzai::Filter::InlineGrafanaMetricsFilter. + def validate_query_params! + return if [:from, :to].all? { |param| query_params.include?(param) } + + raise_error 'Grafana query parameters must include from and to.' + end + + # We may choose to support other panel types in future. + def validate_panel_type! + return if panel && panel[:type] == 'graph' && panel[:lines] + + raise_error 'Panel type must be a line graph.' + end + + # We must require variable definitions to create valid prometheus queries. + def validate_variable_definitions! + return unless grafana_dashboard[:dashboard][:templating] + + return if grafana_dashboard[:dashboard][:templating][:list].all? do |variable| + query_params[:"var-#{variable[:name]}"].present? + end + + raise_error 'All Grafana variables must be defined in the query parameters.' + end + + # We may choose to support further Grafana variables in future. + def validate_global_variables! + return unless panel_contains_unsupported_vars? + + raise_error "Prometheus must not include #{UNSUPPORTED_GRAFANA_GLOBAL_VARS}" + end + + # We may choose to support additional datasources in future. + def validate_datasource! + return if datasource[:access] == 'proxy' && datasource[:type] == 'prometheus' + + raise_error 'Only Prometheus datasources with proxy access in Grafana are supported.' + end + + def panel_contains_unsupported_vars? + panel[:targets].any? do |target| + UNSUPPORTED_GRAFANA_GLOBAL_VARS.any? do |variable| + target[:expr].include?(variable) + end + end + end + + def raise_error(message) + raise Validator::Error, message + end + end +end |