add config support for client key and cert

This commit is contained in:
Mothiki 2017-12-08 16:00:00 -08:00
parent beb8edc35f
commit 4b1150c3f3
6 changed files with 94 additions and 25 deletions

View file

@ -86,6 +86,7 @@ type RealisConfig struct {
logger Logger
InsecureSkipVerify bool
certspath string
clientkey, clientcert string
}
type Backoff struct {
@ -172,6 +173,12 @@ func Certspath(certspath string) ClientOption {
}
}
func ClientCerts(clientKey, clientCert string) ClientOption {
return func(config *RealisConfig) {
config.clientkey, config.clientcert = clientKey, clientCert
}
}
// Using the word set to avoid name collision with Interface
func SetLogger(l Logger) ClientOption {
return func(config *RealisConfig) {
@ -324,18 +331,32 @@ func defaultTTransport(urlstr string, timeoutms int, config *RealisConfig) (thri
}
var transport http.Transport
if config != nil {
tlsConfig:= &tls.Config{}
tlsConfig := &tls.Config{}
if config.InsecureSkipVerify {
tlsConfig.InsecureSkipVerify = true
}
if config.certspath != "" {
rootCAs, err := Getcerts(config.certspath)
rootCAs, err := Getcerts("examples/certs")
if err != nil {
fmt.Println("error occured couldn't fetch certs")
config.logger.Println("error occured couldn't fetch certs")
return nil, err
}
tlsConfig.RootCAs = rootCAs
}
if config.clientkey != "" && config.clientcert == "" {
return nil, fmt.Errorf("have to provide both client key,cert. Only client key provided ")
}
if config.clientkey == "" && config.clientcert != "" {
return nil, fmt.Errorf("have to provide both client key,cert. Only client cert provided ")
}
if config.clientkey != "" && config.clientcert != "" {
cert, err := tls.LoadX509KeyPair(config.clientcert, config.clientkey)
if err != nil {
config.logger.Println("error occured loading client certs and keys")
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
transport.TLSClientConfig = tlsConfig
}
@ -417,7 +438,7 @@ func (r *realisClient) ReestablishConn() error {
//Re-establish using cluster object.
url, err = LeaderFromZK(*r.config.cluster)
if err != nil {
fmt.Errorf("LeaderFromZK error: %+v\n ", err)
r.config.logger.Println("LeaderFromZK error: %+v\n ", err)
}
r.logger.Println("ReestablishConn url: ", url)
if r.config.jsonTransport {
@ -898,7 +919,6 @@ func (r *realisClient) GetTaskStatus(query *aurora.TaskQuery) (tasks []*aurora.S
retryErr := ExponentialBackoff(*r.config.backoff, func() (bool, error) {
resp, clientErr = CheckAndRetryConn(r, func() (*aurora.Response, error) {
fmt.Println(clientErr)
return r.client.GetTasksStatus(query)
})
if clientErr != nil && clientErr.Error() == RetryConnErr.Error() {