2020-07-17 02:09:11 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# NuGet Package Manager Client API
|
|
|
|
#
|
|
|
|
# These API endpoints are not meant to be consumed directly by users. They are
|
|
|
|
# called by the NuGet package manager client when users run commands
|
|
|
|
# like `nuget install` or `nuget push`.
|
2020-12-29 07:10:19 -05:00
|
|
|
#
|
|
|
|
# This is the project level API.
|
2020-07-17 02:09:11 -04:00
|
|
|
module API
|
2020-12-04 10:09:36 -05:00
|
|
|
class NugetProjectPackages < ::API::Base
|
2021-01-01 10:10:11 -05:00
|
|
|
helpers ::API::Helpers::PackagesHelpers
|
2020-07-17 02:09:11 -04:00
|
|
|
helpers ::API::Helpers::Packages::BasicAuthHelpers
|
2021-01-01 10:10:11 -05:00
|
|
|
include ::API::Helpers::Authentication
|
2020-07-17 02:09:11 -04:00
|
|
|
|
2020-10-30 11:08:59 -04:00
|
|
|
feature_category :package_registry
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
PACKAGE_FILENAME = 'package.nupkg'
|
2021-06-18 14:10:13 -04:00
|
|
|
SYMBOL_PACKAGE_FILENAME = 'package.snupkg'
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
default_format :json
|
|
|
|
|
2021-01-01 10:10:11 -05:00
|
|
|
authenticate_with do |accept|
|
2021-02-10 10:11:19 -05:00
|
|
|
accept.token_types(:personal_access_token_with_username, :deploy_token_with_username, :job_token_with_username)
|
2021-01-01 10:10:11 -05:00
|
|
|
.sent_through(:http_basic_auth)
|
|
|
|
end
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
rescue_from ArgumentError do |e|
|
|
|
|
render_api_error!(e.message, 400)
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
after_validation do
|
2020-07-17 02:09:11 -04:00
|
|
|
require_packages_enabled!
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
helpers do
|
2021-06-18 14:10:13 -04:00
|
|
|
params :file_params do
|
|
|
|
requires :package, type: ::API::Validations::Types::WorkhorseFile, desc: 'The package file to be published (generated by Multipart middleware)'
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
def project_or_group
|
|
|
|
authorized_user_project
|
|
|
|
end
|
2021-06-07 08:10:00 -04:00
|
|
|
|
|
|
|
def snowplow_gitlab_standard_context
|
|
|
|
{ project: authorized_user_project, namespace: authorized_user_project.namespace }
|
|
|
|
end
|
2021-06-18 14:10:13 -04:00
|
|
|
|
|
|
|
def authorize_nuget_upload
|
|
|
|
authorize_workhorse!(
|
|
|
|
subject: project_or_group,
|
|
|
|
has_length: false,
|
|
|
|
maximum_size: project_or_group.actual_limits.nuget_max_file_size
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def temp_file_name(symbol_package)
|
|
|
|
return ::Packages::Nuget::TEMPORARY_SYMBOL_PACKAGE_NAME if symbol_package
|
|
|
|
|
|
|
|
::Packages::Nuget::TEMPORARY_PACKAGE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_name(symbol_package)
|
|
|
|
return SYMBOL_PACKAGE_FILENAME if symbol_package
|
|
|
|
|
|
|
|
PACKAGE_FILENAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_nuget_package_file(symbol_package: false)
|
|
|
|
authorize_upload!(project_or_group)
|
|
|
|
bad_request!('File is too large') if project_or_group.actual_limits.exceeded?(:nuget_max_file_size, params[:package].size)
|
|
|
|
|
|
|
|
file_params = params.merge(
|
|
|
|
file: params[:package],
|
|
|
|
file_name: file_name(symbol_package)
|
|
|
|
)
|
|
|
|
|
|
|
|
package = ::Packages::CreateTemporaryPackageService.new(
|
|
|
|
project_or_group, current_user, declared_params.merge(build: current_authenticated_job)
|
|
|
|
).execute(:nuget, name: temp_file_name(symbol_package))
|
|
|
|
|
|
|
|
package_file = ::Packages::CreatePackageFileService.new(package, file_params.merge(build: current_authenticated_job))
|
|
|
|
.execute
|
|
|
|
|
|
|
|
yield(package) if block_given?
|
|
|
|
|
|
|
|
::Packages::Nuget::ExtractionWorker.perform_async(package_file.id) # rubocop:disable CodeReuse/Worker
|
|
|
|
|
|
|
|
created!
|
|
|
|
end
|
2020-12-29 07:10:19 -05:00
|
|
|
end
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
params do
|
2020-12-04 10:09:36 -05:00
|
|
|
requires :id, type: String, desc: 'The ID of a project', regexp: ::API::Concerns::Packages::NugetEndpoints::POSITIVE_INTEGER_REGEX
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
namespace ':id/packages/nuget' do
|
2020-12-04 10:09:36 -05:00
|
|
|
include ::API::Concerns::Packages::NugetEndpoints
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
# https://docs.microsoft.com/en-us/nuget/api/package-publish-resource
|
|
|
|
desc 'The NuGet Package Publish endpoint' do
|
|
|
|
detail 'This feature was introduced in GitLab 12.6'
|
|
|
|
end
|
2020-08-12 20:10:06 -04:00
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
params do
|
2021-06-18 14:10:13 -04:00
|
|
|
use :file_params
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
put urgency: :low do
|
2021-06-18 14:10:13 -04:00
|
|
|
upload_nuget_package_file do |package|
|
|
|
|
track_package_event(
|
|
|
|
'push_package',
|
|
|
|
:nuget,
|
|
|
|
category: 'API::NugetPackages',
|
|
|
|
user: current_user,
|
|
|
|
project: package.project,
|
|
|
|
namespace: package.project.namespace
|
|
|
|
)
|
|
|
|
end
|
|
|
|
rescue ObjectStorage::RemoteStoreError => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, extra: { file_name: params[:file_name], project_id: project_or_group.id })
|
2020-07-17 02:09:11 -04:00
|
|
|
|
2021-06-18 14:10:13 -04:00
|
|
|
forbidden!
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
put 'authorize', urgency: :low do
|
2021-06-18 14:10:13 -04:00
|
|
|
authorize_nuget_upload
|
|
|
|
end
|
2020-07-17 02:09:11 -04:00
|
|
|
|
2021-06-18 14:10:13 -04:00
|
|
|
# https://docs.microsoft.com/en-us/nuget/api/symbol-package-publish-resource
|
|
|
|
desc 'The NuGet Symbol Package Publish endpoint' do
|
|
|
|
detail 'This feature was introduced in GitLab 14.1'
|
|
|
|
end
|
2020-07-17 02:09:11 -04:00
|
|
|
|
2021-06-18 14:10:13 -04:00
|
|
|
params do
|
|
|
|
use :file_params
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
put 'symbolpackage', urgency: :low do
|
2021-07-14 20:10:03 -04:00
|
|
|
upload_nuget_package_file(symbol_package: true) do |package|
|
|
|
|
track_package_event(
|
|
|
|
'push_symbol_package',
|
|
|
|
:nuget,
|
|
|
|
category: 'API::NugetPackages',
|
|
|
|
user: current_user,
|
|
|
|
project: package.project,
|
|
|
|
namespace: package.project.namespace
|
|
|
|
)
|
|
|
|
end
|
2020-07-17 02:09:11 -04:00
|
|
|
rescue ObjectStorage::RemoteStoreError => e
|
2020-12-29 07:10:19 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, extra: { file_name: params[:file_name], project_id: project_or_group.id })
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
forbidden!
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
put 'symbolpackage/authorize', urgency: :low do
|
2021-06-18 14:10:13 -04:00
|
|
|
authorize_nuget_upload
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource
|
|
|
|
params do
|
|
|
|
requires :package_name, type: String, desc: 'The NuGet package name', regexp: API::NO_SLASH_URL_PART_REGEX
|
|
|
|
end
|
|
|
|
namespace '/download/*package_name' do
|
2020-12-29 07:10:19 -05:00
|
|
|
after_validation do
|
|
|
|
authorize_read_package!(project_or_group)
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'The NuGet Content Service - index request' do
|
|
|
|
detail 'This feature was introduced in GitLab 12.8'
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
get 'index', format: :json, urgency: :low do
|
2020-12-29 07:10:19 -05:00
|
|
|
present ::Packages::Nuget::PackagesVersionsPresenter.new(find_packages(params[:package_name])),
|
2020-07-17 02:09:11 -04:00
|
|
|
with: ::API::Entities::Nuget::PackagesVersions
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'The NuGet Content Service - content request' do
|
|
|
|
detail 'This feature was introduced in GitLab 12.8'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :package_version, type: String, desc: 'The NuGet package version', regexp: API::NO_SLASH_URL_PART_REGEX
|
|
|
|
requires :package_filename, type: String, desc: 'The NuGet package filename', regexp: API::NO_SLASH_URL_PART_REGEX
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
get '*package_version/*package_filename', format: [:nupkg, :snupkg], urgency: :low do
|
2020-07-17 02:09:11 -04:00
|
|
|
filename = "#{params[:package_filename]}.#{params[:format]}"
|
2020-12-29 07:10:19 -05:00
|
|
|
package_file = ::Packages::PackageFileFinder.new(find_package(params[:package_name], params[:package_version]), filename, with_file_name_like: true)
|
2020-07-17 02:09:11 -04:00
|
|
|
.execute
|
|
|
|
|
|
|
|
not_found!('Package') unless package_file
|
|
|
|
|
2021-07-14 20:10:03 -04:00
|
|
|
track_package_event(
|
|
|
|
params[:format] == 'snupkg' ? 'pull_symbol_package' : 'pull_package',
|
|
|
|
:nuget,
|
|
|
|
category: 'API::NugetPackages',
|
|
|
|
project: package_file.project,
|
|
|
|
namespace: package_file.project.namespace
|
|
|
|
)
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
# nuget and dotnet don't support 302 Moved status codes, supports_direct_download has to be set to false
|
2022-09-14 14:12:48 -04:00
|
|
|
present_package_file!(package_file, supports_direct_download: false)
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|