Allow audit logging to be disabled.
authorColin Nolan <colin.nolan@sanger.ac.uk>
Tue, 7 Aug 2018 14:36:56 +0000 (15:36 +0100)
committerColin Nolan <colin.nolan@sanger.ac.uk>
Tue, 7 Aug 2018 14:55:45 +0000 (15:55 +0100)
Audit logs not written if `max_audit_log_delete_batch > 0 and max_audit_log_age == 0`.

Arvados-DCO-1.1-Signed-off-by: Colin Nolan <colin.nolan@sanger.ac.uk>

services/api/app/models/arvados_model.rb

index b9edeae06ecf93f0a1b6014eef0772224deed1a1..19f08e7297207481e934c81e1b734b4474d1e298 100644 (file)
@@ -763,36 +763,51 @@ class ArvadosModel < ActiveRecord::Base
     end
   end
 
+  def is_audit_logging_enabled?
+    return Rails.configuration.max_audit_log_age == 0
+             and Rails.configuration.max_audit_log_delete_batch > 0
+  end
+
   def log_start_state
-    @old_attributes = Marshal.load(Marshal.dump(attributes))
-    @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes))
+    if is_audit_logging_enabled?
+      @old_attributes = Marshal.load(Marshal.dump(attributes))
+      @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes))
+    end
   end
 
   def log_change(event_type)
-    log = Log.new(event_type: event_type).fill_object(self)
-    yield log
-    log.save!
-    log_start_state
+    if is_audit_logging_enabled?
+      log = Log.new(event_type: event_type).fill_object(self)
+      yield log
+      log.save!
+      log_start_state
+    end
   end
 
   def log_create
-    log_change('create') do |log|
-      log.fill_properties('old', nil, nil)
-      log.update_to self
+    if is_audit_logging_enabled?
+      log_change('create') do |log|
+        log.fill_properties('old', nil, nil)
+        log.update_to self
+      end
     end
   end
 
   def log_update
-    log_change('update') do |log|
-      log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
-      log.update_to self
+    if is_audit_logging_enabled?
+      log_change('update') do |log|
+        log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
+        log.update_to self
+      end
     end
   end
 
   def log_destroy
-    log_change('delete') do |log|
-      log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
-      log.update_to nil
+    if is_audit_logging_enabled?
+      log_change('delete') do |log|
+        log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
+        log.update_to nil
+      end
     end
   end
 end