Skip to content

Commit

Permalink
add tls support client
Browse files Browse the repository at this point in the history
  • Loading branch information
soyking committed Oct 29, 2017
1 parent 9049b3a commit cedd972
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion conf/config.default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ root_key=e3w_test
dir_value=
addr=etcd:2379,etcd:22379,etcd:32379
username=
password=
password=
cert_file=
key_file=
ca_file=
6 changes: 6 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Config struct {
EtcdEndPoints []string
EtcdUsername string
EtcdPassword string
CertFile string
KeyFile string
CAFile string
}

func Init(filepath string) (*Config, error) {
Expand All @@ -32,6 +35,9 @@ func Init(filepath string) (*Config, error) {
c.EtcdEndPoints = etcdSec.Key("addr").Strings(",")
c.EtcdUsername = etcdSec.Key("username").Value()
c.EtcdPassword = etcdSec.Key("password").Value()
c.CertFile = etcdSec.Key("cert_file").Value()
c.KeyFile = etcdSec.Key("key_file").Value()
c.CAFile = etcdSec.Key("ca_file").Value()

return c, nil
}
17 changes: 17 additions & 0 deletions e3ch/e3ch.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package e3ch

import (
"crypto/tls"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
"github.com/soyking/e3ch"
"github.com/soyking/e3w/conf"
)

func NewE3chClient(config *conf.Config) (*client.EtcdHRCHYClient, error) {
var tlsConfig *tls.Config
var err error
if config.CertFile != "" && config.KeyFile != "" && config.CAFile != "" {
tlsInfo := transport.TLSInfo{
CertFile: config.CertFile,
KeyFile: config.KeyFile,
TrustedCAFile: config.CAFile,
}
tlsConfig, err = tlsInfo.ClientConfig()
if err != nil {
return nil, err
}
}

clt, err := clientv3.New(clientv3.Config{
Endpoints: config.EtcdEndPoints,
Username: config.EtcdUsername,
Password: config.EtcdPassword,
TLS: tlsConfig,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit cedd972

Please sign in to comment.