2021-04-18 15:41:13 -04: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 12:44:57 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-10-14 06:08:40 -04:00
|
|
|
"context"
|
2017-05-06 13:16:59 -04:00
|
|
|
"fmt"
|
2016-09-21 12:44:57 -04:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if config v1 is purged
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateV1(t *testing.T) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
objLayer, fsDir, err := prepareFS(ctx)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
err = newTestConfig(globalMinioDefaultRegion, objLayer)
|
2016-09-21 12:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2022-07-25 15:37:26 -04:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 13:05:16 -05:00
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2016-09-21 12:44:57 -04:00
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalObjectAPI = objLayer
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
// Create a V1 config json file and store it
|
|
|
|
configJSON := "{ \"version\":\"1\", \"accessKeyId\":\"abcde\", \"secretAccessKey\":\"abcdefgh\"}"
|
|
|
|
configPath := rootPath + "/fsUsers.json"
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte(configJSON), 0o644); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
// Check if config v1 is removed from filesystem
|
2020-11-23 11:36:49 -05:00
|
|
|
if _, err := os.Stat(configPath); err == nil || !osIsNotExist(err) {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatal("Config V1 file is not purged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize server config and check again if everything is fine
|
2022-12-19 14:10:14 -05:00
|
|
|
if err := loadConfig(objLayer, nil); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if all migrate code returns nil when config file does not
|
|
|
|
// exist
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateInexistentConfig(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
globalConfigDir = &ConfigDir{path: t.TempDir()}
|
2016-09-21 12:44:57 -04: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 02:42:10 -04:00
|
|
|
if err := migrateV7ToV8(); err != nil {
|
|
|
|
t.Fatal("migrate v7 to v8 should succeed when no config file is found")
|
|
|
|
}
|
2016-10-03 20:29:55 -04:00
|
|
|
if err := migrateV8ToV9(); err != nil {
|
|
|
|
t.Fatal("migrate v8 to v9 should succeed when no config file is found")
|
|
|
|
}
|
2016-11-23 18:00:53 -05:00
|
|
|
if err := migrateV9ToV10(); err != nil {
|
|
|
|
t.Fatal("migrate v9 to v10 should succeed when no config file is found")
|
|
|
|
}
|
2016-12-15 11:23:48 -05:00
|
|
|
if err := migrateV10ToV11(); err != nil {
|
|
|
|
t.Fatal("migrate v10 to v11 should succeed when no config file is found")
|
|
|
|
}
|
2017-01-11 19:41:05 -05:00
|
|
|
if err := migrateV11ToV12(); err != nil {
|
2017-01-09 17:22:10 -05: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-11 19:41:05 -05:00
|
|
|
}
|
2017-02-27 17:59:53 -05:00
|
|
|
if err := migrateV13ToV14(); err != nil {
|
|
|
|
t.Fatal("migrate v13 to v14 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-17 12:29:17 -04:00
|
|
|
if err := migrateV14ToV15(); err != nil {
|
|
|
|
t.Fatal("migrate v14 to v15 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-23 11:27:22 -04:00
|
|
|
if err := migrateV15ToV16(); err != nil {
|
|
|
|
t.Fatal("migrate v15 to v16 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-27 14:27:25 -04:00
|
|
|
if err := migrateV16ToV17(); err != nil {
|
|
|
|
t.Fatal("migrate v16 to v17 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-31 06:34:26 -04:00
|
|
|
if err := migrateV17ToV18(); err != nil {
|
|
|
|
t.Fatal("migrate v17 to v18 should succeed when no config file is found")
|
|
|
|
}
|
2017-06-14 20:27:03 -04:00
|
|
|
if err := migrateV18ToV19(); err != nil {
|
|
|
|
t.Fatal("migrate v18 to v19 should succeed when no config file is found")
|
|
|
|
}
|
2017-12-06 02:18:29 -05: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 17:14:06 -04:00
|
|
|
if err := migrateV21ToV22(); err != nil {
|
|
|
|
t.Fatal("migrate v21 to v22 should succeed when no config file is found")
|
|
|
|
}
|
2018-07-19 18:55:06 -04: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 15:52:14 -04:00
|
|
|
if err := migrateV27ToV28(); err != nil {
|
|
|
|
t.Fatal("migrate v27 to v28 should succeed when no config file is found")
|
|
|
|
}
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
|
|
|
|
2018-11-30 00:16:17 -05:00
|
|
|
// Test if a config migration from v2 to v33 is successfully done
|
|
|
|
func TestServerConfigMigrateV2toV33(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
rootPath := t.TempDir()
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2019-01-02 13:05:16 -05:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2022-10-14 06:08:40 -04:00
|
|
|
objLayer, fsDir, err := prepareFS(ctx)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
|
2019-08-06 15:08:58 -04:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\","), 0o644); err != nil {
|
2016-09-21 12:44:57 -04: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 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte(configJSON), 0o644); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:59:03 -05:00
|
|
|
if err := migrateConfigToMinioSys(objLayer); err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:56:32 -04:00
|
|
|
if err := migrateMinioSysConfig(objLayer); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if err := migrateMinioSysConfigToKV(objLayer); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
// Initialize server config and check again if everything is fine
|
2022-12-19 14:10:14 -05:00
|
|
|
if err := loadConfig(objLayer, nil); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if all migrate code returns error with corrupted config files
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateFaultyConfig(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 13:05:16 -05:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2019-08-06 15:08:58 -04:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\", \"test\":"), 0o644); err != nil {
|
2016-09-21 12:44:57 -04: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 02:42:10 -04:00
|
|
|
if err := migrateV7ToV8(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV7ToV8() should fail with a corrupted json")
|
|
|
|
}
|
2016-10-03 20:29:55 -04:00
|
|
|
if err := migrateV8ToV9(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV8ToV9() should fail with a corrupted json")
|
|
|
|
}
|
2016-11-23 18:00:53 -05:00
|
|
|
if err := migrateV9ToV10(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV9ToV10() should fail with a corrupted json")
|
|
|
|
}
|
2016-12-15 11:23:48 -05:00
|
|
|
if err := migrateV10ToV11(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV10ToV11() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-11 19:41:05 -05:00
|
|
|
if err := migrateV11ToV12(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV11ToV12() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-09 17:22:10 -05:00
|
|
|
if err := migrateV12ToV13(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV12ToV13() should fail with a corrupted json")
|
|
|
|
}
|
2017-02-27 17:59:53 -05:00
|
|
|
if err := migrateV13ToV14(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV13ToV14() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-17 12:29:17 -04:00
|
|
|
if err := migrateV14ToV15(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV14ToV15() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-23 11:27:22 -04:00
|
|
|
if err := migrateV15ToV16(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV15ToV16() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-27 14:27:25 -04:00
|
|
|
if err := migrateV16ToV17(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV16ToV17() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-31 06:34:26 -04:00
|
|
|
if err := migrateV17ToV18(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV17ToV18() should fail with a corrupted json")
|
|
|
|
}
|
2017-06-14 20:27:03 -04:00
|
|
|
if err := migrateV18ToV19(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV18ToV19() should fail with a corrupted json")
|
|
|
|
}
|
2017-12-06 02:18:29 -05: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 17:14:06 -04: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-19 18:55:06 -04: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 15:52:14 -04:00
|
|
|
if err := migrateV27ToV28(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV27ToV28() should fail with a corrupted json")
|
|
|
|
}
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
// Test if all migrate code returns error with corrupted config files
|
|
|
|
func TestServerConfigMigrateCorruptedConfig(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
rootPath := t.TempDir()
|
2019-01-02 13:05:16 -05:00
|
|
|
|
|
|
|
globalConfigDir = &ConfigDir{path: rootPath}
|
2019-08-06 15:08:58 -04:00
|
|
|
configPath := rootPath + SlashSeparator + minioConfigFile
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
for i := 3; i <= 17; i++ {
|
|
|
|
// Create a corrupted config file
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte(fmt.Sprintf("{ \"version\":\"%d\", \"credential\": { \"accessKey\": 1 } }", i)),
|
2022-01-02 12:15:06 -05:00
|
|
|
0o644); err != nil {
|
2017-05-06 13:16:59 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
2022-07-25 15:37:26 -04:00
|
|
|
if err := migrateConfig(); err == nil {
|
2017-05-06 13:16:59 -04:00
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a corrupted config file for version '2'.
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(configPath, []byte("{ \"version\":\"2\", \"credentials\": { \"accessKeyId\": 1 } }"), 0o644); err != nil {
|
2017-05-06 13:16:59 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
2022-07-25 15:37:26 -04:00
|
|
|
if err := migrateConfig(); err == nil {
|
2017-05-06 13:16:59 -04:00
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|