Commit 7752f147 authored by Aleksei Lipniagov's avatar Aleksei Lipniagov

Merge branch '352393-user-impersonation-worker-created-at' into 'master'

Fix impersonation created_at audit event field

See merge request gitlab-org/gitlab!85082
parents 2fb01fd5 6038dc0d
...@@ -44,7 +44,7 @@ module EE ...@@ -44,7 +44,7 @@ module EE
end end
def log_audit_event def log_audit_event
::AuditEvents::UserImpersonationEventCreateWorker.perform_async(current_user.id, user.id, request.remote_ip, 'started') ::AuditEvents::UserImpersonationEventCreateWorker.perform_async(current_user.id, user.id, request.remote_ip, 'started', DateTime.current)
end end
def allowed_user_params def allowed_user_params
......
...@@ -38,7 +38,7 @@ module EE ...@@ -38,7 +38,7 @@ module EE
end end
def log_audit_event def log_audit_event
::AuditEvents::UserImpersonationEventCreateWorker.perform_async(impersonator.id, current_user.id, request.remote_ip, 'stopped') ::AuditEvents::UserImpersonationEventCreateWorker.perform_async(impersonator.id, current_user.id, request.remote_ip, 'stopped', DateTime.current)
end end
def set_current_ip_address(&block) def set_current_ip_address(&block)
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
module AuditEvents module AuditEvents
class ImpersonationAuditEventService < ::AuditEventService class ImpersonationAuditEventService < ::AuditEventService
def initialize(author, ip_address, message) def initialize(author, ip_address, message, created_at)
super(author, author, { super(author, author, {
action: :custom, action: :custom,
custom_message: message, custom_message: message,
ip_address: ip_address ip_address: ip_address
}) }, :database_and_stream, created_at)
end end
end end
end end
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
# and for all of a user's groups when the user is impersonated. # and for all of a user's groups when the user is impersonated.
module AuditEvents module AuditEvents
class UserImpersonationGroupAuditEventService class UserImpersonationGroupAuditEventService
def initialize(impersonator:, user:, remote_ip:, action: :started) def initialize(impersonator:, user:, remote_ip:, action: :started, created_at:)
@impersonator = impersonator @impersonator = impersonator
@user = user @user = user
@remote_ip = remote_ip @remote_ip = remote_ip
@action = action.to_s @action = action.to_s
@created_at = created_at
end end
def execute def execute
...@@ -17,7 +18,7 @@ module AuditEvents ...@@ -17,7 +18,7 @@ module AuditEvents
end end
def log_instance_audit_event def log_instance_audit_event
AuditEvents::ImpersonationAuditEventService.new(@impersonator, @remote_ip, "#{@action.capitalize} Impersonation") AuditEvents::ImpersonationAuditEventService.new(@impersonator, @remote_ip, "#{@action.capitalize} Impersonation", @created_at)
.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
...@@ -30,7 +31,8 @@ module AuditEvents ...@@ -30,7 +31,8 @@ module AuditEvents
author: @impersonator, author: @impersonator,
scope: group, scope: group,
target: @user, target: @user,
message: "Instance administrator #{@action} impersonation of #{@user.username}" message: "Instance administrator #{@action} impersonation of #{@user.username}",
created_at: @created_at
} }
::Gitlab::Audit::Auditor.audit(audit_context) ::Gitlab::Audit::Auditor.audit(audit_context)
......
...@@ -7,11 +7,12 @@ module AuditEvents ...@@ -7,11 +7,12 @@ module AuditEvents
data_consistency :sticky data_consistency :sticky
feature_category :audit_events feature_category :audit_events
def perform(impersonator_id, user_id, remote_ip, action) def perform(impersonator_id, user_id, remote_ip, action, created_at)
::AuditEvents::UserImpersonationGroupAuditEventService.new(impersonator: User.find_by_id(impersonator_id), ::AuditEvents::UserImpersonationGroupAuditEventService.new(impersonator: User.find_by_id(impersonator_id),
user: User.find_by_id(user_id), user: User.find_by_id(user_id),
remote_ip: remote_ip, remote_ip: remote_ip,
action: action).execute action: action,
created_at: created_at).execute
end end
end end
end end
...@@ -19,7 +19,7 @@ RSpec.describe Admin::ImpersonationsController do ...@@ -19,7 +19,7 @@ RSpec.describe Admin::ImpersonationsController do
end end
it 'enqueues a new worker' do it 'enqueues a new worker' do
expect(AuditEvents::UserImpersonationEventCreateWorker).to receive(:perform_async).with(impersonator.id, user.id, anything, 'stopped').once expect(AuditEvents::UserImpersonationEventCreateWorker).to receive(:perform_async).with(impersonator.id, user.id, anything, 'stopped', DateTime.current).once
delete :destroy delete :destroy
end end
......
...@@ -109,7 +109,7 @@ RSpec.describe Admin::UsersController do ...@@ -109,7 +109,7 @@ RSpec.describe Admin::UsersController do
end end
it 'enqueues a new worker' do it 'enqueues a new worker' do
expect(AuditEvents::UserImpersonationEventCreateWorker).to receive(:perform_async).with(admin.id, user.id, anything, 'started').once expect(AuditEvents::UserImpersonationEventCreateWorker).to receive(:perform_async).with(admin.id, user.id, anything, 'started', DateTime.current).once
post :impersonate, params: { id: user.username } post :impersonate, params: { id: user.username }
end end
......
...@@ -7,7 +7,7 @@ RSpec.describe AuditEvents::ImpersonationAuditEventService do ...@@ -7,7 +7,7 @@ RSpec.describe AuditEvents::ImpersonationAuditEventService do
let(:ip_address) { '127.0.0.1' } let(:ip_address) { '127.0.0.1' }
let(:message) { 'Impersonation Started' } let(:message) { 'Impersonation Started' }
let(:logger) { instance_double(Gitlab::AuditJsonLogger) } let(:logger) { instance_double(Gitlab::AuditJsonLogger) }
let(:service) { described_class.new(impersonator, ip_address, message) } let(:service) { described_class.new(impersonator, ip_address, message, 3.weeks.ago) }
describe '#security_event' do describe '#security_event' do
before do before do
......
...@@ -7,7 +7,7 @@ RSpec.describe AuditEvents::UserImpersonationGroupAuditEventService do ...@@ -7,7 +7,7 @@ RSpec.describe AuditEvents::UserImpersonationGroupAuditEventService do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:admin) { create(:admin) } let_it_be(:admin) { create(:admin) }
let(:service) { described_class.new(impersonator: admin, user: user, remote_ip: '111.112.11.2', action: :started) } let(:service) { described_class.new(impersonator: admin, user: user, remote_ip: '111.112.11.2', action: :started, created_at: 3.weeks.ago) }
before do before do
stub_licensed_features(audit_events: true) stub_licensed_features(audit_events: true)
...@@ -21,13 +21,17 @@ RSpec.describe AuditEvents::UserImpersonationGroupAuditEventService do ...@@ -21,13 +21,17 @@ RSpec.describe AuditEvents::UserImpersonationGroupAuditEventService do
end end
it 'creates audit events for both the instance and group level' do it 'creates audit events for both the instance and group level' do
freeze_time do
expect { service.execute }.to change { AuditEvent.count }.by(2) expect { service.execute }.to change { AuditEvent.count }.by(2)
event = AuditEvent.first event = AuditEvent.first
expect(event.details[:custom_message]).to eq("Started Impersonation") expect(event.details[:custom_message]).to eq("Started Impersonation")
expect(event.created_at).to eq(3.weeks.ago)
group_audit_event = AuditEvent.last group_audit_event = AuditEvent.last
expect(group_audit_event.details[:custom_message]).to eq("Instance administrator started impersonation of #{user.username}") expect(group_audit_event.details[:custom_message]).to eq("Instance administrator started impersonation of #{user.username}")
expect(group_audit_event.created_at).to eq(3.weeks.ago)
end
end end
end end
......
...@@ -12,14 +12,17 @@ RSpec.describe AuditEvents::UserImpersonationEventCreateWorker do ...@@ -12,14 +12,17 @@ RSpec.describe AuditEvents::UserImpersonationEventCreateWorker do
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
it 'invokes the UserImpersonationGroupAuditEventService' do it 'invokes the UserImpersonationGroupAuditEventService' do
freeze_time do
expect(::AuditEvents::UserImpersonationGroupAuditEventService).to receive(:new).with( expect(::AuditEvents::UserImpersonationGroupAuditEventService).to receive(:new).with(
impersonator: impersonator, impersonator: impersonator,
user: user, user: user,
remote_ip: '111.112.11.2', remote_ip: '111.112.11.2',
action: action action: action,
created_at: 2.weeks.ago
).and_call_original ).and_call_original
subject.perform(impersonator.id, user.id, '111.112.11.2', action) subject.perform(impersonator.id, user.id, '111.112.11.2', action, 2.weeks.ago)
end
end end
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