api.rb 9.8 KB
Newer Older
1 2
# frozen_string_literal: true

3
module API
4
  class API < ::API::Base
Valery Sizov's avatar
Valery Sizov committed
5
    include APIGuard
6

7 8
    LOG_FILENAME = Rails.root.join("log", "api_json.log")

Stan Hu's avatar
Stan Hu committed
9
    NO_SLASH_URL_PART_REGEX = %r{[^/]+}.freeze
10 11
    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
12
    USER_REQUIREMENTS = { user_id: NO_SLASH_URL_PART_REGEX }.freeze
13
    LOG_FILTERS = ::Rails.application.config.filter_parameters + [/^output$/]
14

15 16 17 18 19
    insert_before Grape::Middleware::Error,
                  GrapeLogging::Middleware::RequestLogger,
                  logger: Logger.new(LOG_FILENAME),
                  formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
                  include: [
20
                    GrapeLogging::Loggers::FilterParameters.new(LOG_FILTERS),
21
                    Gitlab::GrapeLogging::Loggers::ClientEnvLogger.new,
22
                    Gitlab::GrapeLogging::Loggers::RouteLogger.new,
23
                    Gitlab::GrapeLogging::Loggers::UserLogger.new,
24
                    Gitlab::GrapeLogging::Loggers::ExceptionLogger.new,
25
                    Gitlab::GrapeLogging::Loggers::QueueDurationLogger.new,
26
                    Gitlab::GrapeLogging::Loggers::PerfLogger.new,
27
                    Gitlab::GrapeLogging::Loggers::CorrelationIdLogger.new,
28 29
                    Gitlab::GrapeLogging::Loggers::ContextLogger.new,
                    Gitlab::GrapeLogging::Loggers::ContentLogger.new
30
                  ]
31

32
    allow_access_with_scope :api
33
    allow_access_with_scope :read_api, if: -> (request) { request.get? }
34
    prefix :api
35

36
    version 'v3', using: :path do
37 38 39
      route :any, '*path' do
        error!('API V3 is no longer supported. Use API V4 instead.', 410)
      end
40
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
41

42 43
    version 'v4', using: :path

44 45 46 47
    before do
      header['X-Frame-Options'] = 'SAMEORIGIN'
      header['X-Content-Type-Options'] = 'nosniff'
    end
48

49
    before do
Stan Hu's avatar
Stan Hu committed
50 51
      coerce_nil_params_to_array!

52 53 54 55 56
      api_endpoint = env['api.endpoint']
      feature_category = api_endpoint.options[:for].try(:feature_category_for_app, api_endpoint).to_s

      header[Gitlab::Metrics::RequestsRackMiddleware::FEATURE_CATEGORY_HEADER] = feature_category

57
      Gitlab::ApplicationContext.push(
58
        user: -> { @current_user },
59
        project: -> { @project },
60
        namespace: -> { @group },
61 62
        caller_id: route.origin,
        feature_category: feature_category
63 64 65
      )
    end

66 67 68 69
    before do
      set_peek_enabled_for_current_request
    end

70
    # The locale is set to the current user's locale when `current_user` is loaded
71
    after { Gitlab::I18n.use_default_locale }
72

73 74 75 76
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

77
    rescue_from ActiveRecord::RecordNotFound do
78
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
79 80
    end

81 82 83 84
    rescue_from(
      ::ActiveRecord::StaleObjectError,
      ::Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
    ) do
85 86 87
      rack_response({ 'message' => '409 Conflict: Resource lock' }.to_json, 409)
    end

88 89 90 91 92 93 94 95
    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

Connor Shea's avatar
Connor Shea committed
96
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
97 98 99 100 101
    # 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

Connor Shea's avatar
Connor Shea committed
102 103
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
104 105
    end

106
    rescue_from Gitlab::Auth::TooManyIps do |e|
107
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
108 109
    end

110
    rescue_from :all do |exception|
Stan Hu's avatar
Stan Hu committed
111
      handle_api_exception(exception)
112 113
    end

114 115 116 117 118 119 120 121 122
    # 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

Nihad Abbasov's avatar
Nihad Abbasov committed
123
    format :json
124
    formatter :json, Gitlab::Json::GrapeFormatter
125 126 127

    # There is a small chance some users depend on the old behavior.
    # We this change under a feature flag to see if affects GitLab.com users.
128
    if Gitlab::Database.cached_table_exists?('features') && Feature.enabled?(:api_json_content_type)
129 130 131 132
      content_type :json, 'application/json'
    else
      content_type :txt, 'text/plain'
    end
133

134 135
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
    helpers ::API::Helpers
136
    helpers ::API::Helpers::CommonHelpers
137
    helpers ::API::Helpers::PerformanceBarHelpers
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
138

139 140
    namespace do
      after do
141
        ::Users::ActivityService.new(@current_user).execute
142 143 144 145
      end

      # Keep in alphabetical order
      mount ::API::AccessRequests
146
      mount ::API::Admin::Ci::Variables
147
      mount ::API::Admin::InstanceClusters
148
      mount ::API::Admin::Sidekiq
