2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-10-25 22:57:38 +00:00
|
|
|
package config
|
|
|
|
|
2017-11-15 01:53:23 +00:00
|
|
|
import (
|
2018-03-26 19:55:32 +00:00
|
|
|
"crypto/md5"
|
2017-11-15 01:53:23 +00:00
|
|
|
"crypto/tls"
|
2018-03-26 19:55:32 +00:00
|
|
|
"encoding/hex"
|
2017-11-15 01:53:23 +00:00
|
|
|
"fmt"
|
2018-03-26 19:55:32 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-11-15 01:53:23 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2016-10-25 22:57:38 +00:00
|
|
|
// TLSConfig provides TLS related configuration
|
|
|
|
type TLSConfig struct {
|
|
|
|
|
|
|
|
// EnableHTTP enabled TLS for http traffic to the Nomad server and clients
|
2019-04-04 18:16:08 +00:00
|
|
|
EnableHTTP bool `hcl:"http"`
|
2016-10-25 22:57:38 +00:00
|
|
|
|
|
|
|
// EnableRPC enables TLS for RPC and Raft traffic to the Nomad servers
|
2019-04-04 18:16:08 +00:00
|
|
|
EnableRPC bool `hcl:"rpc"`
|
2016-10-25 22:57:38 +00:00
|
|
|
|
|
|
|
// VerifyServerHostname is used to enable hostname verification of servers. This
|
2016-11-03 21:45:03 +00:00
|
|
|
// ensures that the certificate presented is valid for server.<region>.nomad
|
2016-10-25 22:57:38 +00:00
|
|
|
// This prevents a compromised client from being restarted as a server, and then
|
|
|
|
// intercepting request traffic as well as being added as a raft peer. This should be
|
|
|
|
// enabled by default with VerifyOutgoing, but for legacy reasons we cannot break
|
|
|
|
// existing clients.
|
2019-04-04 18:16:08 +00:00
|
|
|
VerifyServerHostname bool `hcl:"verify_server_hostname"`
|
2016-10-25 22:57:38 +00:00
|
|
|
|
|
|
|
// CAFile is a path to a certificate authority file. This is used with VerifyIncoming
|
|
|
|
// or VerifyOutgoing to verify the TLS connection.
|
2019-04-04 18:16:08 +00:00
|
|
|
CAFile string `hcl:"ca_file"`
|
2016-10-25 22:57:38 +00:00
|
|
|
|
|
|
|
// CertFile is used to provide a TLS certificate that is used for serving TLS connections.
|
|
|
|
// Must be provided to serve TLS connections.
|
2019-04-04 18:16:08 +00:00
|
|
|
CertFile string `hcl:"cert_file"`
|
2016-10-25 22:57:38 +00:00
|
|
|
|
2017-11-15 01:53:23 +00:00
|
|
|
// KeyLoader is a helper to dynamically reload TLS configuration
|
|
|
|
KeyLoader *KeyLoader
|
|
|
|
|
|
|
|
keyloaderLock sync.Mutex
|
|
|
|
|
2016-10-25 22:57:38 +00:00
|
|
|
// KeyFile is used to provide a TLS key that is used for serving TLS connections.
|
|
|
|
// Must be provided to serve TLS connections.
|
2019-04-04 18:16:08 +00:00
|
|
|
KeyFile string `hcl:"key_file"`
|
2017-04-26 17:58:19 +00:00
|
|
|
|
2017-10-30 15:31:55 +00:00
|
|
|
// RPCUpgradeMode should be enabled when a cluster is being upgraded
|
2017-10-25 17:59:08 +00:00
|
|
|
// to TLS. Allows servers to accept both plaintext and TLS connections and
|
|
|
|
// should only be a temporary state.
|
2019-04-04 18:16:08 +00:00
|
|
|
RPCUpgradeMode bool `hcl:"rpc_upgrade_mode"`
|
2017-10-25 17:59:08 +00:00
|
|
|
|
2017-04-28 09:45:09 +00:00
|
|
|
// Verify connections to the HTTPS API
|
2019-04-04 18:16:08 +00:00
|
|
|
VerifyHTTPSClient bool `hcl:"verify_https_client"`
|
2018-03-26 19:55:32 +00:00
|
|
|
|
|
|
|
// Checksum is a MD5 hash of the certificate CA File, Certificate file, and
|
|
|
|
// key file.
|
|
|
|
Checksum string
|
2018-05-08 20:32:07 +00:00
|
|
|
|
|
|
|
// TLSCipherSuites are operator-defined ciphers to be used in Nomad TLS
|
|
|
|
// connections
|
2019-04-04 18:16:08 +00:00
|
|
|
TLSCipherSuites string `hcl:"tls_cipher_suites"`
|
2018-05-09 20:30:02 +00:00
|
|
|
|
|
|
|
// TLSMinVersion is used to set the minimum TLS version used for TLS
|
|
|
|
// connections. Should be either "tls10", "tls11", or "tls12".
|
2019-04-04 18:16:08 +00:00
|
|
|
TLSMinVersion string `hcl:"tls_min_version"`
|
2018-05-23 17:34:53 +00:00
|
|
|
|
|
|
|
// TLSPreferServerCipherSuites controls whether the server selects the
|
|
|
|
// client's most preferred ciphersuite, or the server's most preferred
|
|
|
|
// ciphersuite. If true then the server's preference, as expressed in
|
|
|
|
// the order of elements in CipherSuites, is used.
|
2019-04-04 18:16:08 +00:00
|
|
|
TLSPreferServerCipherSuites bool `hcl:"tls_prefer_server_cipher_suites"`
|
2019-04-25 18:54:20 +00:00
|
|
|
|
|
|
|
// ExtraKeysHCL is used by hcl to surface unexpected keys
|
|
|
|
ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"`
|
2016-10-25 22:57:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 01:53:23 +00:00
|
|
|
type KeyLoader struct {
|
|
|
|
cacheLock sync.Mutex
|
|
|
|
certificate *tls.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadKeyPair reloads the TLS certificate based on the specified certificate
|
|
|
|
// and key file. If successful, stores the certificate for further use.
|
|
|
|
func (k *KeyLoader) LoadKeyPair(certFile, keyFile string) (*tls.Certificate, error) {
|
|
|
|
k.cacheLock.Lock()
|
|
|
|
defer k.cacheLock.Unlock()
|
|
|
|
|
|
|
|
// Allow downgrading
|
|
|
|
if certFile == "" && keyFile == "" {
|
|
|
|
k.certificate = nil
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.certificate = &cert
|
|
|
|
return k.certificate, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:55:11 +00:00
|
|
|
func (k *KeyLoader) GetCertificate() *tls.Certificate {
|
|
|
|
k.cacheLock.Lock()
|
|
|
|
defer k.cacheLock.Unlock()
|
|
|
|
return k.certificate
|
|
|
|
}
|
|
|
|
|
2017-11-15 01:53:23 +00:00
|
|
|
// GetOutgoingCertificate fetches the currently-loaded certificate when
|
|
|
|
// accepting a TLS connection. This currently does not consider information in
|
|
|
|
// the ClientHello and only returns the certificate that was last loaded.
|
|
|
|
func (k *KeyLoader) GetOutgoingCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
k.cacheLock.Lock()
|
|
|
|
defer k.cacheLock.Unlock()
|
|
|
|
return k.certificate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientCertificate fetches the currently-loaded certificate when the Server
|
|
|
|
// requests a certificate from the caller. This currently does not consider
|
|
|
|
// information in the ClientHello and only returns the certificate that was last
|
|
|
|
// loaded.
|
|
|
|
func (k *KeyLoader) GetClientCertificate(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
k.cacheLock.Lock()
|
|
|
|
defer k.cacheLock.Unlock()
|
|
|
|
return k.certificate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKeyLoader returns the keyloader for a TLSConfig object. If the keyloader
|
|
|
|
// has not been initialized, it will first do so.
|
|
|
|
func (t *TLSConfig) GetKeyLoader() *KeyLoader {
|
|
|
|
t.keyloaderLock.Lock()
|
|
|
|
defer t.keyloaderLock.Unlock()
|
|
|
|
|
|
|
|
// If the keyloader has not yet been initialized, do it here
|
|
|
|
if t.KeyLoader == nil {
|
|
|
|
t.KeyLoader = &KeyLoader{}
|
|
|
|
}
|
|
|
|
return t.KeyLoader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the fields of TLSConfig to another TLSConfig object. Required as
|
|
|
|
// to not copy mutexes between objects.
|
|
|
|
func (t *TLSConfig) Copy() *TLSConfig {
|
|
|
|
if t == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
new := &TLSConfig{}
|
|
|
|
new.EnableHTTP = t.EnableHTTP
|
|
|
|
new.EnableRPC = t.EnableRPC
|
|
|
|
new.VerifyServerHostname = t.VerifyServerHostname
|
|
|
|
new.CAFile = t.CAFile
|
|
|
|
new.CertFile = t.CertFile
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
// Shallow copy the key loader as its GetOutgoingCertificate method is what
|
|
|
|
// is used by the HTTP server to retrieve the certificate. If we create a new
|
|
|
|
// KeyLoader struct, the HTTP server will still be calling the old
|
|
|
|
// GetOutgoingCertificate method.
|
2017-11-15 01:53:23 +00:00
|
|
|
t.keyloaderLock.Lock()
|
2022-08-18 23:32:04 +00:00
|
|
|
new.KeyLoader = t.KeyLoader
|
2017-11-15 01:53:23 +00:00
|
|
|
t.keyloaderLock.Unlock()
|
|
|
|
|
|
|
|
new.KeyFile = t.KeyFile
|
|
|
|
new.RPCUpgradeMode = t.RPCUpgradeMode
|
|
|
|
new.VerifyHTTPSClient = t.VerifyHTTPSClient
|
2018-03-26 19:55:32 +00:00
|
|
|
|
2018-05-09 20:30:02 +00:00
|
|
|
new.TLSCipherSuites = t.TLSCipherSuites
|
|
|
|
new.TLSMinVersion = t.TLSMinVersion
|
|
|
|
|
2018-05-23 17:34:53 +00:00
|
|
|
new.TLSPreferServerCipherSuites = t.TLSPreferServerCipherSuites
|
|
|
|
|
2018-03-26 19:55:32 +00:00
|
|
|
new.SetChecksum()
|
|
|
|
|
2017-11-15 01:53:23 +00:00
|
|
|
return new
|
|
|
|
}
|
|
|
|
|
2017-11-20 15:38:46 +00:00
|
|
|
func (t *TLSConfig) IsEmpty() bool {
|
|
|
|
if t == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:05:18 +00:00
|
|
|
return !t.EnableHTTP &&
|
|
|
|
!t.EnableRPC &&
|
|
|
|
!t.VerifyServerHostname &&
|
2017-11-20 15:38:46 +00:00
|
|
|
t.CAFile == "" &&
|
|
|
|
t.CertFile == "" &&
|
|
|
|
t.KeyFile == "" &&
|
2020-12-09 19:05:18 +00:00
|
|
|
!t.VerifyHTTPSClient
|
2017-11-20 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 22:57:38 +00:00
|
|
|
// Merge is used to merge two TLS configs together
|
|
|
|
func (t *TLSConfig) Merge(b *TLSConfig) *TLSConfig {
|
2017-11-15 01:53:23 +00:00
|
|
|
result := t.Copy()
|
2016-10-25 22:57:38 +00:00
|
|
|
|
|
|
|
if b.EnableHTTP {
|
|
|
|
result.EnableHTTP = true
|
|
|
|
}
|
|
|
|
if b.EnableRPC {
|
|
|
|
result.EnableRPC = true
|
|
|
|
}
|
|
|
|
if b.VerifyServerHostname {
|
|
|
|
result.VerifyServerHostname = true
|
|
|
|
}
|
|
|
|
if b.CAFile != "" {
|
|
|
|
result.CAFile = b.CAFile
|
|
|
|
}
|
|
|
|
if b.CertFile != "" {
|
|
|
|
result.CertFile = b.CertFile
|
|
|
|
}
|
|
|
|
if b.KeyFile != "" {
|
|
|
|
result.KeyFile = b.KeyFile
|
|
|
|
}
|
2017-04-28 09:45:09 +00:00
|
|
|
if b.VerifyHTTPSClient {
|
|
|
|
result.VerifyHTTPSClient = true
|
2017-04-25 22:29:43 +00:00
|
|
|
}
|
2017-12-12 00:23:45 +00:00
|
|
|
if b.RPCUpgradeMode {
|
|
|
|
result.RPCUpgradeMode = true
|
|
|
|
}
|
2018-05-23 21:25:30 +00:00
|
|
|
if b.TLSCipherSuites != "" {
|
|
|
|
result.TLSCipherSuites = b.TLSCipherSuites
|
|
|
|
}
|
|
|
|
if b.TLSMinVersion != "" {
|
|
|
|
result.TLSMinVersion = b.TLSMinVersion
|
|
|
|
}
|
2018-05-23 17:34:53 +00:00
|
|
|
if b.TLSPreferServerCipherSuites {
|
|
|
|
result.TLSPreferServerCipherSuites = true
|
|
|
|
}
|
2017-11-15 01:53:23 +00:00
|
|
|
return result
|
2016-10-25 22:57:38 +00:00
|
|
|
}
|
2017-11-20 15:38:46 +00:00
|
|
|
|
2018-03-21 16:46:05 +00:00
|
|
|
// CertificateInfoIsEqual compares the fields of two TLS configuration objects
|
|
|
|
// for the fields that are specific to configuring a TLS connection
|
2017-12-05 00:29:43 +00:00
|
|
|
// It is possible for either the calling TLSConfig to be nil, or the TLSConfig
|
|
|
|
// that it is being compared against, so we need to handle both places. See
|
|
|
|
// server.go Reload for example.
|
2018-03-28 21:54:22 +00:00
|
|
|
func (t *TLSConfig) CertificateInfoIsEqual(newConfig *TLSConfig) (bool, error) {
|
2017-12-05 00:29:43 +00:00
|
|
|
if t == nil || newConfig == nil {
|
2018-03-28 21:54:22 +00:00
|
|
|
return t == newConfig, nil
|
2017-12-05 00:29:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:55:32 +00:00
|
|
|
if t.IsEmpty() && newConfig.IsEmpty() {
|
2018-03-28 21:54:22 +00:00
|
|
|
return true, nil
|
2018-03-28 13:41:17 +00:00
|
|
|
} else if t.IsEmpty() || newConfig.IsEmpty() {
|
2018-03-28 21:54:22 +00:00
|
|
|
return false, nil
|
2018-03-26 19:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:41:17 +00:00
|
|
|
// Set the checksum if it hasn't yet been set (this should happen when the
|
|
|
|
// config is parsed but this provides safety in depth)
|
|
|
|
if newConfig.Checksum == "" {
|
|
|
|
err := newConfig.SetChecksum()
|
|
|
|
if err != nil {
|
2018-03-28 21:54:22 +00:00
|
|
|
return false, err
|
2018-03-28 13:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Checksum == "" {
|
|
|
|
err := t.SetChecksum()
|
|
|
|
if err != nil {
|
2018-03-28 21:54:22 +00:00
|
|
|
return false, err
|
2018-03-28 13:41:17 +00:00
|
|
|
}
|
2018-03-26 19:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 21:54:22 +00:00
|
|
|
return t.Checksum == newConfig.Checksum, nil
|
2018-03-26 19:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetChecksum generates and sets the checksum for a TLS configuration
|
|
|
|
func (t *TLSConfig) SetChecksum() error {
|
|
|
|
newCertChecksum, err := createChecksumOfFiles(t.CAFile, t.CertFile, t.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Checksum = newCertChecksum
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFileChecksum(filepath string) (string, error) {
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChecksumOfFiles(inputs ...string) (string, error) {
|
|
|
|
h := md5.New()
|
|
|
|
|
|
|
|
for _, input := range inputs {
|
|
|
|
checksum, err := getFileChecksum(input)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
io.WriteString(h, checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
2017-11-20 15:38:46 +00:00
|
|
|
}
|