| 
									
										
										
										
											2021-04-19 03:41:13 +08: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 12:41:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2021-06-25 07:24:12 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-11-16 01:46:55 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2018-10-09 06:47:13 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:31:43 +08:00
										 |  |  | 	jsoniter "github.com/json-iterator/go" | 
					
						
							| 
									
										
										
										
											2023-06-20 08:53:08 +08:00
										 |  |  | 	"github.com/minio/madmin-go/v3" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/config" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/kms" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	minioConfigPrefix = "config" | 
					
						
							| 
									
										
										
										
											2022-01-25 03:28:45 +08:00
										 |  |  | 	minioConfigBucket = minioMetaBucket + SlashSeparator + minioConfigPrefix | 
					
						
							|  |  |  | 	kvPrefix          = ".kv" | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	// Captures all the previous SetKV operations and allows rollback.
 | 
					
						
							|  |  |  | 	minioConfigHistoryPrefix = minioConfigPrefix + "/history" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 	// MinIO configuration file.
 | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	minioConfigFile = "config.json" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | func listServerConfigHistory(ctx context.Context, objAPI ObjectLayer, withData bool, count int) ( | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	[]madmin.ConfigHistoryEntry, error, | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	var configHistory []madmin.ConfigHistoryEntry | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// List all kvs
 | 
					
						
							|  |  |  | 	marker := "" | 
					
						
							|  |  |  | 	for { | 
					
						
							| 
									
										
										
										
											2019-10-31 04:20:01 +08:00
										 |  |  | 		res, err := objAPI.ListObjects(ctx, minioMetaBucket, minioConfigHistoryPrefix, marker, "", maxObjectList) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for _, obj := range res.Objects { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 			cfgEntry := madmin.ConfigHistoryEntry{ | 
					
						
							|  |  |  | 				RestoreID:  strings.TrimSuffix(path.Base(obj.Name), kvPrefix), | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 				CreateTime: obj.ModTime, // ModTime is createTime for config history entries.
 | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if withData { | 
					
						
							|  |  |  | 				data, err := readConfig(ctx, objAPI, obj.Name) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 					// ignore history file if not readable.
 | 
					
						
							|  |  |  | 					continue | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				data, err = decryptData(data, obj.Name) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					// ignore history file that cannot be loaded.
 | 
					
						
							|  |  |  | 					continue | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 				cfgEntry.Data = string(data) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			configHistory = append(configHistory, cfgEntry) | 
					
						
							|  |  |  | 			count-- | 
					
						
							|  |  |  | 			if count == 0 { | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if !res.IsTruncated { | 
					
						
							|  |  |  | 			// We are done here
 | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		marker = res.NextMarker | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	sort.Slice(configHistory, func(i, j int) bool { | 
					
						
							|  |  |  | 		return configHistory[i].CreateTime.Before(configHistory[j].CreateTime) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	return configHistory, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | func delServerConfigHistory(ctx context.Context, objAPI ObjectLayer, uuidKV string) error { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 	historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV+kvPrefix) | 
					
						
							| 
									
										
										
										
											2021-09-18 10:34:48 +08:00
										 |  |  | 	_, err := objAPI.DeleteObject(ctx, minioMetaBucket, historyFile, ObjectOptions{ | 
					
						
							| 
									
										
										
										
											2023-08-10 07:30:22 +08:00
										 |  |  | 		DeletePrefix:       true, | 
					
						
							|  |  |  | 		DeletePrefixObject: true, // use prefix delete on exact object (this is an optimization to avoid fan-out calls)
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:34:48 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func readServerConfigHistory(ctx context.Context, objAPI ObjectLayer, uuidKV string) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 	historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV+kvPrefix) | 
					
						
							| 
									
										
										
										
											2019-10-30 15:04:39 +08:00
										 |  |  | 	data, err := readConfig(ctx, objAPI, historyFile) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	return decryptData(data, historyFile) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func saveServerConfigHistory(ctx context.Context, objAPI ObjectLayer, kv []byte) error { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 	uuidKV := mustGetUUID() + kvPrefix | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	historyFile := pathJoin(minioConfigHistoryPrefix, uuidKV) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 23:45:30 +08:00
										 |  |  | 	if GlobalKMS != nil { | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 		kv, err = config.EncryptBytes(GlobalKMS, kv, kms.Context{ | 
					
						
							|  |  |  | 			minioMetaBucket: path.Join(minioMetaBucket, historyFile), | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	return saveConfig(ctx, objAPI, historyFile, kv) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-29 10:39:48 +08:00
										 |  |  | func saveServerConfig(ctx context.Context, objAPI ObjectLayer, cfg any) error { | 
					
						
							| 
									
										
										
										
											2021-04-22 23:45:30 +08:00
										 |  |  | 	data, err := json.Marshal(cfg) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	configFile := path.Join(minioConfigPrefix, minioConfigFile) | 
					
						
							| 
									
										
										
										
											2021-04-22 23:45:30 +08:00
										 |  |  | 	if GlobalKMS != nil { | 
					
						
							|  |  |  | 		data, err = config.EncryptBytes(GlobalKMS, data, kms.Context{ | 
					
						
							|  |  |  | 			minioMetaBucket: path.Join(minioMetaBucket, configFile), | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-10 05:00:01 +08:00
										 |  |  | 	return saveConfig(ctx, objAPI, configFile, data) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | // data is optional. If nil it will be loaded from backend.
 | 
					
						
							|  |  |  | func readServerConfig(ctx context.Context, objAPI ObjectLayer, data []byte) (config.Config, error) { | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	srvCfg := config.New() | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		configFile := path.Join(minioConfigPrefix, minioConfigFile) | 
					
						
							|  |  |  | 		data, err = readConfig(ctx, objAPI, configFile) | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 			if errors.Is(err, errConfigNotFound) { | 
					
						
							|  |  |  | 				lookupConfigs(srvCfg, objAPI) | 
					
						
							|  |  |  | 				return srvCfg, nil | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 		data, err = decryptData(data, configFile) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			lookupConfigs(srvCfg, objAPI) | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	json := jsoniter.ConfigCompatibleWithStandardLibrary | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 	if err := json.Unmarshal(data, &srvCfg); err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 08:38:39 +08:00
										 |  |  | 	// Add any missing entries
 | 
					
						
							|  |  |  | 	return srvCfg.Merge(), nil | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ConfigSys - config system.
 | 
					
						
							|  |  |  | type ConfigSys struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Init - initializes config system from config.json.
 | 
					
						
							|  |  |  | func (sys *ConfigSys) Init(objAPI ObjectLayer) error { | 
					
						
							|  |  |  | 	if objAPI == nil { | 
					
						
							|  |  |  | 		return errInvalidArgument | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-16 13:09:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 09:07:43 +08:00
										 |  |  | 	return initConfig(objAPI) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewConfigSys - creates new config system object.
 | 
					
						
							|  |  |  | func NewConfigSys() *ConfigSys { | 
					
						
							|  |  |  | 	return &ConfigSys{} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Initialize and load config from remote etcd or local config directory
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | func initConfig(objAPI ObjectLayer) (err error) { | 
					
						
							| 
									
										
										
										
											2023-08-23 18:07:06 +08:00
										 |  |  | 	bootstrapTraceMsg("load the configuration") | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-23 18:07:06 +08:00
										 |  |  | 			bootstrapTraceMsg(fmt.Sprintf("loading configuration failed: %v", err)) | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2022-12-22 07:52:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-20 04:57:18 +08:00
										 |  |  | 	if objAPI == nil { | 
					
						
							|  |  |  | 		return errServerNotInitialized | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	srvCfg, err := readConfigWithoutMigrate(GlobalContext, objAPI) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-12-20 03:10:14 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 18:07:06 +08:00
										 |  |  | 	bootstrapTraceMsg("lookup the configuration") | 
					
						
							| 
									
										
										
										
											2019-06-14 15:29:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	// Override any values from ENVs.
 | 
					
						
							|  |  |  | 	lookupConfigs(srvCfg, objAPI) | 
					
						
							| 
									
										
										
										
											2018-08-18 09:51:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	// hold the mutex lock before a new config is assigned.
 | 
					
						
							|  |  |  | 	globalServerConfigMu.Lock() | 
					
						
							|  |  |  | 	globalServerConfig = srvCfg | 
					
						
							|  |  |  | 	globalServerConfigMu.Unlock() | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 04:56:08 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } |