SMTP-Credentials und E_Mail-Adressen verschlüsselt abgelegt

This commit is contained in:
concierge committed 2026-11-16 15:43:51 +01:00
1 parent 781aa20253
commit 66659d9a23
32 files changed
+3276 -132

No files matched your search

+81 -2
View File
@@ -139,7 +139,6 @@ CREATE TABLE IF NOT EXISTS people (
last_name TEXT NOT NULL,
roles TEXT NOT NULL,
email TEXT NOT NULL DEFAULT '',
phone TEXT NOT NULL DEFAULT '',
preferred_church_ids TEXT NOT NULL DEFAULT '[]',
preferred_weekdays TEXT NOT NULL DEFAULT '[]',
preferred_service_titles TEXT NOT NULL DEFAULT '[]',
@@ -279,6 +278,43 @@ CREATE TABLE IF NOT EXISTS dispatch_log (
created_by TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_dispatch_log_tenant ON dispatch_log(tenant_id, created_at);
CREATE TABLE IF NOT EXISTS user_keys (
user_id TEXT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
kdf_salt BLOB NOT NULL,
kdf_version TEXT NOT NULL,
public_key BLOB NOT NULL,
wrapped_private BLOB NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS key_wrappers (
scope_type TEXT NOT NULL,
tenant_id TEXT NOT NULL,
scope_id TEXT NOT NULL,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
ephemeral_public BLOB NOT NULL,
wrapped_dek BLOB NOT NULL,
created_at TEXT NOT NULL,
PRIMARY KEY (scope_type, tenant_id, scope_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_key_wrappers_user ON key_wrappers(user_id);
CREATE TABLE IF NOT EXISTS secrets (
tenant_id TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
scope_type TEXT NOT NULL,
scope_id TEXT NOT NULL,
secret_key TEXT NOT NULL,
ciphertext BLOB NOT NULL,
updated_at TEXT NOT NULL,
PRIMARY KEY (tenant_id, scope_type, scope_id, secret_key)
);
CREATE TABLE IF NOT EXISTS secret_state (
tenant_id TEXT PRIMARY KEY REFERENCES tenants(id) ON DELETE CASCADE,
initialized_at TEXT NOT NULL
);
`
func (s *Store) migrate(ctx context.Context) error {
@@ -309,10 +345,53 @@ func (s *Store) migrate(ctx context.Context) error {
}
}
if _, err := s.db.ExecContext(ctx,
"INSERT INTO schema_meta(key, value) VALUES('schemaVersion', '3') "+
"INSERT INTO schema_meta(key, value) VALUES('schemaVersion', '5') "+
"ON CONFLICT(key) DO UPDATE SET value=excluded.value"); err != nil {
return fmt.Errorf("record schema version: %w", err)
}
// Phone numbers are no longer stored anywhere in the web app. Remove both
// the encrypted values and the legacy plaintext column.
if _, err := s.db.ExecContext(ctx,
"DELETE FROM secrets WHERE secret_key='phone'"); err != nil {
return fmt.Errorf("remove encrypted phone numbers: %w", err)
}
hasPhone, err := s.hasColumn(ctx, "people", "phone")
if err != nil {
return fmt.Errorf("inspect people table: %w", err)
}
if hasPhone {
if _, err := s.db.ExecContext(ctx, "ALTER TABLE people DROP COLUMN phone"); err != nil {
return fmt.Errorf("drop people.phone: %w", err)
}
}
// Editors and linked readers hold key material; administrators never do.
// Readers may only keep the scope of their linked person.
if _, err := s.db.ExecContext(ctx,
"DELETE FROM key_wrappers WHERE user_id IN (SELECT id FROM users WHERE role='admin')"); err != nil {
return fmt.Errorf("prune administrator key wrappers: %w", err)
}
if _, err := s.db.ExecContext(ctx,
"DELETE FROM user_keys WHERE user_id IN (SELECT id FROM users WHERE role='admin')"); err != nil {
return fmt.Errorf("prune administrator keys: %w", err)
}
if _, err := s.db.ExecContext(ctx, `
DELETE FROM key_wrappers WHERE user_id IN (
SELECT u.id FROM users u WHERE u.role='reader' AND (
u.linked_tenant_id='' OR u.linked_person_id='' OR NOT (
key_wrappers.scope_type='person'
AND key_wrappers.tenant_id=u.linked_tenant_id
AND key_wrappers.scope_id=u.linked_person_id
)
)
)`); err != nil {
return fmt.Errorf("prune unlinked reader key wrappers: %w", err)
}
// Dispatch logs used to contain the full recipient address. Remove that
// plaintext now that addresses are stored encrypted.
if _, err := s.db.ExecContext(ctx,
"UPDATE dispatch_log SET email='' WHERE email<>''"); err != nil {
return fmt.Errorf("redact dispatch log: %w", err)
}
return nil
}