Skip to content

Commit

Permalink
feat(registry): contrib/registry/zookeeper add digest acl support
Browse files Browse the repository at this point in the history
  • Loading branch information
youzhixiaomutou committed Apr 29, 2022
1 parent 60465cc commit cb460c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion contrib/registry/zookeeper/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type options struct {
ctx context.Context
rootPath string
timeout time.Duration
user string
password string
}

// WithContext with registry context.
Expand All @@ -42,6 +44,20 @@ func WithTimeout(timeout time.Duration) Option {
return func(o *options) { o.timeout = timeout }
}

// WithUser with registry user.
func WithUser(user string) Option {
return func(o *options) {
o.user = user
}
}

// WithPassword with registry password.
func WithPassword(password string) Option {
return func(o *options) {
o.password = password
}
}

// Registry is consul registry
type Registry struct {
opts *options
Expand All @@ -63,6 +79,12 @@ func New(zkServers []string, opts ...Option) (*Registry, error) {
if err != nil {
return nil, err
}
if len(options.user) > 0 && len(options.password) > 0 {
err = conn.AddAuth("digest", []byte(options.user+":"+options.password))
if err != nil {
return nil, err
}
}
return &Registry{
opts: options,
conn: conn,
Expand Down Expand Up @@ -182,7 +204,12 @@ func (r *Registry) ensureName(path string, data []byte, flags int32) error {
return err
}
if !exists {
_, err := r.conn.Create(path, data, flags, zk.WorldACL(zk.PermAll))
var err error
if len(r.opts.user) > 0 && len(r.opts.password) > 0 {
_, err = r.conn.Create(path, data, flags, zk.DigestACL(zk.PermAll, r.opts.user, r.opts.password))
} else {
_, err = r.conn.Create(path, data, flags, zk.WorldACL(zk.PermAll))
}
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/zookeeper/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestRegistry(t *testing.T) {
Endpoints: []string{"http://127.0.0.1:1111"},
}

r, _ := New([]string{"127.0.0.1:2181"})
r, _ := New([]string{"127.0.0.1:2181"}, WithUser("username"), WithPassword("password"))

w, err := r.Watch(ctx, s.Name)
if err != nil {
Expand Down

0 comments on commit cb460c2

Please sign in to comment.