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

+59
View File
@@ -0,0 +1,59 @@
require "test_helper"
# Backups copy the live database with SQLite's online backup API, which can't
# run inside the transaction each test normally runs in: these tests commit
# for real and clean up after themselves.
class DatabaseBackupTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper
self.use_transactional_tests = false
setup do
@owner = create_user("owner@example.com")
@a = create_aircraft(owner: @owner, tail_number: "N12345")
@mech = create_user("mech@example.com").tap { _1.update!(name: "Sam Mechanic") }
@ap = @mech.certificates.create!(kind: "AP", number: "7654321")
end
teardown do
FileUtils.rm_rf(DatabaseBackup::DIR)
[ EntrySignature, LogbookEntry ].each { _1.delete_all }
[ RecordVersion, IntegrityCheck, BackupRun, AircraftEvent, AircraftRole, UserCertificate, Aircraft, Session, Setting, User ].each(&:delete_all)
end
def entry(**attrs)
@a.logbook_entries.create!({ date: Date.new(2026, 10, 1), entry_type: "MAINTENANCE", text: "Changed oil", aircraft_hours_at_entry: 1000 }.merge(attrs))
end
test "the nightly job backs up and verifies the database, prunes old copies, and emails admins on trouble" do
admin = create_user("admin@example.com", admin: true)
entry
Setting.update_integers("backups_kept" => "2")
dir = DatabaseBackup::DIR
FileUtils.rm_rf(dir)
3.times { |i| travel(i.minutes) { DatabaseBackup.run! } }
run = BackupRun.latest
assert run.ok, run.message
assert_match "Verified: SQLite integrity check ok; 1 logbook entries", run.message
assert_equal 2, Dir[dir.join("sam-*.sqlite3")].size
db = SQLite3::Database.new(run.path, readonly: true)
assert_equal 1, db.get_first_value("SELECT COUNT(*) FROM logbook_entries")
db.close
assert_no_emails { RecordsIntegrityJob.perform_now }
assert [ BackupRun.latest.ok, IntegrityCheck.latest.ok ].all?, [ BackupRun.latest.message, IntegrityCheck.latest.problems ].inspect
LogbookEntry.update_all(text: "x") # no signatures, so this alone isn't a problem
EntrySigner.new(entry(date: Date.new(2026, 10, 2)), user: @mech, certificate: @ap, auth_method: "password").sign!
LogbookEntry.where(date: Date.new(2026, 10, 2)).update_all(text: "tampered")
assert_emails 1 do
RecordsIntegrityJob.perform_now
end
assert_equal [ admin.email_address ], ActionMailer::Base.deliveries.last.to
sign_in admin
get admin_about_path
assert_includes response.body, "Records integrity"
assert_includes response.body, "changed after it was signed"
post admin_check_records_path
assert_redirected_to admin_about_path(anchor: "records")
end
end
+114
View File
@@ -0,0 +1,114 @@
require "test_helper"
# Electronic recordkeeping (AC 120-78B chapter 3): history, integrity,
# backups, software versions, the records package, legacy records.
class RecordsIntegrityTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper
setup do
@owner = create_user("owner@example.com")
@a = create_aircraft(owner: @owner, tail_number: "N12345")
@mech = create_user("mech@example.com").tap { _1.update!(name: "Sam Mechanic") }
@ap = @mech.certificates.create!(kind: "AP", number: "7654321")
end
def entry(**attrs)
@a.logbook_entries.create!({ date: Date.new(2026, 10, 1), entry_type: "MAINTENANCE", text: "Changed oil", aircraft_hours_at_entry: 1000 }.merge(attrs))
end
test "every change to a record is kept, with who and what changed, and shown" do
sign_in @owner
post aircraft_logbook_entries_path(@a), params: { logbook_entry: { date: "2026-10-01", log_type: "AC", entry_type: "MAINTENANCE", text: "Changed oil" } }
e = @a.logbook_entries.sole
patch aircraft_logbook_entry_path(@a, e), params: { logbook_entry: { text: "Changed oil and filter" } }
versions = e.versions.to_a
assert_equal %w[create update], versions.map(&:event)
assert_equal [ "Changed oil", "Changed oil and filter" ], versions.last.changes_hash["text"]
assert_equal [ @owner, @owner ], versions.map(&:user)
assert versions.all? { _1.ip_address.present? }
get aircraft_logbook_entry_path(@a, e)
assert_includes response.body, "Change history (2)"
delete aircraft_logbook_entry_path(@a, e)
assert_equal "destroy", RecordVersion.where(item_type: "LogbookEntry", item_id: e.id).last.event # history outlives the record
get aircraft_record_versions_path(@a)
assert_select "li", minimum: 3
assert_includes response.body, "deleted"
assert_raises(ActiveRecord::RecordNotSaved) { RecordVersion.last.update!(event: "x") }
end
test "the integrity check finds broken history chains and altered signed entries" do
e = entry
EntrySigner.new(e, user: @mech, certificate: @ap, auth_method: "password").sign!
other = create_aircraft(owner: @owner)
other.squawks.create!(priority: 2, issue_reported: "Chip in paint")
check = RecordsIntegrity.check!
assert check.ok, check.problems.inspect
assert check.versions_checked >= 4
assert_equal 1, check.signatures_checked
LogbookEntry.where(id: e.id).update_all(text: "Changed oil (edited in the database)")
victim = RecordVersion.where(aircraft_id: other.id).first
RecordVersion.where(id: victim.id).delete_all
check = RecordsIntegrity.check!
refute check.ok
assert(check.problems.any? { _1.include?("the 2026-10-01 logbook entry (signed by Sam Mechanic) changed after it was signed") })
assert(check.problems.any? { _1.include?("doesn't follow the one before it") || _1.include?("missing") } || RecordVersion.where(aircraft_id: other.id).count.zero?)
RecordVersion.where(aircraft_id: @a.id).last.then { RecordVersion.where(id: _1.id).update_all(payload: _1.payload.sub("Changed oil", "Changed gas")) }
assert(RecordsIntegrity.check!.problems.any? { _1.include?("was altered") })
end
test "software versions are recorded" do
with_revision("abc1234") { AppRevision.touch_current! }
with_revision("def5678") { AppRevision.touch_current! }
with_revision("def5678") { AppRevision.touch_current! }
assert_equal %w[def5678 abc1234], AppRevision.latest_first.pluck(:revision) & %w[def5678 abc1234]
assert_equal 1, AppRevision.where(revision: "def5678").count
end
test "the records package has everything for the FAA or a buyer" do
@a.update!(electronic_records_since: Date.new(2026, 1, 1))
@a.documents.create!(name: "Logbook scans 1975-2025", doc_type: "LOG", legacy_record: true)
signed = entry
EntrySigner.new(signed, user: @mech, certificate: @ap, auth_method: "password").sign!
entry(date: Date.new(2026, 10, 5), text: "Unsigned entry")
sign_in @owner
get aircraft_records_package_path(@a)
assert_response :success
assert_match(/filename="n12345-records-\d{4}-\d\d-\d\d\.zip"/, response.headers["Content-Disposition"])
path = File.join(Dir.mktmpdir, "pkg.zip")
File.binwrite(path, response.body)
Zip::File.open(path) do |zip|
def zip.text(name) = read(name).force_encoding("UTF-8")
assert_equal %w[README.txt logbook-entries.pdf n12345.sam.zip record-history.csv signatures.csv status-report.html], zip.entries.map(&:name).sort
readme = zip.text("README.txt")
assert_includes readme, "All 2 logbook entries, oldest first. 1 are signed electronically"
assert_includes readme, "Electronic records since: 2026-01-01"
assert_includes readme, "scans marked \"legacy record\""
sigs = CSV.parse(zip.text("signatures.csv"), headers: true)
assert_equal [ "Sam Mechanic", "7654321", "Valid", signed.reload.signature.content_digest ], sigs.first.fields.values_at(3, 5, 7, 8)
assert_operator CSV.parse(zip.text("record-history.csv"), headers: true).size, :>=, 3
html = zip.text("status-report.html")
assert_includes html, "Maintenance status · 14 CFR 91.417(a)(2)"
refute_includes html, "<script"
assert_includes html, "<style>"
end
assert @a.events.exists?(event_name: "Records package downloaded")
pilot = create_user
@a.roles.create!(user: pilot, role: "pilot")
sign_in pilot
get aircraft_records_package_path(@a)
assert_redirected_to aircraft_path(@a) # owners only
end
private
def with_revision(sha)
saved = ENV["APP_REVISION"]
ENV["APP_REVISION"] = sha
yield
ensure
ENV["APP_REVISION"] = saved
end
end