storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/config-kv-commands.go (about)

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