2024-03-06 17:45:56 +03:00
|
|
|
package vault
|
2020-12-10 01:42:28 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-07-29 11:16:48 +03:00
|
|
|
"fmt"
|
2022-02-16 14:59:35 +03:00
|
|
|
"net/http"
|
2021-03-10 01:04:58 +03:00
|
|
|
"time"
|
2020-12-10 01:42:28 +03:00
|
|
|
|
2024-03-06 17:45:56 +03:00
|
|
|
"dario.cat/mergo"
|
2020-12-10 01:42:28 +03:00
|
|
|
"github.com/hashicorp/vault/api"
|
2021-10-26 23:35:26 +03:00
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-12-10 01:42:28 +03:00
|
|
|
)
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
var DefaultStructTag = "vault"
|
2020-12-10 01:42:28 +03:00
|
|
|
|
|
|
|
type vaultConfig struct {
|
2023-06-03 23:19:38 +03:00
|
|
|
path string
|
|
|
|
token string
|
|
|
|
roleID string
|
|
|
|
secretID string
|
|
|
|
cli *api.Client
|
|
|
|
opts config.Options
|
2020-12-10 01:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *vaultConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *vaultConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:32:33 +03:00
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:35:25 +03:00
|
|
|
if c.opts.Codec == nil {
|
|
|
|
return config.ErrCodecMissing
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:42:28 +03:00
|
|
|
cfg := api.DefaultConfig()
|
2021-03-22 00:34:24 +03:00
|
|
|
cfg.Timeout = 500 * time.Millisecond
|
|
|
|
cfg.MaxRetries = 2
|
2020-12-10 01:42:28 +03:00
|
|
|
path := ""
|
|
|
|
token := ""
|
|
|
|
roleID := ""
|
|
|
|
secretID := ""
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(configKey{}).(*api.Config); ok {
|
|
|
|
cfg = v
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:59:35 +03:00
|
|
|
if v, ok := c.opts.Context.Value(httpClientKey{}).(*http.Client); ok {
|
|
|
|
cfg.HttpClient = v
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:04:58 +03:00
|
|
|
if v, ok := c.opts.Context.Value(timeoutKey{}).(time.Duration); ok {
|
|
|
|
cfg.Timeout = v
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:42:28 +03:00
|
|
|
if v, ok := c.opts.Context.Value(addrKey{}).(string); ok {
|
|
|
|
cfg.Address = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(tokenKey{}).(string); ok {
|
|
|
|
token = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(roleIDKey{}).(string); ok {
|
|
|
|
roleID = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(secretIDKey{}).(string); ok {
|
|
|
|
secretID = v
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := api.NewClient(cfg)
|
2021-09-30 01:34:20 +03:00
|
|
|
if err != nil {
|
2023-03-15 01:32:33 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
2021-09-30 01:34:20 +03:00
|
|
|
}
|
2023-03-15 01:32:33 +03:00
|
|
|
|
2023-03-16 07:25:01 +03:00
|
|
|
if err = config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-15 01:32:33 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-12-10 01:42:28 +03:00
|
|
|
}
|
2021-11-19 02:49:14 +03:00
|
|
|
c.cli = cli
|
|
|
|
c.path = path
|
2023-06-03 23:19:38 +03:00
|
|
|
c.token = token
|
|
|
|
c.roleID = roleID
|
|
|
|
c.secretID = secretID
|
2021-11-19 02:49:14 +03:00
|
|
|
|
2023-06-03 23:19:38 +03:00
|
|
|
if err = c.setToken(); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 01:32:33 +03:00
|
|
|
|
2023-06-03 23:19:38 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-10 01:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-06-03 23:19:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-19 02:49:14 +03:00
|
|
|
|
2023-06-03 23:19:38 +03:00
|
|
|
func (c *vaultConfig) setToken() error {
|
|
|
|
if c.token != "" {
|
|
|
|
c.cli.SetToken(c.token)
|
2021-11-19 02:49:14 +03:00
|
|
|
}
|
2020-12-10 01:42:28 +03:00
|
|
|
|
2023-06-03 23:19:38 +03:00
|
|
|
if c.roleID != "" && c.secretID != "" {
|
|
|
|
rsp, err := c.cli.Logical().Write("auth/approle/login", map[string]interface{}{
|
|
|
|
"role_id": c.roleID,
|
|
|
|
"secret_id": c.secretID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
c.cli.SetToken(rsp.Auth.ClientToken)
|
|
|
|
}
|
2023-03-15 01:32:33 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 01:42:28 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:26:39 +03:00
|
|
|
func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2024-01-15 08:37:00 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:32:33 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
2021-09-30 01:34:20 +03:00
|
|
|
if c.cli == nil {
|
2024-10-12 13:37:53 +03:00
|
|
|
c.opts.Logger.Error(c.opts.Context, "vault load error", fmt.Errorf("vault client not created"))
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("vault client not created")
|
|
|
|
}
|
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
|
|
|
}
|
|
|
|
|
2021-11-19 02:49:14 +03:00
|
|
|
pair, err := c.cli.Logical().Read(c.path)
|
2021-09-30 01:34:20 +03:00
|
|
|
if err != nil {
|
2023-03-15 01:32:33 +03:00
|
|
|
err = fmt.Errorf("vault load path %s err: %w", c.path, err)
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
2020-12-18 03:43:00 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-09-30 01:34:20 +03:00
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
2020-12-18 03:43:00 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
if pair == nil || pair.Data == nil {
|
2023-03-15 01:32:33 +03:00
|
|
|
err = fmt.Errorf("vault load path %s err: %w", c.path, fmt.Errorf("not found"))
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
2023-03-15 01:32:33 +03:00
|
|
|
return err
|
2021-09-30 01:34:20 +03:00
|
|
|
}
|
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
2021-03-05 17:02:17 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
var data []byte
|
|
|
|
var src interface{}
|
|
|
|
data, err = json.Marshal(pair.Data["data"])
|
|
|
|
if err != nil {
|
2023-03-15 01:32:33 +03:00
|
|
|
err = fmt.Errorf("vault load path %s err: %w", c.path, err)
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-19 23:34:51 +03:00
|
|
|
}
|
2021-09-30 01:34:20 +03:00
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:32:33 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err = rutil.Zero(dst)
|
2021-09-30 01:34:20 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(data, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-10-12 13:37:53 +03:00
|
|
|
c.opts.Logger.Error(c.opts.Context, fmt.Sprintf("vault load path %s error", c.path), err)
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
2020-12-19 23:34:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-09-30 01:34:20 +03:00
|
|
|
return config.DefaultAfterLoad(ctx, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
2020-12-18 03:43:00 +03:00
|
|
|
}
|
2023-03-15 01:32:33 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2021-09-30 01:34:20 +03:00
|
|
|
if err != nil {
|
2023-03-15 01:32:33 +03:00
|
|
|
err = fmt.Errorf("vault load path %s err: %w", c.path, err)
|
2021-09-30 01:34:20 +03:00
|
|
|
if !c.opts.AllowFail {
|
2020-12-18 03:43:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 03:43:00 +03:00
|
|
|
return nil
|
2020-12-10 01:42:28 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:26:39 +03:00
|
|
|
func (c *vaultConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2024-01-15 08:37:00 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil {
|
|
|
|
return err
|
2020-12-18 03:43:00 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:34:20 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil {
|
|
|
|
return err
|
2020-12-18 03:43:00 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 01:42:28 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *vaultConfig) String() string {
|
2020-12-13 13:35:25 +03:00
|
|
|
return "vault"
|
2020-12-10 01:42:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:26:58 +03:00
|
|
|
func (c *vaultConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-08-04 16:02:14 +03:00
|
|
|
func (c *vaultConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
w := &vaultWatcher{
|
|
|
|
cli: c.cli,
|
2021-11-19 02:49:14 +03:00
|
|
|
path: c.path,
|
2021-08-04 16:02:14 +03:00
|
|
|
opts: c.opts,
|
|
|
|
wopts: config.NewWatchOptions(opts...),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
vchan: make(chan map[string]interface{}),
|
|
|
|
echan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.run()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:42:28 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &vaultConfig{opts: options}
|
|
|
|
}
|