Merge pull request #44 from unistack-org/master

fixup init
This commit is contained in:
Василий Толстов 2021-11-19 02:49:47 +03:00 committed by GitHub
commit 658d31cc26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,25 +80,34 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
} }
return err return err
} }
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
}
if len(token) == 0 {
rsp, err := cli.Logical().Write("auth/approle/login", map[string]interface{}{ rsp, err := cli.Logical().Write("auth/approle/login", map[string]interface{}{
"role_id": roleID, "role_id": roleID,
"secret_id": secretID, "secret_id": secretID,
}) })
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "vault init approle err: %v", err) c.opts.Logger.Errorf(c.opts.Context, "vault init approle err: %v", err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
} else if err == nil { } else if err == nil {
token = rsp.Auth.ClientToken cli.SetToken(rsp.Auth.ClientToken)
} }
}
cli.SetToken(token)
c.cli = cli
c.path = path
return nil return nil
} }
@ -108,14 +117,6 @@ 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 {
@ -124,9 +125,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(path) pair, err := c.cli.Logical().Read(c.path)
if err != nil { 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 { if !c.opts.AllowFail {
return err return err
} }
@ -134,9 +135,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", 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 { 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) return config.DefaultAfterLoad(ctx, c)
} }
@ -145,7 +146,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", path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
@ -158,13 +159,14 @@ 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", path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.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)
@ -175,7 +177,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", path, err) c.opts.Logger.Errorf(c.opts.Context, "vault load path %s err: %v", c.path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
@ -193,39 +195,6 @@ 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
} }
@ -242,17 +211,9 @@ 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: path, path: c.path,
opts: c.opts, opts: c.opts,
wopts: config.NewWatchOptions(opts...), wopts: config.NewWatchOptions(opts...),
done: make(chan struct{}), done: make(chan struct{}),