2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
class GraphqlController < ApplicationController
|
2021-10-07 11:12:00 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
# Unauthenticated users have access to the API for public data
|
|
|
|
skip_before_action :authenticate_user!
|
2020-05-12 14:07:54 -04:00
|
|
|
|
2021-03-16 14:11:53 -04:00
|
|
|
# Header can be passed by tests to disable SQL query limits.
|
|
|
|
DISABLE_SQL_QUERY_LIMIT_HEADER = 'HTTP_X_GITLAB_DISABLE_SQL_QUERY_LIMIT'
|
2021-03-03 07:11:16 -05:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
# Max size of the query text in characters
|
|
|
|
MAX_QUERY_SIZE = 10_000
|
|
|
|
|
2020-05-12 14:07:54 -04:00
|
|
|
# If a user is using their session to access GraphQL, we need to have session
|
|
|
|
# storage, since the admin-mode check is session wide.
|
|
|
|
# We can't enable this for anonymous users because that would cause users using
|
|
|
|
# enforced SSO from using an auth token to access the API.
|
|
|
|
skip_around_action :set_session_storage, unless: :current_user
|
2019-03-03 07:53:03 -05:00
|
|
|
|
|
|
|
# Allow missing CSRF tokens, this would mean that if a CSRF is invalid or missing,
|
|
|
|
# the user won't be authenticated but can proceed as an anonymous user.
|
|
|
|
#
|
|
|
|
# If a CSRF is valid, the user is authenticated. This makes it easier to play
|
|
|
|
# around in GraphiQL.
|
|
|
|
protect_from_forgery with: :null_session, only: :execute
|
2017-08-16 09:04:41 -04:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
# must come first: current_user is set up here
|
2019-03-03 07:53:03 -05:00
|
|
|
before_action(only: [:execute]) { authenticate_sessionless_user!(:api) }
|
2021-07-01 17:08:38 -04:00
|
|
|
|
|
|
|
before_action :authorize_access_api!
|
2020-05-14 11:08:14 -04:00
|
|
|
before_action :set_user_last_activity
|
2021-02-14 04:09:06 -05:00
|
|
|
before_action :track_vs_code_usage
|
2022-02-01 16:12:02 -05:00
|
|
|
before_action :track_jetbrains_usage
|
2022-03-31 02:08:34 -04:00
|
|
|
before_action :track_gitlab_cli_usage
|
2021-03-16 14:11:53 -04:00
|
|
|
before_action :disable_query_limiting
|
2021-12-06 22:12:22 -05:00
|
|
|
before_action :limit_query_size
|
2017-08-16 09:04:41 -04:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
before_action :disallow_mutations_for_get
|
|
|
|
|
2020-03-02 07:07:57 -05:00
|
|
|
# Since we deactivate authentication from the main ApplicationController and
|
|
|
|
# defer it to :authorize_access_api!, we need to override the bypass session
|
|
|
|
# callback execution order here
|
|
|
|
around_action :sessionless_bypass_admin_mode!, if: :sessionless_user?
|
|
|
|
|
2021-10-07 11:12:00 -04:00
|
|
|
# The default feature category is overridden to read from request
|
2022-04-05 08:10:23 -04:00
|
|
|
feature_category :not_owned # rubocop:todo Gitlab/AvoidFeatureCategoryNotOwned
|
2020-10-06 08:08:38 -04:00
|
|
|
|
2022-01-18 16:16:02 -05:00
|
|
|
# We don't know what the query is going to be, so we can't set a high urgency
|
|
|
|
# See https://gitlab.com/groups/gitlab-org/-/epics/5841 for the work that will
|
|
|
|
# allow us to specify an urgency per query.
|
|
|
|
# Currently, all queries have a default urgency. And this is measured in the `graphql_queries`
|
|
|
|
# SLI. But queries could be multiplexed, so the total duration could be longer.
|
|
|
|
urgency :low, [:execute]
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
def execute
|
2019-05-09 05:27:07 -04:00
|
|
|
result = multiplex? ? execute_multiplex : execute_query
|
2017-08-16 09:04:41 -04:00
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2018-05-21 07:42:07 -04:00
|
|
|
rescue_from StandardError do |exception|
|
2021-12-13 07:12:59 -05:00
|
|
|
@exception_object = exception
|
|
|
|
|
2018-05-21 07:42:07 -04:00
|
|
|
log_exception(exception)
|
|
|
|
|
2020-11-27 04:09:31 -05:00
|
|
|
if Rails.env.test? || Rails.env.development?
|
|
|
|
render_error("Internal server error: #{exception.message}")
|
|
|
|
else
|
|
|
|
render_error("Internal server error")
|
|
|
|
end
|
2018-05-21 07:42:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from Gitlab::Graphql::Variables::Invalid do |exception|
|
|
|
|
render_error(exception.message, status: :unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
2019-07-29 14:36:35 -04:00
|
|
|
rescue_from Gitlab::Graphql::Errors::ArgumentError do |exception|
|
|
|
|
render_error(exception.message, status: :unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
2020-10-07 23:08:39 -04:00
|
|
|
rescue_from ::GraphQL::CoercionError do |exception|
|
|
|
|
render_error(exception.message, status: :unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
2021-10-07 11:12:00 -04:00
|
|
|
override :feature_category
|
|
|
|
def feature_category
|
|
|
|
::Gitlab::FeatureCategories.default.from_request(request) || super
|
|
|
|
end
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
private
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
def disallow_mutations_for_get
|
|
|
|
return unless request.get? || request.head?
|
|
|
|
return unless any_mutating_query?
|
|
|
|
|
|
|
|
raise ::Gitlab::Graphql::Errors::ArgumentError, "Mutations are forbidden in #{request.request_method} requests"
|
|
|
|
end
|
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
def limit_query_size
|
|
|
|
total_size = if multiplex?
|
|
|
|
params[:_json].sum { _1[:query].size }
|
|
|
|
else
|
|
|
|
query.size
|
|
|
|
end
|
|
|
|
|
|
|
|
raise ::Gitlab::Graphql::Errors::ArgumentError, "Query too large" if total_size > MAX_QUERY_SIZE
|
|
|
|
end
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
def any_mutating_query?
|
|
|
|
if multiplex?
|
|
|
|
multiplex_queries.any? { |q| mutation?(q[:query], q[:operation_name]) }
|
|
|
|
else
|
|
|
|
mutation?(query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mutation?(query_string, operation_name = params[:operationName])
|
|
|
|
::GraphQL::Query.new(GitlabSchema, query_string, operation_name: operation_name).mutation?
|
|
|
|
end
|
|
|
|
|
2021-03-16 14:11:53 -04:00
|
|
|
# Tests may mark some GraphQL queries as exempt from SQL query limits
|
|
|
|
def disable_query_limiting
|
|
|
|
return unless Gitlab::QueryLimiting.enabled_for_env?
|
2021-03-03 07:11:16 -05:00
|
|
|
|
2021-03-16 14:11:53 -04:00
|
|
|
disable_issue = request.headers[DISABLE_SQL_QUERY_LIMIT_HEADER]
|
|
|
|
return unless disable_issue
|
|
|
|
|
|
|
|
Gitlab::QueryLimiting.disable!(disable_issue)
|
2021-03-03 07:11:16 -05:00
|
|
|
end
|
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
def set_user_last_activity
|
|
|
|
return unless current_user
|
|
|
|
|
|
|
|
Users::ActivityService.new(current_user).execute
|
|
|
|
end
|
|
|
|
|
2021-02-14 04:09:06 -05:00
|
|
|
def track_vs_code_usage
|
2021-03-03 07:11:16 -05:00
|
|
|
Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter
|
|
|
|
.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
|
2021-02-14 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2022-02-01 16:12:02 -05:00
|
|
|
def track_jetbrains_usage
|
|
|
|
Gitlab::UsageDataCounters::JetBrainsPluginActivityUniqueCounter
|
|
|
|
.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
|
|
|
|
end
|
|
|
|
|
2022-03-31 02:08:34 -04:00
|
|
|
def track_gitlab_cli_usage
|
|
|
|
Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter
|
|
|
|
.track_api_request_when_trackable(user_agent: request.user_agent, user: current_user)
|
|
|
|
end
|
|
|
|
|
2019-05-09 05:27:07 -04:00
|
|
|
def execute_multiplex
|
|
|
|
GitlabSchema.multiplex(multiplex_queries, context: context)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_query
|
|
|
|
variables = build_variables(params[:variables])
|
|
|
|
operation_name = params[:operationName]
|
|
|
|
|
|
|
|
GitlabSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def query
|
2021-12-06 22:12:22 -05:00
|
|
|
params.fetch(:query, '')
|
2019-05-09 05:27:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def multiplex_queries
|
|
|
|
params[:_json].map do |single_query_info|
|
|
|
|
{
|
|
|
|
query: single_query_info[:query],
|
|
|
|
variables: build_variables(single_query_info[:variables]),
|
2019-05-28 09:41:57 -04:00
|
|
|
operation_name: single_query_info[:operationName],
|
|
|
|
context: context
|
2019-05-09 05:27:07 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 05:10:03 -04:00
|
|
|
# When modifying the context, also update GraphqlChannel#context if needed
|
|
|
|
# so that we have similar context when executing queries, mutations, and subscriptions
|
2019-05-09 05:27:07 -04:00
|
|
|
def context
|
2021-04-28 11:09:35 -04:00
|
|
|
api_user = !!sessionless_user?
|
|
|
|
@context ||= {
|
|
|
|
current_user: current_user,
|
|
|
|
is_sessionless_user: api_user,
|
|
|
|
request: request,
|
|
|
|
scope_validator: ::Gitlab::Auth::ScopeValidator.new(api_user, request_authenticator)
|
|
|
|
}
|
2019-05-09 05:27:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_variables(variable_info)
|
|
|
|
Gitlab::Graphql::Variables.new(variable_info).to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def multiplex?
|
|
|
|
params[:_json].present?
|
|
|
|
end
|
|
|
|
|
2019-03-27 10:59:02 -04:00
|
|
|
def authorize_access_api!
|
2021-07-01 17:08:38 -04:00
|
|
|
return if can?(current_user, :access_api)
|
|
|
|
|
|
|
|
render_error('API not accessible for user', status: :forbidden)
|
2019-03-27 10:59:02 -04:00
|
|
|
end
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
# Overridden from the ApplicationController to make the response look like
|
|
|
|
# a GraphQL response. That is nicely picked up in Graphiql.
|
|
|
|
def render_404
|
2018-05-21 07:42:07 -04:00
|
|
|
render_error("Not found!", status: :not_found)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_error(message, status: 500)
|
|
|
|
error = { errors: [message: message] }
|
2017-08-16 09:04:41 -04:00
|
|
|
|
2018-05-21 07:42:07 -04:00
|
|
|
render json: error, status: status
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
2020-09-13 23:09:21 -04:00
|
|
|
|
|
|
|
def append_info_to_payload(payload)
|
|
|
|
super
|
|
|
|
|
|
|
|
# Merging to :metadata will ensure these are logged as top level keys
|
|
|
|
payload[:metadata] ||= {}
|
2021-12-13 07:12:59 -05:00
|
|
|
payload[:metadata][:graphql] = logs
|
|
|
|
|
|
|
|
payload[:exception_object] = @exception_object if @exception_object
|
2020-10-06 08:08:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def logs
|
2021-03-23 14:09:05 -04:00
|
|
|
RequestStore.store[:graphql_logs].to_a
|
2020-09-13 23:09:21 -04:00
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|