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

+1
View File
@@ -1,4 +1,5 @@
class AdCompliance < ApplicationRecord
include Versioned
# Method of compliance, recorded per 91.417(a)(2)(v).
METHODS = {
"inspection" => "Inspection",
+18
View File
@@ -0,0 +1,18 @@
# Which software versions have run, and when (AC 120-78B 3.2.6): recorded
# when the app starts and by the nightly check.
class AppRevision < ApplicationRecord
scope :latest_first, -> { order(first_seen_at: :desc) }
def self.touch_current!
revision = SystemInfo.new.revision
return if revision.blank? || revision == "unknown"
now = Time.current
record = find_or_initialize_by(revision: revision)
record.first_seen_at ||= now
record.built_at ||= SystemInfo.new.built_at
record.last_seen_at = now
record.save!
rescue ActiveRecord::ActiveRecordError => e
Rails.logger.warn("[app revisions] #{e.class}: #{e.message}")
end
end
+5
View File
@@ -0,0 +1,5 @@
class BackupRun < ApplicationRecord
scope :latest_first, -> { order(created_at: :desc) }
def self.latest = latest_first.first
def self.last_good = where(ok: true).latest_first.first
end
+1
View File
@@ -1,4 +1,5 @@
class Component < ApplicationRecord
include Versioned
STATUSES = { "SPARE" => "Spare Part", "IN-USE" => "In Service", "DISPOSED" => "Disposed" }.freeze
# What "since overhaul" counts from, and how logbooks abbreviate it.
OVERHAUL_KINDS = { "major" => "Major overhaul", "factory_overhaul" => "Factory overhaul", "factory_new" => "Factory new or rebuilt (zero time)" }.freeze
+26
View File
@@ -0,0 +1,26 @@
# Keeps every change to the record as a RecordVersion (AC 120-78B 3.2.2):
# full values on create and destroy, [before, after] for each changed field.
module Versioned
extend ActiveSupport::Concern
IGNORED = %w[created_at updated_at].freeze
included do
has_many :versions, -> { chronological }, as: :item, class_name: "RecordVersion", inverse_of: false
after_create { record_version!("create", attributes.except(*IGNORED)) }
after_update { (c = saved_changes.except(*IGNORED)).any? && record_version!("update", c) }
after_destroy { record_version!("destroy", attributes.except(*IGNORED)) }
end
private
def record_version!(event, changes)
RecordVersion.append!(item: self, aircraft_id: versioned_aircraft_id, event: event, changes: changes.as_json)
end
def versioned_aircraft_id
return aircraft_id if has_attribute?(:aircraft_id) && aircraft_id
return component&.aircraft_id if respond_to?(:component)
logbook_entry&.aircraft_id if respond_to?(:logbook_entry)
end
end
+1 -1
View File
@@ -1,4 +1,4 @@
class Current < ActiveSupport::CurrentAttributes
attribute :session
attribute :session, :ip_address
delegate :user, to: :session, allow_nil: true
end
+1
View File
@@ -1,4 +1,5 @@
class Document < ApplicationRecord
include Versioned
include UploadValidation
TYPES = {
+1
View File
@@ -6,6 +6,7 @@
# so any later change shows; seal is an HMAC over the signature itself, keyed
# by this server, so the signature record can't be altered unnoticed either.
class EntrySignature < ApplicationRecord
include Versioned
AUTH_METHODS = { "password" => "password re-entered", "sso" => "single sign-on, signed in again" }.freeze
# "Certified by" conveys the intent to sign (2.2.2).
LABEL = "Certified by".freeze
+1
View File
@@ -1,4 +1,5 @@
class InspectionRecord < ApplicationRecord
include Versioned
belongs_to :inspection_type
belongs_to :aircraft, optional: true
belongs_to :component, optional: true
+4
View File
@@ -0,0 +1,4 @@
class IntegrityCheck < ApplicationRecord
scope :latest_first, -> { order(created_at: :desc) }
def self.latest = latest_first.first
end
+1
View File
@@ -1,4 +1,5 @@
class LogbookEntry < ApplicationRecord
include Versioned
LOG_TYPES = { "AC" => "Aircraft", "ENG" => "Engine", "PROP" => "Propeller", "OTHER" => "Other" }.freeze
ENTRY_TYPES = {
"MAINTENANCE" => "Maintenance", "INSPECTION" => "Inspection", "FLIGHT" => "Flight",
+1
View File
@@ -1,6 +1,7 @@
# A sub-component a logbook entry also covers (the oil filter on the engine…)
# and its times at the entry.
class LogbookEntryPart < ApplicationRecord
include Versioned
belongs_to :logbook_entry
belongs_to :component
+1
View File
@@ -1,4 +1,5 @@
class MajorRecord < ApplicationRecord
include Versioned
RECORD_TYPES = { "repair" => "Major Repair", "alteration" => "Major Alteration" }.freeze
belongs_to :aircraft
+30
View File
@@ -0,0 +1,30 @@
# One change to a maintenance record (create, update, destroy), kept for
# good (AC 120-78B 3.2.2). Each aircraft's versions form a hash chain:
# chain_hash = SHA-256(prev_hash + payload), so deleting, reordering or
# editing a version breaks the chain and the nightly check reports it.
class RecordVersion < ApplicationRecord
belongs_to :user, optional: true
scope :chronological, -> { order(:id) }
before_update { throw :abort }
before_destroy { throw :abort }
def self.digest(prev_hash, payload) = OpenSSL::Digest::SHA256.hexdigest("#{prev_hash}\n#{payload}")
def self.append!(item:, aircraft_id:, event:, changes:)
now = Time.current
payload = { "item_type" => item.class.name, "item_id" => item.id, "event" => event, "changes" => changes,
"user_id" => Current.user&.id, "at" => now.utc.iso8601(6) }.to_json
prev = where(aircraft_id: aircraft_id).order(:id).last&.chain_hash
create!(aircraft_id: aircraft_id, item_type: item.class.name, item_id: item.id, event: event, user_id: Current.user&.id,
ip_address: Current.ip_address, payload: payload, prev_hash: prev, chain_hash: digest(prev, payload), created_at: now)
end
def changes_hash = JSON.parse(payload)["changes"]
# Field names changed by this version (for display).
def changed_fields
event == "update" ? changes_hash.keys : []
end
end
+1
View File
@@ -7,6 +7,7 @@
class Setting < ApplicationRecord
INTEGERS = {
"max_upload_mb" => { default: 50, in: 1..1024, label: "Maximum upload size (MB)" },
"backups_kept" => { default: 14, in: 1..365, label: "Database backups kept" },
"logbook_templates_seeded" => { default: 0, in: 0..1, label: "Built-in logbook templates added" } # internal flag
}.freeze
+1
View File
@@ -1,4 +1,5 @@
class Squawk < ApplicationRecord
include Versioned
include UploadValidation
PRIORITIES = { 0 => "Ground Aircraft", 1 => "Fix Soon", 2 => "Fix at Next Inspection", 3 => "Fix Eventually" }.freeze
+1
View File
@@ -2,6 +2,7 @@
# date, usually after weighing or an equipment change (each alteration that
# changes them needs a new one; its 337 can be linked).
class WeightBalanceRecord < ApplicationRecord
include Versioned
belongs_to :aircraft
belongs_to :major_record, optional: true
belongs_to :document, optional: true