1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 // channel (size=1) with the current keypair
20 currentCert chan *tls.Certificate
23 func (srv *server) Serve(l net.Listener) error {
24 if theConfig.TLSCertificateFile == "" && theConfig.TLSKeyFile == "" {
25 return srv.Server.Serve(l)
27 // https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
28 srv.TLSConfig = &tls.Config{
29 GetCertificate: srv.getCertificate,
30 PreferServerCipherSuites: true,
31 CurvePreferences: []tls.CurveID{
35 MinVersion: tls.VersionTLS12,
36 CipherSuites: []uint16{
37 tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
38 tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
39 tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
40 tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
41 tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
42 tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
45 srv.currentCert = make(chan *tls.Certificate, 1)
46 go srv.refreshCertificate(theConfig.TLSCertificateFile, theConfig.TLSKeyFile)
47 return srv.Server.ServeTLS(l, "", "")
50 func (srv *server) refreshCertificate(certfile, keyfile string) {
51 cert, err := tls.LoadX509KeyPair(certfile, keyfile)
53 log.WithError(err).Fatal("error loading X509 key pair")
55 srv.currentCert <- &cert
57 reload := make(chan os.Signal, 1)
58 signal.Notify(reload, syscall.SIGHUP)
60 cert, err := tls.LoadX509KeyPair(certfile, keyfile)
62 log.WithError(err).Warn("error loading X509 key pair")
65 // Throw away old cert and start using new one
67 srv.currentCert <- &cert
71 func (srv *server) getCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
72 if srv.currentCert == nil {
73 panic("srv.currentCert not initialized")
75 cert := <-srv.currentCert
76 srv.currentCert <- cert