2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module API
module Concerns
module Packages
2021-09-04 01:27:46 +05:30
module DebianPackageEndpoints
2021-06-08 01:23:25 +05:30
extend ActiveSupport :: Concern
DISTRIBUTION_REQUIREMENTS = {
2021-09-30 23:02:18 +05:30
distribution : :: Packages :: Debian :: DISTRIBUTION_REGEX
2021-06-08 01:23:25 +05:30
} . freeze
COMPONENT_ARCHITECTURE_REQUIREMENTS = {
2021-09-30 23:02:18 +05:30
component : :: Packages :: Debian :: COMPONENT_REGEX ,
architecture : :: Packages :: Debian :: ARCHITECTURE_REGEX
2021-06-08 01:23:25 +05:30
} . freeze
2023-06-20 00:43:36 +05:30
LIST_PACKAGE = 'list_package'
2021-06-08 01:23:25 +05:30
included do
feature_category :package_registry
2022-07-16 23:28:13 +05:30
urgency :low
2021-06-08 01:23:25 +05:30
helpers :: API :: Helpers :: PackagesHelpers
helpers :: API :: Helpers :: Packages :: BasicAuthHelpers
2021-09-04 01:27:46 +05:30
include :: API :: Helpers :: Authentication
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
helpers do
params :shared_package_file_params do
2023-03-04 22:38:38 +05:30
requires :distribution , type : String , desc : 'The Debian Codename or Suite' , regexp : Gitlab :: Regex . debian_distribution_regex , documentation : { example : 'my-distro' }
requires :letter , type : String , desc : 'The Debian Classification (first-letter or lib-first-letter)' , documentation : { example : 'a' }
requires :package_name , type : String , desc : 'The Debian Source Package Name' , regexp : Gitlab :: Regex . debian_package_name_regex , documentation : { example : 'my-pkg' }
requires :package_version , type : String , desc : 'The Debian Source Package Version' , regexp : Gitlab :: Regex . debian_version_regex , documentation : { example : '1.0.0' }
requires :file_name , type : String , desc : 'The Debian File Name' , documentation : { example : 'example_1.0.0~alpha2_amd64.deb' }
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
def distribution_from! ( container )
:: Packages :: Debian :: DistributionsFinder . new ( container , codename_or_suite : params [ :distribution ] ) . execute . last!
2021-09-30 23:02:18 +05:30
end
2023-03-17 16:20:25 +05:30
def present_distribution_package_file! ( project )
2021-10-27 15:23:28 +05:30
not_found! unless params [ :package_name ] . start_with? ( params [ :letter ] )
2021-06-08 01:23:25 +05:30
2023-03-17 16:20:25 +05:30
package_file = distribution_from! ( project ) . package_files . with_file_name ( params [ :file_name ] ) . last!
2021-10-27 15:23:28 +05:30
2023-06-20 00:43:36 +05:30
track_debian_package_event 'pull_package'
2022-10-11 01:57:18 +05:30
present_package_file! ( package_file )
end
def present_index_file! ( file_type )
2023-04-23 21:23:45 +05:30
not_found! ( " Format #{ params [ :format ] } is not supported " ) unless params [ :format ] . nil?
2022-10-11 01:57:18 +05:30
relation = " ::Packages::Debian:: #{ project_or_group . class . name } ComponentFile " . constantize
relation = relation
. preload_distribution
. with_container ( project_or_group )
. with_codename_or_suite ( params [ :distribution ] )
. with_component_name ( params [ :component ] )
. with_file_type ( file_type )
. with_architecture_name ( params [ :architecture ] )
. with_compression_type ( nil )
. order_created_asc
2023-05-27 22:25:52 +05:30
# Empty component files are not persisted in DB
no_content! if params [ :file_sha256 ] == :: Packages :: Debian :: EMPTY_FILE_SHA256
2022-10-11 01:57:18 +05:30
relation = relation . with_file_sha256 ( params [ :file_sha256 ] ) if params [ :file_sha256 ]
2023-05-27 22:25:52 +05:30
component_file = relation . last
if component_file . nil? || component_file . empty?
not_found! if params [ :file_sha256 ] # asking for a non existing component file.
no_content! # empty component files are not always persisted in DB
end
2023-06-20 00:43:36 +05:30
track_debian_package_event LIST_PACKAGE
2023-05-27 22:25:52 +05:30
present_carrierwave_file! ( component_file . file )
2021-06-08 01:23:25 +05:30
end
2023-06-20 00:43:36 +05:30
def track_debian_package_event ( action )
if project_or_group . is_a? ( Project )
project = project_or_group
namespace = project_or_group . namespace
else
project = nil
namespace = project_or_group
end
track_package_event ( action , :debian , project : project , namespace : namespace , user : current_user )
end
2021-10-27 15:23:28 +05:30
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
rescue_from ArgumentError do | e |
render_api_error! ( e . message , 400 )
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
rescue_from ActiveRecord :: RecordInvalid do | e |
render_api_error! ( e . message , 400 )
end
2021-06-08 01:23:25 +05:30
2021-12-11 22:18:48 +05:30
authenticate_with do | accept |
2023-07-09 08:55:56 +05:30
accept . token_types ( :personal_access_token_with_username , :deploy_token_with_username , :job_token_with_username )
2021-12-11 22:18:48 +05:30
. sent_through ( :http_basic_auth )
end
2021-10-27 15:23:28 +05:30
format :txt
content_type :txt , 'text/plain'
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
params do
2023-03-04 22:38:38 +05:30
requires :distribution , type : String , desc : 'The Debian Codename or Suite' , regexp : Gitlab :: Regex . debian_distribution_regex , documentation : { example : 'my-distro' }
2021-10-27 15:23:28 +05:30
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
namespace 'dists/*distribution' , requirements : DISTRIBUTION_REQUIREMENTS do
# GET {projects|groups}/:id/packages/debian/dists/*distribution/Release.gpg
2022-10-11 01:57:18 +05:30
# https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
2021-10-27 15:23:28 +05:30
desc 'The Release file signature' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success code : 200
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2021-10-27 15:23:28 +05:30
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
get 'Release.gpg' do
distribution_from! ( project_or_group ) . file_signature
end
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
# GET {projects|groups}/:id/packages/debian/dists/*distribution/Release
2022-10-11 01:57:18 +05:30
# https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
2021-10-27 15:23:28 +05:30
desc 'The unsigned Release file' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success code : 200
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2021-10-27 15:23:28 +05:30
end
get 'Release' do
2023-06-20 00:43:36 +05:30
distribution = distribution_from! ( project_or_group )
track_debian_package_event LIST_PACKAGE
present_carrierwave_file! ( distribution . file )
2021-10-27 15:23:28 +05:30
end
# GET {projects|groups}/:id/packages/debian/dists/*distribution/InRelease
2022-10-11 01:57:18 +05:30
# https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
2021-10-27 15:23:28 +05:30
desc 'The signed Release file' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success code : 200
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2021-10-27 15:23:28 +05:30
end
get 'InRelease' do
2023-06-20 00:43:36 +05:30
distribution = distribution_from! ( project_or_group )
track_debian_package_event LIST_PACKAGE
present_carrierwave_file! ( distribution . signed_file )
2021-06-08 01:23:25 +05:30
end
params do
2023-03-04 22:38:38 +05:30
requires :component , type : String , desc : 'The Debian Component' , regexp : Gitlab :: Regex . debian_component_regex , documentation : { example : 'main' }
2021-06-08 01:23:25 +05:30
end
2022-10-11 01:57:18 +05:30
namespace ':component' , requirements : COMPONENT_ARCHITECTURE_REQUIREMENTS do
params do
2023-03-04 22:38:38 +05:30
requires :architecture , type : String , desc : 'The Debian Architecture' , regexp : Gitlab :: Regex . debian_architecture_regex , documentation : { example : 'binary-amd64' }
2022-10-11 01:57:18 +05:30
end
namespace 'debian-installer/binary-:architecture' do
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/debian-installer/binary-:architecture/Packages
# https://wiki.debian.org/DebianRepository/Format#A.22Packages.22_Indices
desc 'The installer (udeb) binary files index' do
detail 'This feature was introduced in GitLab 15.4'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'Packages' do
present_index_file! ( :di_packages )
end
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/debian-installer/binary-:architecture/by-hash/SHA256/:file_sha256
# https://wiki.debian.org/DebianRepository/Format?action=show&redirect=RepositoryFormat#indices_acquisition_via_hashsums_.28by-hash.29
desc 'The installer (udeb) binary files index by hash' do
detail 'This feature was introduced in GitLab 15.4'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'by-hash/SHA256/:file_sha256' do
present_index_file! ( :di_packages )
end
end
namespace 'source' , requirements : COMPONENT_ARCHITECTURE_REQUIREMENTS do
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/source/Sources
# https://wiki.debian.org/DebianRepository/Format#A.22Sources.22_Indices
desc 'The source files index' do
detail 'This feature was introduced in GitLab 15.4'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'Sources' do
present_index_file! ( :sources )
end
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/source/by-hash/SHA256/:file_sha256
# https://wiki.debian.org/DebianRepository/Format?action=show&redirect=RepositoryFormat#indices_acquisition_via_hashsums_.28by-hash.29
desc 'The source files index by hash' do
detail 'This feature was introduced in GitLab 15.4'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'by-hash/SHA256/:file_sha256' do
present_index_file! ( :sources )
end
end
params do
2023-03-04 22:38:38 +05:30
requires :architecture , type : String , desc : 'The Debian Architecture' , regexp : Gitlab :: Regex . debian_architecture_regex , documentation : { example : 'binary-amd64' }
2021-06-08 01:23:25 +05:30
end
2022-10-11 01:57:18 +05:30
namespace 'binary-:architecture' , requirements : COMPONENT_ARCHITECTURE_REQUIREMENTS do
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/binary-:architecture/Packages
# https://wiki.debian.org/DebianRepository/Format#A.22Packages.22_Indices
desc 'The binary files index' do
detail 'This feature was introduced in GitLab 13.5'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'Packages' do
present_index_file! ( :packages )
end
# GET {projects|groups}/:id/packages/debian/dists/*distribution/:component/binary-:architecture/by-hash/SHA256/:file_sha256
# https://wiki.debian.org/DebianRepository/Format?action=show&redirect=RepositoryFormat#indices_acquisition_via_hashsums_.28by-hash.29
desc 'The binary files index by hash' do
detail 'This feature was introduced in GitLab 15.4'
2023-05-27 22:25:52 +05:30
success [
{ code : 200 } ,
{ code : 202 }
]
2023-03-04 22:38:38 +05:30
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ debian_packages ]
2022-10-11 01:57:18 +05:30
end
get 'by-hash/SHA256/:file_sha256' do
present_index_file! ( :packages )
end
2021-06-08 01:23:25 +05:30
end
end
end
end
end
end
end
end