2019-07-17 22:25:42 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
_ "github.com/mutecomm/go-sqlcipher" // We require go sqlcipher that overrides default implementation
|
|
|
|
"github.com/status-im/migrate/v4"
|
|
|
|
"github.com/status-im/migrate/v4/database/sqlcipher"
|
|
|
|
bindata "github.com/status-im/migrate/v4/source/go_bindata"
|
2019-08-27 12:04:15 +00:00
|
|
|
mvdsmigrations "github.com/vacp2p/mvds/persistenceutil"
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The default number of kdf iterations in sqlcipher (from version 3.0.0)
|
|
|
|
// https://github.com/sqlcipher/sqlcipher/blob/fda4c68bb474da7e955be07a2b807bda1bb19bd2/CHANGELOG.md#300---2013-11-05
|
|
|
|
// https://www.zetetic.net/sqlcipher/sqlcipher-api/#kdf_iter
|
|
|
|
const defaultKdfIterationsNumber = 64000 // nolint: deadcode,varcheck,unused
|
|
|
|
|
|
|
|
// The reduced number of kdf iterations (for performance reasons) which is
|
|
|
|
// currently used for derivation of the database key
|
|
|
|
// https://github.com/status-im/status-go/pull/1343
|
|
|
|
// https://notes.status.im/i8Y_l7ccTiOYq09HVgoFwA
|
|
|
|
const reducedKdfIterationsNumber = 3200
|
|
|
|
|
2020-10-08 14:05:49 +00:00
|
|
|
const InMemoryPath = ":memory:"
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// MigrationConfig is a struct that allows to define bindata migrations.
|
|
|
|
type MigrationConfig struct {
|
|
|
|
AssetNames []string
|
|
|
|
AssetGetter func(name string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens or initializes a new database for a given file path.
|
|
|
|
// MigrationConfig is optional but if provided migrations are applied automatically.
|
2019-08-27 12:04:15 +00:00
|
|
|
func Open(path, key string) (*sql.DB, error) {
|
|
|
|
return open(path, key, reducedKdfIterationsNumber)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
// OpenInMemory opens an in memory SQLite database.
|
|
|
|
// Number of KDF iterations is reduced to 0.
|
|
|
|
func OpenInMemory() (*sql.DB, error) {
|
2020-10-08 14:05:49 +00:00
|
|
|
return open(InMemoryPath, "", 0)
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// OpenWithIter allows to open a new database with a custom number of kdf iterations.
|
|
|
|
// Higher kdf iterations number makes it slower to open the database.
|
2019-08-27 12:04:15 +00:00
|
|
|
func OpenWithIter(path, key string, kdfIter int) (*sql.DB, error) {
|
|
|
|
return open(path, key, kdfIter)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
func open(path string, key string, kdfIter int) (*sql.DB, error) {
|
2020-10-08 14:05:49 +00:00
|
|
|
if path != InMemoryPath {
|
2020-02-21 14:48:53 +00:00
|
|
|
_, err := os.OpenFile(path, os.O_CREATE, 0600)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db, err := sql.Open("sqlite3", path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyString := fmt.Sprintf("PRAGMA key = '%s'", key)
|
|
|
|
|
|
|
|
// Disable concurrent access as not supported by the driver
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
|
|
|
if _, err = db.Exec("PRAGMA foreign_keys=ON"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = db.Exec(keyString); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kdfString := fmt.Sprintf("PRAGMA kdf_iter = '%d'", kdfIter)
|
|
|
|
|
|
|
|
if _, err = db.Exec(kdfString); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
if err := Migrate(db); err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyMigrations allows to apply bindata migrations on the current *sql.DB.
|
|
|
|
// `assetNames` is a list of assets with migrations and `assetGetter` is responsible
|
|
|
|
// for returning the content of the asset with a given name.
|
|
|
|
func ApplyMigrations(db *sql.DB, assetNames []string, assetGetter func(name string) ([]byte, error)) error {
|
|
|
|
resources := bindata.Resource(
|
|
|
|
assetNames,
|
|
|
|
assetGetter,
|
|
|
|
)
|
|
|
|
|
|
|
|
source, err := bindata.WithInstance(resources)
|
|
|
|
if err != nil {
|
2019-07-30 06:14:13 +00:00
|
|
|
return errors.Wrap(err, "failed to create migration source")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
driver, err := sqlcipher.WithInstance(db, &sqlcipher.Config{
|
|
|
|
MigrationsTable: "status_protocol_go_" + sqlcipher.DefaultMigrationsTable,
|
|
|
|
})
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
2019-07-30 06:14:13 +00:00
|
|
|
return errors.Wrap(err, "failed to create driver")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err := migrate.NewWithInstance(
|
|
|
|
"go-bindata",
|
|
|
|
source,
|
|
|
|
"sqlcipher",
|
2019-07-30 06:14:13 +00:00
|
|
|
driver,
|
|
|
|
)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
2019-07-30 06:14:13 +00:00
|
|
|
return errors.Wrap(err, "failed to create migration instance")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 08:31:23 +00:00
|
|
|
version, dirty, err := m.Version()
|
|
|
|
if err != nil && err != migrate.ErrNilVersion {
|
|
|
|
return errors.Wrap(err, "could not get version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force version if dirty
|
|
|
|
if dirty {
|
|
|
|
if err = m.Force(int(version)); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to force migration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
if err = m.Up(); err != migrate.ErrNoChange {
|
2019-07-30 06:14:13 +00:00
|
|
|
return errors.Wrap(err, "failed to migrate")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-27 12:04:15 +00:00
|
|
|
|
|
|
|
func Migrate(database *sql.DB) error {
|
|
|
|
// Apply migrations for all components.
|
|
|
|
err := mvdsmigrations.Migrate(database)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to apply mvds migrations")
|
|
|
|
}
|
|
|
|
|
|
|
|
migrationNames, migrationGetter, err := prepareMigrations(defaultMigrations)
|
|
|
|
if err != nil {
|
2019-11-21 16:19:22 +00:00
|
|
|
return errors.Wrap(err, "failed to prepare status-go/protocol migrations")
|
2019-08-27 12:04:15 +00:00
|
|
|
}
|
|
|
|
err = ApplyMigrations(database, migrationNames, migrationGetter)
|
|
|
|
if err != nil {
|
2019-11-21 16:19:22 +00:00
|
|
|
return errors.Wrap(err, "failed to apply status-go/protocol migrations")
|
2019-08-27 12:04:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|