Skip to content

Commit

Permalink
feat: add auth option for etcd (#32)
Browse files Browse the repository at this point in the history
* docs: fix wrong import path

* feat: add auth option for etcd

* feat: add auth option for etcd
  • Loading branch information
L2ncE authored Dec 26, 2022
1 parent 1bb27d8 commit 3c45168
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
8 changes: 8 additions & 0 deletions etcd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
76 changes: 74 additions & 2 deletions etcd/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ make prepare
make prepare-cluster
```



### run server

```go
Expand All @@ -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, &registry.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)
Expand Down

0 comments on commit 3c45168

Please sign in to comment.