allow to chage save/load/watch path

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-11-18 16:19:34 +03:00
parent ee4257321c
commit dc1fab53f7
4 changed files with 72 additions and 12 deletions

2
go.mod
View File

@ -9,7 +9,7 @@ require (
github.com/hashicorp/vault/api v1.3.0 github.com/hashicorp/vault/api v1.3.0
github.com/imdario/mergo v0.3.12 github.com/imdario/mergo v0.3.12
github.com/pierrec/lz4 v2.6.1+incompatible // indirect github.com/pierrec/lz4 v2.6.1+incompatible // indirect
go.unistack.org/micro/v3 v3.8.7 go.unistack.org/micro/v3 v3.8.9
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect
) )

4
go.sum
View File

@ -236,8 +236,8 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA= go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8= go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
go.unistack.org/micro/v3 v3.8.7 h1:k1zOpJ3uS8MxdhK8annRsa5J/LW7MpqPjwYuekW61wE= go.unistack.org/micro/v3 v3.8.9 h1:F+HAQSHI86F8Xx5D6XKvvuN0TtOuVvJM+OBV8aYTg0Y=
go.unistack.org/micro/v3 v3.8.7/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA= go.unistack.org/micro/v3 v3.8.9/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

View File

@ -31,6 +31,18 @@ func Path(path string) config.Option {
return config.SetOption(pathKey{}, path) return config.SetOption(pathKey{}, path)
} }
func LoadPath(path string) config.LoadOption {
return config.SetLoadOption(pathKey{}, path)
}
func SavePath(path string) config.SaveOption {
return config.SetSaveOption(pathKey{}, path)
}
func WatchPath(path string) config.WatchOption {
return config.SetWatchOption(pathKey{}, path)
}
type roleIDKey struct{} type roleIDKey struct{}
func RoleID(role string) config.Option { func RoleID(role string) config.Option {

View File

@ -108,6 +108,14 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
return err return err
} }
path := c.path
options := config.NewLoadOptions(opts...)
if options.Context != nil {
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
path = v
}
}
if c.cli == nil { if c.cli == nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load err: %v", fmt.Errorf("vault client not created")) c.opts.Logger.Errorf(c.opts.Context, "vault load err: %v", fmt.Errorf("vault client not created"))
if !c.opts.AllowFail { if !c.opts.AllowFail {
@ -116,9 +124,9 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
return config.DefaultAfterLoad(ctx, c) return config.DefaultAfterLoad(ctx, c)
} }
pair, err := c.cli.Logical().Read(c.path) pair, err := c.cli.Logical().Read(path)
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
@ -126,9 +134,9 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
} }
if pair == nil || pair.Data == nil { 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")) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, fmt.Errorf("not found"))
if !c.opts.AllowFail { if !c.opts.AllowFail {
return fmt.Errorf("vault path %s not found", c.path) return fmt.Errorf("vault path %s not found", path)
} }
return config.DefaultAfterLoad(ctx, c) return config.DefaultAfterLoad(ctx, c)
} }
@ -137,7 +145,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
var src interface{} var src interface{}
data, err = json.Marshal(pair.Data["data"]) data, err = json.Marshal(pair.Data["data"])
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
@ -150,14 +158,13 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
} }
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
return config.DefaultAfterLoad(ctx, c) return config.DefaultAfterLoad(ctx, c)
} }
options := config.NewLoadOptions(opts...)
mopts := []func(*mergo.Config){mergo.WithTypeCheck} mopts := []func(*mergo.Config){mergo.WithTypeCheck}
if options.Override { if options.Override {
mopts = append(mopts, mergo.WithOverride) mopts = append(mopts, mergo.WithOverride)
@ -168,7 +175,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
err = mergo.Merge(c.opts.Struct, src, mopts...) err = mergo.Merge(c.opts.Struct, src, mopts...)
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
@ -186,6 +193,39 @@ func (c *vaultConfig) Save(ctx context.Context, opts ...config.SaveOption) error
return err return err
} }
path := c.path
options := config.NewSaveOptions(opts...)
if options.Context != nil {
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
path = v
}
}
if c.cli == nil {
c.opts.Logger.Errorf(c.opts.Context, "vault save err: %v", fmt.Errorf("vault client not created"))
if !c.opts.AllowFail {
return fmt.Errorf("vault client not created")
}
return config.DefaultAfterSave(ctx, c)
}
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
if err != nil {
if !c.opts.AllowFail {
return err
}
return config.DefaultAfterSave(ctx, c)
}
_, err = c.cli.Logical().WriteBytes(path, buf)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault save path %s err: %v", path, err)
if !c.opts.AllowFail {
return err
}
return config.DefaultAfterSave(ctx, c)
}
if err := config.DefaultAfterSave(ctx, c); err != nil { if err := config.DefaultAfterSave(ctx, c); err != nil {
return err return err
} }
@ -202,9 +242,17 @@ func (c *vaultConfig) Name() string {
} }
func (c *vaultConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) { func (c *vaultConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
path := c.path
options := config.NewWatchOptions(opts...)
if options.Context != nil {
if v, ok := options.Context.Value(pathKey{}).(string); ok && v != "" {
path = v
}
}
w := &vaultWatcher{ w := &vaultWatcher{
cli: c.cli, cli: c.cli,
path: c.path, path: path,
opts: c.opts, opts: c.opts,
wopts: config.NewWatchOptions(opts...), wopts: config.NewWatchOptions(opts...),
done: make(chan struct{}), done: make(chan struct{}),