Files
simple-aircraft-manager-rails/app/services/records_integrity.rb
T
rickyandClaude Opus 5.5 68a1719db2 Records integrity: history, nightly backup and check, records package
AC 120-78B chapter 3:
- Every change to logbook entries, sub-components, signatures, inspection
  records, AD compliance, major records, W&B, components, squawks and
  documents is kept as a RecordVersion (who, IP, when, field changes),
  hash-chained per aircraft; versions can't be edited or deleted. Shown on
  each logbook entry and in Activity -> Record history
- Nightly job: SQLite online-backup copy to storage/backups, verified
  (integrity_check + record counts), pruned to the configured number; then
  an integrity check of the history chains and every signature's seal and
  fingerprint. Results on Admin -> About ("Back up and check now"); admins
  are emailed on failure
- Software versions recorded (first/last seen) on Admin -> About
- Records package (Access & Sharing): logbook entries PDF with signatures,
  signatures.csv, record-history.csv, self-contained status report HTML,
  the .sam.zip, and a README, for the FAA/NTSB or a buyer
- Legacy (paper) records: documents can be marked as legacy records;
  aircraft have an "electronic records since" date (status report, package)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 13:21:26 -05:00

32 lines
1.5 KiB
Ruby

# Verifies the records (AC 120-78B 3.2.2 / 3.2.4 / 3.2.8.5.3): each
# aircraft's history chain is unbroken, and every signature's seal and
# fingerprint still match. The result is kept as an IntegrityCheck.
class RecordsIntegrity
def self.check! = new.check!
def check!
problems = []
versions = 0
RecordVersion.distinct.pluck(:aircraft_id).each do |aircraft_id|
prev = nil
RecordVersion.where(aircraft_id: aircraft_id).chronological.find_each do |v|
versions += 1
tail = Aircraft.find_by(id: aircraft_id)&.tail_number || "aircraft #{aircraft_id.inspect}"
problems << "#{tail}: history entry #{v.id} doesn't follow the one before it (missing or reordered history)" if v.prev_hash != prev
problems << "#{tail}: history entry #{v.id} was altered" if v.chain_hash != RecordVersion.digest(v.prev_hash, v.payload)
prev = v.chain_hash
end
end
signatures = 0
EntrySignature.includes(logbook_entry: [ :aircraft, :parts ]).find_each do |sig|
signatures += 1
next if sig.imported? || sig.status != :altered
entry = sig.logbook_entry
problems << "#{entry.aircraft.tail_number}: the #{entry.date} logbook entry (signed by #{sig.signer_name}) " \
"#{sig.seal_valid? ? 'changed after it was signed' : 'has an altered signature'}"
end
AppRevision.touch_current!
IntegrityCheck.create!(ok: problems.empty?, versions_checked: versions, signatures_checked: signatures, problems: problems.first(200))
end
end