github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/config-validate.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  // Check if version of the config is valid
    26  func validateConfigVersion(config *configV10) (bool, string) {
    27  	if config.Version != globalMCConfigVersion {
    28  		return false, fmt.Sprintf("Config version '%s' does not match mc config version '%s', please update your binary.\n",
    29  			config.Version, globalMCConfigVersion)
    30  	}
    31  	return true, ""
    32  }
    33  
    34  // Verifies the config file of the MinIO Client
    35  func validateConfigFile(config *configV10) (bool, []string) {
    36  	ok, err := validateConfigVersion(config)
    37  	validationSuccessful := true
    38  	var errors []string
    39  	if !ok {
    40  		validationSuccessful = false
    41  		errors = append(errors, err)
    42  	}
    43  	aliases := config.Aliases
    44  	for _, aliasConfig := range aliases {
    45  		aliasConfigHealthOk, aliasErrors := validateConfigHost(aliasConfig)
    46  		if !aliasConfigHealthOk {
    47  			validationSuccessful = false
    48  			errors = append(errors, aliasErrors...)
    49  		}
    50  	}
    51  	return validationSuccessful, errors
    52  }
    53  
    54  func validateConfigHost(host aliasConfigV10) (bool, []string) {
    55  	validationSuccessful := true
    56  	var hostErrors []string
    57  	if !isValidAPI(strings.ToLower(host.API)) {
    58  		validationSuccessful = false
    59  		hostErrors = append(hostErrors, errInvalidAPISignature(host.API, host.URL).ToGoError().Error())
    60  	}
    61  	if !isValidHostURL(host.URL) {
    62  		validationSuccessful = false
    63  		hostErrors = append(hostErrors, errInvalidURL(host.URL).ToGoError().Error())
    64  	}
    65  	return validationSuccessful, hostErrors
    66  }