1//
2// MinIO Object Storage (c) 2021 MinIO, Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package madmin
18
19import (
20	"bytes"
21	"context"
22	"io"
23	"net/http"
24)
25
26// GetConfig - returns the config.json of a minio setup, incoming data is encrypted.
27func (adm *AdminClient) GetConfig(ctx context.Context) ([]byte, error) {
28	// Execute GET on /minio/admin/v3/config to get config of a setup.
29	resp, err := adm.executeMethod(ctx,
30		http.MethodGet,
31		requestData{relPath: adminAPIPrefix + "/config"})
32	defer closeResponse(resp)
33	if err != nil {
34		return nil, err
35	}
36
37	if resp.StatusCode != http.StatusOK {
38		return nil, httpRespToErrorResponse(resp)
39	}
40
41	return DecryptData(adm.getSecretKey(), resp.Body)
42}
43
44// SetConfig - set config supplied as config.json for the setup.
45func (adm *AdminClient) SetConfig(ctx context.Context, config io.Reader) (err error) {
46	const maxConfigJSONSize = 256 * 1024 // 256KiB
47
48	// Read configuration bytes
49	configBuf := make([]byte, maxConfigJSONSize+1)
50	n, err := io.ReadFull(config, configBuf)
51	if err == nil {
52		return bytes.ErrTooLarge
53	}
54	if err != io.ErrUnexpectedEOF {
55		return err
56	}
57	configBytes := configBuf[:n]
58	econfigBytes, err := EncryptData(adm.getSecretKey(), configBytes)
59	if err != nil {
60		return err
61	}
62
63	reqData := requestData{
64		relPath: adminAPIPrefix + "/config",
65		content: econfigBytes,
66	}
67
68	// Execute PUT on /minio/admin/v3/config to set config.
69	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
70
71	defer closeResponse(resp)
72	if err != nil {
73		return err
74	}
75
76	if resp.StatusCode != http.StatusOK {
77		return httpRespToErrorResponse(resp)
78	}
79
80	return nil
81}
82