Skip to content

Commit 55531d6

Browse files
fix: allow TLS access on multiple SNI certs (#812)
if GetCertificate() is set never set tls.Certificates
1 parent e328190 commit 55531d6

File tree

4 files changed

+21
-44
lines changed

4 files changed

+21
-44
lines changed

cmd/console/server.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
package main
1818

1919
import (
20-
"context"
2120
"fmt"
2221
"io/ioutil"
2322
"path/filepath"
2423
"strconv"
2524
"time"
2625

27-
xcerts "github.com/minio/pkg/certs"
28-
2926
"github.com/go-openapi/loads"
3027
"github.com/jessevdk/go-flags"
3128
"github.com/minio/cli"
@@ -143,12 +140,8 @@ func loadAllCerts(ctx *cli.Context) error {
143140
return fmt.Errorf("unable to create certs CA directory at %s: failed with %w", certs.GlobalCertsCADir.Get(), err)
144141
}
145142

146-
var manager *xcerts.Manager
147143
// load the certificates and the CAs
148-
restapi.GlobalRootCAs, restapi.GlobalPublicCerts, manager, err = certs.GetAllCertificatesAndCAs()
149-
restapi.GlobalTLSCertsManager = &certs.TLSCertsManager{
150-
Manager: manager,
151-
}
144+
restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager, err = certs.GetAllCertificatesAndCAs()
152145
if err != nil {
153146
return fmt.Errorf("unable to load certificates at %s: failed with %w", certs.GlobalCertsDir.Get(), err)
154147
}
@@ -160,7 +153,7 @@ func loadAllCerts(ctx *cli.Context) error {
160153
swaggerServerCACertificate := ctx.String("tls-ca")
161154
// load tls cert and key from swagger server tls-certificate and tls-key flags
162155
if swaggerServerCertificate != "" && swaggerServerCertificateKey != "" {
163-
if err = restapi.GlobalTLSCertsManager.AddCertificate(context.Background(), swaggerServerCertificate, swaggerServerCertificateKey); err != nil {
156+
if err = restapi.GlobalTLSCertsManager.AddCertificate(swaggerServerCertificate, swaggerServerCertificateKey); err != nil {
164157
return err
165158
}
166159
if x509Certs, err := certs.ParsePublicCertFile(swaggerServerCertificate); err == nil {

pkg/certs/certs.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ func LoadX509KeyPair(certFile, keyFile string) (tls.Certificate, error) {
234234
}
235235

236236
func GetTLSConfig() (x509Certs []*x509.Certificate, manager *xcerts.Manager, err error) {
237-
238237
ctx := context.Background()
239238

240239
if !(isFile(getPublicCertFile()) && isFile(getPrivateKeyFile())) {
@@ -314,30 +313,17 @@ func GetTLSConfig() (x509Certs []*x509.Certificate, manager *xcerts.Manager, err
314313

315314
func GetAllCertificatesAndCAs() (*x509.CertPool, []*x509.Certificate, *xcerts.Manager, error) {
316315
// load all CAs from ~/.console/certs/CAs
317-
GlobalRootCAs, err := xcerts.GetRootCAs(GlobalCertsCADir.Get())
316+
rootCAs, err := xcerts.GetRootCAs(GlobalCertsCADir.Get())
318317
if err != nil {
319318
return nil, nil, nil, err
320319
}
321320
// load all certs from ~/.console/certs
322-
globalPublicCerts, globalTLSCertsManager, err := GetTLSConfig()
321+
publicCerts, certsManager, err := GetTLSConfig()
323322
if err != nil {
324323
return nil, nil, nil, err
325324
}
326-
return GlobalRootCAs, globalPublicCerts, globalTLSCertsManager, nil
327-
}
328-
329-
// TLSCertsManager custom TLS Manager for SNI support
330-
type TLSCertsManager struct {
331-
*xcerts.Manager
332-
}
333-
334-
// AddCertificate check if Manager is initialized and then append a new certificate to it
335-
func (m *TLSCertsManager) AddCertificate(ctx context.Context, publicKey, privateKey string) (err error) {
336-
// If Cert Manager is not nil add more certificates
337-
if m.Manager != nil {
338-
return m.Manager.AddCertificate(publicKey, privateKey)
325+
if rootCAs == nil {
326+
rootCAs = &x509.CertPool{}
339327
}
340-
// Initialize cert manager
341-
m.Manager, err = xcerts.NewManager(ctx, publicKey, privateKey, LoadX509KeyPair)
342-
return err
328+
return rootCAs, publicCerts, certsManager, nil
343329
}

restapi/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"strings"
2525
"time"
2626

27-
"github.com/minio/console/pkg/certs"
27+
xcerts "github.com/minio/pkg/certs"
2828
"github.com/minio/pkg/env"
2929
)
3030

@@ -276,7 +276,7 @@ var (
276276
// GlobalPublicCerts has certificates Console will use to serve clients
277277
GlobalPublicCerts []*x509.Certificate
278278
// GlobalTLSCertsManager custom TLS Manager for SNI support
279-
GlobalTLSCertsManager *certs.TLSCertsManager
279+
GlobalTLSCertsManager *xcerts.Manager
280280
)
281281

282282
// getK8sSAToken assumes the plugin is running inside a k8s pod and extract the current service account from the

restapi/configure_console.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ package restapi
2121
import (
2222
"bytes"
2323
"crypto/tls"
24-
"crypto/x509"
2524
"io"
2625
"io/fs"
26+
"log"
2727
"net/http"
2828
"strings"
2929
"time"
@@ -145,24 +145,13 @@ func configureAPI(api *operations.ConsoleAPI) http.Handler {
145145

146146
// The TLS configuration before HTTPS server starts.
147147
func configureTLS(tlsConfig *tls.Config) {
148-
if GlobalRootCAs == nil {
149-
GlobalRootCAs = &x509.CertPool{}
150-
}
151148
// Add the global public crts as part of global root CAs
152149
for _, publicCrt := range GlobalPublicCerts {
153-
// Add certificates to swagger TLS configuration
154-
tlsConfig.Certificates = append(tlsConfig.Certificates, tls.Certificate{
155-
Certificate: [][]byte{publicCrt.Raw},
156-
Leaf: publicCrt,
157-
})
158150
GlobalRootCAs.AddCert(publicCrt)
159151
}
160152

161153
tlsConfig.RootCAs = GlobalRootCAs
162-
163-
if GlobalTLSCertsManager != nil {
164-
tlsConfig.GetCertificate = GlobalTLSCertsManager.GetCertificate
165-
}
154+
tlsConfig.GetCertificate = GlobalTLSCertsManager.GetCertificate
166155
}
167156

168157
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
@@ -277,9 +266,18 @@ func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
277266
}
278267
}
279268

269+
type logWriter struct{}
270+
271+
func (lw logWriter) Write(b []byte) (int, error) {
272+
LogError(string(bytes.TrimSuffix(b, []byte("\n"))))
273+
return len(b), nil
274+
}
275+
280276
// As soon as server is initialized but not run yet, this function will be called.
281277
// If you need to modify a config, store server instance to stop it individually later, this is the place.
282278
// This function can be called multiple times, depending on the number of serving schemes.
283279
// scheme value will be set accordingly: "http", "https" or "unix"
284-
func configureServer(s *http.Server, scheme, addr string) {
280+
func configureServer(s *http.Server, _, _ string) {
281+
// Turn-off random logging by Go internall
282+
s.ErrorLog = log.New(&logWriter{}, "", 0)
285283
}

0 commit comments

Comments
 (0)