From 761892217857ce86d4fb6f5f5018c1f00d1056bd Mon Sep 17 00:00:00 2001 From: Nikos Filippakis Date: Fri, 19 Jun 2020 20:19:45 +0200 Subject: [PATCH] Use SQL backend for storing crypto material Signed-off-by: Nikos Filippakis --- clients/bot_client.go | 29 +++++++++--- clients/clients.go | 2 +- database/db.go | 102 +++--------------------------------------- database/interface.go | 98 ---------------------------------------- 4 files changed, 29 insertions(+), 202 deletions(-) diff --git a/clients/bot_client.go b/clients/bot_client.go index 74a6202..4c01cc6 100644 --- a/clients/bot_client.go +++ b/clients/bot_client.go @@ -2,6 +2,7 @@ package clients import ( "github.com/matrix-org/go-neb/api" + "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" log "github.com/sirupsen/logrus" "maunium.net/go/mautrix" @@ -22,18 +23,32 @@ type BotClient struct { // InitOlmMachine initializes a BotClient's internal OlmMachine given a client object and a Neb store, // which will be used to store room information. -func (botClient *BotClient) InitOlmMachine(client *mautrix.Client, nebStore *matrix.NEBStore, - cryptoStore crypto.Store) error { +func (botClient *BotClient) InitOlmMachine(client *mautrix.Client, nebStore *matrix.NEBStore) (err error) { - gobStore, err := crypto.NewGobStore("crypto.gob") - if err != nil { - return err + var cryptoStore crypto.Store + cryptoLogger := CryptoMachineLogger{} + if sdb, ok := database.GetServiceDB().(*database.ServiceDB); ok { + // Create an SQL crypto store based on the ServiceDB used + db, dialect := sdb.GetSQLDb() + sqlCryptoStore := crypto.NewSQLCryptoStore(db, dialect, client.DeviceID, []byte(client.DeviceID.String()), cryptoLogger) + // Try to create the tables if they are missing + if err = sqlCryptoStore.CreateTables(); err != nil { + return + } + cryptoStore = sqlCryptoStore + cryptoLogger.Debug("Using SQL backend as the crypto store") + } else { + cryptoStore, err = crypto.NewGobStore(client.DeviceID.String() + ".gob") + if err != nil { + return + } + cryptoLogger.Debug("Using gob storage as the crypto store") } botClient.stateStore = &NebStateStore{&nebStore.InMemoryStore} - olmMachine := crypto.NewOlmMachine(client, CryptoMachineLogger{}, gobStore, botClient.stateStore) + olmMachine := crypto.NewOlmMachine(client, cryptoLogger, cryptoStore, botClient.stateStore) if err = olmMachine.Load(); err != nil { - return nil + return } botClient.olmMachine = olmMachine diff --git a/clients/clients.go b/clients/clients.go index b638dbe..fa56311 100644 --- a/clients/clients.go +++ b/clients/clients.go @@ -358,7 +358,7 @@ func (c *Clients) initClient(botClient *BotClient) error { // TODO: Check that the access token is valid for the userID by peforming // a request against the server. - if err = botClient.InitOlmMachine(client, nebStore, c.db); err != nil { + if err = botClient.InitOlmMachine(client, nebStore); err != nil { return err } diff --git a/database/db.go b/database/db.go index 0d38be8..a050dfd 100644 --- a/database/db.go +++ b/database/db.go @@ -8,13 +8,13 @@ import ( "github.com/matrix-org/go-neb/api" "github.com/matrix-org/go-neb/types" - "maunium.net/go/mautrix/crypto" "maunium.net/go/mautrix/id" ) // A ServiceDB stores the configuration for the services type ServiceDB struct { - db *sql.DB + db *sql.DB + dialect string } // A single global instance of the service DB. @@ -45,7 +45,7 @@ func Open(databaseType, databaseURL string) (serviceDB *ServiceDB, err error) { // https://github.com/mattn/go-sqlite3/issues/274 db.SetMaxOpenConns(1) } - serviceDB = &ServiceDB{db: db} + serviceDB = &ServiceDB{db: db, dialect: databaseType} return } @@ -328,99 +328,9 @@ func (d *ServiceDB) InsertFromConfig(cfg *api.ConfigFile) error { return nil } -// PutAccount NOP -func (d *ServiceDB) PutAccount(*crypto.OlmAccount) error { - return nil -} - -// GetAccount NOP -func (d *ServiceDB) GetAccount() (*crypto.OlmAccount, error) { - return nil, nil -} - -// HasSession NOP -func (d *ServiceDB) HasSession(id.SenderKey) bool { - return false -} - -// GetSessions NOP -func (d *ServiceDB) GetSessions(id.SenderKey) (crypto.OlmSessionList, error) { - return nil, nil -} - -// GetLatestSession NOP -func (d *ServiceDB) GetLatestSession(id.SenderKey) (*crypto.OlmSession, error) { - return nil, nil -} - -// AddSession NOP -func (d *ServiceDB) AddSession(id.SenderKey, *crypto.OlmSession) error { - return nil -} - -// UpdateSession NOP -func (d *ServiceDB) UpdateSession(id.SenderKey, *crypto.OlmSession) error { - return nil -} - -// PutGroupSession NOP -func (d *ServiceDB) PutGroupSession(id.RoomID, id.SenderKey, id.SessionID, *crypto.InboundGroupSession) error { - return nil -} - -// GetGroupSession NOP -func (d *ServiceDB) GetGroupSession(id.RoomID, id.SenderKey, id.SessionID) (*crypto.InboundGroupSession, error) { - return nil, nil -} - -// AddOutboundGroupSession NOP -func (d *ServiceDB) AddOutboundGroupSession(*crypto.OutboundGroupSession) error { - return nil -} - -// UpdateOutboundGroupSession NOP -func (d *ServiceDB) UpdateOutboundGroupSession(*crypto.OutboundGroupSession) error { - return nil -} - -// GetOutboundGroupSession NOP -func (d *ServiceDB) GetOutboundGroupSession(id.RoomID) (*crypto.OutboundGroupSession, error) { - return nil, nil -} - -// RemoveOutboundGroupSession NOP -func (d *ServiceDB) RemoveOutboundGroupSession(id.RoomID) error { - return nil -} - -// ValidateMessageIndex NOP -func (d *ServiceDB) ValidateMessageIndex(senderKey id.SenderKey, sessionID id.SessionID, eventID id.EventID, index uint, timestamp int64) bool { - return false -} - -// GetDevices NOP -func (d *ServiceDB) GetDevices(id.UserID) (map[id.DeviceID]*crypto.DeviceIdentity, error) { - return nil, nil -} - -// GetDevice NOP -func (d *ServiceDB) GetDevice(id.UserID, id.DeviceID) (*crypto.DeviceIdentity, error) { - return nil, nil -} - -// PutDevices NOP -func (d *ServiceDB) PutDevices(id.UserID, map[id.DeviceID]*crypto.DeviceIdentity) error { - return nil -} - -// FilterTrackedUsers NOP -func (d *ServiceDB) FilterTrackedUsers([]id.UserID) []id.UserID { - return nil -} - -// Flush NOP -func (d *ServiceDB) Flush() error { - return nil +// GetSQLDb retrieves the SQL database instance of a ServiceDB and the dialect it uses (sqlite3 or postgres). +func (d *ServiceDB) GetSQLDb() (*sql.DB, string) { + return d.db, d.dialect } func runTransaction(db *sql.DB, fn func(txn *sql.Tx) error) (err error) { diff --git a/database/interface.go b/database/interface.go index c943cdb..6a0532f 100644 --- a/database/interface.go +++ b/database/interface.go @@ -3,14 +3,11 @@ package database import ( "github.com/matrix-org/go-neb/api" "github.com/matrix-org/go-neb/types" - "maunium.net/go/mautrix/crypto" "maunium.net/go/mautrix/id" ) // Storer is the interface which needs to be conformed to in order to persist Go-NEB data type Storer interface { - crypto.Store - StoreMatrixClientConfig(config api.ClientConfig) (oldConfig api.ClientConfig, err error) LoadMatrixClientConfigs() (configs []api.ClientConfig, err error) LoadMatrixClientConfig(userID id.UserID) (config api.ClientConfig, err error) @@ -142,98 +139,3 @@ func (s *NopStorage) StoreBotOptions(opts types.BotOptions) (oldOpts types.BotOp func (s *NopStorage) InsertFromConfig(cfg *api.ConfigFile) error { return nil } - -// PutAccount NOP -func (s *NopStorage) PutAccount(*crypto.OlmAccount) error { - return nil -} - -// GetAccount NOP -func (s *NopStorage) GetAccount() (*crypto.OlmAccount, error) { - return nil, nil -} - -// HasSession NOP -func (s *NopStorage) HasSession(id.SenderKey) bool { - return false -} - -// GetSessions NOP -func (s *NopStorage) GetSessions(id.SenderKey) (crypto.OlmSessionList, error) { - return nil, nil -} - -// GetLatestSession NOP -func (s *NopStorage) GetLatestSession(id.SenderKey) (*crypto.OlmSession, error) { - return nil, nil -} - -// AddSession NOP -func (s *NopStorage) AddSession(id.SenderKey, *crypto.OlmSession) error { - return nil -} - -// UpdateSession NOP -func (s *NopStorage) UpdateSession(id.SenderKey, *crypto.OlmSession) error { - return nil -} - -// PutGroupSession NOP -func (s *NopStorage) PutGroupSession(id.RoomID, id.SenderKey, id.SessionID, *crypto.InboundGroupSession) error { - return nil -} - -// GetGroupSession NOP -func (s *NopStorage) GetGroupSession(id.RoomID, id.SenderKey, id.SessionID) (*crypto.InboundGroupSession, error) { - return nil, nil -} - -// AddOutboundGroupSession NOP -func (s *NopStorage) AddOutboundGroupSession(*crypto.OutboundGroupSession) error { - return nil -} - -// UpdateOutboundGroupSession NOP -func (s *NopStorage) UpdateOutboundGroupSession(*crypto.OutboundGroupSession) error { - return nil -} - -// GetOutboundGroupSession NOP -func (s *NopStorage) GetOutboundGroupSession(id.RoomID) (*crypto.OutboundGroupSession, error) { - return nil, nil -} - -// RemoveOutboundGroupSession NOP -func (s *NopStorage) RemoveOutboundGroupSession(id.RoomID) error { - return nil -} - -// ValidateMessageIndex NOP -func (s *NopStorage) ValidateMessageIndex(senderKey id.SenderKey, sessionID id.SessionID, eventID id.EventID, index uint, timestamp int64) bool { - return false -} - -// GetDevices NOP -func (s *NopStorage) GetDevices(id.UserID) (map[id.DeviceID]*crypto.DeviceIdentity, error) { - return nil, nil -} - -// GetDevice NOP -func (s *NopStorage) GetDevice(id.UserID, id.DeviceID) (*crypto.DeviceIdentity, error) { - return nil, nil -} - -// PutDevices NOP -func (s *NopStorage) PutDevices(id.UserID, map[id.DeviceID]*crypto.DeviceIdentity) error { - return nil -} - -// FilterTrackedUsers NOP -func (s *NopStorage) FilterTrackedUsers([]id.UserID) []id.UserID { - return nil -} - -// Flush NOP -func (s *NopStorage) Flush() error { - return nil -}