Class: Legion::CLI::Audit

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/audit_command.rb

Instance Method Summary collapse

Instance Method Details

#archiveObject



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/legion/cli/audit_command.rb', line 72

def archive
  Connection.ensure_settings
  Connection.ensure_data

  unless Legion::Audit::Archiver.enabled?
    puts 'Audit retention is disabled. Set audit.retention.enabled = true to activate.'
    return
  end

  if options[:dry_run]
    status = Legion::Data::Retention.retention_status(table: :audit_log)
    output = {
      mode:         'DRY RUN',
      hot_records:  status[:active_count],
      warm_records: status[:archived_count],
      oldest_hot:   status[:oldest_active]&.to_s,
      oldest_warm:  status[:oldest_archived]&.to_s,
      hot_days:     Legion::Audit::Archiver.hot_days,
      warm_days:    Legion::Audit::Archiver.warm_days
    }
    if options[:json]
      puts Legion::JSON.dump(output)
    else
      puts 'DRY RUN — no records will be moved'
      output.each { |k, v| puts "  #{k}: #{v}" }
    end
    return
  end

  unless options[:execute]
    puts 'Pass --execute to run archival, or --dry-run to preview.'
    return
  end

  warm_result = Legion::Audit::Archiver.archive_to_warm
  puts "Archived #{warm_result[:moved]} records to warm" unless options[:json]

  cold_result = Legion::Audit::Archiver.archive_to_cold
  puts "Archived #{cold_result[:moved]} records to cold: #{cold_result[:path]}" unless options[:json]

  if Legion::Audit::Archiver.verify_on_archive?
    verify_result = Legion::Audit::Archiver.verify_chain(tier: :warm)
    unless options[:json]
      if verify_result[:valid]
        puts "Chain integrity verified: #{verify_result[:records_checked]} warm records"
      else
        puts "WARNING: chain broken in warm tier after archival (#{verify_result[:broken_links].count} links)"
      end
    end
  end

  puts Legion::JSON.dump({ warm: warm_result, cold: cold_result }) if options[:json]
end

#listObject

rubocop:disable Metrics/AbcSize



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/cli/audit_command.rb', line 20

def list # rubocop:disable Metrics/AbcSize
  Connection.ensure_settings
  Connection.ensure_data

  dataset = Legion::Data::Model::AuditLog.order(Sequel.desc(:id))
  dataset = dataset.where(event_type: options[:event_type]) if options[:event_type]
  dataset = dataset.where(principal_id: options[:principal]) if options[:principal]
  dataset = dataset.where(source: options[:source]) if options[:source]
  dataset = dataset.where(status: options[:status]) if options[:status]
  dataset = dataset.where { created_at >= Time.parse(options[:since]) } if options[:since]
  dataset = dataset.where { created_at <= Time.parse(options[:until]) } if options[:until]
  records = dataset.limit(options[:limit]).all

  if options[:json]
    puts Legion::JSON.dump(records.map(&:values))
  else
    records.each do |r|
      puts "#{r.created_at}  #{r.event_type.ljust(22)} #{r.principal_id.ljust(20)} " \
           "#{r.action.ljust(12)} #{r.resource.ljust(40)} #{r.status}"
    end
    puts "#{records.count} records shown"
  end
end

#restoreObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/legion/cli/audit_command.rb', line 159

def restore
  Connection.ensure_settings
  Connection.ensure_data

  unless Legion::Audit::Archiver.enabled?
    puts 'Audit retention is disabled.'
    return
  end

  db = Legion::Data.connection
  unless db&.table_exists?(:audit_archive_manifests)
    puts 'No archive manifests table found. Has migration 039 been run?'
    exit 1
  end

  date_str = options[:date].tr('-', '')[0, 8]
  manifests = db[:audit_archive_manifests]
              .where(tier: 'cold')
              .where(::Sequel.like(:storage_url, "%#{date_str}%"))
              .all

  if manifests.empty?
    puts "No cold archives found for date: #{options[:date]}"
    exit 1
  end

  restored = 0
  manifests.each do |manifest|
    gz_data = Legion::Audit::ColdStorage.download(path: manifest[:storage_url])
    ndjson  = ::Zlib::GzipReader.new(::StringIO.new(gz_data)).read
    records = ndjson.split("\n").map { |line| Legion::JSON.load(line) }

    db.transaction do
      records.each { |r| db[:audit_log_archive].insert(r.transform_keys(&:to_sym)) }
    end
    restored += records.size
  end

  result = { restored: restored, manifests: manifests.count }
  if options[:json]
    puts Legion::JSON.dump(result)
  else
    puts "Restored #{restored} records from #{manifests.count} cold archive(s) to warm tier"
  end
end

#verifyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/audit_command.rb', line 46

def verify
  Connection.ensure_settings
  Connection.ensure_data

  unless defined?(Legion::Extensions::Audit::Runners::Audit)
    puts 'lex-audit is not loaded'
    exit 1
  end

  runner = Object.new.extend(Legion::Extensions::Audit::Runners::Audit)
  result = runner.verify

  if options[:json]
    puts Legion::JSON.dump(result)
  elsif result[:valid]
    puts "Audit chain valid: #{result[:records_checked]} records verified"
  else
    puts "CHAIN BROKEN at record ##{result[:break_at]} (#{result[:records_checked]} records checked before break)"
    exit 1
  end
end

#verify_chainObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/legion/cli/audit_command.rb', line 131

def verify_chain
  Connection.ensure_settings
  Connection.ensure_data

  tier       = options[:tier].to_sym
  start_date = options[:start] ? Time.parse(options[:start]) : nil
  end_date   = options[:end]   ? Time.parse(options[:end])   : nil

  result = Legion::Audit::Archiver.verify_chain(
    tier:       tier,
    start_date: start_date,
    end_date:   end_date
  )

  if options[:json]
    puts Legion::JSON.dump(result)
  elsif result[:valid]
    puts "Chain valid (#{tier}): #{result[:records_checked]} records verified"
  else
    puts "CHAIN BROKEN in #{tier} tier — #{result[:broken_links].count} broken link(s)"
    result[:broken_links].each { |l| puts "  record ##{l[:id]}: expected #{l[:expected]}, got #{l[:got]}" }
    exit 1
  end
end