| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2018 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2018-10-09 06:47:13 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2020-07-18 08:41:29 +08:00
										 |  |  | 	"unicode/utf8" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:31:43 +08:00
										 |  |  | 	jsoniter "github.com/json-iterator/go" | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/config" | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/madmin" | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	minioConfigPrefix = "config" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 	kvPrefix = ".kv" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) ( | 
					
						
							|  |  |  | 	[]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 { | 
					
						
							|  |  |  | 					return nil, err | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-07-18 08:41:29 +08:00
										 |  |  | 				if globalConfigEncrypted && !utf8.Valid(data) { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:18:26 +08:00
										 |  |  | 					data, err = madmin.DecryptData(globalActiveCred.String(), bytes.NewReader(data)) | 
					
						
							|  |  |  | 					if err != nil { | 
					
						
							|  |  |  | 						return nil, err | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				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) | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	_, err := objAPI.DeleteObject(ctx, minioMetaBucket, historyFile, ObjectOptions{}) | 
					
						
							|  |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-18 08:41:29 +08:00
										 |  |  | 	if globalConfigEncrypted && !utf8.Valid(data) { | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 		data, err = madmin.DecryptData(globalActiveCred.String(), bytes.NewReader(data)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 15:04:39 +08:00
										 |  |  | 	return data, err | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if globalConfigEncrypted { | 
					
						
							|  |  |  | 		kv, err = madmin.EncryptData(globalActiveCred.String(), kv) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	// Save the new config KV settings into the history path.
 | 
					
						
							|  |  |  | 	return saveConfig(ctx, objAPI, historyFile, kv) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 20:24:51 +08:00
										 |  |  | func saveServerConfig(ctx context.Context, objAPI ObjectLayer, config interface{}) error { | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	data, err := json.Marshal(config) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 	if globalConfigEncrypted { | 
					
						
							|  |  |  | 		data, err = madmin.EncryptData(globalActiveCred.String(), data) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 20:24:51 +08:00
										 |  |  | 	configFile := path.Join(minioConfigPrefix, minioConfigFile) | 
					
						
							| 
									
										
										
										
											2018-09-06 23:03:18 +08:00
										 |  |  | 	// Save the new config in the std config path
 | 
					
						
							| 
									
										
										
										
											2018-10-10 05:00:01 +08:00
										 |  |  | 	return saveConfig(ctx, objAPI, configFile, data) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | func readServerConfig(ctx context.Context, objAPI ObjectLayer) (config.Config, error) { | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	configFile := path.Join(minioConfigPrefix, minioConfigFile) | 
					
						
							| 
									
										
										
										
											2019-06-14 15:29:22 +08:00
										 |  |  | 	configData, err := readConfig(ctx, objAPI, configFile) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-28 01:36:08 +08:00
										 |  |  | 		// Config not found for some reason, allow things to continue
 | 
					
						
							|  |  |  | 		// by initializing a new fresh config in safe mode.
 | 
					
						
							| 
									
										
										
										
											2020-06-30 04:07:26 +08:00
										 |  |  | 		if err == errConfigNotFound && newObjectLayerFn() == nil { | 
					
						
							| 
									
										
										
										
											2019-11-28 01:36:08 +08:00
										 |  |  | 			return newServerConfig(), nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-18 08:41:29 +08:00
										 |  |  | 	if globalConfigEncrypted && !utf8.Valid(configData) { | 
					
						
							| 
									
										
										
										
											2019-11-02 06:53:16 +08:00
										 |  |  | 		configData, err = madmin.DecryptData(globalActiveCred.String(), bytes.NewReader(configData)) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if err == madmin.ErrMaliciousData { | 
					
						
							|  |  |  | 				return nil, config.ErrInvalidCredentialsBackendEncrypted(nil) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 08:38:39 +08:00
										 |  |  | 	var srvCfg = config.New() | 
					
						
							| 
									
										
										
										
											2020-01-08 19:31:43 +08:00
										 |  |  | 	var json = jsoniter.ConfigCompatibleWithStandardLibrary | 
					
						
							| 
									
										
										
										
											2020-05-24 08:38:39 +08:00
										 |  |  | 	if err = json.Unmarshal(configData, &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{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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 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
 | 
					
						
							| 
									
										
										
										
											2018-08-20 04:57:18 +08:00
										 |  |  | func initConfig(objAPI ObjectLayer) error { | 
					
						
							|  |  |  | 	if objAPI == nil { | 
					
						
							|  |  |  | 		return errServerNotInitialized | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 15:29:22 +08:00
										 |  |  | 	if isFile(getConfigFile()) { | 
					
						
							|  |  |  | 		if err := migrateConfig(); err != nil { | 
					
						
							| 
									
										
										
										
											2018-09-11 07:15:47 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-06-14 15:29:22 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-20 04:57:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 15:29:22 +08: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'
 | 
					
						
							|  |  |  | 	if err := migrateConfigToMinioSys(objAPI); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Migrates backend '<export_path>/.minio.sys/config/config.json' to latest version.
 | 
					
						
							|  |  |  | 	if err := migrateMinioSysConfig(objAPI); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-08-20 04:57:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-18 09:51:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	// Migrates backend '<export_path>/.minio.sys/config/config.json' to
 | 
					
						
							|  |  |  | 	// latest config format.
 | 
					
						
							|  |  |  | 	if err := migrateMinioSysConfigToKV(objAPI); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-20 04:57:18 +08:00
										 |  |  | 	return loadConfig(objAPI) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | } |