updat for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
420977b3c4
commit
e29fd8d71a
3
go.mod
3
go.mod
@ -14,9 +14,8 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.4.1 // indirect
|
||||
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
github.com/unistack-org/micro/v3 v3.7.4
|
||||
github.com/unistack-org/micro/v3 v3.7.6
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
|
||||
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
|
||||
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
|
||||
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
|
||||
)
|
||||
|
16
go.sum
16
go.sum
@ -75,7 +75,7 @@ github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
|
||||
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
@ -241,7 +241,7 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx
|
||||
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
|
||||
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
||||
@ -260,8 +260,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
|
||||
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
|
||||
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
|
||||
github.com/unistack-org/micro/v3 v3.7.4 h1:nRk7f58v0VsiW7aKL0UQaFCQxFoursHQLoWdeZ1RYbg=
|
||||
github.com/unistack-org/micro/v3 v3.7.4/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
|
||||
github.com/unistack-org/micro/v3 v3.7.6 h1:cobNkaicZR+8nbDWRUmX3/CSLh6ZNSytK2zWth4s4IM=
|
||||
github.com/unistack-org/micro/v3 v3.7.6/go.mod h1:Ke/8WJlNZi4ZYwL9HcsANAbQ66/HocTBEZM+od99/mM=
|
||||
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
|
||||
@ -293,9 +293,8 @@ golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8=
|
||||
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b h1:eB48h3HiRycXNy8E0Gf5e0hv7YT6Kt14L/D73G1fuwo=
|
||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -347,8 +346,9 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
|
||||
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
|
144
vault.go
144
vault.go
@ -12,14 +12,12 @@ import (
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultStructTag = "vault"
|
||||
)
|
||||
var DefaultStructTag = "vault"
|
||||
|
||||
type vaultConfig struct {
|
||||
opts config.Options
|
||||
cli *api.Client
|
||||
path string
|
||||
cli *api.Client
|
||||
opts config.Options
|
||||
}
|
||||
|
||||
func (c *vaultConfig) Options() config.Options {
|
||||
@ -75,9 +73,11 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
|
||||
}
|
||||
|
||||
cli, err := api.NewClient(cfg)
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault init path %s err: %v", path, err)
|
||||
if c.opts.AllowFail {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@ -86,8 +86,11 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
|
||||
"role_id": roleID,
|
||||
"secret_id": secretID,
|
||||
})
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault init approle err: %v", err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
} else if err == nil {
|
||||
token = rsp.Auth.ClientToken
|
||||
}
|
||||
@ -101,71 +104,90 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
|
||||
}
|
||||
|
||||
func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
||||
for _, fn := range c.opts.BeforeLoad {
|
||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.cli == nil && !c.opts.AllowFail {
|
||||
return fmt.Errorf("vault client not created")
|
||||
} else if c.cli == nil && c.opts.AllowFail {
|
||||
return nil
|
||||
if c.cli == nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load err: %v", fmt.Errorf("vault client not created"))
|
||||
if !c.opts.AllowFail {
|
||||
return fmt.Errorf("vault client not created")
|
||||
}
|
||||
return config.DefaultAfterLoad(ctx, c)
|
||||
}
|
||||
|
||||
pair, err := c.cli.Logical().Read(c.path)
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
return config.DefaultAfterLoad(ctx, c)
|
||||
}
|
||||
|
||||
if pair == nil || pair.Data == nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, fmt.Errorf("not found"))
|
||||
if !c.opts.AllowFail {
|
||||
return fmt.Errorf("vault path %s not found", c.path)
|
||||
}
|
||||
return config.DefaultAfterLoad(ctx, c)
|
||||
}
|
||||
|
||||
var data []byte
|
||||
var src interface{}
|
||||
data, err = json.Marshal(pair.Data["data"])
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
return config.DefaultAfterLoad(ctx, c)
|
||||
}
|
||||
|
||||
src, err = rutil.Zero(c.opts.Struct)
|
||||
if err == nil {
|
||||
err = c.opts.Codec.Unmarshal(data, src)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
return config.DefaultAfterLoad(ctx, c)
|
||||
}
|
||||
|
||||
options := config.NewLoadOptions(opts...)
|
||||
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
||||
if options.Override {
|
||||
mopts = append(mopts, mergo.WithOverride)
|
||||
}
|
||||
if options.Append {
|
||||
mopts = append(mopts, mergo.WithAppendSlice)
|
||||
}
|
||||
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
||||
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := config.DefaultAfterLoad(ctx, c); err != nil {
|
||||
return err
|
||||
} else if (pair == nil || pair.Data == nil) && !c.opts.AllowFail {
|
||||
return fmt.Errorf("vault path %s not found", c.path)
|
||||
}
|
||||
|
||||
if err == nil && pair != nil && pair.Data != nil {
|
||||
var data []byte
|
||||
var src interface{}
|
||||
data, err = json.Marshal(pair.Data["data"])
|
||||
if err == nil {
|
||||
src, err = rutil.Zero(c.opts.Struct)
|
||||
if err == nil {
|
||||
err = c.opts.Codec.Unmarshal(data, src)
|
||||
if err == nil {
|
||||
options := config.NewLoadOptions(opts...)
|
||||
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
||||
if options.Override {
|
||||
mopts = append(mopts, mergo.WithOverride)
|
||||
}
|
||||
if options.Append {
|
||||
mopts = append(mopts, mergo.WithAppendSlice)
|
||||
}
|
||||
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, fn := range c.opts.AfterLoad {
|
||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *vaultConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
||||
for _, fn := range c.opts.BeforeSave {
|
||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
if err := config.DefaultBeforeSave(ctx, c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fn := range c.opts.AfterSave {
|
||||
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
if err := config.DefaultAfterSave(ctx, c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -12,13 +12,13 @@ import (
|
||||
)
|
||||
|
||||
type vaultWatcher struct {
|
||||
cli *api.Client
|
||||
path string
|
||||
opts config.Options
|
||||
wopts config.WatchOptions
|
||||
done chan struct{}
|
||||
vchan chan map[string]interface{}
|
||||
echan chan error
|
||||
cli *api.Client
|
||||
opts config.Options
|
||||
wopts config.WatchOptions
|
||||
}
|
||||
|
||||
func (w *vaultWatcher) run() {
|
||||
|
Loading…
Reference in New Issue
Block a user