-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add common TLS configuration #1838
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,16 @@ import ( | |
) | ||
|
||
const ( | ||
tlsEnabled = "tls" | ||
tlsCA = "tls.ca" | ||
tlsCert = "tls.cert" | ||
tlsKey = "tls.key" | ||
tlsServerName = "tls.server-name" | ||
tlsClientCA = "tls.client-ca" | ||
tlsClientCAOld = "tls.client.ca" | ||
tlsPrefix = ".tls" | ||
yurishkuro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tlsEnabledOld = tlsPrefix | ||
tlsEnabled = tlsPrefix + ".enabled" | ||
tlsCA = tlsPrefix + ".ca" | ||
tlsCert = tlsPrefix + ".cert" | ||
tlsKey = tlsPrefix + ".key" | ||
tlsServerName = tlsPrefix + ".server-name" | ||
tlsClientCA = tlsPrefix + ".client-ca" | ||
tlsClientCAOld = tlsPrefix + ".client.ca" | ||
tlsSkipHostVerify = tlsPrefix + ".skip-host-verify" | ||
) | ||
|
||
// ClientFlagsConfig describes which CLI flags for TLS client should be generated. | ||
|
@@ -48,19 +51,22 @@ type ServerFlagsConfig struct { | |
func (c ClientFlagsConfig) AddFlags(flags *flag.FlagSet) { | ||
if c.ShowEnabled { | ||
flags.Bool(c.Prefix+tlsEnabled, false, "Enable TLS when talking to the remote server(s)") | ||
flags.Bool(c.Prefix+tlsEnabledOld, false, "(deprecated) see --"+c.Prefix+tlsEnabled) | ||
} | ||
flags.String(c.Prefix+tlsCA, "", "Path to a TLS CA (Certification Authority) file used to verify the remote server(s) (by default will use the system truststore)") | ||
flags.String(c.Prefix+tlsCert, "", "Path to a TLS Certificate file, used to identify this process to the remote server(s)") | ||
flags.String(c.Prefix+tlsKey, "", "Path to a TLS Private Key file, used to identify this process to the remote server(s)") | ||
if c.ShowServerName { | ||
flags.String(c.Prefix+tlsServerName, "", "Override the TLS server name we expect in the certificate of the remove server(s)") | ||
} | ||
flags.Bool(c.Prefix+tlsSkipHostVerify, false, "(insecure) Skip server's certificate chain and host name verification") | ||
} | ||
|
||
// AddFlags adds flags for TLS to the FlagSet. | ||
func (c ServerFlagsConfig) AddFlags(flags *flag.FlagSet) { | ||
if c.ShowEnabled { | ||
flags.Bool(c.Prefix+tlsEnabled, false, "Enable TLS on the server") | ||
flags.Bool(c.Prefix+tlsEnabledOld, false, "(deprecated) see --"+c.Prefix+tlsEnabled) | ||
} | ||
flags.String(c.Prefix+tlsCert, "", "Path to a TLS Certificate file, used to identify this server to clients") | ||
flags.String(c.Prefix+tlsKey, "", "Path to a TLS Private Key file, used to identify this server to clients") | ||
|
@@ -73,13 +79,18 @@ func (c ClientFlagsConfig) InitFromViper(v *viper.Viper) Options { | |
var p Options | ||
if c.ShowEnabled { | ||
p.Enabled = v.GetBool(c.Prefix + tlsEnabled) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line not necessary |
||
if !p.Enabled { | ||
p.Enabled = v.GetBool(c.Prefix + tlsEnabledOld) | ||
} | ||
} | ||
p.CAPath = v.GetString(c.Prefix + tlsCA) | ||
p.CertPath = v.GetString(c.Prefix + tlsCert) | ||
p.KeyPath = v.GetString(c.Prefix + tlsKey) | ||
if c.ShowServerName { | ||
p.ServerName = v.GetString(c.Prefix + tlsServerName) | ||
} | ||
p.SkipHostVerify = v.GetBool(c.Prefix + tlsSkipHostVerify) | ||
return p | ||
} | ||
|
||
|
@@ -88,6 +99,10 @@ func (c ServerFlagsConfig) InitFromViper(v *viper.Viper) Options { | |
var p Options | ||
if c.ShowEnabled { | ||
p.Enabled = v.GetBool(c.Prefix + tlsEnabled) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no blank line |
||
if !p.Enabled { | ||
p.Enabled = v.GetBool(c.Prefix + tlsEnabledOld) | ||
} | ||
} | ||
p.CertPath = v.GetString(c.Prefix + tlsCert) | ||
p.KeyPath = v.GetString(c.Prefix + tlsKey) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,13 @@ import ( | |
|
||
// Options describes the configuration properties for TLS Connections. | ||
type Options struct { | ||
Enabled bool | ||
CAPath string | ||
CertPath string | ||
KeyPath string | ||
ServerName string // only for client-side TLS config | ||
ClientCAPath string // only for server-side TLS config for client auth | ||
Enabled bool | ||
CAPath string | ||
CertPath string | ||
KeyPath string | ||
ServerName string // only for client-side TLS config | ||
ClientCAPath string // only for server-side TLS config for client auth | ||
SkipHostVerify bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On line 50 add |
||
} | ||
|
||
var systemCertPool = x509.SystemCertPool // to allow overriding in unit test | ||
|
@@ -42,10 +43,11 @@ func (p Options) Config() (*tls.Config, error) { | |
if err != nil { | ||
return nil, errors.Wrap(err, "failed to load CA CertPool") | ||
} | ||
|
||
// #nosec G402 | ||
tlsCfg := &tls.Config{ | ||
RootCAs: certPool, | ||
ServerName: p.ServerName, | ||
RootCAs: certPool, | ||
ServerName: p.ServerName, | ||
InsecureSkipVerify: p.SkipHostVerify, | ||
} | ||
|
||
if (p.CertPath == "" && p.KeyPath != "") || (p.CertPath != "" && p.KeyPath == "") { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ package config | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"io/ioutil" | ||
"net/http" | ||
"path/filepath" | ||
|
@@ -32,6 +31,7 @@ import ( | |
"github.com/uber/jaeger-lib/metrics" | ||
"go.uber.org/zap" | ||
|
||
"github.com/jaegertracing/jaeger/pkg/config/tlscfg" | ||
"github.com/jaegertracing/jaeger/pkg/es" | ||
"github.com/jaegertracing/jaeger/pkg/es/wrapper" | ||
"github.com/jaegertracing/jaeger/storage/spanstore" | ||
|
@@ -60,21 +60,12 @@ type Configuration struct { | |
AllTagsAsFields bool | ||
TagDotReplacement string | ||
Enabled bool | ||
TLS TLSConfig | ||
TLS tlscfg.Options | ||
UseReadWriteAliases bool | ||
CreateIndexTemplates bool | ||
Version uint | ||
} | ||
|
||
// TLSConfig describes the configuration properties to connect tls enabled ElasticSearch cluster | ||
type TLSConfig struct { | ||
Enabled bool | ||
SkipHostVerify bool | ||
CertPath string | ||
KeyPath string | ||
CaPath string | ||
} | ||
|
||
// ClientBuilder creates new es.Client | ||
type ClientBuilder interface { | ||
NewClient(logger *zap.Logger, metricsFactory metrics.Factory) (es.Client, error) | ||
|
@@ -292,7 +283,7 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp | |
} | ||
options = append(options, elastic.SetHttpClient(httpClient)) | ||
if c.TLS.Enabled { | ||
ctlsConfig, err := c.TLS.createTLSConfig() | ||
ctlsConfig, err := c.TLS.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -305,13 +296,12 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp | |
// #nosec G402 | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.TLS.SkipHostVerify}, | ||
} | ||
if c.TLS.CaPath != "" { | ||
ctls := &TLSConfig{CaPath: c.TLS.CaPath} | ||
ca, err := ctls.loadCertificate() | ||
if c.TLS.CAPath != "" { | ||
config, err := c.TLS.Config() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we want to load only CA cert. Does it load only CA. What happens is client cert or key are not specified? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this does the right thing, although it will try to load SystemCertPool if |
||
if err != nil { | ||
return nil, err | ||
} | ||
httpTransport.TLSClientConfig.RootCAs = ca | ||
httpTransport.TLSClientConfig = config | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what was this code doing? It seems deleting it will not preserve some functionality There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you're correct. adding it back. |
||
|
||
token := "" | ||
|
@@ -340,45 +330,6 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp | |
return options, nil | ||
} | ||
|
||
// createTLSConfig creates TLS Configuration to connect with ES Cluster. | ||
func (tlsConfig *TLSConfig) createTLSConfig() (*tls.Config, error) { | ||
rootCerts, err := tlsConfig.loadCertificate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientPrivateKey, err := tlsConfig.loadPrivateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// #nosec | ||
return &tls.Config{ | ||
RootCAs: rootCerts, | ||
Certificates: []tls.Certificate{*clientPrivateKey}, | ||
InsecureSkipVerify: tlsConfig.SkipHostVerify, | ||
}, nil | ||
|
||
} | ||
|
||
// loadCertificate is used to load root certification | ||
func (tlsConfig *TLSConfig) loadCertificate() (*x509.CertPool, error) { | ||
caCert, err := ioutil.ReadFile(tlsConfig.CaPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
certificates := x509.NewCertPool() | ||
certificates.AppendCertsFromPEM(caCert) | ||
return certificates, nil | ||
} | ||
|
||
// loadPrivateKey is used to load the private certificate and key for TLS | ||
func (tlsConfig *TLSConfig) loadPrivateKey() (*tls.Certificate, error) { | ||
privateKey, err := tls.LoadX509KeyPair(tlsConfig.CertPath, tlsConfig.KeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &privateKey, nil | ||
} | ||
|
||
// TokenAuthTransport | ||
type tokenAuthTransport struct { | ||
token string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change necessary? If we're changing this file, we need to run
dep ensure --update
and also check-in the lock file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is - there was a bug in Viper where
isSet
was returning true even in cases where it was not set, but there was a default value. This was fixed in 1.5