storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config-dir.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2015, 2016, 2017, 2018 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 cmd
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  
    23  	homedir "github.com/mitchellh/go-homedir"
    24  )
    25  
    26  const (
    27  	// Default minio configuration directory where below configuration files/directories are stored.
    28  	defaultMinioConfigDir = ".minio"
    29  
    30  	// Directory contains below files/directories for HTTPS configuration.
    31  	certsDir = "certs"
    32  
    33  	// Directory contains all CA certificates other than system defaults for HTTPS.
    34  	certsCADir = "CAs"
    35  
    36  	// Public certificate file for HTTPS.
    37  	publicCertFile = "public.crt"
    38  
    39  	// Private key file for HTTPS.
    40  	privateKeyFile = "private.key"
    41  )
    42  
    43  // ConfigDir - points to a user set directory.
    44  type ConfigDir struct {
    45  	path string
    46  }
    47  
    48  func getDefaultConfigDir() string {
    49  	homeDir, err := homedir.Dir()
    50  	if err != nil {
    51  		return ""
    52  	}
    53  
    54  	return filepath.Join(homeDir, defaultMinioConfigDir)
    55  }
    56  
    57  func getDefaultCertsDir() string {
    58  	return filepath.Join(getDefaultConfigDir(), certsDir)
    59  }
    60  
    61  func getDefaultCertsCADir() string {
    62  	return filepath.Join(getDefaultCertsDir(), certsCADir)
    63  }
    64  
    65  var (
    66  	// Default config, certs and CA directories.
    67  	defaultConfigDir  = &ConfigDir{path: getDefaultConfigDir()}
    68  	defaultCertsDir   = &ConfigDir{path: getDefaultCertsDir()}
    69  	defaultCertsCADir = &ConfigDir{path: getDefaultCertsCADir()}
    70  
    71  	// Points to current configuration directory -- deprecated, to be removed in future.
    72  	globalConfigDir = defaultConfigDir
    73  	// Points to current certs directory set by user with --certs-dir
    74  	globalCertsDir = defaultCertsDir
    75  	// Points to relative path to certs directory and is <value-of-certs-dir>/CAs
    76  	globalCertsCADir = defaultCertsCADir
    77  )
    78  
    79  // Get - returns current directory.
    80  func (dir *ConfigDir) Get() string {
    81  	return dir.path
    82  }
    83  
    84  // Attempts to create all directories, ignores any permission denied errors.
    85  func mkdirAllIgnorePerm(path string) error {
    86  	err := os.MkdirAll(path, 0700)
    87  	if err != nil {
    88  		// It is possible in kubernetes like deployments this directory
    89  		// is already mounted and is not writable, ignore any write errors.
    90  		if osIsPermission(err) {
    91  			err = nil
    92  		}
    93  	}
    94  	return err
    95  }
    96  
    97  func getConfigFile() string {
    98  	return filepath.Join(globalConfigDir.Get(), minioConfigFile)
    99  }
   100  
   101  func getPublicCertFile() string {
   102  	return filepath.Join(globalCertsDir.Get(), publicCertFile)
   103  }
   104  
   105  func getPrivateKeyFile() string {
   106  	return filepath.Join(globalCertsDir.Get(), privateKeyFile)
   107  }