Ensure that setConfig uses latest functionality (#6302)

This commit is contained in:
Harshavardhana
2018-08-17 18:51:34 -07:00
committed by kannappanr
parent 50a817e3d3
commit 9f14433cbd
13 changed files with 150 additions and 905 deletions

View File

@@ -32,21 +32,6 @@ import (
"golang.org/x/crypto/argon2"
)
// NodeSummary - represents the result of an operation part of
// set-config on a node.
type NodeSummary struct {
Name string `json:"name"`
ErrSet bool `json:"errSet"`
ErrMsg string `json:"errMsg"`
}
// SetConfigResult - represents detailed results of a set-config
// operation.
type SetConfigResult struct {
NodeResults []NodeSummary `json:"nodeResults"`
Status bool `json:"status"`
}
// EncryptServerConfigData - encrypts server config data.
func EncryptServerConfigData(password string, data []byte) ([]byte, error) {
salt := make([]byte, 32)
@@ -106,17 +91,17 @@ func (adm *AdminClient) GetConfig() ([]byte, error) {
}
// SetConfig - set config supplied as config.json for the setup.
func (adm *AdminClient) SetConfig(config io.Reader) (r SetConfigResult, err error) {
func (adm *AdminClient) SetConfig(config io.Reader) (err error) {
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")
return fmt.Errorf("too large file")
}
if err != io.ErrUnexpectedEOF {
return r, err
return err
}
configBytes := configBuf[:n]
@@ -127,21 +112,21 @@ func (adm *AdminClient) SetConfig(config io.Reader) (r SetConfigResult, err erro
// 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())
return 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")
return 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())
return errors.New("Duplicate key in json file: " + err.Error())
}
econfigBytes, err := EncryptServerConfigData(adm.secretAccessKey, configBytes)
if err != nil {
return r, err
return err
}
reqData := requestData{
@@ -154,18 +139,12 @@ func (adm *AdminClient) SetConfig(config io.Reader) (r SetConfigResult, err erro
defer closeResponse(resp)
if err != nil {
return r, err
return err
}
if resp.StatusCode != http.StatusOK {
return r, httpRespToErrorResponse(resp)
return httpRespToErrorResponse(resp)
}
jsonBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
err = json.Unmarshal(jsonBytes, &r)
return r, err
return nil
}

View File

@@ -19,7 +19,6 @@ package madmin
import (
"encoding/json"
"fmt"
"net/http"
)
@@ -38,15 +37,15 @@ func (adm *AdminClient) SetCredentials(access, secret string) error {
return err
}
// No TLS?
if !adm.secure {
return fmt.Errorf("credentials cannot be updated over an insecure connection")
ebody, err := EncryptServerConfigData(adm.secretAccessKey, body)
if err != nil {
return err
}
// Setup new request
reqData := requestData{
relPath: "/v1/config/credential",
content: body,
content: ebody,
}
// Execute GET on bucket to list objects.
@@ -62,5 +61,6 @@ func (adm *AdminClient) SetCredentials(access, secret string) error {
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
}
return nil
}