|
|
@ -1,9 +1,15 @@
|
|
|
|
package internal
|
|
|
|
package internal
|
|
|
|
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
import (
|
|
|
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
accounts = make(map[string]Account)
|
|
|
|
accounts = make(map[string]Account)
|
|
|
|
|
|
|
|
tlsConfigs = make(map[string]*tls.Config)
|
|
|
|
lock sync.RWMutex
|
|
|
|
lock sync.RWMutex
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -24,6 +30,32 @@ func AddAccount(endpoints []string, user, pass string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddTLS adds the tls cert files for the given etcd cluster.
|
|
|
|
|
|
|
|
func AddTLS(endpoints []string, certFile, certKeyFile, caFile string, insecureSkipVerify bool) error {
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(certFile, certKeyFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
caData, err := ioutil.ReadFile(caFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
|
|
|
|
pool.AppendCertsFromPEM(caData)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
tlsConfigs[getClusterKey(endpoints)] = &tls.Config{
|
|
|
|
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
|
|
|
|
RootCAs: pool,
|
|
|
|
|
|
|
|
InsecureSkipVerify: insecureSkipVerify,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetAccount gets the username/password for the given etcd cluster.
|
|
|
|
// GetAccount gets the username/password for the given etcd cluster.
|
|
|
|
func GetAccount(endpoints []string) (Account, bool) {
|
|
|
|
func GetAccount(endpoints []string) (Account, bool) {
|
|
|
|
lock.RLock()
|
|
|
|
lock.RLock()
|
|
|
@ -32,3 +64,12 @@ func GetAccount(endpoints []string) (Account, bool) {
|
|
|
|
account, ok := accounts[getClusterKey(endpoints)]
|
|
|
|
account, ok := accounts[getClusterKey(endpoints)]
|
|
|
|
return account, ok
|
|
|
|
return account, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetTLS gets the tls config for the given etcd cluster.
|
|
|
|
|
|
|
|
func GetTLS(endpoints []string) (*tls.Config, bool) {
|
|
|
|
|
|
|
|
lock.RLock()
|
|
|
|
|
|
|
|
defer lock.RUnlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfg, ok := tlsConfigs[getClusterKey(endpoints)]
|
|
|
|
|
|
|
|
return cfg, ok
|
|
|
|
|
|
|
|
}
|
|
|
|