Skip to content

Add support for standard method call interface on Auditor

What does this MR do?

Add support to record single audit event via Auditor

This change extends the public interface of the Gitlab::Audit::Auditor class to allow recording single audit event using standard method call. This allows more flexibility when instrumenting audit events.

After this MR, we can instrument audit events using 2 methods:

  1. Using block to track multiple audit events
  2. Using standard method call to track single audit event

How to test it

From console, we can interact with the new API

pry(main)> context = { author: User.first, scope: Project.first, target: Project.first, message: 'Something hits the fan' }
pry(main)> Gitlab::Audit::Auditor.audit(context)
  License Load (0.6ms)  SELECT "licenses".* FROM "licenses" ORDER BY "licenses"."id" DESC LIMIT 100 /*application:console,line:/Users/tanle/.gem/ruby/2.7.2/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `exec_query'*/
  Route Load (0.3ms)  SELECT "routes".* FROM "routes" WHERE "routes"."source_id" = 1 AND "routes"."source_type" = 'Project' LIMIT 1 /*application:console,line:/Users/tanle/.gem/ruby/2.7.2/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `exec_query'*/
   (0.2ms)  BEGIN /*application:console,line:/Users/tanle/.gem/ruby/2.7.2/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:150:in `begin_db_transaction'*/
  AuditEvent Insert (0.3ms)  INSERT INTO "audit_events" ("author_id","entity_id","entity_type","details","ip_address","author_name","entity_path","target_details","created_at","target_type","target_id") VALUES (1, 1, 'Project', '---
:author_name: Administrator
:target_id: 1
:target_type: Project
:target_details: Gitlab Test
:custom_message: Something hits the fan
:ip_address: 127.0.0.1
:entity_path: gitlab-org/gitlab-test
', '127.0.0.1/32', 'Administrator', 'gitlab-org/gitlab-test', 'Gitlab Test', '2021-04-30 09:50:02.384831', 'Project', 1) /*application:console,line:/Users/tanle/.gem/ruby/2.7.2/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `exec_query'*/
   (0.9ms)  COMMIT /*application:console,line:/Users/tanle/.gem/ruby/2.7.2/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `commit_db_transaction'*/
[
    [0] #<AuditEvent:0x00007fc119ae8878> {
                    :id => nil,
             :author_id => 1,
             :entity_id => 1,
           :entity_type => "Project",
               :details => {
               :author_name => "Administrator",
                 :target_id => 1,
               :target_type => "Project",
            :target_details => "Gitlab Test",
            :custom_message => "Something hits the fan",
                :ip_address => "127.0.0.1",
               :entity_path => "gitlab-org/gitlab-test"
        },
            :ip_address => "127.0.0.1",
           :author_name => "Administrator",
           :entity_path => "gitlab-org/gitlab-test",
        :target_details => "Gitlab Test",
            :created_at => Fri, 30 Apr 2021 09:50:02 UTC +00:00,
           :target_type => "Project",
             :target_id => 1
    }
]

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • [-] Label as security and @ mention @gitlab-com/gl-security/appsec
  • [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • [-] Security reports checked/validated by a reviewer from the AppSec team

Related to #329268 (closed)

Edited by Tan Le

Merge request reports

Loading