Module: StandardAudit

Defined in:
lib/standard_audit.rb,
lib/standard_audit/engine.rb,
lib/standard_audit/version.rb,
lib/standard_audit/auditable.rb,
lib/standard_audit/operation.rb,
lib/standard_audit/subscriber.rb,
lib/standard_audit/audit_scope.rb,
lib/standard_audit/configuration.rb,
lib/standard_audit/metadata_filter.rb,
lib/standard_audit/operation/audit.rb,
app/jobs/standard_audit/cleanup_job.rb,
app/models/standard_audit/audit_log.rb,
lib/standard_audit/checks/retention.rb,
lib/standard_audit/event_subscriber.rb,
lib/standard_audit/record_reference.rb,
lib/standard_audit/reference_preloading.rb,
lib/standard_audit/sensitive_keys_dry_run.rb,
app/jobs/standard_audit/create_audit_log_job.rb,
app/models/standard_audit/application_record.rb,
lib/generators/standard_audit/install/install_generator.rb,
lib/generators/standard_audit/add_checksums/add_checksums_generator.rb,
lib/generators/standard_audit/add_previous_checksum/add_previous_checksum_generator.rb

Defined Under Namespace

Modules: AuditScope, Auditable, Checks, Generators, Operation, RecordReference, ReferencePreloading Classes: ApplicationRecord, AuditLog, CleanupJob, Configuration, CreateAuditLogJob, Engine, EventSubscriber, MetadataFilter, SensitiveKeysDryRun, Subscriber

Constant Summary collapse

RESERVED_METADATA_KEYS =

Metadata keys owned internally by StandardAudit. Never filtered by sensitive_keys even if a user adds them there.

%w[_tags _source].freeze
VERSION =
"0.11.0"

Class Method Summary collapse

Class Method Details

.baseline_configured?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/standard_audit.rb', line 153

def baseline_configured?
  !@baseline_configuration.nil?
end

.batchObject

Buffers record calls and flushes them via insert_all! on block exit. If the block raises, buffered records are dropped — only successful batches are persisted. Nested batches flush independently. Block-form record calls (with AS::Notifications) bypass the buffer and are processed normally since they don't persist records directly. Note: uses Thread.current for storage, which is not fiber-safe. Apps using async adapters (Falcon) should avoid concurrent batches.



118
119
120
121
122
123
124
125
126
127
# File 'lib/standard_audit.rb', line 118

def batch
  previous = Thread.current[:standard_audit_batch]
  buffer = Thread.current[:standard_audit_batch] = []

  yield

  flush_batch(buffer) if buffer.any?
ensure
  Thread.current[:standard_audit_batch] = previous
end

.clear_baseline_configuration!Object

Forgets the configure(baseline: true) block. Mainly for the gem's own specs and for a host that needs a genuinely pristine configuration.



149
150
151
# File 'lib/standard_audit.rb', line 149

def clear_baseline_configuration!
  @baseline_configuration = nil
end

.configObject



47
48
49
# File 'lib/standard_audit.rb', line 47

def config
  @configuration ||= Configuration.new
end

.configure(baseline: false, &block) ⇒ Object

Applies configuration to the single mutable Configuration instance.

baseline: true also remembers the block, so reset_configuration! replays it. That matters because the config object holds behaviour, not just data — before_checksum_hooks in particular. Without a baseline, a suite that installs the rspec plugin (which calls reset_configuration! before every example) silently loses write-time hooks after the first example, and the specs that would notice pass vacuously.

Idiomatic host usage, in config/initializers/standard_audit.rb:

StandardAudit.configure(baseline: true) do |config|
config.subscribe_to "myapp.**"
config.before_checksum :backfill_scope
end

Only the most recent baseline: true block is remembered; call it once, from the initializer.



39
40
41
42
43
44
45
# File 'lib/standard_audit.rb', line 39

def configure(baseline: false, &block)
  return config unless block

  @baseline_configuration = block if baseline
  block.call(config)
  config
end

.event_subscriberObject



133
134
135
# File 'lib/standard_audit.rb', line 133

def event_subscriber
  @event_subscriber ||= EventSubscriber.new
end

.record(event_type, actor: nil, target: nil, scope: nil, metadata: {}, **options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/standard_audit.rb', line 51

def record(event_type, actor: nil, target: nil, scope: nil, metadata: {}, **options)
  return unless config.enabled

  actor ||= config.current_actor_resolver.call

  if block_given?
    # Block form: instrument via ActiveSupport::Notifications and let the
    # Subscriber write the row, which it does with its own dereferencing and
    # filtering. Nothing built below would be used, so it is not built —
    # dereferencing a Relation here would load it eagerly, before the block
    # has run, purely to discard the result.
    ActiveSupport::Notifications.instrument(event_type, .merge(
      actor: actor, target: target, scope: scope
    )) do
      yield
    end
    return
  end

  # Redaction lives in MetadataFilter, shared with Subscriber, so the two
  # write paths cannot drift apart. Record dereferencing is applied on both
  # paths for the same reason: a snapshot of a whole row is as unrecoverable
  # here as it is on the notifications path.
  dereferenced = config. ? RecordReference.call() : 
   = MetadataFilter.call(dereferenced, config: config)

  attrs = {
    event_type: event_type,
    occurred_at: Time.current,
    request_id: options[:request_id] || config.current_request_id_resolver.call,
    ip_address: options[:ip_address] || config.current_ip_address_resolver.call,
    user_agent: options[:user_agent] || config.current_user_agent_resolver.call,
    session_id: options[:session_id] || config.current_session_id_resolver.call,
    metadata: 
  }

  gid_attrs = {
    actor_gid: actor&.to_global_id&.to_s,
    actor_type: actor&.class&.name,
    target_gid: target&.to_global_id&.to_s,
    target_type: target&.class&.name,
    scope_gid: scope&.to_global_id&.to_s,
    scope_type: scope&.class&.name
  }

  if batching?
    Thread.current[:standard_audit_batch] << attrs.merge(gid_attrs)
    nil
  elsif config.async
    StandardAudit::CreateAuditLogJob.perform_later(attrs.merge(gid_attrs).stringify_keys)
  else
    log = StandardAudit::AuditLog.new(attrs)
    log.actor = actor
    log.target = target
    log.scope = scope
    log.save!
    log
  end
end

.reset_configuration!(replay_baseline: true) ⇒ Object

Drops the memoized Configuration. Any block registered with configure(baseline: true) is replayed onto the fresh instance, so a per-example reset restores the app's real configuration rather than the gem defaults.



141
142
143
144
145
# File 'lib/standard_audit.rb', line 141

def reset_configuration!(replay_baseline: true)
  @configuration = nil
  @baseline_configuration&.call(config) if replay_baseline
  config
end

.subscriberObject



129
130
131
# File 'lib/standard_audit.rb', line 129

def subscriber
  @subscriber ||= Subscriber.new
end