Add support for JSON logging for audit events
This will add audit_json.log that writes one line per audit event. For
example:
{
"severity":"INFO",
"time":"2018-10-17T17:38:22.523Z",
"author_id":3,
"entity_id":2,
"entity_type":"Project",
"change":"visibility",
"from":"Private",
"to":"Public",
"author_name":"John Doe4",
"target_id":2,
"target_type":"Project",
"target_details":"namespace2/project2"
}
2018-10-18 15:50:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe AuditEventService do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project_member) { create(:project_member, user: user) }
|
|
|
|
let(:service) { described_class.new(user, project, { action: :destroy }) }
|
|
|
|
let(:logger) { instance_double(Gitlab::AuditJsonLogger) }
|
|
|
|
|
|
|
|
describe '#security_event' do
|
|
|
|
it 'creates an event and logs to a file' do
|
2019-07-03 18:58:05 -04:00
|
|
|
expect(service).to receive(:file_logger).and_return(logger)
|
Add support for JSON logging for audit events
This will add audit_json.log that writes one line per audit event. For
example:
{
"severity":"INFO",
"time":"2018-10-17T17:38:22.523Z",
"author_id":3,
"entity_id":2,
"entity_type":"Project",
"change":"visibility",
"from":"Private",
"to":"Public",
"author_name":"John Doe4",
"target_id":2,
"target_type":"Project",
"target_details":"namespace2/project2"
}
2018-10-18 15:50:21 -04:00
|
|
|
expect(logger).to receive(:info).with(author_id: user.id,
|
|
|
|
entity_id: project.id,
|
|
|
|
entity_type: "Project",
|
|
|
|
action: :destroy)
|
|
|
|
|
|
|
|
expect { service.security_event }.to change(SecurityEvent, :count).by(1)
|
|
|
|
end
|
2019-07-03 18:58:05 -04:00
|
|
|
|
|
|
|
it 'formats from and to fields' do
|
|
|
|
service = described_class.new(
|
|
|
|
user, project,
|
|
|
|
{
|
|
|
|
from: true,
|
|
|
|
to: false,
|
|
|
|
action: :create,
|
|
|
|
target_id: 1
|
|
|
|
})
|
|
|
|
expect(service).to receive(:file_logger).and_return(logger)
|
|
|
|
expect(logger).to receive(:info).with(author_id: user.id,
|
|
|
|
entity_type: 'Project',
|
|
|
|
entity_id: project.id,
|
|
|
|
from: 'true',
|
|
|
|
to: 'false',
|
|
|
|
action: :create,
|
|
|
|
target_id: 1)
|
|
|
|
|
|
|
|
expect { service.security_event }.to change(SecurityEvent, :count).by(1)
|
|
|
|
|
|
|
|
details = SecurityEvent.last.details
|
|
|
|
expect(details[:from]).to be true
|
|
|
|
expect(details[:to]).to be false
|
|
|
|
expect(details[:action]).to eq(:create)
|
|
|
|
expect(details[:target_id]).to eq(1)
|
|
|
|
end
|
Add support for JSON logging for audit events
This will add audit_json.log that writes one line per audit event. For
example:
{
"severity":"INFO",
"time":"2018-10-17T17:38:22.523Z",
"author_id":3,
"entity_id":2,
"entity_type":"Project",
"change":"visibility",
"from":"Private",
"to":"Public",
"author_name":"John Doe4",
"target_id":2,
"target_type":"Project",
"target_details":"namespace2/project2"
}
2018-10-18 15:50:21 -04:00
|
|
|
end
|
|
|
|
end
|