Class: Legion::CLI::Audit
- Inherits:
-
Thor
- Object
- Thor
- Legion::CLI::Audit
- Defined in:
- lib/legion/cli/audit_command.rb
Instance Method Summary collapse
- #archive ⇒ Object
-
#list ⇒ Object
rubocop:disable Metrics/AbcSize.
- #restore ⇒ Object
- #verify ⇒ Object
- #verify_chain ⇒ Object
Instance Method Details
#archive ⇒ Object
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 [: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 [: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 [: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 [:json] cold_result = Legion::Audit::Archiver.archive_to_cold puts "Archived #{cold_result[:moved]} records to cold: #{cold_result[:path]}" unless [:json] if Legion::Audit::Archiver.verify_on_archive? verify_result = Legion::Audit::Archiver.verify_chain(tier: :warm) unless [: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 [:json] end |
#list ⇒ Object
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: [:event_type]) if [:event_type] dataset = dataset.where(principal_id: [:principal]) if [:principal] dataset = dataset.where(source: [:source]) if [:source] dataset = dataset.where(status: [:status]) if [:status] dataset = dataset.where { created_at >= Time.parse([:since]) } if [:since] dataset = dataset.where { created_at <= Time.parse([:until]) } if [:until] records = dataset.limit([:limit]).all if [: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 |
#restore ⇒ Object
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 = [: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: #{[: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 [:json] puts Legion::JSON.dump(result) else puts "Restored #{restored} records from #{manifests.count} cold archive(s) to warm tier" end end |
#verify ⇒ Object
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 [: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_chain ⇒ Object
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 = [:tier].to_sym start_date = [:start] ? Time.parse([:start]) : nil end_date = [:end] ? Time.parse([:end]) : nil result = Legion::Audit::Archiver.verify_chain( tier: tier, start_date: start_date, end_date: end_date ) if [: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 |