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/>.
|
2018-08-15 00:41:47 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"path"
|
2019-10-23 01:59:13 -04:00
|
|
|
"sort"
|
2018-10-08 18:47:13 -04:00
|
|
|
"strings"
|
2020-07-17 20:41:29 -04:00
|
|
|
"unicode/utf8"
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2020-01-08 06:31:43 -05:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2019-10-23 01:59:13 -04:00
|
|
|
"github.com/minio/minio/cmd/config"
|
2021-04-22 11:45:30 -04:00
|
|
|
"github.com/minio/minio/pkg/kms"
|
2019-10-23 01:59:13 -04:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2018-08-15 00:41:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minioConfigPrefix = "config"
|
|
|
|
|
2019-11-05 09:18:26 -05:00
|
|
|
kvPrefix = ".kv"
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Captures all the previous SetKV operations and allows rollback.
|
|
|
|
minioConfigHistoryPrefix = minioConfigPrefix + "/history"
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO configuration file.
|
2018-08-15 00:41:47 -04:00
|
|
|
minioConfigFile = "config.json"
|
|
|
|
)
|
|
|
|
|
2019-11-05 09:18:26 -05:00
|
|
|
func listServerConfigHistory(ctx context.Context, objAPI ObjectLayer, withData bool, count int) (
|
|
|
|
[]madmin.ConfigHistoryEntry, error) {
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
var configHistory []madmin.ConfigHistoryEntry
|
|
|
|
|
|
|
|
// List all kvs
|
|
|
|
marker := ""
|
|
|
|
for {
|
2019-10-30 16:20:01 -04:00
|
|
|
res, err := objAPI.ListObjects(ctx, minioMetaBucket, minioConfigHistoryPrefix, marker, "", maxObjectList)
|
2019-10-23 01:59:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, obj := range res.Objects {
|
2019-11-05 09:18:26 -05:00
|
|
|
cfgEntry := madmin.ConfigHistoryEntry{
|
|
|
|
RestoreID: strings.TrimSuffix(path.Base(obj.Name), kvPrefix),
|
2019-10-23 01:59:13 -04:00
|
|
|
CreateTime: obj.ModTime, // ModTime is createTime for config history entries.
|
2019-11-05 09:18:26 -05:00
|
|
|
}
|
|
|
|
if withData {
|
|
|
|
data, err := readConfig(ctx, objAPI, obj.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
if GlobalKMS != nil {
|
|
|
|
data, err = config.DecryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
obj.Bucket: path.Join(obj.Bucket, obj.Name),
|
|
|
|
})
|
2019-11-05 09:18:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfgEntry.Data = string(data)
|
|
|
|
}
|
|
|
|
configHistory = append(configHistory, cfgEntry)
|
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
if !res.IsTruncated {
|
|
|
|
// We are done here
|
|
|
|
break
|
|
|
|
}
|
|
|
|
marker = res.NextMarker
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
sort.Slice(configHistory, func(i, j int) bool {
|
|
|
|
return configHistory[i].CreateTime.Before(configHistory[j].CreateTime)
|
|
|
|
})
|
|
|
|
return configHistory, nil
|
|
|
|
}
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
func delServerConfigHistory(ctx context.Context, objAPI ObjectLayer, uuidKV string) error {
|
2019-11-05 09:18:26 -05:00
|
|
|
historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV+kvPrefix)
|
2020-06-12 23:04:01 -04:00
|
|
|
_, err := objAPI.DeleteObject(ctx, minioMetaBucket, historyFile, ObjectOptions{})
|
|
|
|
return err
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func readServerConfigHistory(ctx context.Context, objAPI ObjectLayer, uuidKV string) ([]byte, error) {
|
2019-11-05 09:18:26 -05:00
|
|
|
historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV+kvPrefix)
|
2019-10-30 03:04:39 -04:00
|
|
|
data, err := readConfig(ctx, objAPI, historyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-01 18:53:16 -04:00
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
if GlobalKMS != nil {
|
|
|
|
data, err = config.DecryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, historyFile),
|
|
|
|
})
|
2019-11-01 18:53:16 -04:00
|
|
|
}
|
2019-10-30 03:04:39 -04:00
|
|
|
return data, err
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveServerConfigHistory(ctx context.Context, objAPI ObjectLayer, kv []byte) error {
|
2019-11-05 09:18:26 -05:00
|
|
|
uuidKV := mustGetUUID() + kvPrefix
|
2019-10-23 01:59:13 -04:00
|
|
|
historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV)
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
if GlobalKMS != nil {
|
|
|
|
var err error
|
|
|
|
kv, err = config.EncryptBytes(GlobalKMS, kv, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, historyFile),
|
|
|
|
})
|
2019-11-01 18:53:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
return saveConfig(ctx, objAPI, historyFile, kv)
|
|
|
|
}
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
func saveServerConfig(ctx context.Context, objAPI ObjectLayer, cfg interface{}) error {
|
|
|
|
data, err := json.Marshal(cfg)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
var configFile = path.Join(minioConfigPrefix, minioConfigFile)
|
|
|
|
if GlobalKMS != nil {
|
|
|
|
data, err = config.EncryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, configFile),
|
|
|
|
})
|
2019-11-01 18:53:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
return saveConfig(ctx, objAPI, configFile, data)
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
func readServerConfig(ctx context.Context, objAPI ObjectLayer) (config.Config, error) {
|
2018-08-15 00:41:47 -04:00
|
|
|
configFile := path.Join(minioConfigPrefix, minioConfigFile)
|
2021-04-22 11:45:30 -04:00
|
|
|
data, err := readConfig(ctx, objAPI, configFile)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
2019-11-27 12:36:08 -05:00
|
|
|
// Config not found for some reason, allow things to continue
|
|
|
|
// by initializing a new fresh config in safe mode.
|
2020-06-29 16:07:26 -04:00
|
|
|
if err == errConfigNotFound && newObjectLayerFn() == nil {
|
2019-11-27 12:36:08 -05:00
|
|
|
return newServerConfig(), nil
|
|
|
|
}
|
2018-08-15 00:41:47 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
if GlobalKMS != nil && !utf8.Valid(data) {
|
|
|
|
data, err = config.DecryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, configFile),
|
|
|
|
})
|
2019-11-01 18:53:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:38:39 -04:00
|
|
|
var srvCfg = config.New()
|
2020-01-08 06:31:43 -05:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
2021-04-22 11:45:30 -04:00
|
|
|
if err = json.Unmarshal(data, &srvCfg); err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:38:39 -04:00
|
|
|
// Add any missing entries
|
|
|
|
return srvCfg.Merge(), nil
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigSys - config system.
|
|
|
|
type ConfigSys struct{}
|
|
|
|
|
|
|
|
// Load - load config.json.
|
|
|
|
func (sys *ConfigSys) Load(objAPI ObjectLayer) error {
|
|
|
|
return sys.Init(objAPI)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initializes config system from config.json.
|
|
|
|
func (sys *ConfigSys) Init(objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2018-09-16 01:09:51 -04:00
|
|
|
|
2020-02-01 20:07:43 -05:00
|
|
|
return initConfig(objAPI)
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfigSys - creates new config system object.
|
|
|
|
func NewConfigSys() *ConfigSys {
|
|
|
|
return &ConfigSys{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize and load config from remote etcd or local config directory
|
2018-08-19 16:57:18 -04:00
|
|
|
func initConfig(objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:29:22 -04:00
|
|
|
if isFile(getConfigFile()) {
|
|
|
|
if err := migrateConfig(); err != nil {
|
2018-09-10 19:15:47 -04:00
|
|
|
return err
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
2019-06-14 03:29:22 -04:00
|
|
|
}
|
2018-08-19 16:57:18 -04:00
|
|
|
|
2019-06-14 03:29:22 -04:00
|
|
|
// Migrates ${HOME}/.minio/config.json or config.json.deprecated
|
|
|
|
// to '<export_path>/.minio.sys/config/config.json'
|
|
|
|
// ignore if the file doesn't exist.
|
|
|
|
// If etcd is set then migrates /config/config.json
|
|
|
|
// to '<export_path>/.minio.sys/config/config.json'
|
2020-12-01 14:59:03 -05:00
|
|
|
if err := migrateConfigToMinioSys(objAPI); err != nil {
|
2019-06-14 03:29:22 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:59:03 -05:00
|
|
|
// Migrates backend '<export_path>/.minio.sys/config/config.json' to latest version.
|
|
|
|
if err := migrateMinioSysConfig(objAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-17 21:51:34 -04:00
|
|
|
|
2020-12-01 14:59:03 -05:00
|
|
|
// Migrates backend '<export_path>/.minio.sys/config/config.json' to
|
|
|
|
// latest config format.
|
|
|
|
if err := migrateMinioSysConfigToKV(objAPI); err != nil {
|
|
|
|
return err
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:59:03 -05:00
|
|
|
return loadConfig(objAPI)
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|