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	"context"
21	"net/http"
22	"net/url"
23)
24
25// DelConfigKV - delete key from server config.
26func (adm *AdminClient) DelConfigKV(ctx context.Context, k string) (restart bool, err error) {
27	econfigBytes, err := EncryptData(adm.getSecretKey(), []byte(k))
28	if err != nil {
29		return false, err
30	}
31
32	reqData := requestData{
33		relPath: adminAPIPrefix + "/del-config-kv",
34		content: econfigBytes,
35	}
36
37	// Execute DELETE on /minio/admin/v3/del-config-kv to delete config key.
38	resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
39
40	defer closeResponse(resp)
41	if err != nil {
42		return false, err
43	}
44
45	if resp.StatusCode != http.StatusOK {
46		return false, httpRespToErrorResponse(resp)
47	}
48
49	return resp.Header.Get(ConfigAppliedHeader) != ConfigAppliedTrue, nil
50}
51
52const (
53	// ConfigAppliedHeader is the header indicating whether the config was applied without requiring a restart.
54	ConfigAppliedHeader = "x-minio-config-applied"
55
56	// ConfigAppliedTrue is the value set in header if the config was applied.
57	ConfigAppliedTrue = "true"
58)
59
60// SetConfigKV - set key value config to server.
61func (adm *AdminClient) SetConfigKV(ctx context.Context, kv string) (restart bool, err error) {
62	econfigBytes, err := EncryptData(adm.getSecretKey(), []byte(kv))
63	if err != nil {
64		return false, err
65	}
66
67	reqData := requestData{
68		relPath: adminAPIPrefix + "/set-config-kv",
69		content: econfigBytes,
70	}
71
72	// Execute PUT on /minio/admin/v3/set-config-kv to set config key/value.
73	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
74
75	defer closeResponse(resp)
76	if err != nil {
77		return false, err
78	}
79
80	if resp.StatusCode != http.StatusOK {
81		return false, httpRespToErrorResponse(resp)
82	}
83
84	return resp.Header.Get(ConfigAppliedHeader) != ConfigAppliedTrue, nil
85}
86
87// GetConfigKV - returns the key, value of the requested key, incoming data is encrypted.
88func (adm *AdminClient) GetConfigKV(ctx context.Context, key string) ([]byte, error) {
89	v := url.Values{}
90	v.Set("key", key)
91
92	// Execute GET on /minio/admin/v3/get-config-kv?key={key} to get value of key.
93	resp, err := adm.executeMethod(ctx,
94		http.MethodGet,
95		requestData{
96			relPath:     adminAPIPrefix + "/get-config-kv",
97			queryValues: v,
98		})
99	defer closeResponse(resp)
100	if err != nil {
101		return nil, err
102	}
103
104	defer closeResponse(resp)
105
106	if resp.StatusCode != http.StatusOK {
107		return nil, httpRespToErrorResponse(resp)
108	}
109
110	return DecryptData(adm.getSecretKey(), resp.Body)
111}
112