Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
3adc8f91
Commit
3adc8f91
authored
Dec 03, 2021
by
Baodong
Committed by
Mikołaj Wawrzyniak
Dec 03, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feature: add save type to audit event service
parent
439b2200
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
3 deletions
+100
-3
app/services/audit_event_service.rb
app/services/audit_event_service.rb
+8
-3
app/services/concerns/audit_event_save_type.rb
app/services/concerns/audit_event_save_type.rb
+26
-0
ee/spec/services/audit_event_service_spec.rb
ee/spec/services/audit_event_service_spec.rb
+38
-0
spec/services/concerns/audit_event_save_type_spec.rb
spec/services/concerns/audit_event_save_type_spec.rb
+28
-0
No files found.
app/services/audit_event_service.rb
View file @
3adc8f91
# frozen_string_literal: true
class
AuditEventService
include
AuditEventSaveType
# Instantiates a new service
#
# @param [User] author the user who authors the change
...
...
@@ -10,13 +12,16 @@ class AuditEventService
# - Group: events are visible at Group and Instance level
# - User: events are visible at Instance level
# @param [Hash] details extra data of audit event
# @param [Symbol] save_type the type to save the event
# Can be selected from the following, :database, :stream, :database_and_stream .
#
# @return [AuditEventService]
def
initialize
(
author
,
entity
,
details
=
{})
def
initialize
(
author
,
entity
,
details
=
{}
,
save_type
=
:database_and_stream
)
@author
=
build_author
(
author
)
@entity
=
entity
@details
=
details
@ip_address
=
resolve_ip_address
(
@author
)
@save_type
=
save_type
end
# Builds the @details attribute for authentication
...
...
@@ -133,8 +138,8 @@ class AuditEventService
end
def
save_or_track
(
event
)
event
.
save!
stream_event_to_external_destinations
(
event
)
event
.
save!
if
should_save_database?
(
@save_type
)
stream_event_to_external_destinations
(
event
)
if
should_save_stream?
(
@save_type
)
rescue
StandardError
=>
e
Gitlab
::
ErrorTracking
.
track_exception
(
e
,
audit_event_type:
event
.
class
.
to_s
)
end
...
...
app/services/concerns/audit_event_save_type.rb
0 → 100644
View file @
3adc8f91
# frozen_string_literal: true
module
AuditEventSaveType
SAVE_TYPES
=
{
database:
0b01
,
stream:
0b10
,
database_and_stream:
0b11
}.
freeze
# def should_save_stream?(type)
# def should_save_database?(type)
[
:database
,
:stream
].
each
do
|
type
|
define_method
(
"should_save_
#{
type
}
?"
)
do
|
param_type
|
return
false
unless
save_type_valid?
(
param_type
)
# If the current type does not support query, the result of the `&` operation is 0 .
SAVE_TYPES
[
param_type
]
&
SAVE_TYPES
[
type
]
!=
0
end
end
private
def
save_type_valid?
(
type
)
SAVE_TYPES
.
key?
(
type
)
end
end
ee/spec/services/audit_event_service_spec.rb
View file @
3adc8f91
...
...
@@ -646,4 +646,42 @@ RSpec.describe AuditEventService, :request_store do
extended_audit_events:
false
)
end
describe
'save_type'
do
let_it_be
(
:group
)
{
create
(
:group
,
:nested
)
}
let_it_be
(
:project
)
{
create
(
:project
,
group:
group
)
}
before
do
stub_licensed_features
(
external_audit_events:
true
)
end
subject
(
:event
)
{
described_class
.
new
(
user
,
project
,
details
,
save_type
).
for_project
.
security_event
}
context
'with save_type of :database_and_stream'
do
let
(
:save_type
)
{
:database_and_stream
}
it
'save to database and stream'
do
expect
(
AuditEvents
::
AuditEventStreamingWorker
).
to
receive
(
:perform_async
).
once
expect
{
event
}.
to
change
(
AuditEvent
,
:count
).
by
(
1
)
end
end
context
'with save_type of :database'
do
let
(
:save_type
)
{
:database
}
it
'saves to database and is not streamed'
do
expect
(
AuditEvents
::
AuditEventStreamingWorker
).
not_to
receive
(
:perform_async
)
expect
{
event
}.
to
change
(
AuditEvent
,
:count
).
by
(
1
)
end
end
context
'with save_type of :stream'
do
let
(
:save_type
)
{
:stream
}
it
'saves to database and stream'
do
expect
(
AuditEvents
::
AuditEventStreamingWorker
).
to
receive
(
:perform_async
).
once
expect
{
event
}.
to
change
(
AuditEvent
,
:count
).
by
(
0
)
end
end
end
end
spec/services/concerns/audit_event_save_type_spec.rb
0 → 100644
View file @
3adc8f91
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
AuditEventSaveType
do
subject
(
:target
)
{
Object
.
new
.
extend
(
described_class
)
}
describe
'#should_save_database? and #should_save_stream?'
do
using
RSpec
::
Parameterized
::
TableSyntax
where
(
:query_method
,
:query_param
,
:result
)
do
:should_save_stream?
|
:stream
|
true
:should_save_stream?
|
:database_and_stream
|
true
:should_save_database?
|
:database
|
true
:should_save_database?
|
:database_and_stream
|
true
:should_save_stream?
|
:database
|
false
:should_save_stream?
|
nil
|
false
:should_save_database?
|
:stream
|
false
:should_save_database?
|
nil
|
false
end
with_them
do
it
'returns corresponding results according to the query_method and query_param'
do
expect
(
target
.
send
(
query_method
,
query_param
)).
to
eq
result
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment