Class: Textus::Doctor::Check::AuditLog

Inherits:
Textus::Doctor::Check show all
Defined in:
lib/textus/doctor/check/audit_log.rb

Instance Method Summary collapse

Methods inherited from Textus::Doctor::Check

#initialize, name_key

Constructor Details

This class inherits a constructor from Textus::Doctor::Check

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/textus/doctor/check/audit_log.rb', line 7

def call
  out = []
  path = File.join(store.root, "audit.log")
  return out unless File.exist?(path)

  File.foreach(path).with_index(1) do |line, lineno| # rubocop:disable Metrics/BlockLength
    stripped = line.chomp
    next if stripped.empty?

    if stripped.start_with?("{")
      begin
        JSON.parse(stripped)
      rescue JSON::ParserError => e
        out << {
          "code" => "audit.parse_error",
          "level" => "warning",
          "subject" => "#{path}:#{lineno}",
          "message" => "audit log line #{lineno} is invalid JSON: #{e.message}",
          "fix" => "inspect #{path} at line #{lineno} and remove the corrupted row",
        }
      end
    else
      # Legacy TSV (pre-0.5): read-only support retained for on-disk logs
      # written by older textus versions. Never written by current code.
      # Minimum 6 fields.
      fields = stripped.split("\t")
      next if fields.length >= 6

      out << {
        "code" => "audit.parse_error",
        "level" => "warning",
        "subject" => "#{path}:#{lineno}",
        "message" => "audit log line #{lineno} has #{fields.length} fields " \
                     "(expected >=6 for legacy TSV; consider migrating to NDJSON)",
        "fix" => "inspect #{path} at line #{lineno} and remove the corrupted row",
      }
    end
  end
  out
end