Class: Vangrail::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/audit.rb

Overview

In-memory audit stream that hashes labelled values before serialization.

Instance Method Summary collapse

Constructor Details

#initialize(clock: -> { Time.now.utc }) ⇒ AuditLog

Returns a new instance of AuditLog.



27
28
29
30
31
# File 'lib/vangrail/audit.rb', line 27

def initialize(clock: -> { Time.now.utc })
  @clock = clock
  @events = []
  @mutex = Mutex.new
end

Instance Method Details

#eventsObject



46
47
48
# File 'lib/vangrail/audit.rb', line 46

def events
  @mutex.synchronize { @events.dup.freeze }
end

#record(type, **data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vangrail/audit.rb', line 33

def record(type, **data)
  @mutex.synchronize do
    event = AuditEvent.new(
      id: format('event-%06d', @events.length + 1),
      type: type,
      timestamp: timestamp,
      data: sanitize(data),
    )
    @events << event
    event
  end
end

#record_authorization(call:, allowed:, grant: nil, reason_code: nil, reason: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/vangrail/audit.rb', line 64

def record_authorization(call:, allowed:, grant: nil, reason_code: nil, reason: nil)
  record(
    :authorization,
    call_id: call.id,
    grant_id: grant&.id,
    allowed: allowed,
    reason_code: reason_code,
    reason: reason,
  )
end

#record_call_attempt(call) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vangrail/audit.rb', line 50

def record_call_attempt(call)
  record(
    :call_attempt,
    call_id: call.id,
    tool: call.tool,
    conversation_id: call.conversation_id,
    request: call.request,
    arguments: call.fields,
    sink: call.sink,
    confirmed: call.confirmed?,
    transaction: call.transaction?,
  )
end

#to_hObject



75
76
77
# File 'lib/vangrail/audit.rb', line 75

def to_h
  { 'events' => events.map(&:to_h) }
end

#to_json(*options) ⇒ Object



79
80
81
# File 'lib/vangrail/audit.rb', line 79

def to_json(*options)
  JSON.generate(to_h, *options)
end