Skip to content

Commit

Permalink
etcd client with auth
Browse files Browse the repository at this point in the history
  • Loading branch information
soyking committed Nov 3, 2016
1 parent 8a885d8 commit 3daeee3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
1 change: 1 addition & 0 deletions conf/config.default.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[app]
port=8080
auth=false

[etcd]
root_key=e3w_test
Expand Down
2 changes: 2 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type Config struct {
Port string
Auth bool
EtcdRootKey string
EtcdEndPoints []string
EtcdUsername string
Expand All @@ -22,6 +23,7 @@ func Init(filepath string) (*Config, error) {

appSec := cfg.Section("app")
c.Port = appSec.Key("port").Value()
c.Auth = appSec.Key("auth").MustBool()

etcdSec := cfg.Section("etcd")
c.EtcdRootKey = etcdSec.Key("root_key").Value()
Expand Down
36 changes: 36 additions & 0 deletions e3ch/e3ch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package e3ch

import (
"github.com/coreos/etcd/clientv3"
"github.com/soyking/e3ch"
"github.com/soyking/e3w/conf"
)

func NewE3chClient(config *conf.Config) (*client.EtcdHRCHYClient, error) {
clt, err := clientv3.New(clientv3.Config{
Endpoints: config.EtcdEndPoints,
Username: config.EtcdUsername,
Password: config.EtcdPassword,
})
if err != nil {
return nil, err
}

client, err := client.New(clt, config.EtcdRootKey)
if err != nil {
return nil, err
}
return client, client.FormatRootKey()
}

func CloneE3chClient(username, password string, client *client.EtcdHRCHYClient) (*client.EtcdHRCHYClient, error) {
clt, err := clientv3.New(clientv3.Config{
Endpoints: client.EtcdClient().Endpoints(),
Username: username,
Password: password,
})
if err != nil {
return nil, err
}
return client.Clone(clt), nil
}
24 changes: 3 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package main
import (
"flag"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/gin-gonic/gin"
"github.com/soyking/e3ch"
"github.com/soyking/e3w/conf"
"github.com/soyking/e3w/e3ch"
"github.com/soyking/e3w/routers"
"os"
)
Expand All @@ -33,35 +32,18 @@ func init() {
}
}

func initClient(config *conf.Config) (*clientv3.Client, *client.EtcdHRCHYClient, error) {
clt, err := clientv3.New(clientv3.Config{
Endpoints: config.EtcdEndPoints,
Username: config.EtcdUsername,
Password: config.EtcdPassword,
})
if err != nil {
return nil, nil, err
}

client, err := client.New(clt, config.EtcdRootKey)
if err != nil {
return nil, nil, err
}
return clt, client, client.FormatRootKey()
}

func main() {
config, err := conf.Init(configFilepath)
if err != nil {
panic(err)
}

etcdClt, client, err := initClient(config)
client, err := e3ch.NewE3chClient(config)
if err != nil {
panic(err)
}

router := gin.Default()
routers.InitRouters(router, etcdClt, client)
routers.InitRouters(router, config, client)
router.Run(":" + config.Port)
}
26 changes: 21 additions & 5 deletions routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/gin-gonic/gin"
"github.com/soyking/e3ch"
"github.com/soyking/e3w/conf"
"github.com/soyking/e3w/e3ch"
)

const (
ETCD_USERNAME_HEADER = "X-Etcd-Username"
ETCD_PASSWORD_HEADER = "X-Etcd-Password"
)

type e3chHandler func(*gin.Context, *client.EtcdHRCHYClient) (interface{}, error)

type groupHandler func(e3chHandler) respHandler

func withE3chGroup(e3chClt *client.EtcdHRCHYClient) groupHandler {
func withE3chGroup(e3chClt *client.EtcdHRCHYClient, config *conf.Config) groupHandler {
return func(h e3chHandler) respHandler {
return func(c *gin.Context) (interface{}, error) {
// TODO: make new client
return h(c, e3chClt)
clt := e3chClt
if config.Auth {
var err error
username := c.Request.Header.Get(ETCD_USERNAME_HEADER)
password := c.Request.Header.Get(ETCD_PASSWORD_HEADER)
clt, err = e3ch.CloneE3chClient(username, password, e3chClt)
if err != nil {
return nil, err
}
}
return h(c, clt)
}
}
}
Expand All @@ -27,13 +43,13 @@ func etcdWrapper(h etcdHandler) e3chHandler {
}
}

func InitRouters(g *gin.Engine, etcdClt *clientv3.Client, client *client.EtcdHRCHYClient) {
func InitRouters(g *gin.Engine, config *conf.Config, e3chClt *client.EtcdHRCHYClient) {
g.GET("/", func(c *gin.Context) {
c.File("./static/dist/index.html")
})
g.Static("/public", "./static/dist")

e3chGroup := withE3chGroup(client)
e3chGroup := withE3chGroup(e3chClt, config)

// key/value actions
g.GET("/kv/*key", resp(e3chGroup(getKeyHandler)))
Expand Down

0 comments on commit 3daeee3

Please sign in to comment.