debian-mirror-gitlab/app/services/audit_event_service.rb

89 lines
2.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
class AuditEventService
2020-04-08 14:13:33 +05:30
# Instantiates a new service
#
# @param [User] author the user who authors the change
# @param [User, Project, Group] entity the scope which audit event belongs to
# This param is also used to determine the visibility of the audit event.
# - Project: events are visible at Project and Instance level
# - Group: events are visible at Group and Instance level
# - User: events are visible at Instance level
# @param [Hash] details extra data of audit event
#
# @return [AuditEventService]
2015-09-11 14:41:01 +05:30
def initialize(author, entity, details = {})
2020-04-22 19:07:51 +05:30
@author = build_author(author)
2020-04-08 14:13:33 +05:30
@entity = entity
@details = details
2020-07-28 23:09:34 +05:30
@ip_address = (@details[:ip_address].presence || @author.current_sign_in_ip)
2015-09-11 14:41:01 +05:30
end
2020-04-08 14:13:33 +05:30
# Builds the @details attribute for authentication
#
# This uses the @author as the target object being audited
#
# @return [AuditEventService]
2015-09-11 14:41:01 +05:30
def for_authentication
@details = {
with: @details[:with],
target_id: @author.id,
2016-08-24 12:49:21 +05:30
target_type: 'User',
2017-09-10 17:25:29 +05:30
target_details: @author.name
2015-09-11 14:41:01 +05:30
}
self
end
2020-04-08 14:13:33 +05:30
# Writes event to a file and creates an event record in DB
#
# @return [SecurityEvent] persited if saves and non-persisted if fails
2015-09-11 14:41:01 +05:30
def security_event
2018-12-13 13:39:08 +05:30
log_security_event_to_file
log_security_event_to_database
end
2020-04-08 14:13:33 +05:30
# Writes event to a file
2019-12-04 20:38:33 +05:30
def log_security_event_to_file
file_logger.info(base_payload.merge(formatted_details))
end
2018-12-13 13:39:08 +05:30
private
2020-07-28 23:09:34 +05:30
attr_reader :ip_address
2020-04-22 19:07:51 +05:30
def build_author(author)
2020-05-24 23:13:21 +05:30
case author
when User
author.impersonated? ? Gitlab::Audit::ImpersonatedAuthor.new(author) : author
2020-04-22 19:07:51 +05:30
else
Gitlab::Audit::UnauthenticatedAuthor.new(name: author)
end
end
2018-12-13 13:39:08 +05:30
def base_payload
{
2015-09-11 14:41:01 +05:30
author_id: @author.id,
2020-07-28 23:09:34 +05:30
author_name: @author.name,
2015-09-11 14:41:01 +05:30
entity_id: @entity.id,
2018-12-13 13:39:08 +05:30
entity_type: @entity.class.name
}
end
def file_logger
@file_logger ||= Gitlab::AuditJsonLogger.build
end
2019-09-30 21:07:59 +05:30
def formatted_details
@details.merge(@details.slice(:from, :to).transform_values(&:to_s))
end
2018-12-13 13:39:08 +05:30
def log_security_event_to_database
2020-03-13 15:44:24 +05:30
return if Gitlab::Database.read_only?
2018-12-13 13:39:08 +05:30
SecurityEvent.create(base_payload.merge(details: @details))
2015-09-11 14:41:01 +05:30
end
end
2019-12-04 20:38:33 +05:30
AuditEventService.prepend_if_ee('EE::AuditEventService')