149 150 151 152 153 154 155 156
      mount ::API::Appearance
      mount ::API::Applications
      mount ::API::Avatar
      mount ::API::AwardEmoji
      mount ::API::Badges
      mount ::API::Boards
      mount ::API::Branches
      mount ::API::BroadcastMessages
157 158
      mount ::API::Ci::Pipelines
      mount ::API::Ci::PipelineSchedules
159 160
      mount ::API::Ci::Runner
      mount ::API::Ci::Runners
161 162
      mount ::API::Commits
      mount ::API::CommitStatuses
163
      mount ::API::ContainerRegistryEvent
164
      mount ::API::ContainerRepositories
165
      mount ::API::DeployKeys
166
      mount ::API::DeployTokens
167 168 169 170
      mount ::API::Deployments
      mount ::API::Environments
      mount ::API::ErrorTracking
      mount ::API::Events
171 172 173
      mount ::API::FeatureFlags
      mount ::API::FeatureFlagScopes
      mount ::API::FeatureFlagsUserLists
174 175
      mount ::API::Features
      mount ::API::Files
Sean Carroll's avatar
Sean Carroll committed
176
      mount ::API::FreezePeriods
177 178 179
      mount ::API::GroupBoards
      mount ::API::GroupClusters
      mount ::API::GroupExport
180
      mount ::API::GroupImport
181 182 183 184 185
      mount ::API::GroupLabels
      mount ::API::GroupMilestones
      mount ::API::Groups
      mount ::API::GroupContainerRepositories
      mount ::API::GroupVariables
186
      mount ::API::ImportBitbucketServer
187
      mount ::API::ImportGithub
188
      mount ::API::IssueLinks
189
      mount ::API::Invitations
190 191 192 193 194 195 196 197 198 199
      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
200
      mount ::API::MergeRequestApprovals
201
      mount ::API::Metrics::Dashboard::Annotations
202
      mount ::API::Metrics::UserStarredDashboards
203 204 205 206
      mount ::API::Namespaces
      mount ::API::Notes
      mount ::API::Discussions
      mount ::API::ResourceLabelEvents
207
      mount ::API::ResourceMilestoneEvents
Patrick Derichs's avatar
Patrick Derichs committed
208
      mount ::API::ResourceStateEvents
209
      mount ::API::NotificationSettings
210 211 212 213 214 215
      mount ::API::ProjectPackages
      mount ::API::GroupPackages
      mount ::API::PackageFiles
      mount ::API::NugetPackages
      mount ::API::PypiPackages
      mount ::API::ComposerPackages
216 217
      mount ::API::ConanProjectPackages
      mount ::API::ConanInstancePackages
Mathieu Parent's avatar
Mathieu Parent committed
218 219
      mount ::API::DebianGroupPackages
      mount ::API::DebianProjectPackages
220 221
      mount ::API::MavenPackages
      mount ::API::NpmPackages
222
      mount ::API::GenericPackages
223
      mount ::API::GoProxy
224 225 226 227 228 229 230 231 232
      mount ::API::Pages
      mount ::API::PagesDomains
      mount ::API::ProjectClusters
      mount ::API::ProjectContainerRepositories
      mount ::API::ProjectEvents
      mount ::API::ProjectExport
      mount ::API::ProjectImport
      mount ::API::ProjectHooks
      mount ::API::ProjectMilestones
233
      mount ::API::ProjectRepositoryStorageMoves
234 235 236 237 238
      mount ::API::Projects
      mount ::API::ProjectSnapshots
      mount ::API::ProjectSnippets
      mount ::API::ProjectStatistics
      mount ::API::ProjectTemplates
239
      mount ::API::Terraform::State
240
      mount ::API::Terraform::StateVersion
Max Woolf's avatar
Max Woolf committed
241
      mount ::API::PersonalAccessTokens
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
      mount ::API::ProtectedBranches
      mount ::API::ProtectedTags
      mount ::API::Releases
      mount ::API::Release::Links
      mount ::API::RemoteMirrors
      mount ::API::Repositories
      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
Jason Goodman's avatar
Jason Goodman committed
262
      mount ::API::Unleash
alinamihaila's avatar
alinamihaila committed
263
      mount ::API::UsageData
264 265 266 267 268 269 270
      mount ::API::UserCounts
      mount ::API::Users
      mount ::API::Variables
      mount ::API::Version
      mount ::API::Wikis
    end

271
    mount ::API::Internal::Base
272
    mount ::API::Internal::Lfs
273
    mount ::API::Internal::Pages
274
    mount ::API::Internal::Kubernetes
275

276 277 278 279 280 281 282 283 284 285
    version 'v3', using: :path do
      # Although the following endpoints are kept behind V3 namespace,
      # they're not deprecated neither should be removed when V3 get
      # removed.  They're needed as a layer to integrate with Jira
      # Development Panel.
      namespace '/', requirements: ::API::V3::Github::ENDPOINT_REQUIREMENTS do
        mount ::API::V3::Github
      end
    end

286
    route :any, '*path', feature_category: :not_owned do
287
      error!('404 Not Found', 404)
288
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
289
  end
290
end
291

292
API::API.prepend_if_ee('::EE::API::API')