From d4aec16f01fa7fc514d2dcb9030b4baceabe5178 Mon Sep 17 00:00:00 2001 From: L2ncE Date: Mon, 12 Dec 2022 17:23:24 +0800 Subject: [PATCH 1/3] docs: fix wrong import path --- redis/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis/README.md b/redis/README.md index 2d202ed..22fc5d9 100644 --- a/redis/README.md +++ b/redis/README.md @@ -13,13 +13,13 @@ package main import ( "context" - "registry/redis" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/app/server/registry" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/cloudwego/hertz/pkg/protocol/consts" + "github.com/hertz-contrib/registry/redis" ) func main() { @@ -50,12 +50,12 @@ package main import ( "context" - "registry/redis" "github.com/cloudwego/hertz/pkg/app/client" "github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" "github.com/cloudwego/hertz/pkg/common/config" "github.com/cloudwego/hertz/pkg/common/hlog" + "github.com/hertz-contrib/registry/redis" ) func main() { From 63b393478fdb0d1d0c52cc686d81ec951549e71b Mon Sep 17 00:00:00 2001 From: L2ncE Date: Mon, 26 Dec 2022 20:09:31 +0800 Subject: [PATCH 2/3] feat: add auth option for etcd --- etcd/common.go | 8 ++++++ etcd/readme.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/etcd/common.go b/etcd/common.go index d9595d1..078da91 100644 --- a/etcd/common.go +++ b/etcd/common.go @@ -88,6 +88,14 @@ func WithTLSOpt(certFile, keyFile, caFile string) Option { } } +// WithAuthOpt returns an option that authentication by username and password. +func WithAuthOpt(username, password string) Option { + return func(cfg *clientv3.Config) { + cfg.Username = username + cfg.Password = password + } +} + func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, error) { cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { diff --git a/etcd/readme.md b/etcd/readme.md index 82687f2..09cf3e9 100644 --- a/etcd/readme.md +++ b/etcd/readme.md @@ -93,8 +93,6 @@ make prepare make prepare-cluster ``` - - ### run server ```go @@ -120,6 +118,80 @@ go run ./example/client/main.go 2022/08/23 21:11:29.115257 main.go:57: [Info] code=200,body={"ping":"pong2"} ``` +## Authentication + +### Server + +```go +package main + +import ( + "context" + + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/app/server/registry" + "github.com/cloudwego/hertz/pkg/common/utils" + "github.com/cloudwego/hertz/pkg/protocol/consts" + "github.com/hertz-contrib/registry/etcd" +) + +func main() { + r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"}, etcd.WithAuthOpt("root", "123456")) + if err != nil { + panic(err) + } + addr := "127.0.0.1:8888" + h := server.Default( + server.WithHostPorts(addr), + server.WithRegistry(r, ®istry.Info{ + ServiceName: "hertz.test.demo", + Addr: utils.NewNetAddr("tcp", addr), + Weight: 10, + Tags: nil, + })) + h.GET("/ping", func(_ context.Context, ctx *app.RequestContext) { + ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) + }) + h.Spin() +} +``` + +### Client + +```go +package main + +import ( + "context" + + "github.com/cloudwego/hertz/pkg/app/client" + "github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" + "github.com/cloudwego/hertz/pkg/common/config" + "github.com/cloudwego/hertz/pkg/common/hlog" + "github.com/hertz-contrib/registry/etcd" +) + +func main() { + cli, err := client.NewClient() + if err != nil { + panic(err) + } + r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"}, etcd.WithAuthOpt("root", "123456")) + if err != nil { + panic(err) + } + cli.Use(sd.Discovery(r)) + for i := 0; i < 10; i++ { + status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true)) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("HERTZ: code=%d,body=%s", status, string(body)) + } +} +``` + ## Compatibility Compatible with server (3.0.0 - 3.5.4) From c49700b90d6f329c6a7105bc1f190e9f04f49956 Mon Sep 17 00:00:00 2001 From: L2ncE Date: Mon, 26 Dec 2022 20:09:31 +0800 Subject: [PATCH 3/3] feat: add auth option for etcd --- etcd/common.go | 8 ++++++ etcd/readme.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/etcd/common.go b/etcd/common.go index d9595d1..078da91 100644 --- a/etcd/common.go +++ b/etcd/common.go @@ -88,6 +88,14 @@ func WithTLSOpt(certFile, keyFile, caFile string) Option { } } +// WithAuthOpt returns an option that authentication by username and password. +func WithAuthOpt(username, password string) Option { + return func(cfg *clientv3.Config) { + cfg.Username = username + cfg.Password = password + } +} + func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, error) { cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { diff --git a/etcd/readme.md b/etcd/readme.md index 82687f2..09cf3e9 100644 --- a/etcd/readme.md +++ b/etcd/readme.md @@ -93,8 +93,6 @@ make prepare make prepare-cluster ``` - - ### run server ```go @@ -120,6 +118,80 @@ go run ./example/client/main.go 2022/08/23 21:11:29.115257 main.go:57: [Info] code=200,body={"ping":"pong2"} ``` +## Authentication + +### Server + +```go +package main + +import ( + "context" + + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/app/server/registry" + "github.com/cloudwego/hertz/pkg/common/utils" + "github.com/cloudwego/hertz/pkg/protocol/consts" + "github.com/hertz-contrib/registry/etcd" +) + +func main() { + r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"}, etcd.WithAuthOpt("root", "123456")) + if err != nil { + panic(err) + } + addr := "127.0.0.1:8888" + h := server.Default( + server.WithHostPorts(addr), + server.WithRegistry(r, ®istry.Info{ + ServiceName: "hertz.test.demo", + Addr: utils.NewNetAddr("tcp", addr), + Weight: 10, + Tags: nil, + })) + h.GET("/ping", func(_ context.Context, ctx *app.RequestContext) { + ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) + }) + h.Spin() +} +``` + +### Client + +```go +package main + +import ( + "context" + + "github.com/cloudwego/hertz/pkg/app/client" + "github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" + "github.com/cloudwego/hertz/pkg/common/config" + "github.com/cloudwego/hertz/pkg/common/hlog" + "github.com/hertz-contrib/registry/etcd" +) + +func main() { + cli, err := client.NewClient() + if err != nil { + panic(err) + } + r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"}, etcd.WithAuthOpt("root", "123456")) + if err != nil { + panic(err) + } + cli.Use(sd.Discovery(r)) + for i := 0; i < 10; i++ { + status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true)) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("HERTZ: code=%d,body=%s", status, string(body)) + } +} +``` + ## Compatibility Compatible with server (3.0.0 - 3.5.4)