fixup init #44

Merged
vtolstov merged 1 commits from master into v3 2021-11-19 02:49:47 +03:00

109
vault.go
View File

@ -80,26 +80,35 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
}
return err
}
if len(token) == 0 {
rsp, err := cli.Logical().Write("auth/approle/login", map[string]interface{}{
"role_id": roleID,
"secret_id": secretID,
})
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
}
}
cli.SetToken(token)
c.cli = cli
c.path = path
if token != "" {
cli.SetToken(token)
return nil
}
if roleID == "" || secretID == "" {
if !c.opts.AllowFail {
return fmt.Errorf("missing Token or RoleID and SecretID")
}
return nil
}
rsp, err := cli.Logical().Write("auth/approle/login", map[string]interface{}{
"role_id": roleID,
"secret_id": secretID,
})
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 {
cli.SetToken(rsp.Auth.ClientToken)
}
return nil
}
@ -108,14 +117,6 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
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 {
c.opts.Logger.Errorf(c.opts.Context, "vault load err: %v", fmt.Errorf("vault client not created"))
if !c.opts.AllowFail {
@ -124,9 +125,9 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
return config.DefaultAfterLoad(ctx, c)
}
pair, err := c.cli.Logical().Read(path)
pair, err := c.cli.Logical().Read(c.path)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
@ -134,9 +135,9 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
}
if pair == nil || pair.Data == nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, fmt.Errorf("not found"))
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", path)
return fmt.Errorf("vault path %s not found", c.path)
}
return config.DefaultAfterLoad(ctx, c)
}
@ -145,7 +146,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
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", path, err)
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
@ -158,13 +159,14 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
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)
@ -175,7 +177,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
err = mergo.Merge(c.opts.Struct, src, mopts...)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", path, err)
c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
@ -193,39 +195,6 @@ func (c *vaultConfig) Save(ctx context.Context, opts ...config.SaveOption) error
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 {
return err
}
@ -242,17 +211,9 @@ func (c *vaultConfig) Name() string {
}
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{
cli: c.cli,
path: path,
path: c.path,
opts: c.opts,
wopts: config.NewWatchOptions(opts...),
done: make(chan struct{}),