2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2020-04-27 23:09:53 -04:00
|
|
|
class API < Grape::API
|
2014-12-19 09:15:29 -05:00
|
|
|
include APIGuard
|
2017-01-30 12:41:56 -05:00
|
|
|
|
2017-09-07 01:41:15 -04:00
|
|
|
LOG_FILENAME = Rails.root.join("log", "api_json.log")
|
|
|
|
|
2019-05-05 06:19:14 -04:00
|
|
|
NO_SLASH_URL_PART_REGEX = %r{[^/]+}.freeze
|
2018-11-08 07:18:17 -05:00
|
|
|
NAMESPACE_OR_PROJECT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
|
|
|
|
COMMIT_ENDPOINT_REQUIREMENTS = NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze
|
2019-01-31 05:13:23 -05:00
|
|
|
USER_REQUIREMENTS = { user_id: NO_SLASH_URL_PART_REGEX }.freeze
|
2019-07-12 15:13:28 -04:00
|
|
|
LOG_FILTERS = ::Rails.application.config.filter_parameters + [/^output$/]
|
2017-09-23 09:21:32 -04:00
|
|
|
|
2018-05-18 02:48:59 -04:00
|
|
|
insert_before Grape::Middleware::Error,
|
|
|
|
GrapeLogging::Middleware::RequestLogger,
|
|
|
|
logger: Logger.new(LOG_FILENAME),
|
|
|
|
formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
|
|
|
|
include: [
|
2019-07-12 15:13:28 -04:00
|
|
|
GrapeLogging::Loggers::FilterParameters.new(LOG_FILTERS),
|
2019-08-20 14:12:28 -04:00
|
|
|
Gitlab::GrapeLogging::Loggers::ClientEnvLogger.new,
|
2018-09-03 13:03:32 -04:00
|
|
|
Gitlab::GrapeLogging::Loggers::RouteLogger.new,
|
2018-05-26 00:00:26 -04:00
|
|
|
Gitlab::GrapeLogging::Loggers::UserLogger.new,
|
2019-11-11 10:06:42 -05:00
|
|
|
Gitlab::GrapeLogging::Loggers::ExceptionLogger.new,
|
2018-09-04 13:14:22 -04:00
|
|
|
Gitlab::GrapeLogging::Loggers::QueueDurationLogger.new,
|
2018-12-05 15:54:40 -05:00
|
|
|
Gitlab::GrapeLogging::Loggers::PerfLogger.new,
|
2020-05-15 14:07:52 -04:00
|
|
|
Gitlab::GrapeLogging::Loggers::CorrelationIdLogger.new,
|
|
|
|
Gitlab::GrapeLogging::Loggers::ContextLogger.new
|
2018-05-18 02:48:59 -04:00
|
|
|
]
|
2017-09-07 01:41:15 -04:00
|
|
|
|
2017-06-20 03:40:24 -04:00
|
|
|
allow_access_with_scope :api
|
2020-04-08 08:09:42 -04:00
|
|
|
allow_access_with_scope :read_api, if: -> (request) { request.get? }
|
2017-07-25 05:35:45 -04:00
|
|
|
prefix :api
|
2017-06-20 03:40:24 -04:00
|
|
|
|
2017-01-30 12:41:56 -05:00
|
|
|
version 'v3', using: :path do
|
2018-05-15 09:39:33 -04:00
|
|
|
route :any, '*path' do
|
|
|
|
error!('API V3 is no longer supported. Use API V4 instead.', 410)
|
|
|
|
end
|
2017-01-30 12:41:56 -05:00
|
|
|
end
|
2012-07-04 03:48:00 -04:00
|
|
|
|
2018-05-15 09:39:33 -04:00
|
|
|
version 'v4', using: :path
|
|
|
|
|
2017-11-01 05:25:49 -04:00
|
|
|
before do
|
|
|
|
header['X-Frame-Options'] = 'SAMEORIGIN'
|
|
|
|
header['X-Content-Type-Options'] = 'nosniff'
|
|
|
|
end
|
2017-05-03 22:05:38 -04:00
|
|
|
|
2020-01-03 10:08:33 -05:00
|
|
|
before do
|
|
|
|
Gitlab::ApplicationContext.push(
|
2020-03-06 13:08:08 -05:00
|
|
|
user: -> { @current_user },
|
2020-01-03 10:08:33 -05:00
|
|
|
project: -> { @project },
|
2020-01-23 10:08:46 -05:00
|
|
|
namespace: -> { @group },
|
|
|
|
caller_id: route.origin
|
2020-01-03 10:08:33 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-08-02 12:20:31 -04:00
|
|
|
# The locale is set to the current user's locale when `current_user` is loaded
|
2017-05-25 11:22:45 -04:00
|
|
|
after { Gitlab::I18n.use_default_locale }
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2016-06-23 11:14:31 -04:00
|
|
|
rescue_from Gitlab::Access::AccessDeniedError do
|
|
|
|
rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
|
|
|
|
end
|
|
|
|
|
2012-07-05 11:12:09 -04:00
|
|
|
rescue_from ActiveRecord::RecordNotFound do
|
2015-02-02 23:36:54 -05:00
|
|
|
rack_response({ 'message' => '404 Not found' }.to_json, 404)
|
2012-07-05 11:12:09 -04:00
|
|
|
end
|
|
|
|
|
2019-06-21 12:56:47 -04:00
|
|
|
rescue_from(
|
|
|
|
::ActiveRecord::StaleObjectError,
|
|
|
|
::Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
|
|
|
|
) do
|
2018-11-23 11:25:11 -05:00
|
|
|
rack_response({ 'message' => '409 Conflict: Resource lock' }.to_json, 409)
|
|
|
|
end
|
|
|
|
|
2018-04-03 12:47:33 -04:00
|
|
|
rescue_from UploadedFile::InvalidPathError do |e|
|
|
|
|
rack_response({ 'message' => e.message }.to_json, 400)
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from ObjectStorage::RemoteStoreError do |e|
|
|
|
|
rack_response({ 'message' => e.message }.to_json, 500)
|
|
|
|
end
|
|
|
|
|
2016-08-02 17:56:27 -04:00
|
|
|
# Retain 405 error rather than a 500 error for Grape 0.15.0+.
|
2017-01-04 12:24:39 -05:00
|
|
|
# https://github.com/ruby-grape/grape/blob/a3a28f5b5dfbb2797442e006dbffd750b27f2a76/UPGRADING.md#changes-to-method-not-allowed-routes
|
|
|
|
rescue_from Grape::Exceptions::MethodNotAllowed do |e|
|
|
|
|
error! e.message, e.status, e.headers
|
|
|
|
end
|
|
|
|
|
2016-08-02 17:56:27 -04:00
|
|
|
rescue_from Grape::Exceptions::Base do |e|
|
|
|
|
error! e.message, e.status, e.headers
|
2016-07-29 06:14:36 -04:00
|
|
|
end
|
|
|
|
|
2017-02-17 06:52:27 -05:00
|
|
|
rescue_from Gitlab::Auth::TooManyIps do |e|
|
2017-02-17 08:44:57 -05:00
|
|
|
rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
|
2017-02-17 06:52:27 -05:00
|
|
|
end
|
|
|
|
|
2013-02-01 04:42:02 -05:00
|
|
|
rescue_from :all do |exception|
|
2016-08-18 20:06:33 -04:00
|
|
|
handle_api_exception(exception)
|
2013-01-29 12:20:59 -05:00
|
|
|
end
|
|
|
|
|
2020-04-30 17:09:47 -04:00
|
|
|
# This is a specific exception raised by `rack-timeout` gem when Puma
|
|
|
|
# requests surpass its timeout. Given it inherits from Exception, we
|
|
|
|
# should rescue it separately. For more info, see:
|
|
|
|
# - https://github.com/sharpstone/rack-timeout/blob/master/doc/exceptions.md
|
|
|
|
# - https://github.com/ruby-grape/grape#exception-handling
|
|
|
|
rescue_from Rack::Timeout::RequestTimeoutException do |exception|
|
|
|
|
handle_api_exception(exception)
|
|
|
|
end
|
|
|
|
|
2012-06-27 07:32:56 -04:00
|
|
|
format :json
|
2014-02-18 04:40:45 -05:00
|
|
|
content_type :txt, "text/plain"
|
|
|
|
|
2016-04-15 11:35:40 -04:00
|
|
|
# Ensure the namespace is right, otherwise we might load Grape::API::Helpers
|
|
|
|
helpers ::API::Helpers
|
2017-05-04 08:11:15 -04:00
|
|
|
helpers ::API::Helpers::CommonHelpers
|
2016-04-15 11:35:40 -04:00
|
|
|
|
2020-02-10 10:08:54 -05:00
|
|
|
namespace do
|
|
|
|
after do
|
2020-04-07 17:09:46 -04:00
|
|
|
::Users::ActivityService.new(@current_user).execute
|
2020-02-10 10:08:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Keep in alphabetical order
|
|
|
|
mount ::API::AccessRequests
|
2020-05-13 17:08:55 -04:00
|
|
|
mount ::API::Admin::Ci::Variables
|
2020-03-03 10:08:08 -05:00
|
|
|
mount ::API::Admin::Sidekiq
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::Appearance
|
|
|
|
mount ::API::Applications
|
|
|
|
mount ::API::Avatar
|
|
|
|
mount ::API::AwardEmoji
|
|
|
|
mount ::API::Badges
|
|
|
|
mount ::API::Boards
|
|
|
|
mount ::API::Branches
|
|
|
|
mount ::API::BroadcastMessages
|
|
|
|
mount ::API::Commits
|
|
|
|
mount ::API::CommitStatuses
|
2020-03-30 05:07:58 -04:00
|
|
|
mount ::API::ContainerRegistryEvent
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::DeployKeys
|
2020-02-19 22:08:57 -05:00
|
|
|
mount ::API::DeployTokens
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::Deployments
|
|
|
|
mount ::API::Environments
|
|
|
|
mount ::API::ErrorTracking
|
|
|
|
mount ::API::Events
|
|
|
|
mount ::API::Features
|
|
|
|
mount ::API::Files
|
2020-05-15 08:08:28 -04:00
|
|
|
mount ::API::FreezePeriods
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::GroupBoards
|
|
|
|
mount ::API::GroupClusters
|
|
|
|
mount ::API::GroupExport
|
2020-02-11 04:08:39 -05:00
|
|
|
mount ::API::GroupImport
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::GroupLabels
|
|
|
|
mount ::API::GroupMilestones
|
|
|
|
mount ::API::Groups
|
|
|
|
mount ::API::GroupContainerRepositories
|
|
|
|
mount ::API::GroupVariables
|
|
|
|
mount ::API::ImportGithub
|
|
|
|
mount ::API::Issues
|
|
|
|
mount ::API::JobArtifacts
|
|
|
|
mount ::API::Jobs
|
|
|
|
mount ::API::Keys
|
|
|
|
mount ::API::Labels
|
|
|
|
mount ::API::Lint
|
|
|
|
mount ::API::Markdown
|
|
|
|
mount ::API::Members
|
|
|
|
mount ::API::MergeRequestDiffs
|
|
|
|
mount ::API::MergeRequests
|
2020-04-14 14:09:54 -04:00
|
|
|
mount ::API::Metrics::Dashboard::Annotations
|
2020-05-13 14:08:47 -04:00
|
|
|
mount ::API::Metrics::UserStarredDashboards
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::Namespaces
|
|
|
|
mount ::API::Notes
|
|
|
|
mount ::API::Discussions
|
|
|
|
mount ::API::ResourceLabelEvents
|
2020-05-26 11:08:17 -04:00
|
|
|
mount ::API::ResourceMilestoneEvents
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::NotificationSettings
|
|
|
|
mount ::API::Pages
|
|
|
|
mount ::API::PagesDomains
|
|
|
|
mount ::API::Pipelines
|
|
|
|
mount ::API::PipelineSchedules
|
|
|
|
mount ::API::ProjectClusters
|
|
|
|
mount ::API::ProjectContainerRepositories
|
|
|
|
mount ::API::ProjectEvents
|
|
|
|
mount ::API::ProjectExport
|
|
|
|
mount ::API::ProjectImport
|
|
|
|
mount ::API::ProjectHooks
|
|
|
|
mount ::API::ProjectMilestones
|
2020-05-15 05:07:59 -04:00
|
|
|
mount ::API::ProjectRepositoryStorageMoves
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::Projects
|
|
|
|
mount ::API::ProjectSnapshots
|
|
|
|
mount ::API::ProjectSnippets
|
|
|
|
mount ::API::ProjectStatistics
|
|
|
|
mount ::API::ProjectTemplates
|
2020-04-01 08:08:00 -04:00
|
|
|
mount ::API::Terraform::State
|
2020-02-10 10:08:54 -05:00
|
|
|
mount ::API::ProtectedBranches
|
|
|
|
mount ::API::ProtectedTags
|
|
|
|
mount ::API::Releases
|
|
|
|
mount ::API::Release::Links
|
|
|
|
mount ::API::RemoteMirrors
|
|
|
|
mount ::API::Repositories
|
|
|
|
mount ::API::Runner
|
|
|
|
mount ::API::Runners
|
|
|
|
mount ::API::Search
|
|
|
|
mount ::API::Services
|
|
|
|
mount ::API::Settings
|
|
|
|
mount ::API::SidekiqMetrics
|
|
|
|
mount ::API::Snippets
|
|
|
|
mount ::API::Statistics
|
|
|
|
mount ::API::Submodules
|
|
|
|
mount ::API::Subscriptions
|
|
|
|
mount ::API::Suggestions
|
|
|
|
mount ::API::SystemHooks
|
|
|
|
mount ::API::Tags
|
|
|
|
mount ::API::Templates
|
|
|
|
mount ::API::Todos
|
|
|
|
mount ::API::Triggers
|
|
|
|
mount ::API::UserCounts
|
|
|
|
mount ::API::Users
|
|
|
|
mount ::API::Variables
|
|
|
|
mount ::API::Version
|
|
|
|
mount ::API::Wikis
|
|
|
|
end
|
|
|
|
|
2019-09-03 18:07:49 -04:00
|
|
|
mount ::API::Internal::Base
|
2019-09-06 00:06:25 -04:00
|
|
|
mount ::API::Internal::Pages
|
2016-10-07 09:39:57 -04:00
|
|
|
|
|
|
|
route :any, '*path' do
|
2016-10-07 13:18:02 -04:00
|
|
|
error!('404 Not Found', 404)
|
2016-10-07 09:39:57 -04:00
|
|
|
end
|
2012-06-27 07:32:56 -04:00
|
|
|
end
|
2012-06-27 05:26:16 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
API::API.prepend_if_ee('::EE::API::API')
|