2020-08-27 11:10:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class GenericPackages < ::API::Base
|
2020-09-21 23:09:49 -04:00
|
|
|
GENERIC_PACKAGES_REQUIREMENTS = {
|
|
|
|
package_name: API::NO_SLASH_URL_PART_REGEX,
|
|
|
|
file_name: API::NO_SLASH_URL_PART_REGEX
|
|
|
|
}.freeze
|
|
|
|
|
2021-02-17 07:09:26 -05:00
|
|
|
ALLOWED_STATUSES = %w[default hidden].freeze
|
|
|
|
|
2020-10-30 11:08:59 -04:00
|
|
|
feature_category :package_registry
|
2022-05-06 17:08:35 -04:00
|
|
|
urgency :low
|
2020-10-30 11:08:59 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
before do
|
|
|
|
require_packages_enabled!
|
2021-03-15 14:09:05 -04:00
|
|
|
authenticate_non_get!
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2021-01-08 07:10:35 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2020-08-27 11:10:21 -04:00
|
|
|
|
|
|
|
namespace ':id/packages/generic' do
|
2020-09-21 23:09:49 -04:00
|
|
|
namespace ':package_name/*package_version/:file_name', requirements: GENERIC_PACKAGES_REQUIREMENTS do
|
|
|
|
desc 'Workhorse authorize generic package file' do
|
|
|
|
detail 'This feature was introduced in GitLab 13.5'
|
|
|
|
end
|
|
|
|
|
2021-01-08 07:10:35 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2020-09-21 23:09:49 -04:00
|
|
|
|
|
|
|
params do
|
2020-10-01 02:09:59 -04:00
|
|
|
requires :package_name, type: String, desc: 'Package name', regexp: Gitlab::Regex.generic_package_name_regex, file_path: true
|
2020-09-21 23:09:49 -04:00
|
|
|
requires :package_version, type: String, desc: 'Package version', regexp: Gitlab::Regex.generic_package_version_regex
|
|
|
|
requires :file_name, type: String, desc: 'Package file name', regexp: Gitlab::Regex.generic_package_file_name_regex, file_path: true
|
2021-02-17 07:09:26 -05:00
|
|
|
optional :status, type: String, values: ALLOWED_STATUSES, desc: 'Package status'
|
2020-09-21 23:09:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
put 'authorize' do
|
2022-10-04 11:09:33 -04:00
|
|
|
project = authorized_user_project
|
|
|
|
|
2020-09-21 23:09:49 -04:00
|
|
|
authorize_workhorse!(subject: project, maximum_size: project.actual_limits.generic_packages_max_file_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Upload package file' do
|
|
|
|
detail 'This feature was introduced in GitLab 13.5'
|
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
2020-10-01 02:09:59 -04:00
|
|
|
requires :package_name, type: String, desc: 'Package name', regexp: Gitlab::Regex.generic_package_name_regex, file_path: true
|
2020-09-21 23:09:49 -04:00
|
|
|
requires :package_version, type: String, desc: 'Package version', regexp: Gitlab::Regex.generic_package_version_regex
|
|
|
|
requires :file_name, type: String, desc: 'Package file name', regexp: Gitlab::Regex.generic_package_file_name_regex, file_path: true
|
2021-02-17 07:09:26 -05:00
|
|
|
optional :status, type: String, values: ALLOWED_STATUSES, desc: 'Package status'
|
2020-09-21 23:09:49 -04:00
|
|
|
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The package file to be published (generated by Multipart middleware)'
|
2021-10-26 20:13:04 -04:00
|
|
|
optional :select, type: String, values: %w[package_file]
|
2020-09-21 23:09:49 -04:00
|
|
|
end
|
|
|
|
|
2021-01-08 07:10:35 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2020-09-21 23:09:49 -04:00
|
|
|
|
|
|
|
put do
|
2022-10-04 11:09:33 -04:00
|
|
|
project = authorized_user_project
|
|
|
|
|
2020-09-21 23:09:49 -04:00
|
|
|
authorize_upload!(project)
|
|
|
|
bad_request!('File is too large') if max_file_size_exceeded?
|
|
|
|
|
2021-08-19 20:09:17 -04:00
|
|
|
track_package_event('push_package', :generic, project: project, user: current_user, namespace: project.namespace)
|
2020-09-21 23:09:49 -04:00
|
|
|
|
|
|
|
create_package_file_params = declared_params.merge(build: current_authenticated_job)
|
2021-10-26 20:13:04 -04:00
|
|
|
package_file = ::Packages::Generic::CreatePackageFileService
|
2020-09-21 23:09:49 -04:00
|
|
|
.new(project, current_user, create_package_file_params)
|
|
|
|
.execute
|
|
|
|
|
2021-10-26 20:13:04 -04:00
|
|
|
if params[:select] == 'package_file'
|
|
|
|
present package_file
|
|
|
|
else
|
|
|
|
created!
|
|
|
|
end
|
2020-09-21 23:09:49 -04:00
|
|
|
rescue ObjectStorage::RemoteStoreError => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, extra: { file_name: params[:file_name], project_id: project.id })
|
|
|
|
|
|
|
|
forbidden!
|
2021-05-07 02:10:43 -04:00
|
|
|
rescue ::Packages::DuplicatePackageError
|
|
|
|
bad_request!('Duplicate package is not allowed')
|
2020-09-21 23:09:49 -04:00
|
|
|
end
|
2020-10-01 02:09:59 -04:00
|
|
|
|
|
|
|
desc 'Download package file' do
|
|
|
|
detail 'This feature was introduced in GitLab 13.5'
|
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :package_name, type: String, desc: 'Package name', regexp: Gitlab::Regex.generic_package_name_regex, file_path: true
|
|
|
|
requires :package_version, type: String, desc: 'Package version', regexp: Gitlab::Regex.generic_package_version_regex
|
|
|
|
requires :file_name, type: String, desc: 'Package file name', regexp: Gitlab::Regex.generic_package_file_name_regex, file_path: true
|
|
|
|
end
|
|
|
|
|
2021-01-08 07:10:35 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2020-10-01 02:09:59 -04:00
|
|
|
|
|
|
|
get do
|
2022-10-04 11:09:33 -04:00
|
|
|
project = authorized_user_project(action: :read_package)
|
|
|
|
|
2020-10-01 02:09:59 -04:00
|
|
|
authorize_read_package!(project)
|
|
|
|
|
|
|
|
package = ::Packages::Generic::PackageFinder.new(project).execute!(params[:package_name], params[:package_version])
|
|
|
|
package_file = ::Packages::PackageFileFinder.new(package, params[:file_name]).execute!
|
|
|
|
|
2021-08-19 20:09:17 -04:00
|
|
|
track_package_event('pull_package', :generic, project: project, user: current_user, namespace: project.namespace)
|
2020-10-01 02:09:59 -04:00
|
|
|
|
2022-09-14 14:12:48 -04:00
|
|
|
present_package_file!(package_file)
|
2020-10-01 02:09:59 -04:00
|
|
|
end
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
include ::API::Helpers::PackagesHelpers
|
2020-09-21 23:09:49 -04:00
|
|
|
include ::API::Helpers::Packages::BasicAuthHelpers
|
2020-08-27 11:10:21 -04:00
|
|
|
|
2020-09-21 23:09:49 -04:00
|
|
|
def max_file_size_exceeded?
|
2022-10-04 11:09:33 -04:00
|
|
|
authorized_user_project.actual_limits.exceeded?(:generic_packages_max_file_size, params[:file].size)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|