debian-mirror-gitlab/lib/api/helpers/packages/conan/api_helpers.rb

286 lines
9.6 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module API
module Helpers
module Packages
module Conan
module ApiHelpers
2020-11-24 15:15:51 +05:30
include Gitlab::Utils::StrongMemoize
2020-07-28 23:09:34 +05:30
def present_download_urls(entity)
authorize!(:read_package, project)
presenter = ::Packages::Conan::PackagePresenter.new(
2020-11-24 15:15:51 +05:30
package,
2020-07-28 23:09:34 +05:30
current_user,
project,
2021-04-29 21:17:54 +05:30
conan_package_reference: params[:conan_package_reference],
id: params[:id]
2020-07-28 23:09:34 +05:30
)
render_api_error!("No recipe manifest found", 404) if yield(presenter).empty?
present presenter, with: entity
end
def present_package_download_urls
present_download_urls(::API::Entities::ConanPackage::ConanPackageManifest, &:package_urls)
end
def present_recipe_download_urls
present_download_urls(::API::Entities::ConanPackage::ConanRecipeManifest, &:recipe_urls)
end
2020-10-24 23:57:45 +05:30
def recipe_upload_urls
2021-04-29 21:17:54 +05:30
{ upload_urls: file_names.select(&method(:recipe_file?)).to_h do |file_name|
[file_name, build_recipe_file_upload_url(file_name)]
end }
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
def package_upload_urls
2021-04-29 21:17:54 +05:30
{ upload_urls: file_names.select(&method(:package_file?)).to_h do |file_name|
[file_name, build_package_file_upload_url(file_name)]
end }
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
def recipe_file?(file_name)
file_name.in?(::Packages::Conan::FileMetadatum::RECIPE_FILES)
end
def package_file?(file_name)
file_name.in?(::Packages::Conan::FileMetadatum::PACKAGE_FILES)
end
2020-11-24 15:15:51 +05:30
def build_package_file_upload_url(file_name)
options = url_options(file_name).merge(
conan_package_reference: params[:conan_package_reference],
package_revision: ::Packages::Conan::FileMetadatum::DEFAULT_PACKAGE_REVISION
2020-07-28 23:09:34 +05:30
)
2020-11-24 15:15:51 +05:30
package_file_url(options)
end
def build_recipe_file_upload_url(file_name)
recipe_file_url(url_options(file_name))
2020-07-28 23:09:34 +05:30
end
2020-11-24 15:15:51 +05:30
def url_options(file_name)
{
package_name: params[:package_name],
package_version: params[:package_version],
package_username: params[:package_username],
package_channel: params[:package_channel],
file_name: file_name,
recipe_revision: ::Packages::Conan::FileMetadatum::DEFAULT_RECIPE_REVISION
}
end
def package_file_url(options)
case package_scope
when :project
expose_url(
api_v4_projects_packages_conan_v1_files_package_path(
options.merge(id: project.id)
)
2020-07-28 23:09:34 +05:30
)
2020-11-24 15:15:51 +05:30
when :instance
expose_url(
api_v4_packages_conan_v1_files_package_path(options)
)
end
end
def recipe_file_url(options)
case package_scope
when :project
expose_url(
api_v4_projects_packages_conan_v1_files_export_path(
options.merge(id: project.id)
)
)
when :instance
expose_url(
api_v4_packages_conan_v1_files_export_path(options)
)
end
2020-07-28 23:09:34 +05:30
end
def recipe
"%{package_name}/%{package_version}@%{package_username}/%{package_channel}" % params.symbolize_keys
end
def project
strong_memoize(:project) do
2020-11-24 15:15:51 +05:30
case package_scope
when :project
find_project!(params[:id])
when :instance
full_path = ::Packages::Conan::Metadatum.full_path_from(package_username: params[:package_username])
find_project!(full_path)
end
2020-07-28 23:09:34 +05:30
end
end
def package
strong_memoize(:package) do
project.packages
2020-11-24 15:15:51 +05:30
.conan
2020-07-28 23:09:34 +05:30
.with_name(params[:package_name])
.with_version(params[:package_version])
2020-11-24 15:15:51 +05:30
.with_conan_username(params[:package_username])
2020-07-28 23:09:34 +05:30
.with_conan_channel(params[:package_channel])
.order_created
.last
end
end
def token
strong_memoize(:token) do
token = nil
token = ::Gitlab::ConanToken.from_personal_access_token(access_token) if access_token
token = ::Gitlab::ConanToken.from_deploy_token(deploy_token_from_request) if deploy_token_from_request
token = ::Gitlab::ConanToken.from_job(find_job_from_token) if find_job_from_token
token
end
end
def download_package_file(file_type)
authorize!(:read_package, project)
package_file = ::Packages::Conan::PackageFileFinder
.new(
package,
params[:file_name].to_s,
conan_file_type: file_type,
conan_package_reference: params[:conan_package_reference]
).execute!
2021-09-04 01:27:46 +05:30
track_package_event('pull_package', :conan, category: 'API::ConanPackages', user: current_user, project: project, namespace: project.namespace) if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY
2020-07-28 23:09:34 +05:30
present_carrierwave_file!(package_file.file)
end
def find_or_create_package
2021-02-22 17:27:13 +05:30
package || ::Packages::Conan::CreatePackageService.new(
project,
current_user,
params.merge(build: current_authenticated_job)
).execute
2020-07-28 23:09:34 +05:30
end
def track_push_package_event
2020-09-03 11:15:55 +05:30
if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY && params[:file].size > 0 # rubocop: disable Style/ZeroLengthPredicate
2021-09-04 01:27:46 +05:30
track_package_event('push_package', :conan, category: 'API::ConanPackages', user: current_user, project: project, namespace: project.namespace)
2020-07-28 23:09:34 +05:30
end
end
2020-10-24 23:57:45 +05:30
def file_names
json_payload = Gitlab::Json.parse(request.body.read)
bad_request!(nil) unless json_payload.is_a?(Hash)
json_payload.keys
end
2020-07-28 23:09:34 +05:30
def create_package_file_with_type(file_type, current_package)
2020-09-03 11:15:55 +05:30
unless params[:file].size == 0 # rubocop: disable Style/ZeroLengthPredicate
2020-07-28 23:09:34 +05:30
# conan sends two upload requests, the first has no file, so we skip record creation if file.size == 0
2021-02-22 17:27:13 +05:30
::Packages::Conan::CreatePackageFileService.new(
current_package,
params[:file],
params.merge(conan_file_type: file_type, build: current_authenticated_job)
).execute
2020-07-28 23:09:34 +05:30
end
end
def upload_package_file(file_type)
authorize_upload!(project)
2020-11-24 15:15:51 +05:30
bad_request!('File is too large') if project.actual_limits.exceeded?(:conan_max_file_size, params['file.size'].to_i)
2020-07-28 23:09:34 +05:30
current_package = find_or_create_package
track_push_package_event
create_package_file_with_type(file_type, current_package)
rescue ObjectStorage::RemoteStoreError => e
Gitlab::ErrorTracking.track_exception(e, file_name: params[:file_name], project_id: project.id)
forbidden!
end
def find_personal_access_token
2021-04-29 21:17:54 +05:30
find_personal_access_token_from_conan_jwt ||
2020-07-28 23:09:34 +05:30
find_personal_access_token_from_http_basic_auth
end
def find_user_from_job_token
return unless route_authentication_setting[:job_token_allowed]
2021-04-17 20:07:23 +05:30
job = find_job_from_token || return
2021-02-22 17:27:13 +05:30
@current_authenticated_job = job # rubocop:disable Gitlab/ModuleWithInstanceVariables
2020-07-28 23:09:34 +05:30
job.user
end
def deploy_token_from_request
find_deploy_token_from_conan_jwt || find_deploy_token_from_http_basic_auth
end
def find_job_from_token
find_job_from_conan_jwt || find_job_from_http_basic_auth
end
# We need to override this one because it
# looks into Bearer authorization header
def find_oauth_access_token
end
def find_personal_access_token_from_conan_jwt
token = decode_oauth_token_from_jwt
return unless token
PersonalAccessToken.find_by_id_and_user_id(token.access_token_id, token.user_id)
end
def find_deploy_token_from_conan_jwt
token = decode_oauth_token_from_jwt
return unless token
deploy_token = DeployToken.active.find_by_token(token.access_token_id.to_s)
# note: uesr_id is not a user record id, but is the attribute set on ConanToken
return if token.user_id != deploy_token&.username
deploy_token
end
def find_job_from_conan_jwt
token = decode_oauth_token_from_jwt
return unless token
2020-09-03 11:15:55 +05:30
::Ci::AuthJobFinder.new(token: token.access_token_id.to_s).execute
2020-07-28 23:09:34 +05:30
end
def decode_oauth_token_from_jwt
jwt = Doorkeeper::OAuth::Token.from_bearer_authorization(current_request)
return unless jwt
token = ::Gitlab::ConanToken.decode(jwt)
return unless token && token.access_token_id && token.user_id
token
end
2020-11-24 15:15:51 +05:30
def package_scope
params[:id].present? ? :project : :instance
end
2020-07-28 23:09:34 +05:30
end
end
end
end
end