Skip to content

Commit c1e41e6

Browse files
allow console to listen on ipv6 (#781)
also converge tls-host and host, because hostnames have nothing to do with HTTP or HTTPs they are the same for both HTTP and HTTPs. Deprecating the older flag `--tls-host` but it will still be honored as hidden flag.
1 parent 1b7fb2a commit c1e41e6

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

cmd/console/server.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"log"
2424
"os"
2525
"path/filepath"
26+
"strconv"
2627
"time"
2728

2829
"github.com/go-openapi/loads"
@@ -38,32 +39,37 @@ var serverCmd = cli.Command{
3839
Name: "server",
3940
Aliases: []string{"srv"},
4041
Usage: "starts Console server",
41-
Action: startServer,
42+
Action: StartServer,
4243
Flags: []cli.Flag{
4344
cli.StringFlag{
4445
Name: "host",
4546
Value: restapi.GetHostname(),
46-
Usage: "HTTP server hostname",
47+
Usage: "hostname",
4748
},
4849
cli.IntFlag{
4950
Name: "port",
5051
Value: restapi.GetPort(),
51-
Usage: "HTTP Server port",
52+
Usage: "HTTP port",
5253
},
54+
// This is kept here for backward compatibility,
55+
// hostname's do not have HTTP or HTTPs
56+
// hostnames are opaque so using --host
57+
// works for both HTTP and HTTPS setup.
5358
cli.StringFlag{
54-
Name: "tls-host",
55-
Value: restapi.GetTLSHostname(),
56-
Usage: "HTTPS server hostname",
59+
Name: "tls-host",
60+
Value: restapi.GetHostname(),
61+
Usage: "HTTPS hostname",
62+
Hidden: true,
5763
},
5864
cli.IntFlag{
5965
Name: "tls-port",
6066
Value: restapi.GetTLSPort(),
61-
Usage: "HTTPS server port",
67+
Usage: "HTTPS port",
6268
},
6369
cli.StringFlag{
6470
Name: "tls-redirect",
6571
Value: restapi.GetTLSRedirect(),
66-
Usage: "HTTPS redirect by default",
72+
Usage: "toggle HTTP->HTTPS redirect",
6773
},
6874
cli.StringFlag{
6975
Name: "certs-dir",
@@ -73,23 +79,23 @@ var serverCmd = cli.Command{
7379
cli.StringFlag{
7480
Name: "tls-certificate",
7581
Value: "",
76-
Usage: "path tls certificate",
82+
Usage: "path to TLS public certificate",
7783
},
7884
cli.StringFlag{
7985
Name: "tls-key",
8086
Value: "",
81-
Usage: "path tls key",
87+
Usage: "path to TLS private key",
8288
},
8389
cli.StringFlag{
8490
Name: "tls-ca",
8591
Value: "",
86-
Usage: "path tls ca",
92+
Usage: "path to TLS Certificate Authority",
8793
},
8894
},
8995
}
9096

91-
// starts the controller
92-
func startServer(ctx *cli.Context) error {
97+
// StartServer starts the console service
98+
func StartServer(ctx *cli.Context) error {
9399
swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
94100
if err != nil {
95101
log.Fatalln(err)
@@ -126,7 +132,7 @@ func startServer(ctx *cli.Context) error {
126132
server.Port = ctx.Int("port")
127133

128134
restapi.Hostname = ctx.String("host")
129-
restapi.Port = fmt.Sprintf("%v", ctx.Int("port"))
135+
restapi.Port = strconv.Itoa(ctx.Int("port"))
130136

131137
// Set all certs and CAs directories path
132138
certs.GlobalCertsDir, _ = certs.NewConfigDirFromCtx(ctx, "certs-dir", certs.DefaultCertsDir.Get)
@@ -139,26 +145,28 @@ func startServer(ctx *cli.Context) error {
139145
// load the certificates and the CAs
140146
restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = certs.GetAllCertificatesAndCAs()
141147

142-
// TLS flags from swagger server, used to support older versions of minio-operator
143-
swaggerServerCertificate := ctx.String("tls-certificate")
144-
swaggerServerCertificateKey := ctx.String("tls-key")
145-
SwaggerServerCACertificate := ctx.String("tls-ca")
146-
// load tls cert and key from swagger server tls-certificate and tls-key flags
147-
if swaggerServerCertificate != "" && swaggerServerCertificateKey != "" {
148-
if errAddCert := certs.AddCertificate(context.Background(), restapi.GlobalTLSCertsManager, swaggerServerCertificate, swaggerServerCertificateKey); errAddCert != nil {
149-
log.Println(errAddCert)
150-
}
151-
if x509Certs, errParseCert := certs.ParsePublicCertFile(swaggerServerCertificate); errParseCert == nil {
152-
if len(x509Certs) > 0 {
153-
restapi.GlobalPublicCerts = append(restapi.GlobalPublicCerts, x509Certs[0])
148+
{
149+
// TLS flags from swagger server, used to support VMware vsphere operator version.
150+
swaggerServerCertificate := ctx.String("tls-certificate")
151+
swaggerServerCertificateKey := ctx.String("tls-key")
152+
SwaggerServerCACertificate := ctx.String("tls-ca")
153+
// load tls cert and key from swagger server tls-certificate and tls-key flags
154+
if swaggerServerCertificate != "" && swaggerServerCertificateKey != "" {
155+
if errAddCert := certs.AddCertificate(context.Background(),
156+
restapi.GlobalTLSCertsManager, swaggerServerCertificate, swaggerServerCertificateKey); errAddCert != nil {
157+
log.Println(errAddCert)
158+
}
159+
if x509Certs, errParseCert := certs.ParsePublicCertFile(swaggerServerCertificate); errParseCert == nil {
160+
restapi.GlobalPublicCerts = append(restapi.GlobalPublicCerts, x509Certs...)
154161
}
155162
}
156-
}
157-
// load ca cert from swagger server tls-ca flag
158-
if SwaggerServerCACertificate != "" {
159-
caCert, caCertErr := ioutil.ReadFile(SwaggerServerCACertificate)
160-
if caCertErr == nil {
161-
restapi.GlobalRootCAs.AppendCertsFromPEM(caCert)
163+
164+
// load ca cert from swagger server tls-ca flag
165+
if SwaggerServerCACertificate != "" {
166+
caCert, caCertErr := ioutil.ReadFile(SwaggerServerCACertificate)
167+
if caCertErr == nil {
168+
restapi.GlobalRootCAs.AppendCertsFromPEM(caCert)
169+
}
162170
}
163171
}
164172

@@ -170,7 +178,7 @@ func startServer(ctx *cli.Context) error {
170178
server.TLSHost = ctx.String("tls-host")
171179
// Need to store tls-port, tls-host un config variables so secure.middleware can read from there
172180
restapi.TLSPort = fmt.Sprintf("%v", ctx.Int("tls-port"))
173-
restapi.TLSHostname = ctx.String("tls-host")
181+
restapi.Hostname = ctx.String("host")
174182
restapi.TLSRedirect = ctx.String("tls-redirect")
175183
}
176184

restapi/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package restapi
1818

1919
import (
2020
"crypto/x509"
21-
"fmt"
2221
"io/ioutil"
22+
"net"
2323
"strconv"
2424
"strings"
2525
"sync"
@@ -34,10 +34,10 @@ var (
3434
Port = "9090"
3535

3636
// Hostname console hostname
37-
Hostname = "0.0.0.0"
38-
39-
// TLSHostname console tls hostname
40-
TLSHostname = "0.0.0.0"
37+
// avoid listening on 0.0.0.0 by default
38+
// instead listen on all IPv4 and IPv6
39+
// - Hostname should be empty.
40+
Hostname = ""
4141

4242
// TLSPort console tls port
4343
TLSPort = "9443"
@@ -116,7 +116,7 @@ func GetPort() int {
116116
// GetTLSHostname gets console tls hostname set on env variable
117117
// or default one
118118
func GetTLSHostname() string {
119-
return strings.ToLower(env.Get(ConsoleTLSHostname, TLSHostname))
119+
return strings.ToLower(env.Get(ConsoleTLSHostname, Hostname))
120120
}
121121

122122
// GetTLSPort gets console tls port set on env variable
@@ -186,7 +186,7 @@ func getSecureHostsProxyHeaders() []string {
186186

187187
// TLSHost is the host name that is used to redirect HTTP requests to HTTPS. Default is "", which indicates to use the same host.
188188
func getSecureTLSHost() string {
189-
return env.Get(ConsoleSecureTLSHost, fmt.Sprintf("%s:%s", TLSHostname, TLSPort))
189+
return env.Get(ConsoleSecureTLSHost, net.JoinHostPort(Hostname, TLSPort))
190190
}
191191

192192
// STSSeconds is the max-age of the Strict-Transport-Security header. Default is 0, which would NOT include the header.

0 commit comments

Comments
 (0)