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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Audit::Auditor, feature_category: :audit_events do
let(:name) { 'audit_operation' }
let(:author) { create(:user, :with_sign_ins) }
let(:group) { create(:group) }
let(:provider) { 'standard' }
let(:context) do
{ name: name,
author: author,
scope: group,
target: group,
authentication_event: true,
authentication_provider: provider,
message: "Signed in using standard authentication" }
end
let(:logger) { instance_spy(Gitlab::AuditJsonLogger) }
let(:app_logger) { instance_spy(Gitlab::AppLogger) }
subject(:auditor) { described_class }
describe '.audit' do
let(:audit!) { auditor.audit(context) }
before do
allow(Gitlab::Audit::Type::Definition).to receive(:defined?).and_call_original
allow(Gitlab::Audit::Type::Definition).to receive(:defined?).with(name).and_return(true)
end
context 'when yaml definition is not defined' do
before do
allow(Gitlab::Audit::Type::Definition).to receive(:defined?).and_call_original
allow(Gitlab::Audit::Type::Definition).to receive(:defined?).with(name).and_return(false)
end
it 'raises an error' do
expected_error = "Audit event type YML file is not defined for audit_operation. " \
"Please read https://docs.gitlab.com/ee/development/audit_event_guide/" \
"#how-to-instrument-new-audit-events for adding a new audit event"
expect { audit! }.to raise_error(StandardError, expected_error)
end
end
context 'when yaml definition is defined' do
before do
allow(Gitlab::Audit::Type::Definition).to receive(:defined?).and_return(true)
end
it 'does not raise an error' do
expect { audit! }.not_to raise_error
end
end
context 'when authentication event' do
it 'creates an authentication event' do
expect(AuthenticationEvent).to receive(:new).with(
{
user: author,
user_name: author.name,
ip_address: author.current_sign_in_ip,
result: AuthenticationEvent.results[:success],
provider: provider
}
).and_call_original
audit!
authentication_event = AuthenticationEvent.last
expect(authentication_event.user).to eq(author)
expect(authentication_event.user_name).to eq(author.name)
expect(authentication_event.ip_address).to eq(author.current_sign_in_ip)
expect(authentication_event.provider).to eq(provider)
end
it 'logs audit events to database', :aggregate_failures do
freeze_time do
audit!
audit_event = AuditEvent.last
expect(audit_event.author_id).to eq(author.id)
expect(audit_event.entity_id).to eq(group.id)
expect(audit_event.entity_type).to eq(group.class.name)
expect(audit_event.created_at).to eq(Time.zone.now)
expect(audit_event.details[:target_id]).to eq(group.id)
expect(audit_event.details[:target_type]).to eq(group.class.name)
end
end
it 'logs audit events to file' do
expect(::Gitlab::AuditJsonLogger).to receive(:build).and_return(logger)
audit!
expect(logger).to have_received(:info).with(
hash_including(
'id' => AuditEvent.last.id,
'author_id' => author.id,
'author_name' => author.name,
'entity_id' => group.id,
'entity_type' => group.class.name,
'details' => kind_of(Hash)
)
)
end
context 'when overriding the create datetime' do
let(:context) do
{ name: name,
author: author,
scope: group,
target: group,
created_at: 3.weeks.ago,
authentication_event: true,
authentication_provider: provider,
message: "Signed in using standard authentication" }
end
it 'logs audit events to database', :aggregate_failures do
freeze_time do
audit!
audit_event = AuditEvent.last
expect(audit_event.author_id).to eq(author.id)
expect(audit_event.entity_id).to eq(group.id)
expect(audit_event.entity_type).to eq(group.class.name)
expect(audit_event.created_at).to eq(3.weeks.ago)
expect(audit_event.details[:target_id]).to eq(group.id)
expect(audit_event.details[:target_type]).to eq(group.class.name)
end
end
it 'logs audit events to file' do
freeze_time do
expect(::Gitlab::AuditJsonLogger).to receive(:build).and_return(logger)
audit!
expect(logger).to have_received(:info).with(
hash_including(
'id' => AuditEvent.last.id,
'author_id' => author.id,
'author_name' => author.name,
'entity_id' => group.id,
'entity_type' => group.class.name,
'details' => kind_of(Hash),
'created_at' => 3.weeks.ago.iso8601(3)
)
)
end
end
end
context 'when overriding the additional_details' do
additional_details = { action: :custom, from: false, to: true }
let(:context) do
{ name: name,
author: author,
scope: group,
target: group,
created_at: Time.zone.now,
additional_details: additional_details,
authentication_event: true,
authentication_provider: provider,
message: "Signed in using standard authentication" }
end
it 'logs audit events to database' do
freeze_time do
audit!
expect(AuditEvent.last.details).to include(additional_details)
end
end
it 'logs audit events to file' do
freeze_time do
expect(::Gitlab::AuditJsonLogger).to receive(:build).and_return(logger)
audit!
expect(logger).to have_received(:info).with(
hash_including(
'details' => hash_including('action' => 'custom', 'from' => 'false', 'to' => 'true'),
'action' => 'custom',
'from' => 'false',
'to' => 'true'
)
)
end
end
end
context 'when overriding the target_details' do
target_details = "this is my target details"
let(:context) do
{
name: name,
author: author,
scope: group,
target: group,
created_at: Time.zone.now,
target_details: target_details,
authentication_event: true,
authentication_provider: provider,
message: "Signed in using standard authentication"
}
end
it 'logs audit events to database' do
freeze_time do
audit!
audit_event = AuditEvent.last
expect(audit_event.details).to include({ target_details: target_details })
expect(audit_event.target_details).to eq(target_details)
end
end
it 'logs audit events to file' do
freeze_time do
expect(::Gitlab::AuditJsonLogger).to receive(:build).and_return(logger)
audit!
expect(logger).to have_received(:info).with(
hash_including(
'details' => hash_including('target_details' => target_details),
'target_details' => target_details
)
)
end
end
end
end
context 'when authentication event is false' do
let(:target) { group }
let(:context) do
{ name: name, author: author, scope: group,
target: target, authentication_event: false, message: "sample message" }
end
it 'does not create an authentication event' do
expect { auditor.audit(context) }.not_to change(AuthenticationEvent, :count)
end
context 'with permitted target' do
{ feature_flag: :operations_feature_flag }.each do |target_type, factory_name|
context "with #{target_type}" do
let(:target) { build_stubbed factory_name }
it 'logs audit events to database', :aggregate_failures, :freeze_time do
audit!
audit_event = AuditEvent.last
expect(audit_event.author_id).to eq(author.id)
expect(audit_event.entity_id).to eq(group.id)
expect(audit_event.entity_type).to eq(group.class.name)
expect(audit_event.created_at).to eq(Time.zone.now)
expect(audit_event.details[:target_id]).to eq(target.id)
expect(audit_event.details[:target_type]).to eq(target.class.name)
end
end
end
end
end
context 'when authentication event is invalid' do
before do
allow(AuthenticationEvent).to receive(:new).and_raise(ActiveRecord::RecordInvalid)
allow(Gitlab::ErrorTracking).to receive(:track_exception)
end
it 'tracks error' do
audit!
expect(Gitlab::ErrorTracking).to have_received(:track_exception).with(
kind_of(ActiveRecord::RecordInvalid),
{ audit_operation: name }
)
end
it 'does not throw exception' do
expect { auditor.audit(context) }.not_to raise_exception
end
end
context 'when audit events are invalid' do
before do
expect_next_instance_of(AuditEvent) do |instance|
allow(instance).to receive(:save!).and_raise(ActiveRecord::RecordInvalid)
end
allow(Gitlab::ErrorTracking).to receive(:track_exception)
end
it 'tracks error' do
audit!
expect(Gitlab::ErrorTracking).to have_received(:track_exception).with(
kind_of(ActiveRecord::RecordInvalid),
{ audit_operation: name }
)
end
it 'does not throw exception' do
expect { auditor.audit(context) }.not_to raise_exception
end
end
context 'when audit event is not saved in database due to some database infra issue' do
let(:audit!) { auditor.audit(context) }
before do
allow_any_instance_of(auditor) do |auditor_instance|
allow(auditor_instance).to receive(:log_to_database).and_return(nil)
end
end
it 'calls log_to_file_and_stream with in memory events' do
audit!
expect_any_instance_of(auditor) do |auditor_instance|
expect(auditor_instance).to receive(:log_to_file_and_stream).with(include(kind_of(AuditEvent)))
end
end
it 'does not throw exception' do
expect { auditor.audit(context) }.not_to raise_exception
end
end
end
end
|