github.com/minio/madmin-go/v3@v3.0.51/config-kv-commands.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"context"
    24  	"net/http"
    25  	"net/url"
    26  )
    27  
    28  // DelConfigKV - delete key from server config.
    29  func (adm *AdminClient) DelConfigKV(ctx context.Context, k string) (restart bool, err error) {
    30  	econfigBytes, err := EncryptData(adm.getSecretKey(), []byte(k))
    31  	if err != nil {
    32  		return false, err
    33  	}
    34  
    35  	reqData := requestData{
    36  		relPath: adminAPIPrefix + "/del-config-kv",
    37  		content: econfigBytes,
    38  	}
    39  
    40  	// Execute DELETE on /minio/admin/v3/del-config-kv to delete config key.
    41  	resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
    42  
    43  	defer closeResponse(resp)
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  
    48  	if resp.StatusCode != http.StatusOK {
    49  		return false, httpRespToErrorResponse(resp)
    50  	}
    51  
    52  	return resp.Header.Get(ConfigAppliedHeader) != ConfigAppliedTrue, nil
    53  }
    54  
    55  const (
    56  	// ConfigAppliedHeader is the header indicating whether the config was applied without requiring a restart.
    57  	ConfigAppliedHeader = "x-minio-config-applied"
    58  
    59  	// ConfigAppliedTrue is the value set in header if the config was applied.
    60  	ConfigAppliedTrue = "true"
    61  )
    62  
    63  // SetConfigKV - set key value config to server.
    64  func (adm *AdminClient) SetConfigKV(ctx context.Context, kv string) (restart bool, err error) {
    65  	econfigBytes, err := EncryptData(adm.getSecretKey(), []byte(kv))
    66  	if err != nil {
    67  		return false, err
    68  	}
    69  
    70  	reqData := requestData{
    71  		relPath: adminAPIPrefix + "/set-config-kv",
    72  		content: econfigBytes,
    73  	}
    74  
    75  	// Execute PUT on /minio/admin/v3/set-config-kv to set config key/value.
    76  	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
    77  
    78  	defer closeResponse(resp)
    79  	if err != nil {
    80  		return false, err
    81  	}
    82  
    83  	if resp.StatusCode != http.StatusOK {
    84  		return false, httpRespToErrorResponse(resp)
    85  	}
    86  
    87  	return resp.Header.Get(ConfigAppliedHeader) != ConfigAppliedTrue, nil
    88  }
    89  
    90  // GetConfigKV - returns the key, value of the requested key, incoming data is encrypted.
    91  func (adm *AdminClient) GetConfigKV(ctx context.Context, key string) ([]byte, error) {
    92  	v := url.Values{}
    93  	v.Set("key", key)
    94  
    95  	// Execute GET on /minio/admin/v3/get-config-kv?key={key} to get value of key.
    96  	resp, err := adm.executeMethod(ctx,
    97  		http.MethodGet,
    98  		requestData{
    99  			relPath:     adminAPIPrefix + "/get-config-kv",
   100  			queryValues: v,
   101  		})
   102  	defer closeResponse(resp)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	if resp.StatusCode != http.StatusOK {
   108  		return nil, httpRespToErrorResponse(resp)
   109  	}
   110  
   111  	return DecryptData(adm.getSecretKey(), resp.Body)
   112  }
   113  
   114  // KVOptions takes specific inputs for KV functions
   115  type KVOptions struct {
   116  	Env bool
   117  }
   118  
   119  // GetConfigKVWithOptions - returns the key, value of the requested key, incoming data is encrypted.
   120  func (adm *AdminClient) GetConfigKVWithOptions(ctx context.Context, key string, opts KVOptions) ([]byte, error) {
   121  	v := url.Values{}
   122  	v.Set("key", key)
   123  	if opts.Env {
   124  		v.Set("env", "")
   125  	}
   126  
   127  	// Execute GET on /minio/admin/v3/get-config-kv?key={key} to get value of key.
   128  	resp, err := adm.executeMethod(ctx,
   129  		http.MethodGet,
   130  		requestData{
   131  			relPath:     adminAPIPrefix + "/get-config-kv",
   132  			queryValues: v,
   133  		})
   134  	defer closeResponse(resp)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	defer closeResponse(resp)
   140  
   141  	if resp.StatusCode != http.StatusOK {
   142  		return nil, httpRespToErrorResponse(resp)
   143  	}
   144  
   145  	return DecryptData(adm.getSecretKey(), resp.Body)
   146  }