2017-02-20 21:58:50 +01:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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 06:41:47 +02:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2017-02-27 20:40:27 +01:00
|
|
|
"encoding/json"
|
2018-07-03 20:07:46 +02:00
|
|
|
"errors"
|
2018-01-22 23:54:55 +01:00
|
|
|
"fmt"
|
2017-02-27 20:40:27 +01:00
|
|
|
"io"
|
2017-02-20 21:58:50 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-07-03 20:07:46 +02:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/quick"
|
2018-08-15 06:41:47 +02:00
|
|
|
"github.com/minio/sio"
|
|
|
|
"golang.org/x/crypto/argon2"
|
2017-02-27 20:40:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NodeSummary - represents the result of an operation part of
|
|
|
|
// set-config on a node.
|
|
|
|
type NodeSummary struct {
|
2017-03-03 11:53:48 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
ErrSet bool `json:"errSet"`
|
|
|
|
ErrMsg string `json:"errMsg"`
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfigResult - represents detailed results of a set-config
|
|
|
|
// operation.
|
|
|
|
type SetConfigResult struct {
|
|
|
|
NodeResults []NodeSummary `json:"nodeResults"`
|
|
|
|
Status bool `json:"status"`
|
|
|
|
}
|
|
|
|
|
2018-08-15 06:41:47 +02:00
|
|
|
// EncryptServerConfigData - encrypts server config data.
|
|
|
|
func EncryptServerConfigData(password string, data []byte) ([]byte, error) {
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive an encryption key from the master key and the nonce
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32))
|
|
|
|
|
|
|
|
encrypted, err := sio.EncryptReader(bytes.NewReader(data), sio.Config{
|
|
|
|
Key: key[:]},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
edata, err := ioutil.ReadAll(encrypted)
|
|
|
|
return append(salt, edata...), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptServerConfigData - decrypts server config data.
|
|
|
|
func DecryptServerConfigData(password string, data io.Reader) ([]byte, error) {
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(data, salt); err != nil {
|
|
|
|
return nil, err
|
2017-02-20 21:58:50 +01:00
|
|
|
}
|
2018-08-15 06:41:47 +02:00
|
|
|
// derive an encryption key from the master key and the nonce
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32))
|
2017-02-20 21:58:50 +01:00
|
|
|
|
2018-08-15 06:41:47 +02:00
|
|
|
decrypted, err := sio.DecryptReader(data, sio.Config{
|
|
|
|
Key: key[:]},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutil.ReadAll(decrypted)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig - returns the config.json of a minio setup, incoming data is encrypted.
|
|
|
|
func (adm *AdminClient) GetConfig() ([]byte, error) {
|
2018-01-22 23:54:55 +01:00
|
|
|
// Execute GET on /minio/admin/v1/config to get config of a setup.
|
|
|
|
resp, err := adm.executeMethod("GET",
|
|
|
|
requestData{relPath: "/v1/config"})
|
2017-02-20 21:58:50 +01:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:40:27 +01:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, httpRespToErrorResponse(resp)
|
|
|
|
}
|
2018-08-15 06:41:47 +02:00
|
|
|
defer resp.Body.Close()
|
2017-02-27 20:40:27 +01:00
|
|
|
|
2018-08-15 06:41:47 +02:00
|
|
|
return DecryptServerConfigData(adm.secretAccessKey, resp.Body)
|
2017-02-20 21:58:50 +01:00
|
|
|
}
|
2017-02-27 20:40:27 +01:00
|
|
|
|
|
|
|
// SetConfig - set config supplied as config.json for the setup.
|
2018-01-22 23:54:55 +01:00
|
|
|
func (adm *AdminClient) SetConfig(config io.Reader) (r SetConfigResult, err error) {
|
2018-07-03 20:07:46 +02:00
|
|
|
const maxConfigJSONSize = 256 * 1024 // 256KiB
|
|
|
|
|
|
|
|
// Read configuration bytes
|
|
|
|
configBuf := make([]byte, maxConfigJSONSize+1)
|
|
|
|
n, err := io.ReadFull(config, configBuf)
|
|
|
|
if err == nil {
|
|
|
|
return r, fmt.Errorf("too large file")
|
|
|
|
}
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
2018-01-22 23:54:55 +01:00
|
|
|
return r, err
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
2018-07-03 20:07:46 +02: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 {
|
|
|
|
return r, errors.New("Invalid JSON format: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the provided json file has "version" key set
|
|
|
|
if cfg.Version == "" {
|
|
|
|
return r, errors.New("Missing or unset \"version\" key in json file")
|
|
|
|
}
|
|
|
|
// Validate there are no duplicate keys in the JSON
|
|
|
|
if err = quick.CheckDuplicateKeys(string(configBytes)); err != nil {
|
|
|
|
return r, errors.New("Duplicate key in json file: " + err.Error())
|
|
|
|
}
|
2017-02-27 20:40:27 +01:00
|
|
|
|
2018-08-15 06:41:47 +02:00
|
|
|
econfigBytes, err := EncryptServerConfigData(adm.secretAccessKey, configBytes)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:40:27 +01:00
|
|
|
reqData := requestData{
|
2018-05-30 23:49:03 +02:00
|
|
|
relPath: "/v1/config",
|
2018-08-15 06:41:47 +02:00
|
|
|
content: econfigBytes,
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 23:54:55 +01:00
|
|
|
// Execute PUT on /minio/admin/v1/config to set config.
|
2017-02-27 20:40:27 +01:00
|
|
|
resp, err := adm.executeMethod("PUT", reqData)
|
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2018-01-22 23:54:55 +01:00
|
|
|
return r, err
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2018-01-22 23:54:55 +01:00
|
|
|
return r, httpRespToErrorResponse(resp)
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2018-01-22 23:54:55 +01:00
|
|
|
return r, err
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 23:54:55 +01:00
|
|
|
err = json.Unmarshal(jsonBytes, &r)
|
|
|
|
return r, err
|
2017-02-27 20:40:27 +01:00
|
|
|
}
|