summaryrefslogtreecommitdiff
path: root/lib/api/concerns/packages/debian_package_endpoints.rb
blob: 181759a7f38b9470dc25015bcae010e1b1e16123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# frozen_string_literal: true

module API
  module Concerns
    module Packages
      module DebianPackageEndpoints
        extend ActiveSupport::Concern

        DISTRIBUTION_REQUIREMENTS = {
          distribution: ::Packages::Debian::DISTRIBUTION_REGEX
        }.freeze
        COMPONENT_ARCHITECTURE_REQUIREMENTS = {
          component: ::Packages::Debian::COMPONENT_REGEX,
          architecture: ::Packages::Debian::ARCHITECTURE_REGEX
        }.freeze

        included do
          feature_category :package_registry
          urgency :low

          helpers ::API::Helpers::PackagesHelpers
          helpers ::API::Helpers::Packages::BasicAuthHelpers
          include ::API::Helpers::Authentication

          helpers do
            params :shared_package_file_params do
              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' }
            end

            def distribution_from!(container)
              ::Packages::Debian::DistributionsFinder.new(container, codename_or_suite: params[:distribution]).execute.last!
            end

            def present_distribution_package_file!(project)
              not_found! unless params[:package_name].start_with?(params[:letter])

              package_file = distribution_from!(project).package_files.with_file_name(params[:file_name]).last!

              present_package_file!(package_file)
            end

            def present_index_file!(file_type)
              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

              relation = relation.with_file_sha256(params[:file_sha256]) if params[:file_sha256]

              present_carrierwave_file!(relation.last!.file)
            end
          end

          rescue_from ArgumentError do |e|
            render_api_error!(e.message, 400)
          end

          rescue_from ActiveRecord::RecordInvalid do |e|
            render_api_error!(e.message, 400)
          end

          authenticate_with do |accept|
            accept.token_types(:personal_access_token, :deploy_token, :job_token)
                  .sent_through(:http_basic_auth)
          end

          format :txt
          content_type :txt, 'text/plain'

          params do
            requires :distribution, type: String, desc: 'The Debian Codename or Suite', regexp: Gitlab::Regex.debian_distribution_regex, documentation: { example: 'my-distro' }
          end

          namespace 'dists/*distribution', requirements: DISTRIBUTION_REQUIREMENTS do
            # GET {projects|groups}/:id/packages/debian/dists/*distribution/Release.gpg
            # https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
            desc 'The Release file signature' do
              detail 'This feature was introduced in GitLab 13.5'
              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]
            end

            route_setting :authentication, authenticate_non_public: true
            get 'Release.gpg' do
              distribution_from!(project_or_group).file_signature
            end

            # GET {projects|groups}/:id/packages/debian/dists/*distribution/Release
            # https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
            desc 'The unsigned Release file' do
              detail 'This feature was introduced in GitLab 13.5'
              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]
            end

            route_setting :authentication, authenticate_non_public: true
            get 'Release' do
              present_carrierwave_file!(distribution_from!(project_or_group).file)
            end

            # GET {projects|groups}/:id/packages/debian/dists/*distribution/InRelease
            # https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
            desc 'The signed Release file' do
              detail 'This feature was introduced in GitLab 13.5'
              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]
            end

            route_setting :authentication, authenticate_non_public: true
            get 'InRelease' do
              present_carrierwave_file!(distribution_from!(project_or_group).signed_file)
            end

            params do
              requires :component, type: String, desc: 'The Debian Component', regexp: Gitlab::Regex.debian_component_regex, documentation: { example: 'main' }
            end

            namespace ':component', requirements: COMPONENT_ARCHITECTURE_REQUIREMENTS do
              params do
                requires :architecture, type: String, desc: 'The Debian Architecture', regexp: Gitlab::Regex.debian_architecture_regex, documentation: { example: 'binary-amd64' }
              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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                get 'by-hash/SHA256/:file_sha256' do
                  present_index_file!(:sources)
                end
              end

              params do
                requires :architecture, type: String, desc: 'The Debian Architecture', regexp: Gitlab::Regex.debian_architecture_regex, documentation: { example: 'binary-amd64' }
              end

              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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                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'
                  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]
                end

                route_setting :authentication, authenticate_non_public: true
                get 'by-hash/SHA256/:file_sha256' do
                  present_index_file!(:packages)
                end
              end
            end
          end
        end
      end
    end
  end
end