End to bridge encryption implementation
So far this is passing my basic tests, but could use some testing from people that are much more familiar with how this is supposed to work. Refs #27
This commit is contained in:
3
database/migrations/08-add-crypto-store-to-database.sql
Normal file
3
database/migrations/08-add-crypto-store-to-database.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 0
|
||||
-- which is described as "Add crypto store to database".
|
||||
@@ -0,0 +1,3 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 1
|
||||
-- which is described as "Add account_id to crypto store".
|
||||
@@ -0,0 +1,3 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 2
|
||||
-- which is described as "Add megolm withheld data to crypto store".
|
||||
@@ -0,0 +1,3 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 3
|
||||
-- which is described as "Add cross-signing keys to crypto store".
|
||||
@@ -0,0 +1,4 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 4
|
||||
-- which is described as "Replace VARCHAR(255) with TEXT in the crypto
|
||||
-- database".
|
||||
@@ -0,0 +1,4 @@
|
||||
-- This migration is implemented in migrations.go as it comes from
|
||||
-- maunium.net/go/mautrix/crypto/sql_store_upgrade. It runs upgrade at index 5
|
||||
-- which is described as "Split last_used into last_encrypted and
|
||||
-- last_decrypted in crypto store".
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE portal ADD COLUMN encrypted BOOLEAN NOT NULL DEFAULT false;
|
||||
@@ -3,37 +3,18 @@ package migrations
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"sort"
|
||||
|
||||
"github.com/lopezator/migrator"
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
"maunium.net/go/mautrix/crypto/sql_store_upgrade"
|
||||
)
|
||||
|
||||
//go:embed *.sql
|
||||
var embeddedMigrations embed.FS
|
||||
|
||||
var (
|
||||
commonMigrations = []string{
|
||||
"01-initial.sql",
|
||||
"02-attachments.sql",
|
||||
"03-emoji.sql",
|
||||
"04-custom-puppet.sql",
|
||||
"05-additional-puppet-fields.sql",
|
||||
"07-guilds.sql",
|
||||
}
|
||||
|
||||
sqliteMigrations = []string{
|
||||
"06-remove-unique-user-constraint.sqlite.sql",
|
||||
}
|
||||
|
||||
postgresMigrations = []string{
|
||||
"06-remove-unique-user-constraint.postgres.sql",
|
||||
}
|
||||
)
|
||||
|
||||
func migrationFromFile(filename string) *migrator.Migration {
|
||||
func migrationFromFile(description, filename string) *migrator.Migration {
|
||||
return &migrator.Migration{
|
||||
Name: filename,
|
||||
Name: description,
|
||||
Func: func(tx *sql.Tx) error {
|
||||
data, err := embeddedMigrations.ReadFile(filename)
|
||||
if err != nil {
|
||||
@@ -49,31 +30,83 @@ func migrationFromFile(filename string) *migrator.Migration {
|
||||
}
|
||||
}
|
||||
|
||||
func migrationFromFileWithDialect(dialect, description, sqliteFile, postgresFile string) *migrator.Migration {
|
||||
switch dialect {
|
||||
case "sqlite3":
|
||||
return migrationFromFile(description, sqliteFile)
|
||||
case "postgres":
|
||||
return migrationFromFile(description, postgresFile)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Run(db *sql.DB, baseLog log.Logger, dialect string) error {
|
||||
subLogger := baseLog.Sub("Migrations")
|
||||
logger := migrator.LoggerFunc(func(msg string, args ...interface{}) {
|
||||
subLogger.Infof(msg, args...)
|
||||
})
|
||||
|
||||
migrationNames := commonMigrations
|
||||
switch dialect {
|
||||
case "sqlite3":
|
||||
migrationNames = append(migrationNames, sqliteMigrations...)
|
||||
case "postgres":
|
||||
migrationNames = append(migrationNames, postgresMigrations...)
|
||||
}
|
||||
|
||||
sort.Strings(migrationNames)
|
||||
|
||||
migrations := make([]interface{}, len(migrationNames))
|
||||
for idx, name := range migrationNames {
|
||||
migrations[idx] = migrationFromFile(name)
|
||||
}
|
||||
|
||||
m, err := migrator.New(
|
||||
migrator.TableName("version"),
|
||||
migrator.WithLogger(logger),
|
||||
migrator.Migrations(migrations...),
|
||||
migrator.Migrations(
|
||||
migrationFromFile("Initial Schema", "01-initial.sql"),
|
||||
migrationFromFile("Attachments", "02-attachments.sql"),
|
||||
migrationFromFile("Emoji", "03-emoji.sql"),
|
||||
migrationFromFile("Custom Puppets", "04-custom-puppet.sql"),
|
||||
migrationFromFile(
|
||||
"Additional puppet fields",
|
||||
"05-additional-puppet-fields.sql",
|
||||
),
|
||||
migrationFromFileWithDialect(
|
||||
dialect,
|
||||
"Remove unique user constraint",
|
||||
"06-remove-unique-user-constraint.sqlite.sql",
|
||||
"06-remove-unique-user-constraint.postgres.sql",
|
||||
),
|
||||
migrationFromFile("Guild Bridging", "07-guilds.sql"),
|
||||
&migrator.Migration{
|
||||
Name: "Add crypto store to database",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[0](tx, dialect)
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "Add account_id to crypto store",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[1](tx, dialect)
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "Add megolm withheld data to crypto store",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[2](tx, dialect)
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "Add cross-signing keys to crypto store",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[3](tx, dialect)
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "Replace VARCHAR(255) with TEXT in the crypto database",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[4](tx, dialect)
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "Split last_used into last_encrypted and last_decrypted in crypto store",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
return sql_store_upgrade.Upgrades[5](tx, dialect)
|
||||
},
|
||||
},
|
||||
migrationFromFile(
|
||||
"Add encryption column to portal table",
|
||||
"14-add-encrypted-column-to-portal-table.sql",
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user