Skip to content

Commit c7ca26d

Browse files
committed
SNI support for Console
1 parent f967058 commit c7ca26d

File tree

9 files changed

+355
-69
lines changed

9 files changed

+355
-69
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,41 @@ export CONSOLE_MINIO_SERVER=http://localhost:9000
113113
./console server
114114
```
115115

116+
## Run Console with TLS enable
117+
118+
Copy your `public.crt` and `private.key` to `~/.console/certs`, then:
119+
120+
```$xslt
121+
./console server
122+
```
123+
124+
Additionally, `Console` has support for multiple certificates, clients can request them using `SNI`. It expects the following structure:
125+
126+
```$xslt
127+
certs/
128+
129+
├─ public.crt
130+
├─ private.key
131+
132+
├─ example.com/
133+
│ │
134+
│ ├─ public.crt
135+
│ └─ private.key
136+
└─ foobar.org/
137+
138+
├─ public.crt
139+
└─ private.key
140+
...
141+
142+
```
143+
144+
Therefore, we read all filenames in the cert directory and check
145+
for each directory whether it contains a public.crt and private.key.
146+
116147
## Connect Console to a Minio using TLS and a self-signed certificate
117148

149+
Copy the MinIO `ca.crt` under `~/.console/certs/CAs`, then:
118150
```
119-
...
120-
export CONSOLE_MINIO_SERVER_TLS_ROOT_CAS=<certificate_file_name>
121151
export CONSOLE_MINIO_SERVER=https://localhost:9000
122152
./console server
123153
```

cmd/console/server.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ import (
2020
"fmt"
2121
"log"
2222
"os"
23+
"path/filepath"
2324

2425
"github.com/go-openapi/loads"
2526
"github.com/jessevdk/go-flags"
2627
"github.com/minio/cli"
28+
"github.com/minio/console/pkg/certs"
2729
"github.com/minio/console/restapi"
2830
"github.com/minio/console/restapi/operations"
31+
"github.com/minio/minio/cmd/logger"
32+
certsx "github.com/minio/minio/pkg/certs"
2933
)
3034

3135
// starts the server
@@ -56,14 +60,9 @@ var serverCmd = cli.Command{
5660
Usage: "HTTPS server port",
5761
},
5862
cli.StringFlag{
59-
Name: "tls-certificate",
60-
Value: "",
61-
Usage: "filename of public cert",
62-
},
63-
cli.StringFlag{
64-
Name: "tls-key",
65-
Value: "",
66-
Usage: "filename of private key",
63+
Name: "certs-dir",
64+
Value: certs.GlobalCertsCADir.Get(),
65+
Usage: "path to certs directory",
6766
},
6867
},
6968
}
@@ -82,7 +81,9 @@ func startServer(ctx *cli.Context) error {
8281
parser := flags.NewParser(server, flags.Default)
8382
parser.ShortDescription = "MinIO Console Server"
8483
parser.LongDescription = swaggerSpec.Spec().Info.Description
84+
8585
server.ConfigureFlags()
86+
8687
for _, optsGroup := range api.CommandLineOptionsGroups {
8788
_, err := parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
8889
if err != nil {
@@ -106,12 +107,19 @@ func startServer(ctx *cli.Context) error {
106107
restapi.Hostname = ctx.String("host")
107108
restapi.Port = fmt.Sprintf("%v", ctx.Int("port"))
108109

109-
tlsCertificatePath := ctx.String("tls-certificate")
110-
tlsCertificateKeyPath := ctx.String("tls-key")
110+
// Set all certs and CAs directories.
111+
globalCertsDir, _ := certs.NewConfigDirFromCtx(ctx, "certs-dir", certs.DefaultCertsDir.Get)
112+
certs.GlobalCertsCADir = &certs.ConfigDir{Path: filepath.Join(globalCertsDir.Get(), certs.CertsCADir)}
113+
logger.FatalIf(certs.MkdirAllIgnorePerm(certs.GlobalCertsCADir.Get()), "Unable to create certs CA directory at %s", certs.GlobalCertsCADir.Get())
114+
115+
// load all CAs from ~/.console/certs/CAs
116+
restapi.GlobalRootCAs, err = certsx.GetRootCAs(certs.GlobalCertsCADir.Get())
117+
logger.FatalIf(err, "Failed to read root CAs (%v)", err)
118+
// load all certs from ~/.console/certs
119+
restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager, err = certs.GetTLSConfig()
120+
logger.FatalIf(err, "Unable to load the TLS configuration")
111121

112-
if tlsCertificatePath != "" && tlsCertificateKeyPath != "" {
113-
server.TLSCertificate = flags.Filename(tlsCertificatePath)
114-
server.TLSCertificateKey = flags.Filename(tlsCertificateKeyPath)
122+
if len(restapi.GlobalPublicCerts) > 0 && restapi.GlobalRootCAs != nil {
115123
// If TLS certificates are provided enforce the HTTPS schema, meaning console will redirect
116124
// plain HTTP connections to HTTPS server
117125
server.EnabledListeners = []string{"http", "https"}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/minio/minio v0.0.0-20200927172404-27d9bd04e544
2121
github.com/minio/minio-go/v7 v7.0.6-0.20200923173112-bc846cb9b089
2222
github.com/minio/operator v0.0.0-20200930213302-ab2bbdfae96c
23+
github.com/mitchellh/go-homedir v1.1.0
2324
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
2425
github.com/secure-io/sio-go v0.3.1
2526
github.com/stretchr/testify v1.6.1

pkg/certs/certs.go

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
var GlobalContext context.Context
36+
37+
type GetCertificateFunc = certs.GetCertificateFunc
38+
39+
// ConfigDir - points to a user set directory.
40+
type ConfigDir struct {
41+
Path string
42+
}
43+
44+
// Get - returns current directory.
45+
func (dir *ConfigDir) Get() string {
46+
return dir.Path
47+
}
48+
49+
func getDefaultConfigDir() string {
50+
homeDir, err := homedir.Dir()
51+
if err != nil {
52+
return ""
53+
}
54+
return filepath.Join(homeDir, DefaultConsoleConfigDir)
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+
// isFile - returns whether given Path is a file or not.
66+
func isFile(path string) bool {
67+
if fi, err := os.Stat(path); err == nil {
68+
return fi.Mode().IsRegular()
69+
}
70+
71+
return false
72+
}
73+
74+
var (
75+
// Default certs and CA directories.
76+
DefaultCertsDir = &ConfigDir{Path: getDefaultCertsDir()}
77+
DefaultCertsCADir = &ConfigDir{Path: getDefaultCertsCADir()}
78+
79+
// Points to current certs directory set by user with --certs-dir
80+
GlobalCertsDir = DefaultCertsDir
81+
// Points to relative Path to certs directory and is <value-of-certs-dir>/CAs
82+
GlobalCertsCADir = DefaultCertsCADir
83+
)
84+
85+
// Attempts to create all directories, ignores any permission denied errors.
86+
func MkdirAllIgnorePerm(path string) error {
87+
err := os.MkdirAll(path, 0700)
88+
if err != nil {
89+
// It is possible in kubernetes like deployments this directory
90+
// is already mounted and is not writable, ignore any write errors.
91+
if os.IsPermission(err) {
92+
err = nil
93+
}
94+
}
95+
return err
96+
}
97+
98+
func NewConfigDirFromCtx(ctx *cli.Context, option string, getDefaultDir func() string) (*ConfigDir, bool) {
99+
var dir string
100+
var dirSet bool
101+
102+
switch {
103+
case ctx.IsSet(option):
104+
dir = ctx.String(option)
105+
dirSet = true
106+
case ctx.GlobalIsSet(option):
107+
dir = ctx.GlobalString(option)
108+
dirSet = true
109+
// cli package does not expose parent's option option. Below code is workaround.
110+
if dir == "" || dir == getDefaultDir() {
111+
dirSet = false // Unset to false since GlobalIsSet() true is a false positive.
112+
if ctx.Parent().GlobalIsSet(option) {
113+
dir = ctx.Parent().GlobalString(option)
114+
dirSet = true
115+
}
116+
}
117+
default:
118+
// Neither local nor global option is provided. In this case, try to use
119+
// default directory.
120+
dir = getDefaultDir()
121+
if dir == "" {
122+
logger.FatalIf(errors.New("Invalid arguments specified"), "%s option must be provided", option)
123+
}
124+
}
125+
126+
if dir == "" {
127+
logger.FatalIf(errors.New("empty directory"), "%s directory cannot be empty", option)
128+
}
129+
130+
// Disallow relative paths, figure out absolute paths.
131+
dirAbs, err := filepath.Abs(dir)
132+
logger.FatalIf(err, "Unable to fetch absolute path for %s=%s", option, dir)
133+
logger.FatalIf(MkdirAllIgnorePerm(dirAbs), "Unable to create directory specified %s=%s", option, dir)
134+
135+
return &ConfigDir{Path: dirAbs}, dirSet
136+
}
137+
138+
func getPublicCertFile() string {
139+
return filepath.Join(GlobalCertsDir.Get(), PublicCertFile)
140+
}
141+
142+
func getPrivateKeyFile() string {
143+
return filepath.Join(GlobalCertsDir.Get(), PrivateKeyFile)
144+
}
145+
146+
func GetTLSConfig() (x509Certs []*x509.Certificate, manager *certs.Manager, err error) {
147+
148+
GlobalContext = context.Background()
149+
150+
if !(isFile(getPublicCertFile()) && isFile(getPrivateKeyFile())) {
151+
return nil, nil, nil
152+
}
153+
154+
if x509Certs, err = config.ParsePublicCertFile(getPublicCertFile()); err != nil {
155+
return nil, nil, err
156+
}
157+
158+
manager, err = certs.NewManager(GlobalContext, getPublicCertFile(), getPrivateKeyFile(), config.LoadX509KeyPair)
159+
if err != nil {
160+
return nil, nil, err
161+
}
162+
163+
//Console has support for multiple certificates. It expects the following structure:
164+
// certs/
165+
// │
166+
// ├─ public.crt
167+
// ├─ private.key
168+
// │
169+
// ├─ example.com/
170+
// │ │
171+
// │ ├─ public.crt
172+
// │ └─ private.key
173+
// └─ foobar.org/
174+
// │
175+
// ├─ public.crt
176+
// └─ private.key
177+
// ...
178+
//
179+
//Therefore, we read all filenames in the cert directory and check
180+
//for each directory whether it contains a public.crt and private.key.
181+
// If so, we try to add it to certificate manager.
182+
root, err := os.Open(GlobalCertsDir.Get())
183+
if err != nil {
184+
return nil, nil, err
185+
}
186+
defer root.Close()
187+
188+
files, err := root.Readdir(-1)
189+
if err != nil {
190+
return nil, nil, err
191+
}
192+
for _, file := range files {
193+
// Ignore all
194+
// - regular files
195+
// - "CAs" directory
196+
// - any directory which starts with ".."
197+
if file.Mode().IsRegular() || file.Name() == "CAs" || strings.HasPrefix(file.Name(), "..") {
198+
continue
199+
}
200+
if file.Mode()&os.ModeSymlink == os.ModeSymlink {
201+
file, err = os.Stat(filepath.Join(root.Name(), file.Name()))
202+
if err != nil {
203+
// not accessible ignore
204+
continue
205+
}
206+
if !file.IsDir() {
207+
continue
208+
}
209+
}
210+
211+
var (
212+
certFile = filepath.Join(root.Name(), file.Name(), PublicCertFile)
213+
keyFile = filepath.Join(root.Name(), file.Name(), PrivateKeyFile)
214+
)
215+
if !isFile(certFile) || !isFile(keyFile) {
216+
continue
217+
}
218+
if err = manager.AddCertificate(certFile, keyFile); err != nil {
219+
err = fmt.Errorf("Unable to load TLS certificate '%s,%s': %w", certFile, keyFile, err)
220+
logger.LogIf(GlobalContext, err, logger.Application)
221+
}
222+
}
223+
return x509Certs, manager, nil
224+
}

pkg/certs/const.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
const (
20+
// Default minio configuration directory where below configuration files/directories are stored.
21+
DefaultConsoleConfigDir = ".console"
22+
23+
// Directory contains below files/directories for HTTPS configuration.
24+
CertsDir = "certs"
25+
26+
// Directory contains all CA certificates other than system defaults for HTTPS.
27+
CertsCADir = "CAs"
28+
29+
// Public certificate file for HTTPS.
30+
PublicCertFile = "public.crt"
31+
32+
// Private key file for HTTPS.
33+
PrivateKeyFile = "private.key"
34+
)

0 commit comments

Comments
 (0)