| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 22:08:29 +08:00
										 |  |  | 	homedir "github.com/mitchellh/go-homedir" | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	// Default minio configuration directory where below configuration files/directories are stored.
 | 
					
						
							|  |  |  | 	defaultMinioConfigDir = ".minio" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Directory contains below files/directories for HTTPS configuration.
 | 
					
						
							|  |  |  | 	certsDir = "certs" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Directory contains all CA certificates other than system defaults for HTTPS.
 | 
					
						
							|  |  |  | 	certsCADir = "CAs" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Public certificate file for HTTPS.
 | 
					
						
							|  |  |  | 	publicCertFile = "public.crt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Private key file for HTTPS.
 | 
					
						
							|  |  |  | 	privateKeyFile = "private.key" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | // ConfigDir - points to a user set directory.
 | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | type ConfigDir struct { | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 	path string | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 12:13:33 +08:00
										 |  |  | func getDefaultConfigDir() string { | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | 	homeDir, err := homedir.Dir() | 
					
						
							| 
									
										
										
										
											2018-03-01 12:13:33 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return filepath.Join(homeDir, defaultMinioConfigDir) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | func getDefaultCertsDir() string { | 
					
						
							|  |  |  | 	return filepath.Join(getDefaultConfigDir(), certsDir) | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | func getDefaultCertsCADir() string { | 
					
						
							|  |  |  | 	return filepath.Join(getDefaultCertsDir(), certsCADir) | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	// Default config, certs and CA directories.
 | 
					
						
							|  |  |  | 	defaultConfigDir  = &ConfigDir{path: getDefaultConfigDir()} | 
					
						
							|  |  |  | 	defaultCertsDir   = &ConfigDir{path: getDefaultCertsDir()} | 
					
						
							|  |  |  | 	defaultCertsCADir = &ConfigDir{path: getDefaultCertsCADir()} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Points to current configuration directory -- deprecated, to be removed in future.
 | 
					
						
							|  |  |  | 	globalConfigDir = defaultConfigDir | 
					
						
							|  |  |  | 	// Points to current certs directory set by user with --certs-dir
 | 
					
						
							|  |  |  | 	globalCertsDir = defaultCertsDir | 
					
						
							|  |  |  | 	// Points to relative path to certs directory and is <value-of-certs-dir>/CAs
 | 
					
						
							|  |  |  | 	globalCertsCADir = defaultCertsCADir | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get - returns current directory.
 | 
					
						
							|  |  |  | func (dir *ConfigDir) Get() string { | 
					
						
							|  |  |  | 	return dir.path | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | // Attempts to create all directories, ignores any permission denied errors.
 | 
					
						
							|  |  |  | func mkdirAllIgnorePerm(path string) error { | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	err := os.MkdirAll(path, 0o700) | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// It is possible in kubernetes like deployments this directory
 | 
					
						
							|  |  |  | 		// is already mounted and is not writable, ignore any write errors.
 | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 		if osIsPermission(err) { | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 			err = nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getConfigFile() string { | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 	return filepath.Join(globalConfigDir.Get(), minioConfigFile) | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getPublicCertFile() string { | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 	return filepath.Join(globalCertsDir.Get(), publicCertFile) | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getPrivateKeyFile() string { | 
					
						
							| 
									
										
										
										
											2019-01-03 02:05:16 +08:00
										 |  |  | 	return filepath.Join(globalCertsDir.Get(), privateKeyFile) | 
					
						
							| 
									
										
										
										
											2017-03-09 11:20:01 +08:00
										 |  |  | } |