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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::MailRoom, feature_category: :build do
let(:default_port) { 143 }
let(:log_path) { Rails.root.join('log', 'mail_room_json.log').to_s }
let(:fake_redis_queues) do
double(
url: "localhost",
db: 99,
sentinels: [{ host: 'localhost', port: 1234 }],
sentinels?: true
)
end
let(:yml_config) do
{
enabled: true,
host: 'mail.example.com',
address: 'address@example.com',
user: 'user@example.com',
password: 'password',
port: default_port,
ssl: false,
start_tls: false,
mailbox: 'inbox',
idle_timeout: 60,
log_path: log_path,
expunge_deleted: false
}
end
let(:custom_config) { {} }
let(:incoming_email_config) { yml_config.merge(custom_config) }
let(:service_desk_email_config) { yml_config.merge(custom_config) }
let(:configs) do
{
incoming_email: incoming_email_config,
service_desk_email: service_desk_email_config
}
end
before do
allow(Gitlab::Redis::Queues).to receive(:new).and_return(fake_redis_queues)
allow(described_class).to receive(:load_yaml).and_return(configs)
described_class.instance_variable_set(:@enabled_configs, nil)
end
after do
described_class.instance_variable_set(:@enabled_configs, nil)
end
describe '#enabled_configs' do
let(:first_value) { described_class.enabled_configs.each_value.first }
context 'when both email and address is set' do
it 'returns email configs' do
expect(described_class.enabled_configs.size).to eq(2)
end
end
context 'when the yml file cannot be found' do
before do
allow(described_class).to receive(:config_file).and_return('not_existing_file')
end
it 'returns an empty list' do
expect(described_class.enabled_configs).to be_empty
end
end
context 'when email is disabled' do
let(:custom_config) { { enabled: false } }
it 'returns an empty list' do
expect(described_class.enabled_configs).to be_empty
end
end
context 'when email is enabled but address is not set' do
let(:custom_config) { { enabled: true, address: '' } }
it 'returns an empty list' do
expect(described_class.enabled_configs).to be_empty
end
end
context 'when a config value is missing from the yml file' do
let(:yml_config) { {} }
let(:custom_config) { { enabled: true, address: 'address@example.com' } }
it 'overwrites missing values with the default' do
expect(first_value[:port]).to eq(Gitlab::MailRoom::DEFAULT_CONFIG[:port])
end
end
context 'when only incoming_email config is present' do
let(:configs) { { incoming_email: incoming_email_config } }
it 'returns only encoming_email' do
expect(described_class.enabled_configs.size).to eq(1)
expect(first_value[:worker]).to eq('EmailReceiverWorker')
end
end
describe 'setting up redis settings' do
it 'sets delivery method to Sidekiq by default' do
config = first_value
expect(config).to include(
delivery_method: 'sidekiq'
)
end
it 'sets redis config' do
config = first_value
expect(config).to include(
redis_url: 'localhost',
redis_db: 99,
sentinels: [{ host: 'localhost', port: 1234 }]
)
end
end
describe 'setting up the log path' do
context 'if the log path is a relative path' do
let(:custom_config) { { log_path: 'tiny_log.log' } }
it 'expands the log path to an absolute value' do
new_path = Pathname.new(first_value[:log_path])
expect(new_path.absolute?).to be_truthy
end
end
context 'if the log path is absolute path' do
let(:custom_config) { { log_path: '/dev/null' } }
it 'leaves the path as-is' do
expect(first_value[:log_path]).to eq '/dev/null'
end
end
end
end
describe '#enabled_mailbox_types' do
context 'when all mailbox types are enabled' do
it 'returns the mailbox types' do
expect(described_class.enabled_mailbox_types).to match(%w[incoming_email service_desk_email])
end
end
context 'when an mailbox_types is disabled' do
let(:incoming_email_config) { yml_config.merge(enabled: false) }
it 'returns the mailbox types' do
expect(described_class.enabled_mailbox_types).to match(%w[service_desk_email])
end
end
context 'when email is disabled' do
let(:custom_config) { { enabled: false } }
it 'returns an empty array' do
expect(described_class.enabled_mailbox_types).to match_array([])
end
end
end
describe '#worker_for' do
context 'matched mailbox types' do
it 'returns the constantized worker class' do
expect(described_class.worker_for('incoming_email')).to eql(EmailReceiverWorker)
expect(described_class.worker_for('service_desk_email')).to eql(ServiceDeskEmailReceiverWorker)
end
end
context 'non-existing mailbox_type' do
it 'returns nil' do
expect(described_class.worker_for('another_mailbox_type')).to be(nil)
end
end
end
describe 'config/mail_room.yml' do
let(:mail_room_template) { ERB.new(File.read(Rails.root.join("./config/mail_room.yml"))).result }
let(:mail_room_yml) { YAML.safe_load(mail_room_template, permitted_classes: [Symbol]) }
shared_examples 'renders mail-specific config file correctly' do
it 'renders mail room config file correctly' do
expect(mail_room_yml[:mailboxes]).to be_an(Array)
expect(mail_room_yml[:mailboxes].length).to eq(2)
expect(mail_room_yml[:mailboxes]).to all(
match(
a_hash_including(
host: 'mail.example.com',
port: default_port,
ssl: false,
start_tls: false,
email: 'user@example.com',
password: 'password',
idle_timeout: 60,
logger: {
log_path: log_path
},
name: 'inbox',
delete_after_delivery: true,
expunge_deleted: false
)
)
)
end
end
shared_examples 'renders arbitration options correctly' do
it 'renders arbitration options correctly' do
expect(mail_room_yml[:mailboxes]).to be_an(Array)
expect(mail_room_yml[:mailboxes].length).to eq(2)
expect(mail_room_yml[:mailboxes]).to all(
match(
a_hash_including(
arbitration_method: "redis",
arbitration_options: {
redis_url: "localhost",
namespace: "mail_room:gitlab",
sentinels: [{ host: "localhost", port: 1234 }]
}
)
)
)
end
end
shared_examples 'renders the sidekiq delivery method and options correctly' do
it 'renders the sidekiq delivery method and options correctly' do
expect(mail_room_yml[:mailboxes]).to be_an(Array)
expect(mail_room_yml[:mailboxes].length).to eq(2)
expect(mail_room_yml[:mailboxes][0]).to match(
a_hash_including(
delivery_method: 'sidekiq',
delivery_options: {
redis_url: "localhost",
redis_db: 99,
queue: "default",
worker: "EmailReceiverWorker",
sentinels: [{ host: "localhost", port: 1234 }]
}
)
)
expect(mail_room_yml[:mailboxes][1]).to match(
a_hash_including(
delivery_method: 'sidekiq',
delivery_options: {
redis_url: "localhost",
redis_db: 99,
queue: "default",
worker: "ServiceDeskEmailReceiverWorker",
sentinels: [{ host: "localhost", port: 1234 }]
}
)
)
end
end
context 'when delivery_method is implicit' do
it_behaves_like 'renders mail-specific config file correctly'
it_behaves_like 'renders arbitration options correctly'
it_behaves_like 'renders the sidekiq delivery method and options correctly'
end
context 'when delivery_method is explicitly sidekiq' do
let(:custom_config) { { delivery_method: 'sidekiq' } }
it_behaves_like 'renders mail-specific config file correctly'
it_behaves_like 'renders arbitration options correctly'
it_behaves_like 'renders the sidekiq delivery method and options correctly'
end
context 'when delivery_method is webhook (internally postback in mail_room)' do
let(:custom_config) do
{
delivery_method: 'webhook',
gitlab_url: 'http://gitlab.example',
secret_file: '/path/to/secret/file'
}
end
it_behaves_like 'renders mail-specific config file correctly'
it_behaves_like 'renders arbitration options correctly'
it 'renders the webhook (postback) delivery method and options correctly' do
expect(mail_room_yml[:mailboxes]).to be_an(Array)
expect(mail_room_yml[:mailboxes].length).to eq(2)
expect(mail_room_yml[:mailboxes][0]).to match(
a_hash_including(
delivery_method: 'postback',
delivery_options: {
delivery_url: "http://gitlab.example/api/v4/internal/mail_room/incoming_email",
content_type: "text/plain",
jwt_auth_header: Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER,
jwt_issuer: Gitlab::MailRoom::INTERNAL_API_REQUEST_JWT_ISSUER,
jwt_algorithm: 'HS256',
jwt_secret_path: '/path/to/secret/file'
}
)
)
expect(mail_room_yml[:mailboxes][1]).to match(
a_hash_including(
delivery_method: 'postback',
delivery_options: {
delivery_url: "http://gitlab.example/api/v4/internal/mail_room/service_desk_email",
content_type: "text/plain",
jwt_auth_header: Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER,
jwt_issuer: Gitlab::MailRoom::INTERNAL_API_REQUEST_JWT_ISSUER,
jwt_algorithm: 'HS256',
jwt_secret_path: '/path/to/secret/file'
}
)
)
end
end
end
describe 'mailroom encrypted configuration' do
context "when parsing secrets.yml" do
let(:application_secrets_file) { Rails.root.join('spec/fixtures/mail_room/secrets.yml.erb').to_s }
let(:encrypted_settings_key_base) { '0123456789abcdef' * 8 }
before do
allow(described_class).to receive(:application_secrets_file).and_return(application_secrets_file)
stub_env('KEY', 'an environment variable value')
described_class.instance_variable_set(:@application_secrets, nil)
end
after do
described_class.instance_variable_set(:@application_secrets, nil)
end
it 'reads in the secrets.yml file as erb and merges shared and test environments' do
application_secrets = described_class.send(:application_secrets)
expect(application_secrets).to match(a_hash_including(
a_shared_key: 'this key is shared',
an_overriden_shared_key: 'the merge overwrote this key',
an_environment_specific_key: 'test environment value',
erb_env_key: 'an environment variable value',
encrypted_settings_key_base: encrypted_settings_key_base
))
expect(application_secrets[:an_unread_key]).to be_nil
end
end
context "when parsing gitlab.yml" do
let(:plain_configs) { configs }
let(:shared_path_config) do
{ shared: { path: '/this/is/my/shared_path' } }.merge(configs)
end
let(:encrypted_settings_config) do
{
shared: { path: '/this/is/my/shared_path' },
encrypted_settings: { path: '/this/is/my_custom_encrypted_path' }
}.merge(configs)
end
let(:encrypted_file_config) do
configs.deep_merge({
incoming_email: { encrypted_secret_file: '/custom_incoming_secret.yaml.enc' },
service_desk_email: { encrypted_secret_file: '/custom_service_desk_secret.yaml.enc' }
})
end
it 'returns default encrypted_secret_file path' do
allow(described_class).to receive(:load_yaml).and_return(plain_configs)
expect(described_class.send(:encrypted_secret_file, :incoming_email))
.to end_with('shared/encrypted_settings/incoming_email.yaml.enc')
expect(described_class.send(:encrypted_secret_file, :service_desk_email))
.to end_with('shared/encrypted_settings/service_desk_email.yaml.enc')
end
it 'returns encrypted_secret_file relative to custom shared path' do
allow(described_class).to receive(:load_yaml).and_return(shared_path_config)
expect(described_class.send(:encrypted_secret_file, :incoming_email))
.to eq('/this/is/my/shared_path/encrypted_settings/incoming_email.yaml.enc')
expect(described_class.send(:encrypted_secret_file, :service_desk_email))
.to eq('/this/is/my/shared_path/encrypted_settings/service_desk_email.yaml.enc')
end
it 'returns custom encrypted_secret_file' do
allow(described_class).to receive(:load_yaml).and_return(encrypted_file_config)
expect(described_class.send(:encrypted_secret_file, :incoming_email))
.to eq('/custom_incoming_secret.yaml.enc')
expect(described_class.send(:encrypted_secret_file, :service_desk_email))
.to eq('/custom_service_desk_secret.yaml.enc')
end
end
context 'when using encrypted secrets' do
let(:mail_room_template) { ERB.new(File.read(Rails.root.join("./config/mail_room.yml"))).result }
let(:mail_room_yml) { YAML.safe_load(mail_room_template, permitted_classes: [Symbol]) }
let(:application_secrets) { { encrypted_settings_key_base: '0123456789abcdef' * 8 } } # gitleaks:allow
let(:configs) do
{
encrypted_settings: { path: 'spec/fixtures/mail_room/encrypted_secrets' }
}.merge({
incoming_email: incoming_email_config,
service_desk_email: service_desk_email_config
})
end
before do
allow(described_class).to receive(:application_secrets).and_return(application_secrets)
end
it 'renders the encrypted secrets into the configuration correctly' do
expect(mail_room_yml[:mailboxes]).to be_an(Array)
expect(mail_room_yml[:mailboxes].length).to eq(2)
expect(mail_room_yml[:mailboxes][0]).to match(
a_hash_including(
password: 'abc123',
email: 'incoming-test-account@gitlab.com'
)
)
expect(mail_room_yml[:mailboxes][1]).to match(
a_hash_including(
password: '123abc',
email: 'service-desk-test-account@gitlab.example.com'
)
)
end
end
end
end
|