github.com/minio/madmin-go@v1.7.5/config-kv-commands.go (about)

     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  
    17  package madmin
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  	"net/url"
    23  )
    24  
    25  // DelConfigKV - delete key from server config.
    26  func (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  
    52  const (
    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.
    61  func (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.
    88  func (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  	if resp.StatusCode != http.StatusOK {
   105  		return nil, httpRespToErrorResponse(resp)
   106  	}
   107  
   108  	return DecryptData(adm.getSecretKey(), resp.Body)
   109  }
   110  
   111  // KVOptions takes specific inputs for KV functions
   112  type KVOptions struct {
   113  	Env bool
   114  }
   115  
   116  // GetConfigKVWithOptions - returns the key, value of the requested key, incoming data is encrypted.
   117  func (adm *AdminClient) GetConfigKVWithOptions(ctx context.Context, key string, opts KVOptions) ([]byte, error) {
   118  	v := url.Values{}
   119  	v.Set("key", key)
   120  	if opts.Env {
   121  		v.Set("env", "")
   122  	}
   123  
   124  	// Execute GET on /minio/admin/v3/get-config-kv?key={key} to get value of key.
   125  	resp, err := adm.executeMethod(ctx,
   126  		http.MethodGet,
   127  		requestData{
   128  			relPath:     adminAPIPrefix + "/get-config-kv",
   129  			queryValues: v,
   130  		})
   131  	defer closeResponse(resp)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	defer closeResponse(resp)
   137  
   138  	if resp.StatusCode != http.StatusOK {
   139  		return nil, httpRespToErrorResponse(resp)
   140  	}
   141  
   142  	return DecryptData(adm.getSecretKey(), resp.Body)
   143  }