Commit 8d5bc964 authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch 'feature/add_save_type_to_audit_event_service' into 'master'

Feature: add save type to audit event service

See merge request gitlab-org/gitlab!74757
parents a3542f83 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
......
# 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
......@@ -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
# 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment