Skip to content

Commit 4169d1e

Browse files
committed
models: format zkclient.go & etcdclient.go
1 parent 3adf407 commit 4169d1e

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

cmd/proxy/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func AutoOnlineWithDashboard(p *proxy.Proxy, dashboard string) {
294294
log.Panicf("online proxy failed")
295295
}
296296

297-
func AutoOnlineWithCoordinator(p *proxy.Proxy, name, addr string, auth string) {
297+
func AutoOnlineWithCoordinator(p *proxy.Proxy, name, addr, auth string) {
298298
client, err := models.NewClient(name, addr, auth, time.Minute)
299299
if err != nil {
300300
log.PanicErrorf(err, "create '%s' client to '%s' failed", name, addr)

pkg/models/etcd/etcdclient.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ func New(addrlist string, auth string, timeout time.Duration) (*Client, error) {
4545
timeout = time.Second * 5
4646
}
4747

48-
config := client.Config{
48+
config := client.Config{
4949
Endpoints: endpoints, Transport: client.DefaultTransport,
5050
HeaderTimeoutPerRequest: time.Second * 5,
5151
}
5252

5353
if auth != "" {
54-
a := strings.Split(auth, ":")
55-
config.Username = a[0]
56-
config.Password = a[1]
54+
split := strings.Split(auth, ":")
55+
if len(split) != 2 || split[0] == "" {
56+
return nil, errors.Errorf("invalid auth")
57+
}
58+
config.Username = split[0]
59+
config.Password = split[1]
5760
}
5861

5962
c, err := client.New(config)

pkg/models/zk/zkclient.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type Client struct {
2828
conn *zk.Conn
2929

3030
addrlist string
31-
auth string
31+
username string
32+
password string
3233
timeout time.Duration
3334

3435
logger *zkLogger
@@ -55,9 +56,17 @@ func NewWithLogfunc(addrlist string, auth string, timeout time.Duration, logfunc
5556
timeout = time.Second * 5
5657
}
5758
c := &Client{
58-
addrlist: addrlist, auth: auth, timeout: timeout,
59+
addrlist: addrlist, timeout: timeout,
5960
logger: &zkLogger{logfunc},
6061
}
62+
if auth != "" {
63+
split := strings.Split(auth, ":")
64+
if len(split) != 2 || split[0] == "" {
65+
return nil, errors.Errorf("invalid auth")
66+
}
67+
c.username = split[0]
68+
c.password = split[1]
69+
}
6170
if err := c.reset(); err != nil {
6271
return nil, err
6372
}
@@ -78,8 +87,9 @@ func (c *Client) reset() error {
7887

7988
c.logger.Printf("zkclient setup new connection to %s", c.addrlist)
8089

81-
if c.auth != "" {
82-
if err := c.conn.AddAuth("digest", []byte(c.auth)); err != nil {
90+
if c.username != "" {
91+
var auth = fmt.Sprintf("%s:%s", c.username, c.password)
92+
if err := c.conn.AddAuth("digest", []byte(auth)); err != nil {
8393
return errors.Trace(err)
8494
}
8595
}
@@ -162,15 +172,13 @@ func (c *Client) mkdir(conn *zk.Conn, path string) error {
162172
if err := c.mkdir(conn, filepath.Dir(path)); err != nil {
163173
return err
164174
}
165-
var acl []zk.ACL
166-
167-
if c.auth != "" {
168-
auth := strings.Split(c.auth, ":")
169-
acl = zk.DigestACL(zk.PermAll, auth[0], auth[1])
170-
} else {
171-
acl = zk.WorldACL(zk.PermAll)
172-
}
173-
_, err := conn.Create(path, []byte{}, 0, acl)
175+
_, err := conn.Create(path, []byte{}, 0, func() []zk.ACL {
176+
const perm = zk.PermAll
177+
if c.username != "" {
178+
return zk.DigestACL(perm, c.username, c.password)
179+
}
180+
return zk.WorldACL(perm)
181+
}())
174182
if err != nil && errors.NotEqual(err, zk.ErrNodeExists) {
175183
return errors.Trace(err)
176184
}
@@ -228,16 +236,13 @@ func (c *Client) create(conn *zk.Conn, path string, data []byte, flag int32) (st
228236
if err := c.mkdir(conn, filepath.Dir(path)); err != nil {
229237
return "", err
230238
}
231-
232-
var acl []zk.ACL
233-
234-
if c.auth != "" {
235-
auth := strings.Split(c.auth, ":")
236-
acl = zk.DigestACL(zk.PermAdmin|zk.PermRead|zk.PermWrite, auth[0], auth[1])
237-
} else {
238-
acl = zk.WorldACL(zk.PermAdmin|zk.PermRead|zk.PermWrite)
239-
}
240-
p, err := conn.Create(path, data, flag, acl)
239+
p, err := conn.Create(path, data, flag, func() []zk.ACL {
240+
const perm = zk.PermAdmin | zk.PermRead | zk.PermWrite
241+
if c.username != "" {
242+
return zk.DigestACL(perm, c.username, c.password)
243+
}
244+
return zk.WorldACL(perm)
245+
}())
241246
if err != nil {
242247
return "", errors.Trace(err)
243248
}

0 commit comments

Comments
 (0)