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>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-07 13:21:26 -05:00
1 parent d719f663bc
commit 68a1719db2
53 files changed
+750 -12

No files matched your search

+2 -2
View File
@@ -50,7 +50,7 @@ module AircraftArchive
end,
documents: a.documents.map do |d|
{ id: id(d), aircraft_id: id(a), collection_id: id(d.document_collection_id), doc_type: d.doc_type, name: d.name,
description: d.description, visibility: d.visibility, expires_on: d.expires_on&.iso8601, components: [] }
description: d.description, visibility: d.visibility, expires_on: d.expires_on&.iso8601, legacy_record: d.legacy_record, components: [] }
end,
document_images: a.documents.includes(pages_attachments: :blob).flat_map do |d|
d.pages.map { |page| { id: id(page), document_id: id(d), notes: "", image: file(page, "health/documents") } }
@@ -125,7 +125,7 @@ module AircraftArchive
def aircraft_hash(a)
{ id: id(a), tail_number: a.tail_number, make: a.make, model: a.model, serial_number: a.serial_number,
description: a.description, purchased: a.purchased&.iso8601, status: a.status, time_basis: a.time_basis,
description: a.description, purchased: a.purchased&.iso8601, status: a.status, time_basis: a.time_basis, electronic_records_since: a.electronic_records_since&.iso8601,
loading_stations: a.loading_stations, cg_envelope: a.cg_envelope, ad_watch_terms: a.ad_watch_terms, tach_time: dec(a.tach_time),
tach_time_offset: dec(a.tach_time_offset), hobbs_time: dec(a.hobbs_time), hobbs_time_offset: dec(a.hobbs_time_offset),
picture: file(a.picture, "aircraft_pictures"), icao_type: a.icao_type }
+2 -1
View File
@@ -143,6 +143,7 @@ module AircraftArchive
make: a["make"].to_s, model: a["model"].to_s, serial_number: a["serial_number"].to_s, description: a["description"].to_s,
purchased: date(a["purchased"]), status: Aircraft::STATUSES.key?(a["status"]) ? a["status"] : "AVAILABLE",
time_basis: Aircraft::TIME_BASES.key?(a["time_basis"]) ? a["time_basis"] : "tach",
electronic_records_since: date(a["electronic_records_since"]),
loading_stations: a["loading_stations"].is_a?(Hash) ? a["loading_stations"] : nil,
cg_envelope: a["cg_envelope"].is_a?(Array) ? a["cg_envelope"] : nil, ad_watch_terms: a["ad_watch_terms"].to_s.first(500).presence,
tach_time: num(a["tach_time"] || a["flight_time"]) || 0, tach_time_offset: num(a["tach_time_offset"]) || 0,
@@ -207,7 +208,7 @@ module AircraftArchive
d = @aircraft.documents.new(name: data["name"].to_s.presence || "Untitled", description: data["description"].to_s,
doc_type: data["doc_type"].presence, visibility: visibility(data["visibility"]),
document_collection_id: @map[:collection][data["collection_id"].to_s],
expires_on: date(data["expires_on"]))
expires_on: date(data["expires_on"]), legacy_record: data["legacy_record"] == true)
d.doc_type = "OTHER" unless Document::TYPES.key?(d.doc_type)
d.save!(validate: false)
@map[:document][data["id"].to_s] = d.id
+57
View File
@@ -0,0 +1,57 @@
# Built-in backup of the main database (AC 120-78B 3.2.7): a consistent
# snapshot (SQLite's online backup API, safe while the app runs) in storage/backups,
# checked by opening it and running SQLite's integrity check, and the newest
# few kept. Uploaded files aren't copied (they're already in storage/ and,
# being deduplicated, cheap to back up with the rest of the volume).
class DatabaseBackup
DIR = Rails.root.join("storage", "backups")
def self.run! = new.run!
def run!
FileUtils.mkdir_p(DIR)
path = DIR.join("sam-#{Time.current.utc.strftime('%Y%m%d-%H%M%S')}.sqlite3")
copy(path)
message = verify(path)
bytes = File.size(path)
prune(keep_path: path)
BackupRun.create!(ok: true, path: path.to_s, bytes: bytes, message: message)
rescue StandardError => e
Rails.logger.error("[backup] #{e.class}: #{e.message}")
BackupRun.create!(ok: false, path: path&.to_s, message: "#{e.class.name.demodulize}: #{e.message}".truncate(500))
end
private
def copy(path)
source = ActiveRecord::Base.connection.raw_connection
dest = SQLite3::Database.new(path.to_s)
backup = SQLite3::Backup.new(dest, "main", source, "main")
result = backup.step(-1)
backup.finish
raise "the database was busy (SQLite code #{result}); try again" unless result == SQLite3::Constants::ErrorCode::DONE
ensure
dest&.close
end
# Opens the copy and checks it: SQLite's integrity check, and the same
# number of logbook entries and signatures as the live database.
def verify(path)
db = SQLite3::Database.new(path.to_s, readonly: true)
result = db.get_first_value("PRAGMA integrity_check")
raise "integrity check failed: #{result}" unless result == "ok"
counts = %w[logbook_entries entry_signatures].map { |t| [ t, db.get_first_value("SELECT COUNT(*) FROM #{t}") ] }.to_h
live = { "logbook_entries" => LogbookEntry.count, "entry_signatures" => EntrySignature.count }
raise "record counts differ (#{counts} vs #{live})" unless counts == live
"Verified: SQLite integrity check ok; #{counts['logbook_entries']} logbook entries, #{counts['entry_signatures']} signatures"
ensure
db&.close
end
# Keeps the newest few (by file time), always including the one just made.
def prune(keep_path:)
keep = Setting.integer("backups_kept")
older = Dir[DIR.join("sam-*.sqlite3")].reject { _1 == keep_path.to_s }.sort_by { File.mtime(_1) }.reverse
older.drop(keep - 1).each { File.delete(_1) }
end
end
+31
View File
@@ -0,0 +1,31 @@
# 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
+100
View File
@@ -0,0 +1,100 @@
require "csv"
# Everything about an aircraft's records in one ZIP, for the FAA or NTSB on
# request (91.417(c), AC 120-78B 3.2.8.6) or a buyer at sale (91.419,
# 3.2.8.12): the logbook entries as a PDF with their electronic signatures,
# a signature register, the full change history, the status report, and the
# data archive (.sam.zip) to load into another system.
class RecordsPackage
def initialize(aircraft, source:)
@aircraft = aircraft
@source = source
@generated_at = Time.current
end
def filename = "#{@aircraft.tail_number.downcase}-records-#{@generated_at.to_date.iso8601}.zip"
def write_to(path)
entries = @aircraft.logbook_entries.includes(:component, :parts, :signature, inspection_records: :inspection_type).order(:date, :id).to_a
Zip::OutputStream.open(path) do |zip|
add(zip, "README.txt", readme(entries))
add(zip, "logbook-entries.pdf", LogbookEntryPdf.new(@aircraft, entries, layout: "letter").render) if entries.any?
add(zip, "signatures.csv", signatures_csv(entries))
add(zip, "record-history.csv", history_csv)
add(zip, "status-report.html", status_report_html)
archive = Tempfile.new([ "archive", ".sam.zip" ])
AircraftArchive::Exporter.new(@aircraft, source: @source).write_to(archive.path)
zip.put_next_entry("#{@aircraft.tail_number.downcase}.sam.zip")
File.open(archive.path, "rb") { IO.copy_stream(_1, zip) }
ensure
archive&.close!
end
path
end
private
def add(zip, name, content)
zip.put_next_entry(name)
zip.write(content)
end
def readme(entries)
signed = entries.count(&:signed?)
check = IntegrityCheck.latest
<<~TXT
Maintenance records for #{@aircraft.tail_number} (#{[ @aircraft.make, @aircraft.model ].compact_blank.join(' ')}, S/N #{@aircraft.serial_number.presence || 'not recorded'})
Prepared #{@generated_at.utc.strftime('%Y-%m-%d %H:%M UTC')} by Simple Aircraft Manager (#{@source}), software version #{SystemInfo.new.revision}.
Electronic records since: #{@aircraft.electronic_records_since&.iso8601 || 'not stated'}. Earlier records are the paper logbooks#{' (scans marked "legacy record" are in the archive)' if @aircraft.documents.where(legacy_record: true).exists?}.
Contents
- logbook-entries.pdf All #{entries.size} logbook entries, oldest first. #{signed} are signed electronically: each shows
"Certified by" the signer and certificate, the time (UTC) and the first 16 characters of its fingerprint.
- signatures.csv Every electronic signature: signer, certificate, statement, time, status and full fingerprint.
- record-history.csv Every change to the maintenance records (created, changed, deleted), with who and when.
- status-report.html Maintenance status per 14 CFR 91.417(a)(2): times, life-limited parts, inspections, ADs, alterations.
- #{@aircraft.tail_number.downcase}.sam.zip All records and files as data, to load into another system.
Signatures
Each electronic signature records who signed, with which certificate, what they certified, when, and a SHA-256
fingerprint of the entry's content at signing. A signed entry can't be edited; corrections are new signed entries
that supersede the original (both are included). Status "Valid" means the entry and signature are unchanged since
signing; "Superseded" means a later signed correction replaces it.
Last records integrity check: #{check ? "#{check.created_at.utc.strftime('%Y-%m-%d %H:%M UTC')}, #{check.ok ? 'no problems found' : "#{check.problems.size} problem(s) found"}" : 'not run yet'}.
TXT
end
def signatures_csv(entries)
CSV.generate do |csv|
csv << %w[entry_date log entry_type signer certificate_kind certificate_number signed_at_utc status fingerprint statement corrects_entry_dated]
entries.select(&:signed?).each do |e|
s = e.signature
csv << [ e.date, LogbookEntry::LOG_TYPES[e.log_type], LogbookEntry::ENTRY_TYPES[e.entry_type], s.signer_name, s.certificate_label,
s.certificate_number, s.signed_at.utc.iso8601, s.imported? ? "imported from #{s.origin}" : s.status_label,
s.content_digest, s.statement, e.supersedes&.date ]
end
end
end
def history_csv
users = User.pluck(:id, :email_address).to_h
CSV.generate do |csv|
csv << %w[id at_utc record record_id event by changed_fields chain_hash]
RecordVersion.where(aircraft_id: @aircraft.id).chronological.find_each do |v|
csv << [ v.id, v.created_at.utc.iso8601, v.item_type.underscore.humanize, v.item_id, v.event, users[v.user_id] || "system",
v.event == "update" ? v.changed_fields.join(" ") : "", v.chain_hash ]
end
end
end
# The status report as one self-contained HTML file (styles inlined).
def status_report_html
html = ApplicationController.render("maintenance_reports/show", layout: "report",
assigns: { report: MaintenanceReport.new(@aircraft) })
css = %w[tailwind.css application.css].filter_map { Rails.application.assets.load_path.find(_1)&.content }.join("\n")
html.gsub(%r{<link rel="stylesheet"[^>]*>\s*}, "").sub("</head>", "<style>#{css}</style></head>")
.gsub(%r{<script\b[^>]*>.*?</script>}m, "").gsub(%r{<link rel="modulepreload"[^>]*>\s*}, "")
end
end