2017-02-20 15:58:50 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2017 MinIO, Inc.
|
2017-02-20 15:58:50 -05: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 madmin
|
|
|
|
|
|
|
|
import (
|
2018-08-15 00:41:47 -04:00
|
|
|
"bytes"
|
2017-02-27 14:40:27 -05:00
|
|
|
"encoding/json"
|
2018-07-03 14:07:46 -04:00
|
|
|
"errors"
|
2017-02-27 14:40:27 -05:00
|
|
|
"io"
|
2017-02-20 15:58:50 -05:00
|
|
|
"net/http"
|
2018-07-03 14:07:46 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/quick"
|
2017-02-27 14:40:27 -05:00
|
|
|
)
|
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
// GetConfig - returns the config.json of a minio setup, incoming data is encrypted.
|
|
|
|
func (adm *AdminClient) GetConfig() ([]byte, error) {
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute GET on /minio/admin/v2/config to get config of a setup.
|
2018-01-22 17:54:55 -05:00
|
|
|
resp, err := adm.executeMethod("GET",
|
2019-10-23 00:01:14 -04:00
|
|
|
requestData{relPath: adminAPIPrefix + "/config"})
|
2017-02-20 15:58:50 -05:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-27 14:40:27 -05:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, httpRespToErrorResponse(resp)
|
|
|
|
}
|
2018-09-05 19:47:14 -04:00
|
|
|
defer closeResponse(resp)
|
2017-02-27 14:40:27 -05:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return DecryptData(adm.secretAccessKey, resp.Body)
|
2017-02-20 15:58:50 -05:00
|
|
|
}
|
2017-02-27 14:40:27 -05:00
|
|
|
|
|
|
|
// SetConfig - set config supplied as config.json for the setup.
|
2018-08-17 21:51:34 -04:00
|
|
|
func (adm *AdminClient) SetConfig(config io.Reader) (err error) {
|
2018-07-03 14:07:46 -04:00
|
|
|
const maxConfigJSONSize = 256 * 1024 // 256KiB
|
|
|
|
|
|
|
|
// Read configuration bytes
|
|
|
|
configBuf := make([]byte, maxConfigJSONSize+1)
|
|
|
|
n, err := io.ReadFull(config, configBuf)
|
|
|
|
if err == nil {
|
2018-10-09 17:00:01 -04:00
|
|
|
return bytes.ErrTooLarge
|
2018-07-03 14:07:46 -04:00
|
|
|
}
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
2018-08-17 21:51:34 -04:00
|
|
|
return err
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
2018-07-03 14:07:46 -04:00
|
|
|
configBytes := configBuf[:n]
|
|
|
|
|
|
|
|
type configVersion struct {
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
}
|
|
|
|
var cfg configVersion
|
|
|
|
|
|
|
|
// Check if read data is in json format
|
|
|
|
if err = json.Unmarshal(configBytes, &cfg); err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
return errors.New("Invalid JSON format: " + err.Error())
|
2018-07-03 14:07:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the provided json file has "version" key set
|
|
|
|
if cfg.Version == "" {
|
2018-08-17 21:51:34 -04:00
|
|
|
return errors.New("Missing or unset \"version\" key in json file")
|
2018-07-03 14:07:46 -04:00
|
|
|
}
|
|
|
|
// Validate there are no duplicate keys in the JSON
|
|
|
|
if err = quick.CheckDuplicateKeys(string(configBytes)); err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
return errors.New("Duplicate key in json file: " + err.Error())
|
2018-07-03 14:07:46 -04:00
|
|
|
}
|
2017-02-27 14:40:27 -05:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
econfigBytes, err := EncryptData(adm.secretAccessKey, configBytes)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
return err
|
2018-08-15 00:41:47 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 14:40:27 -05:00
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/config",
|
2018-08-15 00:41:47 -04:00
|
|
|
content: econfigBytes,
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Execute PUT on /minio/admin/v2/config to set config.
|
2017-02-27 14:40:27 -05:00
|
|
|
resp, err := adm.executeMethod("PUT", reqData)
|
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2018-08-17 21:51:34 -04:00
|
|
|
return err
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2018-08-17 21:51:34 -04:00
|
|
|
return httpRespToErrorResponse(resp)
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
2018-08-17 21:51:34 -04:00
|
|
|
return nil
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|