Commit 55f387b3 authored by Max Woolf's avatar Max Woolf

Merge branch 'ld-move-audit_events-services-out-of-EE-namespace' into 'master'

Move AuditEvents services out of EE namespace [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!62423
parents 9c76d457 e8586eac
...@@ -32,7 +32,7 @@ module EE ...@@ -32,7 +32,7 @@ module EE
end end
def log_audit_event def log_audit_event
EE::AuditEvents::ImpersonationAuditEventService.new(current_user, request.remote_ip, 'Started Impersonation') AuditEvents::ImpersonationAuditEventService.new(current_user, request.remote_ip, 'Started Impersonation')
.for_user(full_path: user.username, entity_id: user.id).security_event .for_user(full_path: user.username, entity_id: user.id).security_event
end end
......
...@@ -32,7 +32,7 @@ module EE ...@@ -32,7 +32,7 @@ module EE
end end
def log_audit_event def log_audit_event
EE::AuditEvents::ImpersonationAuditEventService.new(impersonator, request.remote_ip, 'Stopped Impersonation') AuditEvents::ImpersonationAuditEventService.new(impersonator, request.remote_ip, 'Stopped Impersonation')
.for_user(full_path: current_user.username, entity_id: current_user.id).security_event .for_user(full_path: current_user.username, entity_id: current_user.id).security_event
end end
......
# frozen_string_literal: true
module AuditEvents
class BulkInsertService
BATCH_SIZE = 100
# service_collection - An array of audit event services that must respond to:
# - enabled?
# - attributes (Hash of AuditEvent attributes)
# - write_log
def initialize(service_collection)
@service_collection = service_collection
end
def execute
collection = @service_collection.select(&:enabled?)
return if collection.empty?
collection.in_groups_of(BATCH_SIZE, false) do |services|
::Gitlab::Database.bulk_insert(::AuditEvent.table_name, services.map(&:attributes)) # rubocop:disable Gitlab/BulkInsert
services.each(&:log_security_event_to_file)
end
end
end
end
# frozen_string_literal: true
module AuditEvents
class CustomAuditEventService < ::AuditEventService
def initialize(author, entity, ip_address, custom_message)
super(author, entity, {
action: :custom,
custom_message: custom_message,
ip_address: ip_address
})
end
end
end
# frozen_string_literal: true
module AuditEvents
class ImpersonationAuditEventService < ::AuditEventService
def initialize(author, ip_address, message)
super(author, author, {
action: :custom,
custom_message: message,
ip_address: ip_address
})
end
end
end
# frozen_string_literal: true
module AuditEvents
class ProtectedBranchAuditEventService < ::AuditEventService
def initialize(author, protected_branch, action)
push_access_levels = protected_branch.push_access_levels.map(&:humanize)
merge_access_levels = protected_branch.merge_access_levels.map(&:humanize)
super(author, protected_branch.project,
action => 'protected_branch',
author_name: author.name,
target_id: protected_branch.id,
target_type: protected_branch.class.name,
target_details: protected_branch.name,
push_access_levels: push_access_levels,
merge_access_levels: merge_access_levels
)
end
end
end
# frozen_string_literal: true
module AuditEvents
class ReleaseArtifactsDownloadedAuditEventService < ReleaseAuditEventService
def message
'Repository External Resource Download Started'
end
end
end
# frozen_string_literal: true
module AuditEvents
class ReleaseAssociateMilestoneAuditEventService < ReleaseAuditEventService
def message
milestones = @release.milestone_titles
milestones = "[none]" if milestones.blank?
"Milestones associated with release changed to #{milestones}"
end
end
end
# frozen_string_literal: true
module AuditEvents
class ReleaseAuditEventService < ::AuditEventService
attr_reader :release
def initialize(author, entity, ip_address, release)
@release = release
super(author, entity, {
custom_message: message,
ip_address: ip_address,
target_id: release.id,
target_type: release.class.name,
target_details: release.name
})
end
def message
nil
end
end
end
# frozen_string_literal: true
module AuditEvents
class ReleaseCreatedAuditEventService < ReleaseAuditEventService
def message
simple_message = "Created Release #{release.tag}"
milestone_count = release.milestones.count
if milestone_count > 0
"#{simple_message} with #{'Milestone'.pluralize(milestone_count)} #{release.milestone_titles}"
else
simple_message
end
end
end
end
# frozen_string_literal: true
module AuditEvents
class ReleaseUpdatedAuditEventService < ReleaseAuditEventService
def message
"Updated Release #{release.tag}"
end
end
end
# frozen_string_literal: true
module AuditEvents
class RepositoryDownloadStartedAuditEventService < CustomAuditEventService
def initialize(author, entity, ip_address)
super(author, entity, ip_address, 'Repository Download Started')
end
end
end
# frozen_string_literal: true
module AuditEvents
class RepositoryPushAuditEventService < ::AuditEventService
def initialize(author, project, target_ref, from, to)
super(author, project, {
updated_ref: ::Gitlab::Git.ref_name(target_ref),
author_name: author.name,
from: Commit.truncate_sha(from),
to: Commit.truncate_sha(to),
target_details: project.full_path
})
end
def attributes
base_payload.merge(created_at: DateTime.current,
details: @details.to_yaml)
end
def enabled?
super && @entity.push_audit_events_enabled?
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class BulkInsertService
BATCH_SIZE = 100
# service_collection - An array of audit event services that must respond to:
# - enabled?
# - attributes (Hash of AuditEvent attributes)
# - write_log
def initialize(service_collection)
@service_collection = service_collection
end
def execute
collection = @service_collection.select(&:enabled?)
return if collection.empty?
collection.in_groups_of(BATCH_SIZE, false) do |services|
::Gitlab::Database.bulk_insert(::AuditEvent.table_name, services.map(&:attributes)) # rubocop:disable Gitlab/BulkInsert
services.each(&:log_security_event_to_file)
end
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class CustomAuditEventService < ::AuditEventService
def initialize(author, entity, ip_address, custom_message)
super(author, entity, {
action: :custom,
custom_message: custom_message,
ip_address: ip_address
})
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ImpersonationAuditEventService < ::AuditEventService
def initialize(author, ip_address, message)
super(author, author, {
action: :custom,
custom_message: message,
ip_address: ip_address
})
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ProtectedBranchAuditEventService < ::AuditEventService
def initialize(author, protected_branch, action)
push_access_levels = protected_branch.push_access_levels.map(&:humanize)
merge_access_levels = protected_branch.merge_access_levels.map(&:humanize)
super(author, protected_branch.project,
action => 'protected_branch',
author_name: author.name,
target_id: protected_branch.id,
target_type: protected_branch.class.name,
target_details: protected_branch.name,
push_access_levels: push_access_levels,
merge_access_levels: merge_access_levels
)
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ReleaseArtifactsDownloadedAuditEventService < ReleaseAuditEventService
def message
'Repository External Resource Download Started'
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ReleaseAssociateMilestoneAuditEventService < ReleaseAuditEventService
def message
milestones = @release.milestone_titles
milestones = "[none]" if milestones.blank?
"Milestones associated with release changed to #{milestones}"
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ReleaseAuditEventService < ::AuditEventService
attr_reader :release
def initialize(author, entity, ip_address, release)
@release = release
super(author, entity, {
custom_message: message,
ip_address: ip_address,
target_id: release.id,
target_type: release.class.name,
target_details: release.name
})
end
def message
nil
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ReleaseCreatedAuditEventService < ReleaseAuditEventService
def message
simple_message = "Created Release #{release.tag}"
milestone_count = release.milestones.count
if milestone_count > 0
"#{simple_message} with #{'Milestone'.pluralize(milestone_count)} #{release.milestone_titles}"
else
simple_message
end
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class ReleaseUpdatedAuditEventService < ReleaseAuditEventService
def message
"Updated Release #{release.tag}"
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class RepositoryDownloadStartedAuditEventService < CustomAuditEventService
def initialize(author, entity, ip_address)
super(author, entity, ip_address, 'Repository Download Started')
end
end
end
end
# frozen_string_literal: true
module EE
module AuditEvents
class RepositoryPushAuditEventService < ::AuditEventService
def initialize(author, project, target_ref, from, to)
super(author, project, {
updated_ref: ::Gitlab::Git.ref_name(target_ref),
author_name: author.name,
from: Commit.truncate_sha(from),
to: Commit.truncate_sha(to),
target_details: project.full_path
})
end
def attributes
base_payload.merge(created_at: DateTime.current,
details: @details.to_yaml)
end
def enabled?
super && @entity.push_audit_events_enabled?
end
end
end
end
...@@ -5,7 +5,7 @@ module EE ...@@ -5,7 +5,7 @@ module EE
module Loggable module Loggable
def log_audit_event(protected_branch_service, action) def log_audit_event(protected_branch_service, action)
if protected_branch_service.errors.blank? if protected_branch_service.errors.blank?
::EE::AuditEvents::ProtectedBranchAuditEventService ::AuditEvents::ProtectedBranchAuditEventService
.new(current_user, protected_branch_service, action) .new(current_user, protected_branch_service, action)
.security_event .security_event
end end
......
...@@ -31,7 +31,7 @@ module Groups ...@@ -31,7 +31,7 @@ module Groups
end end
def log_audit_event def log_audit_event
EE::AuditEvents::CustomAuditEventService.new( AuditEvents::CustomAuditEventService.new(
current_user, current_user,
group, group,
nil, nil,
......
...@@ -28,7 +28,7 @@ module Groups ...@@ -28,7 +28,7 @@ module Groups
end end
def log_audit_event def log_audit_event
EE::AuditEvents::CustomAuditEventService.new( AuditEvents::CustomAuditEventService.new(
current_user, current_user,
group, group,
nil, nil,
......
...@@ -16,7 +16,7 @@ class RepositoryPushAuditEventWorker # rubocop:disable Scalability/IdempotentWor ...@@ -16,7 +16,7 @@ class RepositoryPushAuditEventWorker # rubocop:disable Scalability/IdempotentWor
after = change['after'] after = change['after']
ref = change['ref'] ref = change['ref']
service = EE::AuditEvents::RepositoryPushAuditEventService service = AuditEvents::RepositoryPushAuditEventService
.new(user, project, ref, before, after) .new(user, project, ref, before, after)
.tap { |event| event.prepare_security_event } .tap { |event| event.prepare_security_event }
...@@ -25,6 +25,6 @@ class RepositoryPushAuditEventWorker # rubocop:disable Scalability/IdempotentWor ...@@ -25,6 +25,6 @@ class RepositoryPushAuditEventWorker # rubocop:disable Scalability/IdempotentWor
service if service.enabled? service if service.enabled?
end.compact! end.compact!
EE::AuditEvents::BulkInsertService.new(changes).execute AuditEvents::BulkInsertService.new(changes).execute
end end
end end
...@@ -130,7 +130,7 @@ module EE ...@@ -130,7 +130,7 @@ module EE
override :send_git_archive override :send_git_archive
def send_git_archive(repository, **kwargs) def send_git_archive(repository, **kwargs)
EE::AuditEvents::RepositoryDownloadStartedAuditEventService.new( AuditEvents::RepositoryDownloadStartedAuditEventService.new(
current_user, current_user,
repository.project, repository.project,
ip_address ip_address
......
...@@ -34,7 +34,7 @@ module EE ...@@ -34,7 +34,7 @@ module EE
override :log_release_created_audit_event override :log_release_created_audit_event
def log_release_created_audit_event(release) def log_release_created_audit_event(release)
EE::AuditEvents::ReleaseCreatedAuditEventService.new( AuditEvents::ReleaseCreatedAuditEventService.new(
current_user, current_user,
user_project, user_project,
request.ip, request.ip,
...@@ -44,7 +44,7 @@ module EE ...@@ -44,7 +44,7 @@ module EE
override :log_release_updated_audit_event override :log_release_updated_audit_event
def log_release_updated_audit_event def log_release_updated_audit_event
EE::AuditEvents::ReleaseUpdatedAuditEventService.new( AuditEvents::ReleaseUpdatedAuditEventService.new(
current_user, current_user,
user_project, user_project,
request.ip, request.ip,
...@@ -54,7 +54,7 @@ module EE ...@@ -54,7 +54,7 @@ module EE
override :log_release_milestones_updated_audit_event override :log_release_milestones_updated_audit_event
def log_release_milestones_updated_audit_event def log_release_milestones_updated_audit_event
EE::AuditEvents::ReleaseAssociateMilestoneAuditEventService.new( AuditEvents::ReleaseAssociateMilestoneAuditEventService.new(
current_user, current_user,
user_project, user_project,
request.ip, request.ip,
......
...@@ -44,7 +44,7 @@ RSpec.describe 'Admin::AuditLogs', :js do ...@@ -44,7 +44,7 @@ RSpec.describe 'Admin::AuditLogs', :js do
let(:release) { create(:release, project: project, tag: 'v0.1', author: user) } let(:release) { create(:release, project: project, tag: 'v0.1', author: user) }
before do before do
EE::AuditEvents::ReleaseCreatedAuditEventService.new(user, project, '127.0.0.1', release).security_event AuditEvents::ReleaseCreatedAuditEventService.new(user, project, '127.0.0.1', release).security_event
end end
it 'shows the related audit event' do it 'shows the related audit event' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::BulkInsertService do RSpec.describe AuditEvents::BulkInsertService do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:entity) { create(:project) } let(:entity) { create(:project) }
let(:entity_type) { 'Project' } let(:entity_type) { 'Project' }
...@@ -11,7 +11,7 @@ RSpec.describe EE::AuditEvents::BulkInsertService do ...@@ -11,7 +11,7 @@ RSpec.describe EE::AuditEvents::BulkInsertService do
let(:to) { 'a7bce79c3a8cb367877b53e315799b69acb700fo' } let(:to) { 'a7bce79c3a8cb367877b53e315799b69acb700fo' }
let!(:collection) do let!(:collection) do
Array.new(3).map do Array.new(3).map do
EE::AuditEvents::RepositoryPushAuditEventService.new(user, entity, target_ref, from, to) AuditEvents::RepositoryPushAuditEventService.new(user, entity, target_ref, from, to)
end end
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::CustomAuditEventService do RSpec.describe AuditEvents::CustomAuditEventService do
describe '#security_event' do describe '#security_event' do
include_examples 'logs the custom audit event' do include_examples 'logs the custom audit event' do
let(:user) { create(:user) } let(:user) { create(:user) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ImpersonationAuditEventService do RSpec.describe AuditEvents::ImpersonationAuditEventService do
let(:impersonator) { create(:user) } let(:impersonator) { create(:user) }
let(:ip_address) { '127.0.0.1' } let(:ip_address) { '127.0.0.1' }
let(:message) { 'Impersonation Started' } let(:message) { 'Impersonation Started' }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ProtectedBranchAuditEventService, :request_store do RSpec.describe AuditEvents::ProtectedBranchAuditEventService, :request_store do
let(:merge_level) { 'Maintainers' } let(:merge_level) { 'Maintainers' }
let(:push_level) { 'No one' } let(:push_level) { 'No one' }
let_it_be(:author) { create(:user, :with_sign_ins) } let_it_be(:author) { create(:user, :with_sign_ins) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ReleaseArtifactsDownloadedAuditEventService do RSpec.describe AuditEvents::ReleaseArtifactsDownloadedAuditEventService do
describe '#security_event' do describe '#security_event' do
include_examples 'logs the release audit event' do include_examples 'logs the release audit event' do
let(:release) { create(:release, project: entity) } let(:release) { create(:release, project: entity) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ReleaseAssociateMilestoneAuditEventService do RSpec.describe AuditEvents::ReleaseAssociateMilestoneAuditEventService do
describe '#security_event' do describe '#security_event' do
context 'with no milestones' do context 'with no milestones' do
include_examples 'logs the release audit event' do include_examples 'logs the release audit event' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ReleaseCreatedAuditEventService do RSpec.describe AuditEvents::ReleaseCreatedAuditEventService do
describe '#security_event' do describe '#security_event' do
context 'with no milestones' do context 'with no milestones' do
include_examples 'logs the release audit event' do include_examples 'logs the release audit event' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::ReleaseUpdatedAuditEventService do RSpec.describe AuditEvents::ReleaseUpdatedAuditEventService do
describe '#security_event' do describe '#security_event' do
include_examples 'logs the release audit event' do include_examples 'logs the release audit event' do
let(:release) { create(:release, project: entity) } let(:release) { create(:release, project: entity) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::RepositoryDownloadStartedAuditEventService do RSpec.describe AuditEvents::RepositoryDownloadStartedAuditEventService do
describe '#security_event' do describe '#security_event' do
include_examples 'logs the custom audit event' do include_examples 'logs the custom audit event' do
let(:user) { create(:user) } let(:user) { create(:user) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::AuditEvents::RepositoryPushAuditEventService do RSpec.describe AuditEvents::RepositoryPushAuditEventService do
let(:user) { create(:user, :with_sign_ins) } let(:user) { create(:user, :with_sign_ins) }
let(:entity) { create(:project) } let(:entity) { create(:project) }
let(:entity_type) { 'Project' } let(:entity_type) { 'Project' }
......
...@@ -67,7 +67,7 @@ RSpec.describe RepositoryPushAuditEventWorker do ...@@ -67,7 +67,7 @@ RSpec.describe RepositoryPushAuditEventWorker do
end end
it 'does not create events' do it 'does not create events' do
expect_next_instance_of(EE::AuditEvents::RepositoryPushAuditEventService) do |instance| expect_next_instance_of(AuditEvents::RepositoryPushAuditEventService) do |instance|
expect(instance).to receive(:enabled?) { false } expect(instance).to receive(:enabled?) { false }
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