Repository: go-kratos/kratos
Pull request #1964 (2022-04-29 06:12:50)

feat(registry): contrib/registry/zookeeper add digest acl support
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a conventional commits format title: `<type>[optional scope]: <description>`
    
    Some suggestion on <type>:

    fix: A bug fix
    feat: A new feature
    test: Adding missing tests or correcting existing tests
    refactor: A code change that neither fixes a bug nor adds a feature
    break: Changes has break change

    docs: Documentation only changes
    deps: Changes external dependencies
    style: Changes that do not affect the meaning of the code (white-space, formatting, etc)
    chore Daily work, examples, etc.
    ci: Changes to our CI configuration files and scripts
-->

#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->

The current registrar with zookeeper doesn't have any method for auth.
I think add a simple digest acl support is necessary. 

#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If you PR is not fully resolved issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->

#### Other special notes for reviewer:
<!--
* Somethings that need extra attention for reviewer
* Some additional notes, TODO list, etc.
-->


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1964?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#1964](https://codecov.io/gh/go-kratos/kratos/pull/1964?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (cb460c2) into [main](https://codecov.io/gh/go-kratos/kratos/commit/60465ccca2daf587f089b48ecd49670c35437ee1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (60465cc) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##             main    #1964   +/-   ##
=======================================
  Coverage   80.67%   80.67%           
=======================================
  Files          81       81           
  Lines        3618     3618           
=======================================
  Hits         2919     2919           
  Misses        492      492           
  Partials      207      207           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1964?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1964?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [60465cc...cb460c2](https://codecov.io/gh/go-kratos/kratos/pull/1964?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/contrib/registry/zookeeper/register.go b/contrib/registry/zookeeper/register.go
index 341058b5..adae0050 100644
--- a/contrib/registry/zookeeper/register.go
+++ b/contrib/registry/zookeeper/register.go
@@ -25,6 +25,8 @@ type options struct {
 	ctx      context.Context
 	rootPath string
 	timeout  time.Duration
+	user     string
+	password string
 }
 
 // WithContext with registry context.
@@ -42,6 +44,14 @@ func WithTimeout(timeout time.Duration) Option {
 	return func(o *options) { o.timeout = timeout }
 }
 
+// WithDigestACL with registry password.
+func WithDigestACL(user string, password string) Option {
+	return func(o *options) {
+		o.user = user
+		o.password = password
+	}
+}
+
 // Registry is consul registry
 type Registry struct {
 	opts     *options
@@ -63,6 +73,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,
@@ -182,7 +198,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
 		}


Test patch:
diff --git a/contrib/registry/zookeeper/register_test.go b/contrib/registry/zookeeper/register_test.go
index f746b104..9293e2b9 100644
--- a/contrib/registry/zookeeper/register_test.go
+++ b/contrib/registry/zookeeper/register_test.go
@@ -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"}, WithDigestACL("username", "password"))
 
 	w, err := r.Watch(ctx, s.Name)
 	if err != nil {