File: object_store_settings_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (428 lines) | stat: -rw-r--r-- 15,372 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('config', 'object_store_settings.rb')

RSpec.describe ObjectStoreSettings, feature_category: :shared do
  describe '#parse!' do
    let(:settings) { GitlabSettings::Options.build(config) }

    subject { described_class.new(settings).parse! }

    context 'with valid config' do
      let(:connection) do
        {
          'provider' => 'AWS',
          'aws_access_key_id' => 'AWS_ACCESS_KEY_ID',
          'aws_secret_access_key' => 'AWS_SECRET_ACCESS_KEY',
          'region' => 'us-east-1'
        }
      end

      let(:config) do
        {
          'lfs' => { 'enabled' => true },
          'artifacts' => { 'enabled' => true },
          'external_diffs' => { 'enabled' => false },
          'pages' => { 'enabled' => true },
          'ci_secure_files' => { 'enabled' => true },
          'object_store' => {
            'enabled' => true,
            'connection' => connection,
            'proxy_download' => true,
            'objects' => {
              'artifacts' => {
                'bucket' => 'artifacts',
                'proxy_download' => false
              },
              'lfs' => {
                'bucket' => 'lfs-objects'
              },
              'external_diffs' => {
                'bucket' => 'external_diffs',
                'enabled' => false
              },
              'pages' => {
                'bucket' => 'pages'
              },
              'ci_secure_files' => {
                'bucket' => 'ci_secure_files'
              }
            }
          }
        }
      end

      shared_examples 'consolidated settings for objects accelerated by Workhorse' do
        it 'consolidates active object storage settings' do
          expect(subject).to be_present

          described_class::WORKHORSE_ACCELERATED_TYPES.each do |object_type|
            # Use to_h to avoid https://gitlab.com/gitlab-org/gitlab/-/issues/286873
            section = subject.try(object_type).to_h

            next unless section.dig('object_store', 'enabled')

            expect(section['object_store']['connection']).to eq(connection)
            expect(section['object_store']['consolidated_settings']).to be true
          end
        end
      end

      it 'sets correct default values' do
        subject

        expect(settings.artifacts['enabled']).to be true
        expect(settings.artifacts['object_store']['enabled']).to be true
        expect(settings.artifacts['object_store']['connection'].to_hash).to eq(connection)
        expect(settings.artifacts['object_store']['direct_upload']).to be true
        expect(settings.artifacts['object_store']['proxy_download']).to be false
        expect(settings.artifacts['object_store']['remote_directory']).to eq('artifacts')
        expect(settings.artifacts['object_store']['bucket_prefix']).to eq(nil)
        expect(settings.artifacts['object_store']['consolidated_settings']).to be true
        expect(settings.artifacts).to eq(settings['artifacts'])

        expect(settings.lfs['enabled']).to be true
        expect(settings.lfs['object_store']['enabled']).to be true
        expect(settings.lfs['object_store']['connection'].to_hash).to eq(connection)
        expect(settings.lfs['object_store']['direct_upload']).to be true
        expect(settings.lfs['object_store']['proxy_download']).to be true
        expect(settings.lfs['object_store']['remote_directory']).to eq('lfs-objects')
        expect(settings.lfs['object_store']['bucket_prefix']).to eq(nil)
        expect(settings.lfs['object_store']['consolidated_settings']).to be true
        expect(settings.lfs).to eq(settings['lfs'])

        expect(settings.pages['enabled']).to be true
        expect(settings.pages['object_store']['enabled']).to be true
        expect(settings.pages['object_store']['connection'].to_hash).to eq(connection)
        expect(settings.pages['object_store']['remote_directory']).to eq('pages')
        expect(settings.pages['object_store']['bucket_prefix']).to eq(nil)
        expect(settings.pages['object_store']['consolidated_settings']).to be true
        expect(settings.pages).to eq(settings['pages'])

        expect(settings.external_diffs['enabled']).to be false
        expect(settings.external_diffs['object_store']).to be_nil
        expect(settings.external_diffs).to eq(settings['external_diffs'])
      end

      it 'supports bucket prefixes' do
        config['object_store']['objects']['artifacts']['bucket'] = 'gitlab/artifacts'
        config['object_store']['objects']['lfs']['bucket'] = 'gitlab/lfs'

        subject

        expect(settings.artifacts['object_store']['remote_directory']).to eq('gitlab')
        expect(settings.artifacts['object_store']['bucket_prefix']).to eq('artifacts')
        expect(settings.lfs['object_store']['remote_directory']).to eq('gitlab')
        expect(settings.lfs['object_store']['bucket_prefix']).to eq('lfs')
      end

      context 'when the same section-specified connection is specified' do
        before do
          config['artifacts'] = GitlabSettings::Options.build(
            {
              'enabled' => true,
              'object_store' => {
                'enabled' => true,
                'connection' => connection
              }
            }
          )
        end

        it_behaves_like 'consolidated settings for objects accelerated by Workhorse'
      end

      context 'when a different section-specified connection is specified' do
        let(:gcs_connection) { GitlabSettings::Options.build("provider" => "GCS") }

        before do
          config['artifacts'] = GitlabSettings::Options.build(
            {
              'enabled' => true,
              'object_store' => {
                'enabled' => true,
                'connection' => gcs_connection
              }
            }
          )
        end

        it 'disables consolidated object settings' do
          expect(settings.artifacts['enabled']).to be true
          expect(settings.artifacts['object_store']['connection']).to eq(gcs_connection)

          described_class::WORKHORSE_ACCELERATED_TYPES.each do |object_type|
            section = subject.try(object_type).to_h

            next unless section.dig('object_store', 'enabled')

            expect(section['object_store']['consolidated_settings']).to_be falsey
          end
        end
      end

      context 'CI secure files' do
        let(:ci_secure_files_connection) { { 'provider' => 'Google', 'google_application_default' => true } }

        before do
          config['ci_secure_files'] = {
            'enabled' => true,
            'object_store' => {
              'enabled' => true,
              'connection' => ci_secure_files_connection
            }
          }
        end

        it_behaves_like 'consolidated settings for objects accelerated by Workhorse'

        it 'allows CI secure files to define its own connection' do
          expect { subject }.not_to raise_error

          expect(settings.ci_secure_files['object_store']['connection'].to_hash).to eq(ci_secure_files_connection)
          expect(settings.ci_secure_files['object_store']['consolidated_settings']).to be_falsey
        end
      end

      context 'with Google CDN enabled' do
        let(:cdn_config) do
          {
            'provider' => 'Google',
            'url' => 'https://cdn.example.org',
            'key_name' => 'stanhu-key',
            'key' => Base64.urlsafe_encode64(SecureRandom.hex)
          }
        end

        before do
          config['object_store']['objects']['artifacts']['cdn'] = cdn_config
        end

        it 'populates artifacts CDN config' do
          subject

          expect(settings.artifacts['object_store']['cdn'].to_hash).to eq(cdn_config)
        end
      end

      it 'raises an error when a bucket is missing' do
        config['object_store']['objects']['lfs'].delete('bucket')

        expect { subject }.to raise_error(/Object storage for lfs must have a bucket specified/)
      end

      it 'does not raise error if pages bucket is missing' do
        config['object_store']['objects']['pages'].delete('bucket')

        expect { subject }.not_to raise_error
        expect(settings.pages['object_store']).to eq(nil)
      end

      it 'does not raise error if ci_secure_files config is missing' do
        config['object_store']['objects'].delete('ci_secure_files')

        expect { subject }.not_to raise_error
        expect(settings.ci_secure_files['object_store']).to eq(nil)
      end

      context 'GitLab Pages' do
        let(:pages_connection) { { 'provider' => 'Google', 'google_application_default' => true } }

        before do
          config['pages'] = {
            'enabled' => true,
            'object_store' => {
              'enabled' => true,
              'connection' => pages_connection
            }
          }
        end

        it_behaves_like 'consolidated settings for objects accelerated by Workhorse'

        it 'allows pages to define its own connection' do
          expect { subject }.not_to raise_error

          expect(settings.pages['object_store']['connection'].to_hash).to eq(pages_connection)
          expect(settings.pages['object_store']['consolidated_settings']).to be_falsey
        end
      end

      context 'when object storage is disabled for artifacts with no bucket' do
        before do
          config['artifacts'] = {
            'enabled' => true,
            'object_store' => {}
          }
          config['object_store']['objects']['artifacts'] = {
            'enabled' => false
          }
        end

        it_behaves_like 'consolidated settings for objects accelerated by Workhorse'

        it 'does not enable consolidated settings for artifacts' do
          subject

          expect(settings.artifacts['enabled']).to be true
          expect(settings.artifacts['object_store']['remote_directory']).to be_nil
          expect(settings.artifacts['object_store']['bucket_prefix']).to be_nil
          expect(settings.artifacts['object_store']['enabled']).to be_falsey
          expect(settings.artifacts['object_store']['consolidated_settings']).to be_falsey
        end
      end

      context 'when object storage is disabled for ci_secure_files with no bucket' do
        before do
          config['ci_secure_files'] = {
            'enabled' => true,
            'object_store' => {}
          }
          config['object_store']['objects']['ci_secure_files'] = {
            'enabled' => false
          }
        end

        it 'does not enable consolidated settings for ci_secure_files' do
          subject

          expect(settings.ci_secure_files['enabled']).to be true
          expect(settings.ci_secure_files['object_store']['remote_directory']).to be_nil
          expect(settings.ci_secure_files['object_store']['bucket_prefix']).to be_nil
          expect(settings.ci_secure_files['object_store']['enabled']).to be_falsey
          expect(settings.ci_secure_files['object_store']['consolidated_settings']).to be_falsey
        end
      end

      context 'with legacy config' do
        let(:legacy_settings) do
          {
            'enabled' => true,
            'remote_directory' => 'some-bucket',
            'direct_upload' => false,
            'proxy_download' => false,
            'connection' => {
              'provider' => 'GCS'
            }
          }
        end

        before do
          settings.lfs['object_store'] = described_class.legacy_parse(legacy_settings, 'lfs')
        end

        it 'does not alter config if legacy settings are specified' do
          subject

          expect(settings.artifacts['object_store']).to be_nil
          expect(settings.lfs['object_store']['remote_directory']).to eq('some-bucket')
          expect(settings.lfs['object_store']['bucket_prefix']).to eq(nil)
          expect(settings.lfs['object_store']['direct_upload']).to eq(true)
          expect(settings.external_diffs['object_store']).to be_nil
        end
      end
    end
  end

  describe '.legacy_parse' do
    it 'sets correct default values' do
      settings = described_class.legacy_parse(nil, 'artifacts')

      expect(settings['enabled']).to be false
      expect(settings['direct_upload']).to be true
      expect(settings['remote_directory']).to be nil
      expect(settings['bucket_prefix']).to be nil
    end

    it 'respects original values' do
      original_settings = GitlabSettings::Options.build({
        'enabled' => true,
        'remote_directory' => 'artifacts'
      })

      settings = described_class.legacy_parse(original_settings, 'artifacts')

      expect(settings['enabled']).to be true
      expect(settings['direct_upload']).to be true
      expect(settings['remote_directory']).to eq 'artifacts'
      expect(settings['bucket_prefix']).to be nil
    end

    it 'supports bucket prefixes' do
      original_settings = GitlabSettings::Options.build({
        'enabled' => true,
        'remote_directory' => 'gitlab/artifacts'
      })

      settings = described_class.legacy_parse(original_settings, 'artifacts')
      expect(settings['remote_directory']).to eq 'gitlab'
      expect(settings['bucket_prefix']).to eq 'artifacts'
    end
  end

  describe '.split_bucket_prefix' do
    using RSpec::Parameterized::TableSyntax

    subject { described_class.split_bucket_prefix(input) }

    context 'valid inputs' do
      where(:input, :bucket, :prefix) do
        nil | nil | nil
        '' | nil | nil
        'bucket' | 'bucket' | nil
        'bucket/prefix' | 'bucket' | 'prefix'
        'bucket/pre/fix' | 'bucket' | 'pre/fix'
      end

      with_them do
        it { expect(subject).to eq([bucket, prefix]) }
      end
    end

    context 'invalid inputs' do
      where(:input) do
        [
          ['bucket/'],
          ['bucket/.'],
          ['bucket/..'],
          ['bucket/prefix/'],
          ['bucket/prefix/.'],
          ['bucket/prefix/..'],
          ['/bucket/prefix'],
          ['./bucket/prefix'],
          ['../bucket/prefix'],
          ['bucket//prefix'],
          ['bucket/./prefix'],
          ['bucket/../prefix']
        ]
      end

      with_them do
        it { expect { subject }.to raise_error(/invalid bucket/) }
      end
    end
  end

  describe '.enabled_endpoint_uris' do
    subject(:enabled_endpoint_uris) { described_class.enabled_endpoint_uris }

    it 'returns a list of enabled endpoint URIs' do
      stub_config(
        artifacts: { enabled: true, object_store: { enabled: true, connection: { endpoint: 'http://example1.com' } } },
        external_diffs: {
          enabled: true, object_store: { enabled: true, connection: { endpoint: 'http://example1.com' } }
        },
        lfs: { enabled: false, object_store: { enabled: true, connection: { endpoint: 'http://example2.com' } } },
        uploads: { enabled: true, object_store: { enabled: false, connection: { endpoint: 'http://example3.com' } } },
        packages: { enabled: true, object_store: { enabled: true, connection: { provider: 'AWS' } } },
        pages: { enabled: true, object_store: { enabled: true, connection: { endpoint: 'http://example4.com' } } }
      )

      expect(enabled_endpoint_uris).to contain_exactly(
        URI('http://example1.com'),
        URI('http://example4.com')
      )
    end
  end
end