2017-10-03 19:03:58 +05:30
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 22:20:22 +05:30
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.etcd.io/etcd/clientv3/namespace"
|
|
|
|
"go.etcd.io/etcd/pkg/transport"
|
2018-09-03 12:14:44 +05:30
|
|
|
|
2019-02-22 17:49:23 +05:30
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-09-03 12:14:44 +05:30
|
|
|
"github.com/dexidp/dex/storage"
|
2017-10-03 19:03:58 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultDialTimeout = 2 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// SSL represents SSL options for etcd databases.
|
|
|
|
type SSL struct {
|
2017-11-06 20:16:18 +05:30
|
|
|
ServerName string `json:"serverName" yaml:"serverName"`
|
|
|
|
CAFile string `json:"caFile" yaml:"caFile"`
|
|
|
|
KeyFile string `json:"keyFile" yaml:"keyFile"`
|
|
|
|
CertFile string `json:"certFile" yaml:"certFile"`
|
2017-10-03 19:03:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Etcd options for connecting to etcd databases.
|
|
|
|
// If you are using a shared etcd cluster for storage, it might be useful to
|
|
|
|
// configure an etcd namespace either via Namespace field or using `etcd grpc-proxy
|
|
|
|
// --namespace=<prefix>`
|
|
|
|
type Etcd struct {
|
2017-11-06 20:16:18 +05:30
|
|
|
Endpoints []string `json:"endpoints" yaml:"endpoints"`
|
|
|
|
Namespace string `json:"namespace" yaml:"namespace"`
|
|
|
|
Username string `json:"username" yaml:"username"`
|
|
|
|
Password string `json:"password" yaml:"password"`
|
|
|
|
SSL SSL `json:"ssl" yaml:"ssl"`
|
2017-10-03 19:03:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new storage implementation backed by Etcd
|
2019-02-22 17:49:23 +05:30
|
|
|
func (p *Etcd) Open(logger log.Logger) (storage.Storage, error) {
|
2017-10-03 19:03:58 +05:30
|
|
|
return p.open(logger)
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:49:23 +05:30
|
|
|
func (p *Etcd) open(logger log.Logger) (*conn, error) {
|
2017-10-03 19:03:58 +05:30
|
|
|
cfg := clientv3.Config{
|
|
|
|
Endpoints: p.Endpoints,
|
2018-08-03 02:37:23 +05:30
|
|
|
DialTimeout: defaultDialTimeout,
|
2017-10-03 19:03:58 +05:30
|
|
|
Username: p.Username,
|
|
|
|
Password: p.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfgtls *transport.TLSInfo
|
|
|
|
tlsinfo := transport.TLSInfo{}
|
|
|
|
if p.SSL.CertFile != "" {
|
|
|
|
tlsinfo.CertFile = p.SSL.CertFile
|
|
|
|
cfgtls = &tlsinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SSL.KeyFile != "" {
|
|
|
|
tlsinfo.KeyFile = p.SSL.KeyFile
|
|
|
|
cfgtls = &tlsinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SSL.CAFile != "" {
|
2020-01-30 22:20:22 +05:30
|
|
|
tlsinfo.TrustedCAFile = p.SSL.CAFile
|
2017-10-03 19:03:58 +05:30
|
|
|
cfgtls = &tlsinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SSL.ServerName != "" {
|
|
|
|
tlsinfo.ServerName = p.SSL.ServerName
|
|
|
|
cfgtls = &tlsinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfgtls != nil {
|
|
|
|
clientTLS, err := cfgtls.ClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.TLS = clientTLS
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(p.Namespace) > 0 {
|
|
|
|
db.KV = namespace.NewKV(db.KV, p.Namespace)
|
|
|
|
}
|
|
|
|
c := &conn{
|
|
|
|
db: db,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|