|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2020 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package certs |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/x509" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/minio/cli" |
| 29 | + "github.com/minio/minio/cmd/config" |
| 30 | + "github.com/minio/minio/cmd/logger" |
| 31 | + "github.com/minio/minio/pkg/certs" |
| 32 | + "github.com/mitchellh/go-homedir" |
| 33 | +) |
| 34 | + |
| 35 | +type GetCertificateFunc = certs.GetCertificateFunc |
| 36 | + |
| 37 | +// ConfigDir - points to a user set directory. |
| 38 | +type ConfigDir struct { |
| 39 | + Path string |
| 40 | +} |
| 41 | + |
| 42 | +// Get - returns current directory. |
| 43 | +func (dir *ConfigDir) Get() string { |
| 44 | + return dir.Path |
| 45 | +} |
| 46 | + |
| 47 | +func getDefaultConfigDir() string { |
| 48 | + homeDir, err := homedir.Dir() |
| 49 | + if err != nil { |
| 50 | + return "" |
| 51 | + } |
| 52 | + return filepath.Join(homeDir, DefaultConsoleConfigDir) |
| 53 | +} |
| 54 | + |
| 55 | +func getDefaultCertsDir() string { |
| 56 | + return filepath.Join(getDefaultConfigDir(), CertsDir) |
| 57 | +} |
| 58 | + |
| 59 | +func getDefaultCertsCADir() string { |
| 60 | + return filepath.Join(getDefaultCertsDir(), CertsCADir) |
| 61 | +} |
| 62 | + |
| 63 | +// isFile - returns whether given Path is a file or not. |
| 64 | +func isFile(path string) bool { |
| 65 | + if fi, err := os.Stat(path); err == nil { |
| 66 | + return fi.Mode().IsRegular() |
| 67 | + } |
| 68 | + |
| 69 | + return false |
| 70 | +} |
| 71 | + |
| 72 | +var ( |
| 73 | + // DefaultCertsDir certs directory. |
| 74 | + DefaultCertsDir = &ConfigDir{Path: getDefaultCertsDir()} |
| 75 | + // DefaultCertsCADir CA directory. |
| 76 | + DefaultCertsCADir = &ConfigDir{Path: getDefaultCertsCADir()} |
| 77 | + // GlobalCertsDir points to current certs directory set by user with --certs-dir |
| 78 | + GlobalCertsDir = DefaultCertsDir |
| 79 | + // GlobalCertsCADir points to relative Path to certs directory and is <value-of-certs-dir>/CAs |
| 80 | + GlobalCertsCADir = DefaultCertsCADir |
| 81 | +) |
| 82 | + |
| 83 | +// MkdirAllIgnorePerm attempts to create all directories, ignores any permission denied errors. |
| 84 | +func MkdirAllIgnorePerm(path string) error { |
| 85 | + err := os.MkdirAll(path, 0700) |
| 86 | + if err != nil { |
| 87 | + // It is possible in kubernetes like deployments this directory |
| 88 | + // is already mounted and is not writable, ignore any write errors. |
| 89 | + if os.IsPermission(err) { |
| 90 | + err = nil |
| 91 | + } |
| 92 | + } |
| 93 | + return err |
| 94 | +} |
| 95 | + |
| 96 | +func NewConfigDirFromCtx(ctx *cli.Context, option string, getDefaultDir func() string) (*ConfigDir, bool) { |
| 97 | + var dir string |
| 98 | + var dirSet bool |
| 99 | + |
| 100 | + switch { |
| 101 | + case ctx.IsSet(option): |
| 102 | + dir = ctx.String(option) |
| 103 | + dirSet = true |
| 104 | + case ctx.GlobalIsSet(option): |
| 105 | + dir = ctx.GlobalString(option) |
| 106 | + dirSet = true |
| 107 | + // cli package does not expose parent's option option. Below code is workaround. |
| 108 | + if dir == "" || dir == getDefaultDir() { |
| 109 | + dirSet = false // Unset to false since GlobalIsSet() true is a false positive. |
| 110 | + if ctx.Parent().GlobalIsSet(option) { |
| 111 | + dir = ctx.Parent().GlobalString(option) |
| 112 | + dirSet = true |
| 113 | + } |
| 114 | + } |
| 115 | + default: |
| 116 | + // Neither local nor global option is provided. In this case, try to use |
| 117 | + // default directory. |
| 118 | + dir = getDefaultDir() |
| 119 | + if dir == "" { |
| 120 | + logger.FatalIf(errors.New("invalid arguments specified"), "%s option must be provided", option) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if dir == "" { |
| 125 | + logger.FatalIf(errors.New("empty directory"), "%s directory cannot be empty", option) |
| 126 | + } |
| 127 | + |
| 128 | + // Disallow relative paths, figure out absolute paths. |
| 129 | + dirAbs, err := filepath.Abs(dir) |
| 130 | + logger.FatalIf(err, "Unable to fetch absolute path for %s=%s", option, dir) |
| 131 | + logger.FatalIf(MkdirAllIgnorePerm(dirAbs), "Unable to create directory specified %s=%s", option, dir) |
| 132 | + |
| 133 | + return &ConfigDir{Path: dirAbs}, dirSet |
| 134 | +} |
| 135 | + |
| 136 | +func getPublicCertFile() string { |
| 137 | + return filepath.Join(GlobalCertsDir.Get(), PublicCertFile) |
| 138 | +} |
| 139 | + |
| 140 | +func getPrivateKeyFile() string { |
| 141 | + return filepath.Join(GlobalCertsDir.Get(), PrivateKeyFile) |
| 142 | +} |
| 143 | + |
| 144 | +func GetTLSConfig() (x509Certs []*x509.Certificate, manager *certs.Manager, err error) { |
| 145 | + |
| 146 | + ctx := context.Background() |
| 147 | + |
| 148 | + if !(isFile(getPublicCertFile()) && isFile(getPrivateKeyFile())) { |
| 149 | + return nil, nil, nil |
| 150 | + } |
| 151 | + |
| 152 | + if x509Certs, err = config.ParsePublicCertFile(getPublicCertFile()); err != nil { |
| 153 | + return nil, nil, err |
| 154 | + } |
| 155 | + |
| 156 | + manager, err = certs.NewManager(ctx, getPublicCertFile(), getPrivateKeyFile(), config.LoadX509KeyPair) |
| 157 | + if err != nil { |
| 158 | + return nil, nil, err |
| 159 | + } |
| 160 | + |
| 161 | + //Console has support for multiple certificates. It expects the following structure: |
| 162 | + // certs/ |
| 163 | + // │ |
| 164 | + // ├─ public.crt |
| 165 | + // ├─ private.key |
| 166 | + // │ |
| 167 | + // ├─ example.com/ |
| 168 | + // │ │ |
| 169 | + // │ ├─ public.crt |
| 170 | + // │ └─ private.key |
| 171 | + // └─ foobar.org/ |
| 172 | + // │ |
| 173 | + // ├─ public.crt |
| 174 | + // └─ private.key |
| 175 | + // ... |
| 176 | + // |
| 177 | + //Therefore, we read all filenames in the cert directory and check |
| 178 | + //for each directory whether it contains a public.crt and private.key. |
| 179 | + // If so, we try to add it to certificate manager. |
| 180 | + root, err := os.Open(GlobalCertsDir.Get()) |
| 181 | + if err != nil { |
| 182 | + return nil, nil, err |
| 183 | + } |
| 184 | + defer root.Close() |
| 185 | + |
| 186 | + files, err := root.Readdir(-1) |
| 187 | + if err != nil { |
| 188 | + return nil, nil, err |
| 189 | + } |
| 190 | + for _, file := range files { |
| 191 | + // Ignore all |
| 192 | + // - regular files |
| 193 | + // - "CAs" directory |
| 194 | + // - any directory which starts with ".." |
| 195 | + if file.Mode().IsRegular() || file.Name() == "CAs" || strings.HasPrefix(file.Name(), "..") { |
| 196 | + continue |
| 197 | + } |
| 198 | + if file.Mode()&os.ModeSymlink == os.ModeSymlink { |
| 199 | + file, err = os.Stat(filepath.Join(root.Name(), file.Name())) |
| 200 | + if err != nil { |
| 201 | + // not accessible ignore |
| 202 | + continue |
| 203 | + } |
| 204 | + if !file.IsDir() { |
| 205 | + continue |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + var ( |
| 210 | + certFile = filepath.Join(root.Name(), file.Name(), PublicCertFile) |
| 211 | + keyFile = filepath.Join(root.Name(), file.Name(), PrivateKeyFile) |
| 212 | + ) |
| 213 | + if !isFile(certFile) || !isFile(keyFile) { |
| 214 | + continue |
| 215 | + } |
| 216 | + if err = manager.AddCertificate(certFile, keyFile); err != nil { |
| 217 | + err = fmt.Errorf("unable to load TLS certificate '%s,%s': %w", certFile, keyFile, err) |
| 218 | + logger.LogIf(ctx, err, logger.Application) |
| 219 | + } |
| 220 | + } |
| 221 | + return x509Certs, manager, nil |
| 222 | +} |
0 commit comments