diff --git a/cmd/revertprovider.go b/cmd/revertprovider.go index 8b47f13e..64d82e4c 100644 --- a/cmd/revertprovider.go +++ b/cmd/revertprovider.go @@ -26,8 +26,8 @@ Please take a look at the usage below to customize the options.`, Run: func(cmd *cobra.Command, args []string) { logger.DisableLogger() logger.EnableConsoleLogger(zerolog.DebugLevel) - if revertProviderTargetVersion != 10 { - logger.WarnToConsole("Unsupported target version, 10 is the only supported one") + if revertProviderTargetVersion != 15 { + logger.WarnToConsole("Unsupported target version, 15 is the only supported one") os.Exit(1) } configDir = util.CleanDirInput(configDir) @@ -57,7 +57,7 @@ Please take a look at the usage below to customize the options.`, func init() { addConfigFlags(revertProviderCmd) - revertProviderCmd.Flags().IntVar(&revertProviderTargetVersion, "to-version", 10, `10 means the version supported in v2.1.x`) + revertProviderCmd.Flags().IntVar(&revertProviderTargetVersion, "to-version", 15, `15 means the version supported in v2.2.1`) revertProviderCmd.MarkFlagRequired("to-version") //nolint:errcheck rootCmd.AddCommand(revertProviderCmd) diff --git a/dataprovider/bolt.go b/dataprovider/bolt.go index 36c10bcd..c631be9b 100644 --- a/dataprovider/bolt.go +++ b/dataprovider/bolt.go @@ -35,7 +35,7 @@ var ( sharesBucket, dbVersionBucket} ) -// BoltProvider auth provider for bolt key/value store +// BoltProvider defines the auth provider for bolt key/value store type BoltProvider struct { dbHandle *bolt.DB } @@ -1419,21 +1419,11 @@ func (p *BoltProvider) migrateDatabase() error { case version == boltDatabaseVersion: providerLog(logger.LevelDebug, "bolt database is up to date, current version: %v", version) return ErrNoInitRequired - case version < 10: + case version < 15: err = fmt.Errorf("database version %v is too old, please see the upgrading docs", version) providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err - case version == 10: - return updateBoltDatabaseVersion(p.dbHandle, 15) - case version == 11: - return updateBoltDatabaseVersion(p.dbHandle, 15) - case version == 12: - return updateBoltDatabaseVersion(p.dbHandle, 15) - case version == 13: - return updateBoltDatabaseVersion(p.dbHandle, 15) - case version == 14: - return updateBoltDatabaseVersion(p.dbHandle, 15) default: if version > boltDatabaseVersion { providerLog(logger.LevelError, "database version %v is newer than the supported one: %v", version, @@ -1455,8 +1445,6 @@ func (p *BoltProvider) revertDatabase(targetVersion int) error { return errors.New("current version match target version, nothing to do") } switch dbVersion.Version { - case 15, 14, 13, 12, 11: - return updateBoltDatabaseVersion(p.dbHandle, 10) default: return fmt.Errorf("database version not handled: %v", dbVersion.Version) } @@ -1721,7 +1709,7 @@ func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) { v := bucket.Get(dbVersionKey) if v == nil { dbVersion = schemaVersion{ - Version: 10, + Version: 15, } return nil } @@ -1730,7 +1718,7 @@ func getBoltDatabaseVersion(dbHandle *bolt.DB) (schemaVersion, error) { return dbVersion, err } -func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error { +/*func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error { err := dbHandle.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(dbVersionBucket) if bucket == nil { @@ -1746,4 +1734,4 @@ func updateBoltDatabaseVersion(dbHandle *bolt.DB, version int) error { return bucket.Put(dbVersionKey, buf) }) return err -} +}*/ diff --git a/dataprovider/memory.go b/dataprovider/memory.go index c0b9ed5b..68a75b23 100644 --- a/dataprovider/memory.go +++ b/dataprovider/memory.go @@ -46,7 +46,7 @@ type memoryProviderHandle struct { sharesIDs []string } -// MemoryProvider auth provider for a memory store +// MemoryProvider defines the auth provider for a memory store type MemoryProvider struct { dbHandle *memoryProviderHandle } diff --git a/dataprovider/mysql.go b/dataprovider/mysql.go index 0b209e6c..880a9e0a 100644 --- a/dataprovider/mysql.go +++ b/dataprovider/mysql.go @@ -33,71 +33,51 @@ const ( mysqlInitialSQL = "CREATE TABLE `{{schema_version}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `version` integer NOT NULL);" + "CREATE TABLE `{{admins}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `username` varchar(255) NOT NULL UNIQUE, " + "`description` varchar(512) NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NULL, `status` integer NOT NULL, " + - "`permissions` longtext NOT NULL, `filters` longtext NULL, `additional_info` longtext NULL);" + + "`permissions` longtext NOT NULL, `filters` longtext NULL, `additional_info` longtext NULL, `last_login` bigint NOT NULL, " + + "`created_at` bigint NOT NULL, `updated_at` bigint NOT NULL);" + + "CREATE TABLE `{{defender_hosts}}` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, " + + "`ip` varchar(50) NOT NULL UNIQUE, `ban_time` bigint NOT NULL, `updated_at` bigint NOT NULL);" + + "CREATE TABLE `{{defender_events}}` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, " + + "`date_time` bigint NOT NULL, `score` integer NOT NULL, `host_id` bigint NOT NULL);" + + "ALTER TABLE `{{defender_events}}` ADD CONSTRAINT `{{prefix}}defender_events_host_id_fk_defender_hosts_id` " + + "FOREIGN KEY (`host_id`) REFERENCES `{{defender_hosts}}` (`id`) ON DELETE CASCADE;" + "CREATE TABLE `{{folders}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(255) NOT NULL UNIQUE, " + - "`description` varchar(512) NULL, `path` varchar(512) NULL, `used_quota_size` bigint NOT NULL, " + + "`description` varchar(512) NULL, `path` longtext NULL, `used_quota_size` bigint NOT NULL, " + "`used_quota_files` integer NOT NULL, `last_quota_update` bigint NOT NULL, `filesystem` longtext NULL);" + "CREATE TABLE `{{users}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `username` varchar(255) NOT NULL UNIQUE, " + "`status` integer NOT NULL, `expiration_date` bigint NOT NULL, `description` varchar(512) NULL, `password` longtext NULL, " + - "`public_keys` longtext NULL, `home_dir` varchar(512) NOT NULL, `uid` integer NOT NULL, `gid` integer NOT NULL, " + + "`public_keys` longtext NULL, `home_dir` longtext NOT NULL, `uid` integer NOT NULL, `gid` integer NOT NULL, " + "`max_sessions` integer NOT NULL, `quota_size` bigint NOT NULL, `quota_files` integer NOT NULL, " + "`permissions` longtext NOT NULL, `used_quota_size` bigint NOT NULL, `used_quota_files` integer NOT NULL, " + "`last_quota_update` bigint NOT NULL, `upload_bandwidth` integer NOT NULL, `download_bandwidth` integer NOT NULL, " + - "`last_login` bigint NOT NULL, `filters` longtext NULL, `filesystem` longtext NULL, `additional_info` longtext NULL);" + - "CREATE TABLE `{{folders_mapping}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `virtual_path` varchar(512) NOT NULL, " + + "`last_login` bigint NOT NULL, `filters` longtext NULL, `filesystem` longtext NULL, `additional_info` longtext NULL, " + + "`created_at` bigint NOT NULL, `updated_at` bigint NOT NULL, `email` varchar(255) NULL);" + + "CREATE TABLE `{{folders_mapping}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `virtual_path` longtext NOT NULL, " + "`quota_size` bigint NOT NULL, `quota_files` integer NOT NULL, `folder_id` integer NOT NULL, `user_id` integer NOT NULL);" + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}unique_mapping` UNIQUE (`user_id`, `folder_id`);" + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}folders_mapping_folder_id_fk_folders_id` FOREIGN KEY (`folder_id`) REFERENCES `{{folders}}` (`id`) ON DELETE CASCADE;" + "ALTER TABLE `{{folders_mapping}}` ADD CONSTRAINT `{{prefix}}folders_mapping_user_id_fk_users_id` FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" + - "INSERT INTO {{schema_version}} (version) VALUES (10);" - mysqlV11SQL = "CREATE TABLE `{{api_keys}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(255) NOT NULL, `key_id` varchar(50) NOT NULL UNIQUE," + - "`api_key` varchar(255) NOT NULL UNIQUE, `scope` integer NOT NULL, `created_at` bigint NOT NULL, `updated_at` bigint NOT NULL, `last_use_at` bigint NOT NULL, " + - "`expires_at` bigint NOT NULL, `description` longtext NULL, `admin_id` integer NULL, `user_id` integer NULL);" + - "ALTER TABLE `{{api_keys}}` ADD CONSTRAINT `{{prefix}}api_keys_admin_id_fk_admins_id` FOREIGN KEY (`admin_id`) REFERENCES `{{admins}}` (`id`) ON DELETE CASCADE;" + - "ALTER TABLE `{{api_keys}}` ADD CONSTRAINT `{{prefix}}api_keys_user_id_fk_users_id` FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" - mysqlV11DownSQL = "DROP TABLE `{{api_keys}}` CASCADE;" - mysqlV12SQL = "ALTER TABLE `{{admins}}` ADD COLUMN `created_at` bigint DEFAULT 0 NOT NULL;" + - "ALTER TABLE `{{admins}}` ALTER COLUMN `created_at` DROP DEFAULT;" + - "ALTER TABLE `{{admins}}` ADD COLUMN `updated_at` bigint DEFAULT 0 NOT NULL;" + - "ALTER TABLE `{{admins}}` ALTER COLUMN `updated_at` DROP DEFAULT;" + - "ALTER TABLE `{{admins}}` ADD COLUMN `last_login` bigint DEFAULT 0 NOT NULL;" + - "ALTER TABLE `{{admins}}` ALTER COLUMN `last_login` DROP DEFAULT;" + - "ALTER TABLE `{{users}}` ADD COLUMN `created_at` bigint DEFAULT 0 NOT NULL;" + - "ALTER TABLE `{{users}}` ALTER COLUMN `created_at` DROP DEFAULT;" + - "ALTER TABLE `{{users}}` ADD COLUMN `updated_at` bigint DEFAULT 0 NOT NULL;" + - "ALTER TABLE `{{users}}` ALTER COLUMN `updated_at` DROP DEFAULT;" + - "CREATE INDEX `{{prefix}}users_updated_at_idx` ON `{{users}}` (`updated_at`);" - mysqlV12DownSQL = "ALTER TABLE `{{admins}}` DROP COLUMN `updated_at`;" + - "ALTER TABLE `{{admins}}` DROP COLUMN `created_at`;" + - "ALTER TABLE `{{admins}}` DROP COLUMN `last_login`;" + - "ALTER TABLE `{{users}}` DROP COLUMN `created_at`;" + - "ALTER TABLE `{{users}}` DROP COLUMN `updated_at`;" - - mysqlV13SQL = "ALTER TABLE `{{users}}` ADD COLUMN `email` varchar(255) NULL;" - mysqlV13DownSQL = "ALTER TABLE `{{users}}` DROP COLUMN `email`;" - mysqlV14SQL = "CREATE TABLE `{{shares}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, " + + "CREATE TABLE `{{shares}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, " + "`share_id` varchar(60) NOT NULL UNIQUE, `name` varchar(255) NOT NULL, `description` varchar(512) NULL, " + "`scope` integer NOT NULL, `paths` longtext NOT NULL, `created_at` bigint NOT NULL, " + "`updated_at` bigint NOT NULL, `last_use_at` bigint NOT NULL, `expires_at` bigint NOT NULL, " + "`password` longtext NULL, `max_tokens` integer NOT NULL, `used_tokens` integer NOT NULL, " + "`allow_from` longtext NULL, `user_id` integer NOT NULL);" + "ALTER TABLE `{{shares}}` ADD CONSTRAINT `{{prefix}}shares_user_id_fk_users_id` " + - "FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" - mysqlV14DownSQL = "DROP TABLE `{{shares}}` CASCADE;" - mysqlV15SQL = "CREATE TABLE `{{defender_hosts}}` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, " + - "`ip` varchar(50) NOT NULL UNIQUE, `ban_time` bigint NOT NULL, `updated_at` bigint NOT NULL);" + - "CREATE TABLE `{{defender_events}}` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, " + - "`date_time` bigint NOT NULL, `score` integer NOT NULL, `host_id` bigint NOT NULL);" + - "ALTER TABLE `{{defender_events}}` ADD CONSTRAINT `{{prefix}}defender_events_host_id_fk_defender_hosts_id` " + - "FOREIGN KEY (`host_id`) REFERENCES `{{defender_hosts}}` (`id`) ON DELETE CASCADE;" + + "FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" + + "CREATE TABLE `{{api_keys}}` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(255) NOT NULL, `key_id` varchar(50) NOT NULL UNIQUE," + + "`api_key` varchar(255) NOT NULL UNIQUE, `scope` integer NOT NULL, `created_at` bigint NOT NULL, `updated_at` bigint NOT NULL, `last_use_at` bigint NOT NULL, " + + "`expires_at` bigint NOT NULL, `description` longtext NULL, `admin_id` integer NULL, `user_id` integer NULL);" + + "ALTER TABLE `{{api_keys}}` ADD CONSTRAINT `{{prefix}}api_keys_admin_id_fk_admins_id` FOREIGN KEY (`admin_id`) REFERENCES `{{admins}}` (`id`) ON DELETE CASCADE;" + + "ALTER TABLE `{{api_keys}}` ADD CONSTRAINT `{{prefix}}api_keys_user_id_fk_users_id` FOREIGN KEY (`user_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE;" + + "CREATE INDEX `{{prefix}}users_updated_at_idx` ON `{{users}}` (`updated_at`);" + "CREATE INDEX `{{prefix}}defender_hosts_updated_at_idx` ON `{{defender_hosts}}` (`updated_at`);" + "CREATE INDEX `{{prefix}}defender_hosts_ban_time_idx` ON `{{defender_hosts}}` (`ban_time`);" + - "CREATE INDEX `{{prefix}}defender_events_date_time_idx` ON `{{defender_events}}` (`date_time`);" - mysqlV15DownSQL = "DROP TABLE `{{defender_events}}` CASCADE;" + - "DROP TABLE `{{defender_hosts}}` CASCADE;" + "CREATE INDEX `{{prefix}}defender_events_date_time_idx` ON `{{defender_events}}` (`date_time`);" + + "INSERT INTO {{schema_version}} (version) VALUES (15);" ) -// MySQLProvider auth provider for MySQL/MariaDB database +// MySQLProvider defines the auth provider for MySQL/MariaDB database type MySQLProvider struct { dbHandle *sql.DB } @@ -373,17 +353,22 @@ func (p *MySQLProvider) initializeDatabase() error { if errors.Is(err, sql.ErrNoRows) { return errSchemaVersionEmpty } + logger.InfoToConsole("creating initial database schema, version 15") + providerLog(logger.LevelInfo, "creating initial database schema, version 15") initialSQL := strings.ReplaceAll(mysqlInitialSQL, "{{schema_version}}", sqlTableSchemaVersion) initialSQL = strings.ReplaceAll(initialSQL, "{{admins}}", sqlTableAdmins) initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{api_keys}}", sqlTableAPIKeys) + initialSQL = strings.ReplaceAll(initialSQL, "{{shares}}", sqlTableShares) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_events}}", sqlTableDefenderEvents) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_hosts}}", sqlTableDefenderHosts) initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, strings.Split(initialSQL, ";"), 10) + return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, strings.Split(initialSQL, ";"), 15) } -//nolint:dupl func (p *MySQLProvider) migrateDatabase() error { dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true) if err != nil { @@ -394,21 +379,11 @@ func (p *MySQLProvider) migrateDatabase() error { case version == sqlDatabaseVersion: providerLog(logger.LevelDebug, "sql database is up to date, current version: %v", version) return ErrNoInitRequired - case version < 10: + case version < 15: err = fmt.Errorf("database version %v is too old, please see the upgrading docs", version) providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err - case version == 10: - return updateMySQLDatabaseFromV10(p.dbHandle) - case version == 11: - return updateMySQLDatabaseFromV11(p.dbHandle) - case version == 12: - return updateMySQLDatabaseFromV12(p.dbHandle) - case version == 13: - return updateMySQLDatabaseFromV13(p.dbHandle) - case version == 14: - return updateMySQLDatabaseFromV14(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database version %v is newer than the supported one: %v", version, @@ -431,16 +406,6 @@ func (p *MySQLProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { - case 15: - return downgradeMySQLDatabaseFromV15(p.dbHandle) - case 14: - return downgradeMySQLDatabaseFromV14(p.dbHandle) - case 13: - return downgradeMySQLDatabaseFromV13(p.dbHandle) - case 12: - return downgradeMySQLDatabaseFromV12(p.dbHandle) - case 11: - return downgradeMySQLDatabaseFromV11(p.dbHandle) default: return fmt.Errorf("database version not handled: %v", dbVersion.Version) } @@ -458,149 +423,3 @@ func (p *MySQLProvider) resetDatabase() error { sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, strings.Split(sql, ";"), 0) } - -func updateMySQLDatabaseFromV10(dbHandle *sql.DB) error { - if err := updateMySQLDatabaseFrom10To11(dbHandle); err != nil { - return err - } - return updateMySQLDatabaseFromV11(dbHandle) -} - -func updateMySQLDatabaseFromV11(dbHandle *sql.DB) error { - if err := updateMySQLDatabaseFrom11To12(dbHandle); err != nil { - return err - } - return updateMySQLDatabaseFromV12(dbHandle) -} - -func updateMySQLDatabaseFromV12(dbHandle *sql.DB) error { - if err := updateMySQLDatabaseFrom12To13(dbHandle); err != nil { - return err - } - return updateMySQLDatabaseFromV13(dbHandle) -} - -func updateMySQLDatabaseFromV13(dbHandle *sql.DB) error { - if err := updateMySQLDatabaseFrom13To14(dbHandle); err != nil { - return err - } - return updateMySQLDatabaseFromV14(dbHandle) -} - -func updateMySQLDatabaseFromV14(dbHandle *sql.DB) error { - return updateMySQLDatabaseFrom14To15(dbHandle) -} - -func downgradeMySQLDatabaseFromV15(dbHandle *sql.DB) error { - if err := downgradeMySQLDatabaseFrom15To14(dbHandle); err != nil { - return err - } - return downgradeMySQLDatabaseFromV14(dbHandle) -} - -func downgradeMySQLDatabaseFromV14(dbHandle *sql.DB) error { - if err := downgradeMySQLDatabaseFrom14To13(dbHandle); err != nil { - return err - } - return downgradeMySQLDatabaseFromV13(dbHandle) -} - -func downgradeMySQLDatabaseFromV13(dbHandle *sql.DB) error { - if err := downgradeMySQLDatabaseFrom13To12(dbHandle); err != nil { - return err - } - return downgradeMySQLDatabaseFromV12(dbHandle) -} - -func downgradeMySQLDatabaseFromV12(dbHandle *sql.DB) error { - if err := downgradeMySQLDatabaseFrom12To11(dbHandle); err != nil { - return err - } - return downgradeMySQLDatabaseFromV11(dbHandle) -} - -func downgradeMySQLDatabaseFromV11(dbHandle *sql.DB) error { - return downgradeMySQLDatabaseFrom11To10(dbHandle) -} - -func updateMySQLDatabaseFrom13To14(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 13 -> 14") - providerLog(logger.LevelInfo, "updating database version: 13 -> 14") - sql := strings.ReplaceAll(mysqlV14SQL, "{{shares}}", sqlTableShares) - sql = strings.ReplaceAll(sql, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 14) -} - -func updateMySQLDatabaseFrom14To15(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 14 -> 15") - providerLog(logger.LevelInfo, "updating database version: 14 -> 15") - sql := strings.ReplaceAll(mysqlV15SQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 15) -} - -func downgradeMySQLDatabaseFrom15To14(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 15 -> 14") - providerLog(logger.LevelInfo, "downgrading database version: 15 -> 14") - sql := strings.ReplaceAll(mysqlV15DownSQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 14) -} - -func downgradeMySQLDatabaseFrom14To13(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 14 -> 13") - providerLog(logger.LevelInfo, "downgrading database version: 14 -> 13") - sql := strings.ReplaceAll(mysqlV14DownSQL, "{{shares}}", sqlTableShares) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 13) -} - -func updateMySQLDatabaseFrom12To13(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 12 -> 13") - providerLog(logger.LevelInfo, "updating database version: 12 -> 13") - sql := strings.ReplaceAll(mysqlV13SQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 13) -} - -func downgradeMySQLDatabaseFrom13To12(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 13 -> 12") - providerLog(logger.LevelInfo, "downgrading database version: 13 -> 12") - sql := strings.ReplaceAll(mysqlV13DownSQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 12) -} - -func updateMySQLDatabaseFrom11To12(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 11 -> 12") - providerLog(logger.LevelInfo, "updating database version: 11 -> 12") - sql := strings.ReplaceAll(mysqlV12SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 12) -} - -func downgradeMySQLDatabaseFrom12To11(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 12 -> 11") - providerLog(logger.LevelInfo, "downgrading database version: 12 -> 11") - sql := strings.ReplaceAll(mysqlV12DownSQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 11) -} - -func updateMySQLDatabaseFrom10To11(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 10 -> 11") - providerLog(logger.LevelInfo, "updating database version: 10 -> 11") - sql := strings.ReplaceAll(mysqlV11SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{api_keys}}", sqlTableAPIKeys) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 11) -} - -func downgradeMySQLDatabaseFrom11To10(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 11 -> 10") - providerLog(logger.LevelInfo, "downgrading database version: 11 -> 10") - sql := strings.ReplaceAll(mysqlV11DownSQL, "{{api_keys}}", sqlTableAPIKeys) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, strings.Split(sql, ";"), 10) -} diff --git a/dataprovider/pgsql.go b/dataprovider/pgsql.go index 5ecd2f90..531ccbe5 100644 --- a/dataprovider/pgsql.go +++ b/dataprovider/pgsql.go @@ -32,63 +32,34 @@ DROP TABLE IF EXISTS "{{defender_hosts}}" CASCADE; DROP TABLE IF EXISTS "{{schema_version}}" CASCADE; ` pgsqlInitial = `CREATE TABLE "{{schema_version}}" ("id" serial NOT NULL PRIMARY KEY, "version" integer NOT NULL); - CREATE TABLE "{{admins}}" ("id" serial NOT NULL PRIMARY KEY, "username" varchar(255) NOT NULL UNIQUE, +CREATE TABLE "{{admins}}" ("id" serial NOT NULL PRIMARY KEY, "username" varchar(255) NOT NULL UNIQUE, "description" varchar(512) NULL, "password" varchar(255) NOT NULL, "email" varchar(255) NULL, "status" integer NOT NULL, -"permissions" text NOT NULL, "filters" text NULL, "additional_info" text NULL); +"permissions" text NOT NULL, "filters" text NULL, "additional_info" text NULL, "last_login" bigint NOT NULL, +"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL); +CREATE TABLE "{{defender_hosts}}" ("id" bigserial NOT NULL PRIMARY KEY, "ip" varchar(50) NOT NULL UNIQUE, +"ban_time" bigint NOT NULL, "updated_at" bigint NOT NULL); +CREATE TABLE "{{defender_events}}" ("id" bigserial NOT NULL PRIMARY KEY, "date_time" bigint NOT NULL, "score" integer NOT NULL, +"host_id" bigint NOT NULL); +ALTER TABLE "{{defender_events}}" ADD CONSTRAINT "{{prefix}}defender_events_host_id_fk_defender_hosts_id" FOREIGN KEY +("host_id") REFERENCES "{{defender_hosts}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; CREATE TABLE "{{folders}}" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(255) NOT NULL UNIQUE, "description" varchar(512) NULL, -"path" varchar(512) NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, +"path" text NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, "filesystem" text NULL); CREATE TABLE "{{users}}" ("id" serial NOT NULL PRIMARY KEY, "username" varchar(255) NOT NULL UNIQUE, "status" integer NOT NULL, "expiration_date" bigint NOT NULL, "description" varchar(512) NULL, "password" text NULL, "public_keys" text NULL, -"home_dir" varchar(512) NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, "max_sessions" integer NOT NULL, +"home_dir" text NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, "max_sessions" integer NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "permissions" text NOT NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, "upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL, "last_login" bigint NOT NULL, "filters" text NULL, "filesystem" text NULL, -"additional_info" text NULL); -CREATE TABLE "{{folders_mapping}}" ("id" serial NOT NULL PRIMARY KEY, "virtual_path" varchar(512) NOT NULL, +"additional_info" text NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "email" varchar(255) NULL); +CREATE TABLE "{{folders_mapping}}" ("id" serial NOT NULL PRIMARY KEY, "virtual_path" text NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "folder_id" integer NOT NULL, "user_id" integer NOT NULL); ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}unique_mapping" UNIQUE ("user_id", "folder_id"); ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}folders_mapping_folder_id_fk_folders_id" -FOREIGN KEY ("folder_id") REFERENCES "{{folders}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED; +FOREIGN KEY ("folder_id") REFERENCES "{{folders}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; ALTER TABLE "{{folders_mapping}}" ADD CONSTRAINT "{{prefix}}folders_mapping_user_id_fk_users_id" -FOREIGN KEY ("user_id") REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED; -CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); -CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); -INSERT INTO {{schema_version}} (version) VALUES (10); -` - pgsqlV11SQL = `CREATE TABLE "{{api_keys}}" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(255) NOT NULL, -"key_id" varchar(50) NOT NULL UNIQUE, "api_key" varchar(255) NOT NULL UNIQUE, "scope" integer NOT NULL, -"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL,"expires_at" bigint NOT NULL, -"description" text NULL, "admin_id" integer NULL, "user_id" integer NULL); -ALTER TABLE "{{api_keys}}" ADD CONSTRAINT "{{prefix}}api_keys_admin_id_fk_admins_id" FOREIGN KEY ("admin_id") -REFERENCES "{{admins}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; -ALTER TABLE "{{api_keys}}" ADD CONSTRAINT "{{prefix}}api_keys_user_id_fk_users_id" FOREIGN KEY ("user_id") -REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; -CREATE INDEX "{{prefix}}api_keys_admin_id_idx" ON "{{api_keys}}" ("admin_id"); -CREATE INDEX "{{prefix}}api_keys_user_id_idx" ON "{{api_keys}}" ("user_id"); -` - pgsqlV11DownSQL = `DROP TABLE "{{api_keys}}" CASCADE;` - pgsqlV12SQL = `ALTER TABLE "{{admins}}" ADD COLUMN "created_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{admins}}" ALTER COLUMN "created_at" DROP DEFAULT; -ALTER TABLE "{{admins}}" ADD COLUMN "updated_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{admins}}" ALTER COLUMN "updated_at" DROP DEFAULT; -ALTER TABLE "{{admins}}" ADD COLUMN "last_login" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{admins}}" ALTER COLUMN "last_login" DROP DEFAULT; -ALTER TABLE "{{users}}" ADD COLUMN "created_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{users}}" ALTER COLUMN "created_at" DROP DEFAULT; -ALTER TABLE "{{users}}" ADD COLUMN "updated_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{users}}" ALTER COLUMN "updated_at" DROP DEFAULT; -CREATE INDEX "{{prefix}}users_updated_at_idx" ON "{{users}}" ("updated_at"); -` - pgsqlV12DownSQL = `ALTER TABLE "{{users}}" DROP COLUMN "updated_at" CASCADE; -ALTER TABLE "{{users}}" DROP COLUMN "created_at" CASCADE; -ALTER TABLE "{{admins}}" DROP COLUMN "created_at" CASCADE; -ALTER TABLE "{{admins}}" DROP COLUMN "updated_at" CASCADE; -ALTER TABLE "{{admins}}" DROP COLUMN "last_login" CASCADE; -` - pgsqlV13SQL = `ALTER TABLE "{{users}}" ADD COLUMN "email" varchar(255) NULL;` - pgsqlV13DownSQL = `ALTER TABLE "{{users}}" DROP COLUMN "email" CASCADE;` - pgsqlV14SQL = `CREATE TABLE "{{shares}}" ("id" serial NOT NULL PRIMARY KEY, +FOREIGN KEY ("user_id") REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; +CREATE TABLE "{{shares}}" ("id" serial NOT NULL PRIMARY KEY, "share_id" varchar(60) NOT NULL UNIQUE, "name" varchar(255) NOT NULL, "description" varchar(512) NULL, "scope" integer NOT NULL, "paths" text NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL, "expires_at" bigint NOT NULL, "password" text NULL, @@ -96,26 +67,29 @@ ALTER TABLE "{{admins}}" DROP COLUMN "last_login" CASCADE; "user_id" integer NOT NULL); ALTER TABLE "{{shares}}" ADD CONSTRAINT "{{prefix}}shares_user_id_fk_users_id" FOREIGN KEY ("user_id") REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; +CREATE TABLE "{{api_keys}}" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(255) NOT NULL, +"key_id" varchar(50) NOT NULL UNIQUE, "api_key" varchar(255) NOT NULL UNIQUE, "scope" integer NOT NULL, +"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL,"expires_at" bigint NOT NULL, +"description" text NULL, "admin_id" integer NULL, "user_id" integer NULL); +ALTER TABLE "{{api_keys}}" ADD CONSTRAINT "{{prefix}}api_keys_admin_id_fk_admins_id" FOREIGN KEY ("admin_id") +REFERENCES "{{admins}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; +ALTER TABLE "{{api_keys}}" ADD CONSTRAINT "{{prefix}}api_keys_user_id_fk_users_id" FOREIGN KEY ("user_id") +REFERENCES "{{users}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; +CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); +CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); +CREATE INDEX "{{prefix}}api_keys_admin_id_idx" ON "{{api_keys}}" ("admin_id"); +CREATE INDEX "{{prefix}}api_keys_user_id_idx" ON "{{api_keys}}" ("user_id"); +CREATE INDEX "{{prefix}}users_updated_at_idx" ON "{{users}}" ("updated_at"); CREATE INDEX "{{prefix}}shares_user_id_idx" ON "{{shares}}" ("user_id"); -` - pgsqlV14DownSQL = `DROP TABLE "{{shares}}" CASCADE;` - pgsqlV15SQL = `CREATE TABLE "{{defender_hosts}}" ("id" bigserial NOT NULL PRIMARY KEY, "ip" varchar(50) NOT NULL UNIQUE, -"ban_time" bigint NOT NULL, "updated_at" bigint NOT NULL); -CREATE TABLE "{{defender_events}}" ("id" bigserial NOT NULL PRIMARY KEY, "date_time" bigint NOT NULL, "score" integer NOT NULL, -"host_id" bigint NOT NULL); -ALTER TABLE "{{defender_events}}" ADD CONSTRAINT "{{prefix}}defender_events_host_id_fk_defender_hosts_id" FOREIGN KEY -("host_id") REFERENCES "{{defender_hosts}}" ("id") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE; CREATE INDEX "{{prefix}}defender_hosts_updated_at_idx" ON "{{defender_hosts}}" ("updated_at"); CREATE INDEX "{{prefix}}defender_hosts_ban_time_idx" ON "{{defender_hosts}}" ("ban_time"); CREATE INDEX "{{prefix}}defender_events_date_time_idx" ON "{{defender_events}}" ("date_time"); CREATE INDEX "{{prefix}}defender_events_host_id_idx" ON "{{defender_events}}" ("host_id"); -` - pgsqlV15DownSQL = `DROP TABLE "{{defender_events}}" CASCADE; -DROP TABLE "{{defender_hosts}}" CASCADE; +INSERT INTO {{schema_version}} (version) VALUES (15); ` ) -// PGSQLProvider auth provider for PostgreSQL database +// PGSQLProvider defines the auth provider for PostgreSQL database type PGSQLProvider struct { dbHandle *sql.DB } @@ -391,11 +365,17 @@ func (p *PGSQLProvider) initializeDatabase() error { if errors.Is(err, sql.ErrNoRows) { return errSchemaVersionEmpty } + logger.InfoToConsole("creating initial database schema, version 15") + providerLog(logger.LevelInfo, "creating initial database schema, version 15") initialSQL := strings.ReplaceAll(pgsqlInitial, "{{schema_version}}", sqlTableSchemaVersion) initialSQL = strings.ReplaceAll(initialSQL, "{{admins}}", sqlTableAdmins) initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{api_keys}}", sqlTableAPIKeys) + initialSQL = strings.ReplaceAll(initialSQL, "{{shares}}", sqlTableShares) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_events}}", sqlTableDefenderEvents) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_hosts}}", sqlTableDefenderHosts) initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) if config.Driver == CockroachDataProviderName { // Cockroach does not support deferrable constraint validation, we don't need them, @@ -404,10 +384,9 @@ func (p *PGSQLProvider) initializeDatabase() error { initialSQL = strings.ReplaceAll(initialSQL, "DEFERRABLE INITIALLY DEFERRED", "") } - return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{initialSQL}, 10) + return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{initialSQL}, 15) } -//nolint:dupl func (p *PGSQLProvider) migrateDatabase() error { dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true) if err != nil { @@ -418,21 +397,11 @@ func (p *PGSQLProvider) migrateDatabase() error { case version == sqlDatabaseVersion: providerLog(logger.LevelDebug, "sql database is up to date, current version: %v", version) return ErrNoInitRequired - case version < 10: + case version < 15: err = fmt.Errorf("database version %v is too old, please see the upgrading docs", version) providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err - case version == 10: - return updatePGSQLDatabaseFromV10(p.dbHandle) - case version == 11: - return updatePGSQLDatabaseFromV11(p.dbHandle) - case version == 12: - return updatePGSQLDatabaseFromV12(p.dbHandle) - case version == 13: - return updatePGSQLDatabaseFromV13(p.dbHandle) - case version == 14: - return updatePGSQLDatabaseFromV14(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database version %v is newer than the supported one: %v", version, @@ -455,16 +424,6 @@ func (p *PGSQLProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { - case 15: - return downgradePGSQLDatabaseFromV15(p.dbHandle) - case 14: - return downgradePGSQLDatabaseFromV14(p.dbHandle) - case 13: - return downgradePGSQLDatabaseFromV13(p.dbHandle) - case 12: - return downgradePGSQLDatabaseFromV12(p.dbHandle) - case 11: - return downgradePGSQLDatabaseFromV11(p.dbHandle) default: return fmt.Errorf("database version not handled: %v", dbVersion.Version) } @@ -482,149 +441,3 @@ func (p *PGSQLProvider) resetDatabase() error { sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{sql}, 0) } - -func updatePGSQLDatabaseFromV10(dbHandle *sql.DB) error { - if err := updatePGSQLDatabaseFrom10To11(dbHandle); err != nil { - return err - } - return updatePGSQLDatabaseFromV11(dbHandle) -} - -func updatePGSQLDatabaseFromV11(dbHandle *sql.DB) error { - if err := updatePGSQLDatabaseFrom11To12(dbHandle); err != nil { - return err - } - return updatePGSQLDatabaseFromV12(dbHandle) -} - -func updatePGSQLDatabaseFromV12(dbHandle *sql.DB) error { - if err := updatePGSQLDatabaseFrom12To13(dbHandle); err != nil { - return err - } - return updatePGSQLDatabaseFromV13(dbHandle) -} - -func updatePGSQLDatabaseFromV13(dbHandle *sql.DB) error { - if err := updatePGSQLDatabaseFrom13To14(dbHandle); err != nil { - return err - } - return updatePGSQLDatabaseFromV14(dbHandle) -} - -func updatePGSQLDatabaseFromV14(dbHandle *sql.DB) error { - return updatePGSQLDatabaseFrom14To15(dbHandle) -} - -func downgradePGSQLDatabaseFromV15(dbHandle *sql.DB) error { - if err := downgradePGSQLDatabaseFrom15To14(dbHandle); err != nil { - return err - } - return downgradePGSQLDatabaseFromV14(dbHandle) -} - -func downgradePGSQLDatabaseFromV14(dbHandle *sql.DB) error { - if err := downgradePGSQLDatabaseFrom14To13(dbHandle); err != nil { - return err - } - return downgradePGSQLDatabaseFromV13(dbHandle) -} - -func downgradePGSQLDatabaseFromV13(dbHandle *sql.DB) error { - if err := downgradePGSQLDatabaseFrom13To12(dbHandle); err != nil { - return err - } - return downgradePGSQLDatabaseFromV12(dbHandle) -} - -func downgradePGSQLDatabaseFromV12(dbHandle *sql.DB) error { - if err := downgradePGSQLDatabaseFrom12To11(dbHandle); err != nil { - return err - } - return downgradePGSQLDatabaseFromV11(dbHandle) -} - -func downgradePGSQLDatabaseFromV11(dbHandle *sql.DB) error { - return downgradePGSQLDatabaseFrom11To10(dbHandle) -} - -func updatePGSQLDatabaseFrom13To14(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 13 -> 14") - providerLog(logger.LevelInfo, "updating database version: 13 -> 14") - sql := strings.ReplaceAll(pgsqlV14SQL, "{{shares}}", sqlTableShares) - sql = strings.ReplaceAll(sql, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 14) -} - -func updatePGSQLDatabaseFrom14To15(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 14 -> 15") - providerLog(logger.LevelInfo, "updating database version: 14 -> 15") - sql := strings.ReplaceAll(pgsqlV15SQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 15) -} - -func downgradePGSQLDatabaseFrom15To14(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 15 -> 14") - providerLog(logger.LevelInfo, "downgrading database version: 15 -> 14") - sql := strings.ReplaceAll(pgsqlV15DownSQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 14) -} - -func downgradePGSQLDatabaseFrom14To13(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 14 -> 13") - providerLog(logger.LevelInfo, "downgrading database version: 14 -> 13") - sql := strings.ReplaceAll(pgsqlV14DownSQL, "{{shares}}", sqlTableShares) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 13) -} - -func updatePGSQLDatabaseFrom12To13(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 12 -> 13") - providerLog(logger.LevelInfo, "updating database version: 12 -> 13") - sql := strings.ReplaceAll(pgsqlV13SQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 13) -} - -func downgradePGSQLDatabaseFrom13To12(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 13 -> 12") - providerLog(logger.LevelInfo, "downgrading database version: 13 -> 12") - sql := strings.ReplaceAll(pgsqlV13DownSQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 12) -} - -func updatePGSQLDatabaseFrom11To12(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 11 -> 12") - providerLog(logger.LevelInfo, "updating database version: 11 -> 12") - sql := strings.ReplaceAll(pgsqlV12SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 12) -} - -func downgradePGSQLDatabaseFrom12To11(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 12 -> 11") - providerLog(logger.LevelInfo, "downgrading database version: 12 -> 11") - sql := strings.ReplaceAll(pgsqlV12DownSQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 11) -} - -func updatePGSQLDatabaseFrom10To11(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 10 -> 11") - providerLog(logger.LevelInfo, "updating database version: 10 -> 11") - sql := strings.ReplaceAll(pgsqlV11SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{api_keys}}", sqlTableAPIKeys) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 11) -} - -func downgradePGSQLDatabaseFrom11To10(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 11 -> 10") - providerLog(logger.LevelInfo, "downgrading database version: 11 -> 10") - sql := strings.ReplaceAll(pgsqlV11DownSQL, "{{api_keys}}", sqlTableAPIKeys) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 10) -} diff --git a/dataprovider/sqlite.go b/dataprovider/sqlite.go index eeefa98b..4fb25fb8 100644 --- a/dataprovider/sqlite.go +++ b/dataprovider/sqlite.go @@ -35,75 +35,53 @@ DROP TABLE IF EXISTS "{{schema_version}}"; sqliteInitialSQL = `CREATE TABLE "{{schema_version}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "version" integer NOT NULL); CREATE TABLE "{{admins}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "username" varchar(255) NOT NULL UNIQUE, "description" varchar(512) NULL, "password" varchar(255) NOT NULL, "email" varchar(255) NULL, "status" integer NOT NULL, -"permissions" text NOT NULL, "filters" text NULL, "additional_info" text NULL); -CREATE TABLE "{{folders}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(255) NOT NULL UNIQUE, -"description" varchar(512) NULL, "path" varchar(512) NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, -"last_quota_update" bigint NOT NULL, "filesystem" text NULL); -CREATE TABLE "{{users}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "username" varchar(255) NOT NULL UNIQUE, -"status" integer NOT NULL, "expiration_date" bigint NOT NULL, "description" varchar(512) NULL, "password" text NULL, -"public_keys" text NULL, "home_dir" varchar(512) NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, -"max_sessions" integer NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "permissions" text NOT NULL, -"used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, -"upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL, "last_login" bigint NOT NULL, "filters" text NULL, -"filesystem" text NULL, "additional_info" text NULL); -CREATE TABLE "{{folders_mapping}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "virtual_path" varchar(512) NOT NULL, -"quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "folder_id" integer NOT NULL REFERENCES "{{folders}}" ("id") -ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, -CONSTRAINT "{{prefix}}unique_mapping" UNIQUE ("user_id", "folder_id")); -CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); -CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); -INSERT INTO {{schema_version}} (version) VALUES (10); -` - sqliteV11SQL = `CREATE TABLE "{{api_keys}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(255) NOT NULL, -"key_id" varchar(50) NOT NULL UNIQUE, "api_key" varchar(255) NOT NULL UNIQUE, "scope" integer NOT NULL, "created_at" bigint NOT NULL, -"updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL, "expires_at" bigint NOT NULL, "description" text NULL, -"admin_id" integer NULL REFERENCES "{{admins}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, -"user_id" integer NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED); -CREATE INDEX "{{prefix}}api_keys_admin_id_idx" ON "api_keys" ("admin_id"); -CREATE INDEX "{{prefix}}api_keys_user_id_idx" ON "api_keys" ("user_id"); -` - sqliteV11DownSQL = `DROP TABLE "{{api_keys}}";` - sqliteV12SQL = `ALTER TABLE "{{admins}}" ADD COLUMN "created_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{admins}}" ADD COLUMN "updated_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{admins}}" ADD COLUMN "last_login" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{users}}" ADD COLUMN "created_at" bigint DEFAULT 0 NOT NULL; -ALTER TABLE "{{users}}" ADD COLUMN "updated_at" bigint DEFAULT 0 NOT NULL; -CREATE INDEX "{{prefix}}users_updated_at_idx" ON "{{users}}" ("updated_at"); -` - sqliteV12DownSQL = `DROP INDEX "{{prefix}}users_updated_at_idx"; -ALTER TABLE "{{users}}" DROP COLUMN "updated_at"; -ALTER TABLE "{{users}}" DROP COLUMN "created_at"; -ALTER TABLE "{{admins}}" DROP COLUMN "created_at"; -ALTER TABLE "{{admins}}" DROP COLUMN "updated_at"; -ALTER TABLE "{{admins}}" DROP COLUMN "last_login"; -` - sqliteV13SQL = `ALTER TABLE "{{users}}" ADD COLUMN "email" varchar(255) NULL;` - sqliteV13DownSQL = `ALTER TABLE "{{users}}" DROP COLUMN "email";` - sqliteV14SQL = `CREATE TABLE "{{shares}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, -"share_id" varchar(60) NOT NULL UNIQUE, "name" varchar(255) NOT NULL, "description" varchar(512) NULL, -"scope" integer NOT NULL, "paths" text NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, -"last_use_at" bigint NOT NULL, "expires_at" bigint NOT NULL, "password" text NULL, "max_tokens" integer NOT NULL, -"used_tokens" integer NOT NULL, "allow_from" text NULL, -"user_id" integer NOT NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED); -CREATE INDEX "{{prefix}}shares_user_id_idx" ON "{{shares}}" ("user_id"); -` - sqliteV14DownSQL = `DROP TABLE "{{shares}}";` - sqliteV15SQL = `CREATE TABLE "{{defender_hosts}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, -"ip" varchar(50) NOT NULL UNIQUE, "ban_time" bigint NOT NULL, "updated_at" bigint NOT NULL); +"permissions" text NOT NULL, "filters" text NULL, "additional_info" text NULL, "last_login" bigint NOT NULL, +"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL); +CREATE TABLE "{{defender_hosts}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "ip" varchar(50) NOT NULL UNIQUE, +"ban_time" bigint NOT NULL, "updated_at" bigint NOT NULL); CREATE TABLE "{{defender_events}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "date_time" bigint NOT NULL, "score" integer NOT NULL, "host_id" integer NOT NULL REFERENCES "{{defender_hosts}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE "{{folders}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(255) NOT NULL UNIQUE, +"description" varchar(512) NULL, "path" text NULL, "used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, +"last_quota_update" bigint NOT NULL, "filesystem" text NULL); +CREATE TABLE "{{users}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "username" varchar(255) NOT NULL UNIQUE, +"status" integer NOT NULL, "expiration_date" bigint NOT NULL, "description" varchar(512) NULL, "password" text NULL, +"public_keys" text NULL, "home_dir" text NOT NULL, "uid" integer NOT NULL, "gid" integer NOT NULL, +"max_sessions" integer NOT NULL, "quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "permissions" text NOT NULL, +"used_quota_size" bigint NOT NULL, "used_quota_files" integer NOT NULL, "last_quota_update" bigint NOT NULL, +"upload_bandwidth" integer NOT NULL, "download_bandwidth" integer NOT NULL, "last_login" bigint NOT NULL, "filters" text NULL, +"filesystem" text NULL, "additional_info" text NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, +"email" varchar(255) NULL); +CREATE TABLE "{{folders_mapping}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "virtual_path" text NOT NULL, +"quota_size" bigint NOT NULL, "quota_files" integer NOT NULL, "folder_id" integer NOT NULL REFERENCES "{{folders}}" ("id") +ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, +CONSTRAINT "{{prefix}}unique_mapping" UNIQUE ("user_id", "folder_id")); +CREATE TABLE "{{shares}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "share_id" varchar(60) NOT NULL UNIQUE, +"name" varchar(255) NOT NULL, "description" varchar(512) NULL, "scope" integer NOT NULL, "paths" text NOT NULL, +"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL, "expires_at" bigint NOT NULL, +"password" text NULL, "max_tokens" integer NOT NULL, "used_tokens" integer NOT NULL, "allow_from" text NULL, +"user_id" integer NOT NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE "{{api_keys}}" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(255) NOT NULL, +"key_id" varchar(50) NOT NULL UNIQUE, "api_key" varchar(255) NOT NULL UNIQUE, "scope" integer NOT NULL, +"created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "last_use_at" bigint NOT NULL, "expires_at" bigint NOT NULL, +"description" text NULL, "admin_id" integer NULL REFERENCES "{{admins}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, +"user_id" integer NULL REFERENCES "{{users}}" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED); +CREATE INDEX "{{prefix}}folders_mapping_folder_id_idx" ON "{{folders_mapping}}" ("folder_id"); +CREATE INDEX "{{prefix}}folders_mapping_user_id_idx" ON "{{folders_mapping}}" ("user_id"); +CREATE INDEX "{{prefix}}api_keys_admin_id_idx" ON "{{api_keys}}" ("admin_id"); +CREATE INDEX "{{prefix}}api_keys_user_id_idx" ON "{{api_keys}}" ("user_id"); +CREATE INDEX "{{prefix}}users_updated_at_idx" ON "{{users}}" ("updated_at"); +CREATE INDEX "{{prefix}}shares_user_id_idx" ON "{{shares}}" ("user_id"); CREATE INDEX "{{prefix}}defender_hosts_updated_at_idx" ON "{{defender_hosts}}" ("updated_at"); CREATE INDEX "{{prefix}}defender_hosts_ban_time_idx" ON "{{defender_hosts}}" ("ban_time"); CREATE INDEX "{{prefix}}defender_events_date_time_idx" ON "{{defender_events}}" ("date_time"); CREATE INDEX "{{prefix}}defender_events_host_id_idx" ON "{{defender_events}}" ("host_id"); -` - sqliteV15DownSQL = `DROP TABLE "{{defender_events}}"; -DROP TABLE "{{defender_hosts}}"; +INSERT INTO {{schema_version}} (version) VALUES (15); ` ) -// SQLiteProvider auth provider for SQLite database +// SQLiteProvider defines the auth provider for SQLite database type SQLiteProvider struct { dbHandle *sql.DB } @@ -372,17 +350,22 @@ func (p *SQLiteProvider) initializeDatabase() error { if errors.Is(err, sql.ErrNoRows) { return errSchemaVersionEmpty } + logger.InfoToConsole("creating initial database schema, version 15") + providerLog(logger.LevelInfo, "creating initial database schema, version 15") initialSQL := strings.ReplaceAll(sqliteInitialSQL, "{{schema_version}}", sqlTableSchemaVersion) initialSQL = strings.ReplaceAll(initialSQL, "{{admins}}", sqlTableAdmins) initialSQL = strings.ReplaceAll(initialSQL, "{{folders}}", sqlTableFolders) initialSQL = strings.ReplaceAll(initialSQL, "{{users}}", sqlTableUsers) initialSQL = strings.ReplaceAll(initialSQL, "{{folders_mapping}}", sqlTableFoldersMapping) + initialSQL = strings.ReplaceAll(initialSQL, "{{api_keys}}", sqlTableAPIKeys) + initialSQL = strings.ReplaceAll(initialSQL, "{{shares}}", sqlTableShares) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_events}}", sqlTableDefenderEvents) + initialSQL = strings.ReplaceAll(initialSQL, "{{defender_hosts}}", sqlTableDefenderHosts) initialSQL = strings.ReplaceAll(initialSQL, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{initialSQL}, 10) + return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{initialSQL}, 15) } -//nolint:dupl func (p *SQLiteProvider) migrateDatabase() error { dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true) if err != nil { @@ -393,21 +376,11 @@ func (p *SQLiteProvider) migrateDatabase() error { case version == sqlDatabaseVersion: providerLog(logger.LevelDebug, "sql database is up to date, current version: %v", version) return ErrNoInitRequired - case version < 10: + case version < 15: err = fmt.Errorf("database version %v is too old, please see the upgrading docs", version) providerLog(logger.LevelError, "%v", err) logger.ErrorToConsole("%v", err) return err - case version == 10: - return updateSQLiteDatabaseFromV10(p.dbHandle) - case version == 11: - return updateSQLiteDatabaseFromV11(p.dbHandle) - case version == 12: - return updateSQLiteDatabaseFromV12(p.dbHandle) - case version == 13: - return updateSQLiteDatabaseFromV13(p.dbHandle) - case version == 14: - return updateSQLiteDatabaseFromV14(p.dbHandle) default: if version > sqlDatabaseVersion { providerLog(logger.LevelError, "database version %v is newer than the supported one: %v", version, @@ -430,16 +403,6 @@ func (p *SQLiteProvider) revertDatabase(targetVersion int) error { } switch dbVersion.Version { - case 15: - return downgradeSQLiteDatabaseFromV15(p.dbHandle) - case 14: - return downgradeSQLiteDatabaseFromV14(p.dbHandle) - case 13: - return downgradeSQLiteDatabaseFromV13(p.dbHandle) - case 12: - return downgradeSQLiteDatabaseFromV12(p.dbHandle) - case 11: - return downgradeSQLiteDatabaseFromV11(p.dbHandle) default: return fmt.Errorf("database version not handled: %v", dbVersion.Version) } @@ -458,152 +421,6 @@ func (p *SQLiteProvider) resetDatabase() error { return sqlCommonExecSQLAndUpdateDBVersion(p.dbHandle, []string{sql}, 0) } -func updateSQLiteDatabaseFromV10(dbHandle *sql.DB) error { - if err := updateSQLiteDatabaseFrom10To11(dbHandle); err != nil { - return err - } - return updateSQLiteDatabaseFromV11(dbHandle) -} - -func updateSQLiteDatabaseFromV11(dbHandle *sql.DB) error { - if err := updateSQLiteDatabaseFrom11To12(dbHandle); err != nil { - return err - } - return updateSQLiteDatabaseFromV12(dbHandle) -} - -func updateSQLiteDatabaseFromV12(dbHandle *sql.DB) error { - if err := updateSQLiteDatabaseFrom12To13(dbHandle); err != nil { - return err - } - return updateSQLiteDatabaseFromV13(dbHandle) -} - -func updateSQLiteDatabaseFromV13(dbHandle *sql.DB) error { - if err := updateSQLiteDatabaseFrom13To14(dbHandle); err != nil { - return err - } - return updateSQLiteDatabaseFromV14(dbHandle) -} - -func updateSQLiteDatabaseFromV14(dbHandle *sql.DB) error { - return updateSQLiteDatabaseFrom14To15(dbHandle) -} - -func downgradeSQLiteDatabaseFromV15(dbHandle *sql.DB) error { - if err := downgradeSQLiteDatabaseFrom15To14(dbHandle); err != nil { - return err - } - return downgradeSQLiteDatabaseFromV14(dbHandle) -} - -func downgradeSQLiteDatabaseFromV14(dbHandle *sql.DB) error { - if err := downgradeSQLiteDatabaseFrom14To13(dbHandle); err != nil { - return err - } - return downgradeSQLiteDatabaseFromV13(dbHandle) -} - -func downgradeSQLiteDatabaseFromV13(dbHandle *sql.DB) error { - if err := downgradeSQLiteDatabaseFrom13To12(dbHandle); err != nil { - return err - } - return downgradeSQLiteDatabaseFromV12(dbHandle) -} - -func downgradeSQLiteDatabaseFromV12(dbHandle *sql.DB) error { - if err := downgradeSQLiteDatabaseFrom12To11(dbHandle); err != nil { - return err - } - return downgradeSQLiteDatabaseFromV11(dbHandle) -} - -func downgradeSQLiteDatabaseFromV11(dbHandle *sql.DB) error { - return downgradeSQLiteDatabaseFrom11To10(dbHandle) -} - -func updateSQLiteDatabaseFrom13To14(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 13 -> 14") - providerLog(logger.LevelInfo, "updating database version: 13 -> 14") - sql := strings.ReplaceAll(sqliteV14SQL, "{{shares}}", sqlTableShares) - sql = strings.ReplaceAll(sql, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 14) -} - -func updateSQLiteDatabaseFrom14To15(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 14 -> 15") - providerLog(logger.LevelInfo, "updating database version: 14 -> 15") - sql := strings.ReplaceAll(sqliteV15SQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 15) -} - -func downgradeSQLiteDatabaseFrom15To14(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 15 -> 14") - providerLog(logger.LevelInfo, "downgrading database version: 15 -> 14") - sql := strings.ReplaceAll(sqliteV15DownSQL, "{{defender_events}}", sqlTableDefenderEvents) - sql = strings.ReplaceAll(sql, "{{defender_hosts}}", sqlTableDefenderHosts) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 14) -} - -func downgradeSQLiteDatabaseFrom14To13(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 14 -> 13") - providerLog(logger.LevelInfo, "downgrading database version: 14 -> 13") - sql := strings.ReplaceAll(sqliteV14DownSQL, "{{shares}}", sqlTableShares) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 13) -} - -func updateSQLiteDatabaseFrom12To13(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 12 -> 13") - providerLog(logger.LevelInfo, "updating database version: 12 -> 13") - sql := strings.ReplaceAll(sqliteV13SQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 13) -} - -func downgradeSQLiteDatabaseFrom13To12(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 13 -> 12") - providerLog(logger.LevelInfo, "downgrading database version: 13 -> 12") - sql := strings.ReplaceAll(sqliteV13DownSQL, "{{users}}", sqlTableUsers) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 12) -} - -func updateSQLiteDatabaseFrom11To12(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 11 -> 12") - providerLog(logger.LevelInfo, "updating database version: 11 -> 12") - sql := strings.ReplaceAll(sqliteV12SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 12) -} - -func downgradeSQLiteDatabaseFrom12To11(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 12 -> 11") - providerLog(logger.LevelInfo, "downgrading database version: 12 -> 11") - sql := strings.ReplaceAll(sqliteV12DownSQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 11) -} - -func updateSQLiteDatabaseFrom10To11(dbHandle *sql.DB) error { - logger.InfoToConsole("updating database version: 10 -> 11") - providerLog(logger.LevelInfo, "updating database version: 10 -> 11") - sql := strings.ReplaceAll(sqliteV11SQL, "{{users}}", sqlTableUsers) - sql = strings.ReplaceAll(sql, "{{admins}}", sqlTableAdmins) - sql = strings.ReplaceAll(sql, "{{api_keys}}", sqlTableAPIKeys) - sql = strings.ReplaceAll(sql, "{{prefix}}", config.SQLTablesPrefix) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 11) -} - -func downgradeSQLiteDatabaseFrom11To10(dbHandle *sql.DB) error { - logger.InfoToConsole("downgrading database version: 11 -> 10") - providerLog(logger.LevelInfo, "downgrading database version: 11 -> 10") - sql := strings.ReplaceAll(sqliteV11DownSQL, "{{api_keys}}", sqlTableAPIKeys) - return sqlCommonExecSQLAndUpdateDBVersion(dbHandle, []string{sql}, 10) -} - /*func setPragmaFK(dbHandle *sql.DB, value string) error { ctx, cancel := context.WithTimeout(context.Background(), longSQLQueryTimeout) defer cancel() diff --git a/go.mod b/go.mod index 135008c4..f9bf6f86 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Azure/azure-storage-blob-go v0.14.0 github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 github.com/alexedwards/argon2id v0.0.0-20211130144151-3585854a6387 - github.com/aws/aws-sdk-go v1.42.28 + github.com/aws/aws-sdk-go v1.42.30 github.com/cockroachdb/cockroach-go/v2 v2.2.5 github.com/eikenb/pipeat v0.0.0-20210603033007-44fc3ffce52b github.com/fclairamb/ftpserverlib v0.17.0 @@ -25,7 +25,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.0 github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 github.com/klauspost/compress v1.13.6 - github.com/lestrrat-go/jwx v1.2.14 + github.com/lestrrat-go/jwx v1.2.15 github.com/lib/pq v1.10.4 github.com/lithammer/shortuuid/v3 v3.0.7 github.com/mattn/go-sqlite3 v1.14.10 @@ -56,7 +56,7 @@ require ( golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 - google.golang.org/api v0.63.0 + google.golang.org/api v0.64.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0 ) @@ -68,16 +68,11 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/boombuler/barcode v1.0.1 // indirect github.com/cenkalti/backoff v2.2.1+incompatible // indirect - github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect - github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect - github.com/cncf/xds/go v0.0.0-20211216145620-d92e9ce0af51 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect - github.com/envoyproxy/go-control-plane v0.10.1 // indirect - github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect github.com/fatih/color v1.13.0 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/go-ole/go-ole v1.2.6 // indirect @@ -132,7 +127,7 @@ require ( golang.org/x/tools v0.1.8 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb // indirect + google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect google.golang.org/grpc v1.43.0 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/ini.v1 v1.66.2 // indirect diff --git a/go.sum b/go.sum index 47ac017d..f703b442 100644 --- a/go.sum +++ b/go.sum @@ -146,8 +146,8 @@ github.com/aws/aws-sdk-go v1.15.27/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZo github.com/aws/aws-sdk-go v1.37.0/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go v1.38.68/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go v1.40.34/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= -github.com/aws/aws-sdk-go v1.42.28 h1:YJtgL7IGSN41saY4JLW08jya5nU0vGcuAeAa1OL2M6c= -github.com/aws/aws-sdk-go v1.42.28/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc= +github.com/aws/aws-sdk-go v1.42.30 h1:GvzWHwAdE5ZQ9UOcq0lX+PTzVJ4+sm1DjYrk6nUSTgA= +github.com/aws/aws-sdk-go v1.42.30/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc= github.com/aws/aws-sdk-go-v2 v1.7.0/go.mod h1:tb9wi5s61kTDA5qCkcDbt3KRVV74GGslQkl/DRdX/P4= github.com/aws/aws-sdk-go-v2 v1.9.0/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2/config v1.7.0/go.mod h1:w9+nMZ7soXCe5nT46Ri354SNhXDQ6v+V5wqDjnZE+GY= @@ -176,7 +176,6 @@ github.com/casbin/casbin/v2 v2.31.6/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRt github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/census-instrumentation/opencensus-proto v0.3.0 h1:t/LhUZLVitR1Ow2YOnduCsavhwFUklBMoGVYUCqmCqk= github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -193,7 +192,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 h1:hzAQntlaYRkVSFEfj9OTWlVV1H155FMD8BTKktLv0QI= github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= @@ -201,8 +199,6 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= -github.com/cncf/xds/go v0.0.0-20211216145620-d92e9ce0af51 h1:F6fR7MjvOIk+FLQOeBCAbbKItVgbdj0l9VWPiHeBEiY= -github.com/cncf/xds/go v0.0.0-20211216145620-d92e9ce0af51/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/cockroachdb/cockroach-go/v2 v2.2.5 h1:tfPdGHO5YpmrpN2ikJZYpaSGgU8WALwwjH3s+msiTQ0= github.com/cockroachdb/cockroach-go/v2 v2.2.5/go.mod h1:q4ZRgO6CQpwNyEvEwSxwNrOSVchsmzrBnAv3HuZ3Abc= @@ -250,10 +246,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= -github.com/envoyproxy/go-control-plane v0.10.1 h1:cgDRLG7bs59Zd+apAWuzLQL95obVYAymNJek76W3mgw= github.com/envoyproxy/go-control-plane v0.10.1/go.mod h1:AY7fTTXNdv/aJ2O5jwpxAPOWUZ7hQAEvzN5Pf27BkQQ= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/envoyproxy/protoc-gen-validate v0.6.2 h1:JiO+kJTpmYGjEodY7O1Zk8oZcNz1+f30UtwtXoFUPzE= github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= @@ -312,7 +306,6 @@ github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22 github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= github.com/goccy/go-json v0.7.6/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.9.0 h1:2flW7bkbrRgU8VuDi0WXDqTmPimjv1thfxkPe8sug+8= github.com/goccy/go-json v0.9.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -578,8 +571,8 @@ github.com/lestrrat-go/httpcc v1.0.0/go.mod h1:tGS/u00Vh5N6FHNkExqGGNId8e0Big+++ github.com/lestrrat-go/iter v1.0.1 h1:q8faalr2dY6o8bV45uwrxq12bRa1ezKrB6oM9FUgN4A= github.com/lestrrat-go/iter v1.0.1/go.mod h1:zIdgO1mRKhn8l9vrZJZz9TUMMFbQbLeTsbqPDrJ/OJc= github.com/lestrrat-go/jwx v1.2.6/go.mod h1:tJuGuAI3LC71IicTx82Mz1n3w9woAs2bYJZpkjJQ5aU= -github.com/lestrrat-go/jwx v1.2.14 h1:69OeaiFKCTn8xDmBGzHTgv/GBoO1LJcXw99GfYCDKzg= -github.com/lestrrat-go/jwx v1.2.14/go.mod h1:3Q3Re8TaOcVTdpx4Tvz++OWmryDklihTDqrrwQiyS2A= +github.com/lestrrat-go/jwx v1.2.15 h1:58CEGJpf1TS3NJASMfMkTp6stlvPTsqs1xxAu/Yf/uM= +github.com/lestrrat-go/jwx v1.2.15/go.mod h1:DJKaoM8f1OvYVwWoW45gBrUxMlpD4FHjT0UnrW3iX28= github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4= github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -1146,8 +1139,9 @@ google.golang.org/api v0.58.0/go.mod h1:cAbP2FsxoGVNwtgNAmmn3y5G1TWAiVYRmg4yku3l google.golang.org/api v0.59.0/go.mod h1:sT2boj7M9YJxZzgeZqXogmhfmRWDtPzT31xkieUbuZU= google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I= google.golang.org/api v0.62.0/go.mod h1:dKmwPCydfsad4qCH08MSdgWjfHOyfpd4VtDGgRFdavw= -google.golang.org/api v0.63.0 h1:n2bqqK895ygnBpdPDYetfy23K7fJ22wsrZKCyfuRkkA= google.golang.org/api v0.63.0/go.mod h1:gs4ij2ffTRXwuzzgJl/56BdwJaA194ijkfn++9tDuPo= +google.golang.org/api v0.64.0 h1:l3pi8ncrQgB9+ncFw3A716L8lWujnXniBYbxWqqy6tE= +google.golang.org/api v0.64.0/go.mod h1:931CdxA8Rm4t6zqTFGSsgwbAEZ2+GMYurbndwSimebM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -1231,8 +1225,9 @@ google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9/go.mod h1:5CzLGKJ6 google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/genproto v0.0.0-20211221195035-429b39de9b1c/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= -google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb h1:ZrsicilzPCS/Xr8qtBZZLpy4P9TYXAfl49ctG1/5tgw= google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= +google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 h1:Et6SkiuvnBn+SgrSYXs/BrUpGB4mbdwt4R3vaPIlicA= +google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=