2021-04-18 12:41:13 -07:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-09-21 17:44:57 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-10-14 03:08:40 -07:00
|
|
|
"context"
|
2017-05-06 10:16:59 -07:00
|
|
|
"fmt"
|
2016-09-21 17:44:57 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-10-03 17:29:55 -07:00
|
|
|
// Test if config v1 is purged
|
2016-09-21 17:44:57 +01:00
|
|
|
func TestServerConfigMigrateV1(t *testing.T) {
|
2022-10-14 03:08:40 -07:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
objLayer, fsDir, err := prepareFS(ctx)
|
2018-08-14 21:41:47 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
err = newTestConfig(globalMinioDefaultRegion, objLayer)
|
2016-09-21 17:44:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2022-07-26 03:37:26 +08:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 10:05:16 -08:00
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2016-09-21 17:44:57 +01:00
|
|
|
|
2018-08-14 21:41:47 -07:00
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalObjectAPI = objLayer
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
2016-09-21 17:44:57 +01:00
|
|
|
// Create a V1 config json file and store it
|
|
|
|
configJSON := "{ \"version\":\"1\", \"accessKeyId\":\"abcde\", \"secretAccessKey\":\"abcdefgh\"}"
|
|
|
|
configPath := rootPath + "/fsUsers.json"
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte(configJSON), 0o644); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2018-08-14 21:41:47 -07:00
|
|
|
|
2016-09-21 17:44:57 +01:00
|
|
|
// Check if config v1 is removed from filesystem
|
2020-11-23 08:36:49 -08:00
|
|
|
if _, err := os.Stat(configPath); err == nil || !osIsNotExist(err) {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatal("Config V1 file is not purged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize server config and check again if everything is fine
|
2022-12-19 20:10:14 +01:00
|
|
|
if err := loadConfig(objLayer, nil); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:29:55 -07:00
|
|
|
// Test if all migrate code returns nil when config file does not
|
|
|
|
// exist
|
2016-09-21 17:44:57 +01:00
|
|
|
func TestServerConfigMigrateInexistentConfig(t *testing.T) {
|
2022-07-26 03:37:26 +08:00
|
|
|
globalConfigDir = &ConfigDir{path: t.TempDir()}
|
2016-09-21 17:44:57 +01:00
|
|
|
|
|
|
|
if err := migrateV2ToV3(); err != nil {
|
|
|
|
t.Fatal("migrate v2 to v3 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV3ToV4(); err != nil {
|
|
|
|
t.Fatal("migrate v3 to v4 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV4ToV5(); err != nil {
|
|
|
|
t.Fatal("migrate v4 to v5 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV5ToV6(); err != nil {
|
|
|
|
t.Fatal("migrate v5 to v6 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV6ToV7(); err != nil {
|
|
|
|
t.Fatal("migrate v6 to v7 should succeed when no config file is found")
|
|
|
|
}
|
2016-09-30 07:42:10 +01:00
|
|
|
if err := migrateV7ToV8(); err != nil {
|
|
|
|
t.Fatal("migrate v7 to v8 should succeed when no config file is found")
|
|
|
|
}
|
2016-10-03 17:29:55 -07:00
|
|
|
if err := migrateV8ToV9(); err != nil {
|
|
|
|
t.Fatal("migrate v8 to v9 should succeed when no config file is found")
|
|
|
|
}
|
2016-11-24 00:00:53 +01:00
|
|
|
if err := migrateV9ToV10(); err != nil {
|
|
|
|
t.Fatal("migrate v9 to v10 should succeed when no config file is found")
|
|
|
|
}
|
2016-12-15 21:53:48 +05:30
|
|
|
if err := migrateV10ToV11(); err != nil {
|
|
|
|
t.Fatal("migrate v10 to v11 should succeed when no config file is found")
|
|
|
|
}
|
2017-01-12 01:41:05 +01:00
|
|
|
if err := migrateV11ToV12(); err != nil {
|
2017-01-09 22:22:10 +00:00
|
|
|
t.Fatal("migrate v11 to v12 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV12ToV13(); err != nil {
|
|
|
|
t.Fatal("migrate v12 to v13 should succeed when no config file is found")
|
2017-01-12 01:41:05 +01:00
|
|
|
}
|
2017-02-27 23:59:53 +01:00
|
|
|
if err := migrateV13ToV14(); err != nil {
|
|
|
|
t.Fatal("migrate v13 to v14 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-17 21:59:17 +05:30
|
|
|
if err := migrateV14ToV15(); err != nil {
|
|
|
|
t.Fatal("migrate v14 to v15 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-23 16:27:22 +01:00
|
|
|
if err := migrateV15ToV16(); err != nil {
|
|
|
|
t.Fatal("migrate v15 to v16 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-27 23:57:25 +05:30
|
|
|
if err := migrateV16ToV17(); err != nil {
|
|
|
|
t.Fatal("migrate v16 to v17 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-31 16:04:26 +05:30
|
|
|
if err := migrateV17ToV18(); err != nil {
|
|
|
|
t.Fatal("migrate v17 to v18 should succeed when no config file is found")
|
|
|
|
}
|
2017-06-15 01:27:03 +01:00
|
|
|
if err := migrateV18ToV19(); err != nil {
|
|
|
|
t.Fatal("migrate v18 to v19 should succeed when no config file is found")
|
|
|
|
}
|
2017-12-05 23:18:29 -08:00
|
|
|
if err := migrateV19ToV20(); err != nil {
|
|
|
|
t.Fatal("migrate v19 to v20 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV20ToV21(); err != nil {
|
|
|
|
t.Fatal("migrate v20 to v21 should succeed when no config file is found")
|
|
|
|
}
|
2018-03-28 14:14:06 -07:00
|
|
|
if err := migrateV21ToV22(); err != nil {
|
|
|
|
t.Fatal("migrate v21 to v22 should succeed when no config file is found")
|
|
|
|
}
|
2018-07-20 00:55:06 +02:00
|
|
|
if err := migrateV22ToV23(); err != nil {
|
|
|
|
t.Fatal("migrate v22 to v23 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV23ToV24(); err != nil {
|
|
|
|
t.Fatal("migrate v23 to v24 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV24ToV25(); err != nil {
|
|
|
|
t.Fatal("migrate v24 to v25 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV25ToV26(); err != nil {
|
|
|
|
t.Fatal("migrate v25 to v26 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV26ToV27(); err != nil {
|
|
|
|
t.Fatal("migrate v26 to v27 should succeed when no config file is found")
|
|
|
|
}
|
2018-08-17 12:52:14 -07:00
|
|
|
if err := migrateV27ToV28(); err != nil {
|
|
|
|
t.Fatal("migrate v27 to v28 should succeed when no config file is found")
|
|
|
|
}
|
2016-09-21 17:44:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 10:46:17 +05:30
|
|
|
// Test if a config migration from v2 to v33 is successfully done
|
|
|
|
func TestServerConfigMigrateV2toV33(t *testing.T) {
|
2022-07-26 03:37:26 +08:00
|
|
|
rootPath := t.TempDir()
|
2022-10-14 03:08:40 -07:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2019-01-02 10:05:16 -08:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2018-08-14 21:41:47 -07:00
|
|
|
|
2022-10-14 03:08:40 -07:00
|
|
|
objLayer, fsDir, err := prepareFS(ctx)
|
2018-08-14 21:41:47 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
|
2019-08-06 12:08:58 -07:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2016-09-21 17:44:57 +01:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\","), 0o644); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err == nil {
|
|
|
|
t.Fatal("migration should fail with corrupted config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := "accessfoo"
|
|
|
|
secretKey := "secretfoo"
|
|
|
|
|
|
|
|
// Create a V2 config json file and store it
|
|
|
|
configJSON := "{ \"version\":\"2\", \"credentials\": {\"accessKeyId\":\"" + accessKey + "\", \"secretAccessKey\":\"" + secretKey + "\", \"region\":\"us-east-1\"}, \"mongoLogger\":{\"addr\":\"127.0.0.1:3543\", \"db\":\"foodb\", \"collection\":\"foo\"}, \"syslogLogger\":{\"network\":\"127.0.0.1:543\", \"addr\":\"addr\"}, \"fileLogger\":{\"filename\":\"log.out\"}}"
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte(configJSON), 0o644); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2017-05-06 10:16:59 -07:00
|
|
|
|
2016-09-21 17:44:57 +01:00
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:59:03 -08:00
|
|
|
if err := migrateConfigToMinioSys(objLayer); err != nil {
|
2018-08-14 21:41:47 -07:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2023-04-21 13:56:08 -07:00
|
|
|
srvCfg, err := readConfigWithoutMigrate(context.Background(), objLayer)
|
|
|
|
if err != nil {
|
2018-09-20 14:56:32 -07:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2023-04-21 13:56:08 -07:00
|
|
|
if err = saveServerConfig(GlobalContext, objLayer, srvCfg); err != nil {
|
2019-10-22 22:59:13 -07:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:44:57 +01:00
|
|
|
// Initialize server config and check again if everything is fine
|
2022-12-19 20:10:14 +01:00
|
|
|
if err := loadConfig(objLayer, nil); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:29:55 -07:00
|
|
|
// Test if all migrate code returns error with corrupted config files
|
2016-09-21 17:44:57 +01:00
|
|
|
func TestServerConfigMigrateFaultyConfig(t *testing.T) {
|
2022-07-26 03:37:26 +08:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 10:05:16 -08:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2019-08-06 12:08:58 -07:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2016-09-21 17:44:57 +01:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\", \"test\":"), 0o644); err != nil {
|
2016-09-21 17:44:57 +01:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
|
|
|
if err := migrateV2ToV3(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV2ToV3() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV3ToV4(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV3ToV4() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV4ToV5(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV4ToV5() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV5ToV6(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV5ToV6() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV6ToV7(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV6ToV7() should fail with a corrupted json")
|
|
|
|
}
|
2016-09-30 07:42:10 +01:00
|
|
|
if err := migrateV7ToV8(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV7ToV8() should fail with a corrupted json")
|
|
|
|
}
|
2016-10-03 17:29:55 -07:00
|
|
|
if err := migrateV8ToV9(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV8ToV9() should fail with a corrupted json")
|
|
|
|
}
|
2016-11-24 00:00:53 +01:00
|
|
|
if err := migrateV9ToV10(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV9ToV10() should fail with a corrupted json")
|
|
|
|
}
|
2016-12-15 21:53:48 +05:30
|
|
|
if err := migrateV10ToV11(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV10ToV11() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-12 01:41:05 +01:00
|
|
|
if err := migrateV11ToV12(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV11ToV12() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-09 22:22:10 +00:00
|
|
|
if err := migrateV12ToV13(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV12ToV13() should fail with a corrupted json")
|
|
|
|
}
|
2017-02-27 23:59:53 +01:00
|
|
|
if err := migrateV13ToV14(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV13ToV14() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-17 21:59:17 +05:30
|
|
|
if err := migrateV14ToV15(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV14ToV15() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-23 16:27:22 +01:00
|
|
|
if err := migrateV15ToV16(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV15ToV16() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-27 23:57:25 +05:30
|
|
|
if err := migrateV16ToV17(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV16ToV17() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-31 16:04:26 +05:30
|
|
|
if err := migrateV17ToV18(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV17ToV18() should fail with a corrupted json")
|
|
|
|
}
|
2017-06-15 01:27:03 +01:00
|
|
|
if err := migrateV18ToV19(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV18ToV19() should fail with a corrupted json")
|
|
|
|
}
|
2017-12-05 23:18:29 -08:00
|
|
|
if err := migrateV19ToV20(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV19ToV20() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV20ToV21(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV20ToV21() should fail with a corrupted json")
|
|
|
|
}
|
2018-03-28 14:14:06 -07:00
|
|
|
if err := migrateV21ToV22(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV21ToV22() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV22ToV23(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV22ToV23() should fail with a corrupted json")
|
|
|
|
}
|
2018-07-20 00:55:06 +02:00
|
|
|
if err := migrateV23ToV24(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV23ToV24() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV24ToV25(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV24ToV25() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV25ToV26(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV25ToV26() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV26ToV27(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV26ToV27() should fail with a corrupted json")
|
|
|
|
}
|
2018-08-17 12:52:14 -07:00
|
|
|
if err := migrateV27ToV28(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV27ToV28() should fail with a corrupted json")
|
|
|
|
}
|
2016-09-21 17:44:57 +01:00
|
|
|
}
|
2017-05-06 10:16:59 -07:00
|
|
|
|
|
|
|
// Test if all migrate code returns error with corrupted config files
|
|
|
|
func TestServerConfigMigrateCorruptedConfig(t *testing.T) {
|
2022-07-26 03:37:26 +08:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 10:05:16 -08:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2019-08-06 12:08:58 -07:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2017-05-06 10:16:59 -07:00
|
|
|
|
|
|
|
for i := 3; i <= 17; i++ {
|
|
|
|
// Create a corrupted config file
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte(fmt.Sprintf("{ \"version\":\"%d\", \"credential\": { \"accessKey\": 1 } }", i)),
|
2022-01-02 09:15:06 -08:00
|
|
|
0o644); err != nil {
|
2017-05-06 10:16:59 -07:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
2022-07-26 03:37:26 +08:00
|
|
|
if err := migrateConfig(); err == nil {
|
2017-05-06 10:16:59 -07:00
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a corrupted config file for version '2'.
|
2022-09-19 20:05:16 +02:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\", \"credentials\": { \"accessKeyId\": 1 } }"), 0o644); err != nil {
|
2017-05-06 10:16:59 -07:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
2022-07-26 03:37:26 +08:00
|
|
|
if err := migrateConfig(); err == nil {
|
2017-05-06 10:16:59 -07:00
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|