-
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
Fixed "Using Client Certificate for Elastic Search Authentication" #1139
Changes from 1 commit
1786537
cb7fc0c
cb489f0
e299315
b915241
9b0050e
1fe7a51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Chun Lin Yang <clyang@cn.ibm.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,11 +50,11 @@ type Configuration struct { | |
TagsFilePath string | ||
AllTagsAsFields bool | ||
TagDotReplacement string | ||
TLS TLS | ||
TLS TLSConfig | ||
} | ||
|
||
// TLS Config | ||
type TLS struct { | ||
// TLSConfig describes the configuration properties to connect tls enabled ElasticSearch cluster | ||
type TLSConfig struct { | ||
Enabled bool | ||
CertPath string | ||
KeyPath string | ||
|
@@ -203,37 +203,34 @@ func (c *Configuration) GetTagDotReplacement() string { | |
|
||
// GetConfigs wraps the configs to feed to the ElasticSearch client init | ||
func (c *Configuration) GetConfigs(logger *zap.Logger) []elastic.ClientOptionFunc { | ||
options := make([]elastic.ClientOptionFunc, 3) | ||
options := make([]elastic.ClientOptionFunc, 0) | ||
options = append(options, elastic.SetURL(c.Servers...), elastic.SetSniff(c.Sniffer)) | ||
if c.TLS.Enabled { | ||
tlsConfig, err := c.CreateTLSConfig(logger) | ||
ctlsConfig, err := c.TLS.createTLSConfig(logger) | ||
if err != nil { | ||
return nil | ||
} | ||
httpClient := &http.Client{ | ||
Timeout: c.Timeout, | ||
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. Seems like this is something extra, but I think it's ok to have it here. 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. This is only being applied when TLS is enabled. Can you make it available for every case? 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. @pavolloffay Yes. This is only being applied for httpclient which is being used by TLS case. I cannot find where to add for every case. we use 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. So the driver does not use the client when tls is disabled? Something like this would not work?
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. for elastic v5, if no HttpClient is configured, then http.DefaultClient is used. so your suggestion should be working. I will have a test later. Thanks. |
||
Transport: &http.Transport{ | ||
TLSClientConfig: tlsConfig, | ||
TLSClientConfig: ctlsConfig, | ||
}, | ||
} | ||
options[0] = elastic.SetHttpClient(httpClient) | ||
options[1] = elastic.SetURL(c.Servers...) | ||
options[2] = elastic.SetSniff(c.Sniffer) | ||
return options | ||
options = append(options, elastic.SetHttpClient(httpClient)) | ||
} else { | ||
options = append(options, elastic.SetBasicAuth(c.Username, c.Password)) | ||
} | ||
clyang82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options[0] = elastic.SetURL(c.Servers...) | ||
options[1] = elastic.SetBasicAuth(c.Username, c.Password) | ||
options[2] = elastic.SetSniff(c.Sniffer) | ||
return options | ||
} | ||
|
||
// CreateTLSConfig creates TLS Configuration to connect with ES Cluster. | ||
func (c *Configuration) CreateTLSConfig(logger *zap.Logger) (*tls.Config, error) { | ||
rootCerts, err := c.LoadCertificatesFrom() | ||
// createTLSConfig creates TLS Configuration to connect with ES Cluster. | ||
func (tlsConfig *TLSConfig) createTLSConfig(logger *zap.Logger) (*tls.Config, error) { | ||
rootCerts, err := tlsConfig.loadCertificate() | ||
if err != nil { | ||
logger.Fatal("Couldn't load root certificate", zap.Error(err)) | ||
clyang82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if len(c.TLS.CertPath) > 0 && len(c.TLS.KeyPath) > 0 { | ||
clientPrivateKey, err := c.LoadPrivateKeyFrom() | ||
if len(tlsConfig.CertPath) > 0 && len(tlsConfig.KeyPath) > 0 { | ||
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. why is this check needed? If it fails you return nil, so shouldn't it be an error to set 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. removed |
||
clientPrivateKey, err := tlsConfig.loadPrivateKeyFrom() | ||
if err != nil { | ||
logger.Fatal("Couldn't setup client authentication", zap.Error(err)) | ||
} | ||
|
@@ -245,9 +242,9 @@ func (c *Configuration) CreateTLSConfig(logger *zap.Logger) (*tls.Config, error) | |
return nil, err | ||
} | ||
|
||
// LoadCertificatesFrom is used to load root certification | ||
func (c *Configuration) LoadCertificatesFrom() (*x509.CertPool, error) { | ||
caCert, err := ioutil.ReadFile(c.TLS.CaPath) | ||
// 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 | ||
} | ||
|
@@ -256,9 +253,9 @@ func (c *Configuration) LoadCertificatesFrom() (*x509.CertPool, error) { | |
return certificates, nil | ||
} | ||
|
||
// LoadPrivateKeyFrom is used to load the private certificate and key for TLS | ||
func (c *Configuration) LoadPrivateKeyFrom() (*tls.Certificate, error) { | ||
privateKey, err := tls.LoadX509KeyPair(c.TLS.CertPath, c.TLS.KeyPath) | ||
// loadPrivateKeyFrom is used to load the private certificate and key for TLS | ||
func (tlsConfig *TLSConfig) loadPrivateKeyFrom() (*tls.Certificate, error) { | ||
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. nit: remove "From" suffix 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. OK |
||
privateKey, err := tls.LoadX509KeyPair(tlsConfig.CertPath, tlsConfig.KeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
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.
can combine
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.
Thanks