From 303c90322832bd41d9323547b60399587b26eaa1 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Fri, 2 Oct 2026 08:24:42 +0000 Subject: [PATCH] database.py: rebuild key_checks and ai_security_checks for per-key checks (section 19) --- database.py | 263 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 197 insertions(+), 66 deletions(-) diff --git a/database.py b/database.py index 06bc628..b1cb6fb 100644 --- a/database.py +++ b/database.py @@ -4,14 +4,13 @@ from pathlib import Path import sqlite3 -SCHEMA_VERSION = 4 +SCHEMA_VERSION = 5 FIXED_KEYS = ( (1, "SeedSigner", "hardware", "SeedSigner"), (2, "Blockstream Jade", "hardware", "Blockstream Jade"), (3, "Nunchuk", "cloud", "Nunchuk"), ) -SCHEMA_STATEMENTS = ( - """ +KEYS_TABLE = """ CREATE TABLE IF NOT EXISTS keys ( id INTEGER PRIMARY KEY, key_number INTEGER NOT NULL UNIQUE CHECK (key_number BETWEEN 1 AND 3), @@ -20,12 +19,14 @@ SCHEMA_STATEMENTS = ( device TEXT NOT NULL CHECK (device IN ('SeedSigner', 'Blockstream Jade', 'Nunchuk')), created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP ) - """, """ +HEALTH_CHECKS_TABLE = """ CREATE TABLE IF NOT EXISTS health_checks ( id INTEGER PRIMARY KEY, checked_at TEXT NOT NULL, next_check_due TEXT NOT NULL, + wallet_software TEXT CHECK (wallet_software IN ('Sparrow Wallet', 'Nunchuk')), + software_version TEXT, software_compatibility_status TEXT NOT NULL DEFAULT 'IKKJE_VURDERT' CHECK (software_compatibility_status IN ('OK', 'PROBLEM', 'IKKJE_VURDERT')), sparrow_version TEXT, @@ -34,8 +35,8 @@ SCHEMA_STATEMENTS = ( contact_info_verified INTEGER CHECK (contact_info_verified IN (0, 1)), comments TEXT ) - """, """ +KEY_CHECKS_TABLE = """ CREATE TABLE IF NOT EXISTS key_checks ( id INTEGER PRIMARY KEY, health_check_id INTEGER NOT NULL REFERENCES health_checks(id) ON DELETE CASCADE, @@ -43,19 +44,16 @@ SCHEMA_STATEMENTS = ( available INTEGER CHECK (available IN (0, 1)), hardware_works INTEGER CHECK (hardware_works IN (0, 1)), firmware_version TEXT, - seed_backup_status TEXT CHECK (seed_backup_status IN ('OK', 'PROBLEM', 'IKKJE_VURDERT')), + seed_backup_verified TEXT CHECK (seed_backup_verified IN ('OK', 'PROBLEM')), cloud_health_status TEXT CHECK (cloud_health_status IN ('OK', 'PROBLEM', 'IKKJE_VURDERT')), cloud_provider_operational INTEGER CHECK (cloud_provider_operational IN (0, 1)), - address_verified INTEGER CHECK (address_verified IN (0, 1)), - seed_backup_verified INTEGER CHECK (seed_backup_verified IN (0, 1)), - address_path_verified INTEGER CHECK (address_path_verified IN (0, 1)), - derivation_height TEXT, + address_path_verified TEXT CHECK (address_path_verified IN ('OK', 'PROBLEM')), address_path TEXT, comments TEXT, UNIQUE (health_check_id, key_id) ) - """, """ +TOOL_CHECKS_TABLE = """ CREATE TABLE IF NOT EXISTS tool_checks ( id INTEGER PRIMARY KEY, health_check_id INTEGER NOT NULL REFERENCES health_checks(id) ON DELETE CASCADE, @@ -69,23 +67,28 @@ SCHEMA_STATEMENTS = ( notes TEXT, UNIQUE (health_check_id, product) ) - """, """ +AI_SECURITY_CHECKS_TABLE = """ CREATE TABLE IF NOT EXISTS ai_security_checks ( id INTEGER PRIMARY KEY, health_check_id INTEGER NOT NULL REFERENCES health_checks(id) ON DELETE CASCADE, provider TEXT NOT NULL CHECK (provider IN ('SeedSigner', 'Blockstream Jade', 'Nunchuk', 'Sparrow Wallet')), - status TEXT NOT NULL CHECK (status IN ('TRYGG', 'PATCHA', 'SÅRBAR', 'KOMPROMITTERT')), + status TEXT NOT NULL CHECK (status IN ('SECURE', 'PATCHED', 'VULNERABLE', 'COMPROMISED')), comments TEXT, UNIQUE (health_check_id, provider) ) - """, + """ +SCHEMA_STATEMENTS = ( + KEYS_TABLE, + HEALTH_CHECKS_TABLE, + KEY_CHECKS_TABLE, + TOOL_CHECKS_TABLE, + AI_SECURITY_CHECKS_TABLE, ) OPTIONAL_COLUMNS = ( - ("key_checks", "address_verified", "INTEGER CHECK (address_verified IN (0, 1))"), - ("key_checks", "derivation_height", "TEXT"), ("health_checks", "software_comments", "TEXT"), - ("key_checks", "seed_backup_verified", "INTEGER CHECK (seed_backup_verified IN (0, 1))"), + ("health_checks", "wallet_software", "TEXT"), + ("health_checks", "software_version", "TEXT"), ) INDEX_STATEMENTS = ( ( @@ -94,10 +97,19 @@ INDEX_STATEMENTS = ( "CREATE INDEX IF NOT EXISTS idx_health_checks_checked_at ON health_checks(checked_at DESC)", ), ) +# Columns that only exist in the pre-section-19 key_checks layout. +LEGACY_KEY_CHECK_COLUMNS = ("seed_backup_status", "address_verified", "derivation_height") +# Value mapping used when the security status values became English. +SECURITY_STATUS_MIGRATION = ( + ("TRYGG", "SECURE"), + ("PATCHA", "PATCHED"), + ("SÅRBAR", "VULNERABLE"), + ("KOMPROMITTERT", "COMPROMISED"), +) def initialize_database(database_path="data/helsesjekk.sqlite3"): - """Create the schema and safely add missing columns to existing databases.""" + """Create the schema and safely migrate existing databases.""" path = Path(database_path) path.parent.mkdir(parents=True, exist_ok=True) connection = sqlite3.connect(path, timeout=5) @@ -115,6 +127,8 @@ def initialize_database(database_path="data/helsesjekk.sqlite3"): connection.execute(statement) for table, column, definition in OPTIONAL_COLUMNS: _ensure_column(connection, table, column, definition) + _migrate_key_checks(connection) + _migrate_ai_security_checks(connection) for table, column, statement in INDEX_STATEMENTS: _ensure_index(connection, table, column, statement) connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION}") @@ -128,10 +142,23 @@ def initialize_database(database_path="data/helsesjekk.sqlite3"): return path +def _table_columns(connection, table): + """Return the column names of a table, or an empty set when it is missing.""" + return {row[1] for row in connection.execute(f"PRAGMA table_info({table})")} + + +def _table_sql(connection, table): + """Return the stored CREATE statement of a table, or None when it is missing.""" + row = connection.execute( + "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", + (table,), + ).fetchone() + return row[0] if row else None + + def _ensure_column(connection, table, column, definition): """Add a table column only when it is missing.""" - columns = {row[1] for row in connection.execute(f"PRAGMA table_info({table})")} - if column not in columns: + if column not in _table_columns(connection, table): connection.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}") @@ -142,11 +169,104 @@ def _ensure_index(connection, table, column, statement): creating the index would then fail. Such a database cannot serve health checks until the column exists, so the index is skipped instead. """ - columns = {row[1] for row in connection.execute(f"PRAGMA table_info({table})")} - if column in columns: + if column in _table_columns(connection, table): connection.execute(statement) +def _column_or_null(columns, name): + """Return the column name, or a NULL literal when the column is missing.""" + return name if name in columns else "NULL" + + +def _verification_expression(columns, mappings): + """Build a CASE expression that maps legacy values to OK or PROBLEM. + + Each mapping is a (column, SQL value, result) triple. Mappings are evaluated + in order, so the preferred source column must come first. + """ + cases = [ + f"WHEN {column} = {value} THEN '{result}'" + for column, value, result in mappings + if column in columns + ] + if not cases: + return "NULL" + return "CASE " + " ".join(cases) + " ELSE NULL END" + + +def _migrate_key_checks(connection): + """Rebuild key_checks when it still uses the pre-section-19 layout. + + The legacy table stored the verification results as 0/1 flags and kept the + removed columns seed_backup_status, address_verified, and derivation_height. + SQLite cannot change a CHECK constraint, so the table is rebuilt once and the + legacy flags are folded into the OK/PROBLEM values. + """ + columns = _table_columns(connection, "key_checks") + if not columns or not set(LEGACY_KEY_CHECK_COLUMNS) & columns: + return + + connection.execute("ALTER TABLE key_checks RENAME TO key_checks_legacy") + connection.execute(KEY_CHECKS_TABLE) + if {"health_check_id", "key_id"} <= columns: + seed_backup = _verification_expression(columns, ( + ("seed_backup_verified", 1, "OK"), + ("seed_backup_verified", 0, "PROBLEM"), + ("seed_backup_status", "'OK'", "OK"), + ("seed_backup_status", "'PROBLEM'", "PROBLEM"), + )) + address_path = _verification_expression(columns, ( + ("address_path_verified", 1, "OK"), + ("address_path_verified", 0, "PROBLEM"), + ("address_verified", 1, "OK"), + ("address_verified", 0, "PROBLEM"), + )) + connection.execute(f""" + INSERT INTO key_checks ( + id, health_check_id, key_id, available, hardware_works, firmware_version, + seed_backup_verified, cloud_health_status, cloud_provider_operational, + address_path_verified, address_path, comments + ) + SELECT + {_column_or_null(columns, "id")}, + health_check_id, + key_id, + {_column_or_null(columns, "available")}, + {_column_or_null(columns, "hardware_works")}, + {_column_or_null(columns, "firmware_version")}, + {seed_backup}, + {_column_or_null(columns, "cloud_health_status")}, + {_column_or_null(columns, "cloud_provider_operational")}, + {address_path}, + {_column_or_null(columns, "address_path")}, + {_column_or_null(columns, "comments")} + FROM key_checks_legacy + WHERE health_check_id IS NOT NULL AND key_id IS NOT NULL + """) + connection.execute("DROP TABLE key_checks_legacy") + + +def _migrate_ai_security_checks(connection): + """Rebuild ai_security_checks when it still allows the Norwegian values.""" + sql = _table_sql(connection, "ai_security_checks") + if sql is None or "SECURE" in sql: + return + + connection.execute("ALTER TABLE ai_security_checks RENAME TO ai_security_checks_legacy") + connection.execute(AI_SECURITY_CHECKS_TABLE) + cases = " ".join( + f"WHEN '{old}' THEN '{new}'" for old, new in SECURITY_STATUS_MIGRATION + ) + connection.execute(f""" + INSERT INTO ai_security_checks (id, health_check_id, provider, status, comments) + SELECT id, health_check_id, provider, + CASE status {cases} ELSE status END, + comments + FROM ai_security_checks_legacy + """) + connection.execute("DROP TABLE ai_security_checks_legacy") + + def seed_fixed_keys(database_path="data/helsesjekk.sqlite3"): """Ensure the three fixed key profiles exist without overwriting custom names.""" connection = sqlite3.connect(database_path, timeout=5) @@ -185,7 +305,7 @@ def list_keys(database_path="data/helsesjekk.sqlite3"): def save_health_check(data, database_path="data/helsesjekk.sqlite3"): - """Store one validated health check together with its related rows.""" + """Store one validated health check for a single key together with its rows.""" connection = sqlite3.connect(database_path, timeout=5) connection.execute("PRAGMA foreign_keys = ON") try: @@ -193,58 +313,51 @@ def save_health_check(data, database_path="data/helsesjekk.sqlite3"): cursor = connection.execute( """ INSERT INTO health_checks ( - checked_at, next_check_due, software_compatibility_status, - sparrow_version, nunchuk_version, software_comments, + checked_at, next_check_due, wallet_software, software_version, + software_compatibility_status, software_comments, contact_info_verified, comments ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, ( data["checked_at"], data["next_check_due"], + data["wallet_software"], + data["software_version"], data["software_compatibility_status"], - data["sparrow_version"], - data["nunchuk_version"], data["software_comments"], data["contact_info_verified"], data["comments"], ), ) health_check_id = cursor.lastrowid - connection.executemany( + key_check = data["key_check"] + connection.execute( """ INSERT INTO key_checks ( health_check_id, key_id, available, hardware_works, firmware_version, - seed_backup_status, seed_backup_verified, cloud_health_status, - cloud_provider_operational, address_verified, derivation_height, + seed_backup_verified, cloud_health_status, cloud_provider_operational, address_path_verified, address_path, comments - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, - [ - ( - health_check_id, - entry["key_id"], - entry["available"], - entry["hardware_works"], - entry["firmware_version"], - entry["seed_backup_status"], - entry["seed_backup_verified"], - entry["cloud_health_status"], - entry["cloud_provider_operational"], - entry["address_verified"], - entry["derivation_height"], - entry["address_path_verified"], - entry["address_path"], - entry["comments"], - ) - for entry in data["key_checks"] - ], + ( + health_check_id, + key_check["key_id"], + key_check["available"], + key_check["hardware_works"], + key_check["firmware_version"], + key_check["seed_backup_verified"], + key_check["cloud_health_status"], + key_check["cloud_provider_operational"], + key_check["address_path_verified"], + key_check["address_path"], + key_check["comments"], + ), ) connection.executemany( """ INSERT INTO tool_checks ( - health_check_id, product, bip327_status, slip39_status, - version, checked_at, notes - ) VALUES (?, ?, ?, ?, ?, ?, ?) + health_check_id, product, bip327_status, slip39_status + ) VALUES (?, ?, ?, ?) """, [ ( @@ -252,9 +365,6 @@ def save_health_check(data, database_path="data/helsesjekk.sqlite3"): entry["product"], entry["bip327_status"], entry["slip39_status"], - entry["version"], - entry["checked_at"], - entry["notes"], ) for entry in data["tool_checks"] ], @@ -275,19 +385,41 @@ def save_health_check(data, database_path="data/helsesjekk.sqlite3"): def list_health_checks(database_path="data/helsesjekk.sqlite3"): - """Return saved health check summaries, newest first.""" + """Return saved health check summaries, newest first. + + Each summary carries the key numbers of the check, so the history can show + which key a submission belongs to. + """ connection = sqlite3.connect(database_path, timeout=5) connection.row_factory = sqlite3.Row try: rows = connection.execute( """ SELECT id, checked_at, next_check_due, software_compatibility_status, - contact_info_verified, comments + wallet_software, contact_info_verified, comments FROM health_checks ORDER BY checked_at DESC, id DESC """ ).fetchall() - return [dict(row) for row in rows] + summaries = [dict(row) for row in rows] + if summaries: + placeholders = ", ".join("?" for _ in summaries) + key_rows = connection.execute( + f""" + SELECT kc.health_check_id, k.key_number + FROM key_checks AS kc + JOIN keys AS k ON k.id = kc.key_id + WHERE kc.health_check_id IN ({placeholders}) + ORDER BY k.key_number + """, + [entry["id"] for entry in summaries], + ).fetchall() + numbers = {} + for row in key_rows: + numbers.setdefault(row["health_check_id"], []).append(row["key_number"]) + for entry in summaries: + entry["key_numbers"] = numbers.get(entry["id"], []) + return summaries finally: connection.close() @@ -299,9 +431,9 @@ def get_health_check(health_check_id, database_path="data/helsesjekk.sqlite3"): try: record = connection.execute( """ - SELECT id, checked_at, next_check_due, software_compatibility_status, - sparrow_version, nunchuk_version, software_comments, - contact_info_verified, comments + SELECT id, checked_at, next_check_due, wallet_software, software_version, + software_compatibility_status, sparrow_version, nunchuk_version, + software_comments, contact_info_verified, comments FROM health_checks WHERE id = ? """, (health_check_id,), @@ -316,10 +448,9 @@ def get_health_check(health_check_id, database_path="data/helsesjekk.sqlite3"): """ SELECT kc.id, kc.key_id, k.key_number, k.name, k.type, k.device, kc.available, kc.hardware_works, kc.firmware_version, - kc.seed_backup_status, kc.seed_backup_verified, - kc.cloud_health_status, kc.cloud_provider_operational, - kc.address_verified, kc.derivation_height, - kc.address_path_verified, kc.address_path, kc.comments + kc.seed_backup_verified, kc.cloud_health_status, + kc.cloud_provider_operational, kc.address_path_verified, + kc.address_path, kc.comments FROM key_checks AS kc JOIN keys AS k ON k.id = kc.key_id WHERE kc.health_check_id = ?