debian-mirror-gitlab/app/controllers/admin/application_settings_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

336 lines
12 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class Admin::ApplicationSettingsController < Admin::ApplicationController
2018-12-05 23:21:45 +05:30
include InternalRedirect
2021-09-30 23:02:18 +05:30
include IntegrationsHelper
2019-09-04 21:01:54 +05:30
2020-03-13 15:44:24 +05:30
# NOTE: Use @application_setting in this controller when you need to access
# application_settings after it has been modified. This is because the
2020-05-24 23:13:21 +05:30
# ApplicationSetting model uses Gitlab::ProcessMemoryCache for caching and the
2020-03-13 15:44:24 +05:30
# cache might be stale immediately after an update.
# https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/30233
2020-04-22 19:07:51 +05:30
before_action :set_application_setting, except: :integrations
2020-03-13 15:44:24 +05:30
2021-04-29 21:17:54 +05:30
before_action :disable_query_limiting, only: [:usage_data]
2020-06-23 00:09:42 +05:30
2023-05-27 22:25:52 +05:30
before_action do
push_frontend_feature_flag(:ci_variables_pages, current_user)
end
2022-06-21 17:19:12 +05:30
feature_category :not_owned, [ # rubocop:todo Gitlab/AvoidFeatureCategoryNotOwned
2022-10-11 01:57:18 +05:30
:general, :reporting, :metrics_and_profiling, :network,
:preferences, :update, :reset_health_check_token
]
2021-01-03 14:25:43 +05:30
feature_category :metrics, [
2022-10-11 01:57:18 +05:30
:create_self_monitoring_project,
:status_create_self_monitoring_project,
:delete_self_monitoring_project,
:status_delete_self_monitoring_project
]
2022-07-23 23:45:48 +05:30
urgency :low, [
2022-10-11 01:57:18 +05:30
:create_self_monitoring_project,
:status_create_self_monitoring_project,
:delete_self_monitoring_project,
:status_delete_self_monitoring_project,
:reset_error_tracking_access_token
]
2021-01-03 14:25:43 +05:30
feature_category :source_code_management, [:repository, :clear_repository_check_states]
feature_category :continuous_integration, [:ci_cd, :reset_registration_token]
2022-07-16 23:28:13 +05:30
urgency :low, [:ci_cd, :reset_registration_token]
2022-04-04 11:22:00 +05:30
feature_category :service_ping, [:usage_data, :service_usage_data]
2021-01-03 14:25:43 +05:30
feature_category :integrations, [:integrations]
feature_category :pages, [:lets_encrypt_terms_of_service]
2022-08-13 15:12:31 +05:30
feature_category :error_tracking, [:reset_error_tracking_access_token]
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
VALID_SETTING_PANELS = %w[general repository
2019-09-30 21:07:59 +05:30
ci_cd reporting metrics_and_profiling
2023-03-04 22:38:38 +05:30
network preferences].freeze
2018-12-05 23:21:45 +05:30
2020-03-13 15:44:24 +05:30
# The current size of a sidekiq job's jid is 24 characters. The size of the
# jid is an internal detail of Sidekiq, and they do not guarantee that it'll
# stay the same. We chose 50 to give us room in case the size of the jid
# increases. The jid is alphanumeric, so 50 is very generous. There is a spec
# that ensures that the constant value is more than the size of an actual jid.
PARAM_JOB_ID_MAX_SIZE = 50
2019-12-04 20:38:33 +05:30
VALID_SETTING_PANELS.each do |action|
define_method(action) { perform_update if submitted? }
2018-12-05 23:21:45 +05:30
end
2020-04-22 19:07:51 +05:30
def integrations
2021-01-03 14:25:43 +05:30
return not_found unless instance_level_integrations?
2021-06-08 01:23:25 +05:30
@integrations = Integration.find_or_initialize_all_non_project_specific(Integration.for_instance).sort_by(&:title)
2020-04-22 19:07:51 +05:30
end
2022-04-04 11:22:00 +05:30
def service_usage_data
2022-07-16 23:28:13 +05:30
@service_ping_data_present = prerecorded_service_ping_data.present?
2022-04-04 11:22:00 +05:30
end
2015-04-26 12:48:37 +05:30
def update
2019-09-30 21:07:59 +05:30
perform_update
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def usage_data
respond_to do |format|
format.html do
2022-07-16 23:28:13 +05:30
usage_data_json = Gitlab::Json.pretty_generate(service_ping_data)
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
render html: Gitlab::Highlight.highlight('payload.json', usage_data_json, language: 'json')
2017-08-17 22:00:37 +05:30
end
2022-05-07 20:08:51 +05:30
format.json do
Gitlab::UsageDataCounters::ServiceUsageDataCounter.count(:download_payload_click)
2023-01-13 00:05:48 +05:30
render json: Gitlab::Json.dump(service_ping_data)
2022-05-07 20:08:51 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
def reset_registration_token
2022-05-07 20:08:51 +05:30
::Ci::Runners::ResetRegistrationTokenService.new(@application_setting, current_user).execute
2018-12-05 23:21:45 +05:30
2019-07-07 11:18:12 +05:30
flash[:notice] = _('New runners registration token has been generated!')
2015-12-23 02:04:40 +05:30
redirect_to admin_runners_path
end
2016-06-02 11:05:42 +05:30
def reset_health_check_token
@application_setting.reset_health_check_access_token!
2019-07-07 11:18:12 +05:30
flash[:notice] = _('New health check access token has been generated!')
2019-02-15 15:39:39 +05:30
redirect_back_or_default
2016-06-02 11:05:42 +05:30
end
2022-08-13 15:12:31 +05:30
def reset_error_tracking_access_token
@application_setting.reset_error_tracking_access_token!
2023-05-27 22:25:52 +05:30
redirect_to general_admin_application_settings_path, notice: _('New error tracking access token has been generated!')
2022-08-13 15:12:31 +05:30
end
2016-06-02 11:05:42 +05:30
def clear_repository_check_states
2020-03-13 15:44:24 +05:30
RepositoryCheck::ClearWorker.perform_async # rubocop:disable CodeReuse/Worker
2016-06-02 11:05:42 +05:30
redirect_to(
2020-03-13 15:44:24 +05:30
general_admin_application_settings_path,
2019-07-07 11:18:12 +05:30
notice: _('Started asynchronous removal of all repository check states.')
2016-06-02 11:05:42 +05:30
)
end
2019-09-04 21:01:54 +05:30
# Getting ToS url requires `directory` api call to Let's Encrypt
# which could result in 500 error/slow rendering on settings page
# Because of that we use separate controller action
def lets_encrypt_terms_of_service
redirect_to ::Gitlab::LetsEncrypt.terms_of_service_url
end
2020-03-13 15:44:24 +05:30
# Specs are in spec/requests/self_monitoring_project_spec.rb
def create_self_monitoring_project
2021-12-11 22:18:48 +05:30
job_id = SelfMonitoringProjectCreateWorker.with_status.perform_async # rubocop:disable CodeReuse/Worker
2020-03-13 15:44:24 +05:30
render status: :accepted, json: {
job_id: job_id,
monitor_status: status_create_self_monitoring_project_admin_application_settings_path
}
end
# Specs are in spec/requests/self_monitoring_project_spec.rb
def status_create_self_monitoring_project
job_id = params[:job_id].to_s
unless job_id.length <= PARAM_JOB_ID_MAX_SIZE
return render status: :bad_request, json: {
2023-03-17 16:20:25 +05:30
message: format(_('Parameter "job_id" cannot exceed length of %{job_id_max_size}'), job_id_max_size: PARAM_JOB_ID_MAX_SIZE)
2020-03-13 15:44:24 +05:30
}
end
if SelfMonitoringProjectCreateWorker.in_progress?(job_id) # rubocop:disable CodeReuse/Worker
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
return render status: :accepted, json: {
message: _('Job to create self-monitoring project is in progress')
}
end
2023-03-04 22:38:38 +05:30
return render status: :ok, json: self_monitoring_data if @application_setting.self_monitoring_project_id.present?
2020-03-13 15:44:24 +05:30
render status: :bad_request, json: {
message: _('Self-monitoring project does not exist. Please check logs ' \
'for any error messages')
}
end
# Specs are in spec/requests/self_monitoring_project_spec.rb
def delete_self_monitoring_project
2021-12-11 22:18:48 +05:30
job_id = SelfMonitoringProjectDeleteWorker.with_status.perform_async # rubocop:disable CodeReuse/Worker
2020-03-13 15:44:24 +05:30
render status: :accepted, json: {
job_id: job_id,
monitor_status: status_delete_self_monitoring_project_admin_application_settings_path
}
end
# Specs are in spec/requests/self_monitoring_project_spec.rb
def status_delete_self_monitoring_project
job_id = params[:job_id].to_s
unless job_id.length <= PARAM_JOB_ID_MAX_SIZE
return render status: :bad_request, json: {
2023-03-17 16:20:25 +05:30
message: format(_('Parameter "job_id" cannot exceed length of %{job_id_max_size}'), job_id_max_size: PARAM_JOB_ID_MAX_SIZE)
2020-03-13 15:44:24 +05:30
}
end
if SelfMonitoringProjectDeleteWorker.in_progress?(job_id) # rubocop:disable CodeReuse/Worker
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
return render status: :accepted, json: {
message: _('Job to delete self-monitoring project is in progress')
}
end
if @application_setting.self_monitoring_project_id.nil?
return render status: :ok, json: {
message: _('Self-monitoring project has been successfully deleted')
}
end
render status: :bad_request, json: {
message: _('Self-monitoring project was not deleted. Please check logs ' \
'for any error messages')
}
end
2015-04-26 12:48:37 +05:30
private
2020-03-13 15:44:24 +05:30
def self_monitoring_data
{
project_id: @application_setting.self_monitoring_project_id,
project_full_path: @application_setting.self_monitoring_project&.full_path
}
end
2015-04-26 12:48:37 +05:30
def set_application_setting
2019-09-30 21:07:59 +05:30
@application_setting = ApplicationSetting.current_without_cache
2020-11-24 15:15:51 +05:30
@plans = Plan.all
2015-04-26 12:48:37 +05:30
end
2021-04-29 21:17:54 +05:30
def disable_query_limiting
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/29418')
2019-09-04 21:01:54 +05:30
end
2022-05-07 20:08:51 +05:30
def application_setting_params # rubocop:disable Metrics/AbcSize
2018-05-09 12:01:36 +05:30
params[:application_setting] ||= {}
2015-09-25 12:07:36 +05:30
2018-05-09 12:01:36 +05:30
if params[:application_setting].key?(:enabled_oauth_sign_in_sources)
enabled_oauth_sign_in_sources = params[:application_setting].delete(:enabled_oauth_sign_in_sources)
enabled_oauth_sign_in_sources&.delete("")
2016-06-02 11:05:42 +05:30
2018-05-09 12:01:36 +05:30
params[:application_setting][:disabled_oauth_sign_in_sources] =
AuthHelper.button_based_providers.map(&:to_s) -
Array(enabled_oauth_sign_in_sources)
end
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +05:30
params[:application_setting][:import_sources]&.delete("")
2021-09-30 23:02:18 +05:30
params[:application_setting][:valid_runner_registrars]&.delete("")
2017-09-10 17:25:29 +05:30
params[:application_setting][:restricted_visibility_levels]&.delete("")
2021-07-02 01:05:55 +05:30
if params[:application_setting].key?(:required_instance_ci_template)
2023-03-04 22:38:38 +05:30
if params[:application_setting][:required_instance_ci_template].empty?
params[:application_setting][:required_instance_ci_template] = nil
end
2021-07-02 01:05:55 +05:30
end
2020-05-30 21:06:31 +05:30
remove_blank_params_for!(:elasticsearch_aws_secret_access_key, :eks_secret_access_key)
2021-01-29 00:20:46 +05:30
# TODO Remove domain_denylist_raw in APIv5 (See https://gitlab.com/gitlab-org/gitlab-foss/issues/67204)
params.delete(:domain_denylist_raw) if params[:domain_denylist_file]
params.delete(:domain_denylist_raw) if params[:domain_denylist]
params.delete(:domain_allowlist_raw) if params[:domain_allowlist]
2016-06-02 11:05:42 +05:30
2022-05-07 20:08:51 +05:30
if params[:application_setting].key?(:user_email_lookup_limit)
params[:application_setting][:search_rate_limit] ||= params[:application_setting][:user_email_lookup_limit]
end
2021-07-02 01:05:55 +05:30
params[:application_setting].permit(visible_application_setting_attributes)
2017-08-17 22:00:37 +05:30
end
2018-11-20 20:47:30 +05:30
def recheck_user_consent?
return false unless session[:ask_for_usage_stats_consent]
return false unless params[:application_setting]
params[:application_setting].key?(:usage_ping_enabled) || params[:application_setting].key?(:version_check_enabled)
end
2017-09-10 17:25:29 +05:30
def visible_application_setting_attributes
2019-07-07 11:18:12 +05:30
[
*::ApplicationSettingsHelper.visible_attributes,
*::ApplicationSettingsHelper.external_authorization_service_attributes,
2021-03-11 19:13:27 +05:30
*ApplicationSetting.kroki_formats_attributes.keys.map { |key| "kroki_formats_#{key}".to_sym },
2019-09-30 21:07:59 +05:30
:lets_encrypt_notification_email,
:lets_encrypt_terms_of_service_accepted,
2021-01-29 00:20:46 +05:30
:domain_denylist_file,
2019-10-12 21:52:04 +05:30
:raw_blob_request_limit,
2020-04-22 19:07:51 +05:30
:issues_create_limit,
2021-03-11 19:13:27 +05:30
:notes_create_limit,
2022-07-16 23:28:13 +05:30
:pipeline_limit_per_project_user_sha,
2020-07-28 23:09:34 +05:30
:default_branch_name,
2017-08-17 22:00:37 +05:30
disabled_oauth_sign_in_sources: [],
import_sources: [],
2021-04-17 20:07:23 +05:30
restricted_visibility_levels: [],
2021-09-30 23:02:18 +05:30
repository_storages_weighted: {},
valid_runner_registrars: []
2017-08-17 22:00:37 +05:30
]
2015-04-26 12:48:37 +05:30
end
2019-07-31 22:56:46 +05:30
2019-09-30 21:07:59 +05:30
def submitted?
request.patch?
end
2019-07-31 22:56:46 +05:30
2019-09-30 21:07:59 +05:30
def perform_update
2021-09-04 01:27:46 +05:30
successful = ::ApplicationSettings::UpdateService
2019-09-30 21:07:59 +05:30
.new(@application_setting, current_user, application_setting_params)
.execute
2023-03-04 22:38:38 +05:30
session[:ask_for_usage_stats_consent] = current_user.requires_usage_stats_consent? if recheck_user_consent?
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
redirect_path = referer_path(request) || general_admin_application_settings_path
2019-09-30 21:07:59 +05:30
respond_to do |format|
if successful
format.json { head :ok }
format.html { redirect_to redirect_path, notice: _('Application settings saved successfully') }
else
format.json { head :bad_request }
format.html { render_update_error }
end
end
end
def render_update_error
2019-12-21 20:55:43 +05:30
action = valid_setting_panels.include?(action_name) ? action_name : :general
2019-09-30 21:07:59 +05:30
2020-04-08 14:13:33 +05:30
flash[:alert] = _('Application settings update failed')
2019-09-30 21:07:59 +05:30
render action
2019-07-31 22:56:46 +05:30
end
2019-12-21 20:55:43 +05:30
2020-05-30 21:06:31 +05:30
def remove_blank_params_for!(*keys)
params[:application_setting].delete_if { |setting, value| setting.to_sym.in?(keys) && value.blank? }
end
2019-12-21 20:55:43 +05:30
# overridden in EE
def valid_setting_panels
VALID_SETTING_PANELS
end
2022-07-16 23:28:13 +05:30
def service_ping_data
prerecorded_service_ping_data || Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values)
end
def prerecorded_service_ping_data
Rails.cache.fetch(Gitlab::Usage::ServicePingReport::CACHE_KEY) || ::RawUsageData.for_current_reporting_cycle.first&.payload
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Admin::ApplicationSettingsController.prepend_mod_with('Admin::ApplicationSettingsController')