This commit is contained in:
90
provider/vault/secret.go
Normal file
90
provider/vault/secret.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*SecretKV2)(nil)
|
||||
|
||||
type SecretOption func(*SecretKV2)
|
||||
|
||||
func WithSecretResolve(f func(context.Context, config.Key) (string, string)) SecretOption {
|
||||
return func(s *SecretKV2) { s.resolve = f }
|
||||
}
|
||||
|
||||
func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
|
||||
s := SecretKV2{
|
||||
client: client,
|
||||
resolve: key.LastIndexField(":", "value", key.PrefixName("secret/data/", key.NsAppName("/"))),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&s)
|
||||
}
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
type SecretKV2 struct {
|
||||
client *api.Client
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *SecretKV2) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
path, _ := p.resolve(ctx, key)
|
||||
|
||||
return path != ""
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Name() string {
|
||||
return "vault"
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path, field := p.resolve(ctx, key)
|
||||
|
||||
s, err := p.client.Logical().Read(path)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
|
||||
}
|
||||
|
||||
if s == nil || len(s.Data) == 0 {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if len(s.Warnings) > 0 {
|
||||
return config.Variable{},
|
||||
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrVariableNotFound, s.Warnings, path, field, p.Name())
|
||||
}
|
||||
|
||||
d, ok := s.Data["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if val, ok := d[field]; ok {
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(fmt.Sprint(val)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
md, err := json.Marshal(d)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JBytes(md),
|
||||
}, nil
|
||||
}
|
||||
26
provider/vault/secret_test.go
Normal file
26
provider/vault/secret_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package vault_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/config/provider/vault"
|
||||
"gitoa.ru/go-4devs/config/test"
|
||||
)
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cl, err := test.NewVault()
|
||||
require.NoError(t, err)
|
||||
|
||||
provider := vault.NewSecretKV2(cl)
|
||||
|
||||
read := []test.Read{
|
||||
test.NewReadConfig("database"),
|
||||
test.NewRead("db:dsn", test.DSN),
|
||||
test.NewRead("db:timeout", time.Minute),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
}
|
||||
Reference in New Issue
Block a user