Section 28: save_key_settings can write the type a rotation picks

This commit is contained in:
ai-bot committed 2026-10-09 11:44:18 +00:00
1 parent 2e90dd4ca4
commit 395ecb4a32
1 file changed
+12 -7
+12 -7
View File
@@ -628,20 +628,25 @@ def key_has_check(key_id, database_path="data/helsesjekk.sqlite3"):
return count > 0 return count > 0
def save_key_settings(key_id, name, address_path, database_path="data/helsesjekk.sqlite3"): def save_key_settings(key_id, name, address_path, database_path="data/helsesjekk.sqlite3", *, key_type=None):
"""Store the name and the address path of one key. """Store the name, the address path, and the type of one key.
The name is only a label, while the address path is part of the key's The name is only a label, while the address path and the type are part of the
identity (sections 25.1 and 25.2). Whether the path may change is decided by key's identity (sections 25.1, 25.2 and 28). Whether the path may change is
the caller, which reads the key's saved checks from the same database. decided by the caller, which reads the key's saved checks from the same
database. ``key_type`` is only passed by a rotation, which replaces the key
with a new one and may pick the other type; the settings never change it, and
the stored type is kept when the argument is left out.
""" """
connection = sqlite3.connect(database_path, timeout=5) connection = sqlite3.connect(database_path, timeout=5)
connection.execute("PRAGMA foreign_keys = ON") connection.execute("PRAGMA foreign_keys = ON")
try: try:
with connection: with connection:
# COALESCE keeps the stored type when no new one is passed, so the
# settings and a rotation can share this one statement.
cursor = connection.execute( cursor = connection.execute(
"UPDATE keys SET name = ?, address_path = ? WHERE id = ?", "UPDATE keys SET name = ?, address_path = ?, type = COALESCE(?, type) WHERE id = ?",
(name, address_path, key_id), (name, address_path, key_type, key_id),
) )
if cursor.rowcount != 1: if cursor.rowcount != 1:
raise ValueError(f"Key {key_id} does not exist") raise ValueError(f"Key {key_id} does not exist")