add definition config
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
andrey
2024-01-25 18:17:27 +03:00
parent 303433a336
commit 3945d61f17
52 changed files with 1382 additions and 377 deletions

View File

@@ -4,25 +4,42 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/hashicorp/vault/api"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
)
const (
Name = "vault"
Separator = "/"
Prefix = "secret/data/"
ValueName = "value"
)
var _ config.Provider = (*SecretKV2)(nil)
type SecretOption func(*SecretKV2)
func WithSecretResolve(f func(context.Context, config.Key) (string, string)) SecretOption {
func WithSecretResolve(f func(key []string) (string, string)) SecretOption {
return func(s *SecretKV2) { s.resolve = f }
}
func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
func NewSecretKV2(namespace, appName string, client *api.Client, opts ...SecretOption) *SecretKV2 {
prov := SecretKV2{
client: client,
resolve: key.LastIndexField(":", "value", key.PrefixName("secret/data/", key.NsAppName("/"))),
client: client,
resolve: func(key []string) (string, string) {
keysLen := len(key)
if keysLen == 1 {
return "", key[0]
}
return strings.Join(key[:keysLen-1], Separator), key[keysLen-1]
},
name: Name,
prefix: Prefix + namespace + Separator + appName,
}
for _, opt := range opts {
@@ -34,57 +51,69 @@ func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
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 != ""
resolve func(key []string) (string, string)
name string
prefix string
}
func (p *SecretKV2) Name() string {
return "vault"
return p.name
}
func (p *SecretKV2) Key(in []string) (string, string) {
path, val := p.resolve(in)
if path == "" {
return p.prefix, val
}
func (p *SecretKV2) Read(ctx context.Context, key config.Key) (config.Variable, error) {
path, field := p.resolve(ctx, key)
return p.prefix + Separator + path, val
}
func (p *SecretKV2) read(path, key string) (*api.Secret, error) {
secret, 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())
return nil, err
}
if secret == nil && key != ValueName {
return p.read(path+Separator+key, ValueName)
}
return secret, err
}
func (p *SecretKV2) Value(ctx context.Context, key ...string) (config.Value, error) {
path, field := p.Key(key)
secret, err := p.read(path, field)
if err != nil {
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
}
if secret == nil || len(secret.Data) == 0 {
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
log.Println(secret == nil)
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
}
if len(secret.Warnings) > 0 {
return config.Variable{},
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrVariableNotFound, secret.Warnings, path, field, p.Name())
return nil,
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrValueNotFound, secret.Warnings, path, field, p.Name())
}
data, ok := secret.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())
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
}
if val, ok := data[field]; ok {
return config.Variable{
Name: path + field,
Provider: p.Name(),
Value: value.JString(fmt.Sprint(val)),
}, nil
return value.JString(fmt.Sprint(val)), nil
}
if val, ok := data[ValueName]; ok {
return value.JString(fmt.Sprint(val)), nil
}
md, err := json.Marshal(data)
if err != nil {
return config.Variable{}, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
return nil, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
}
return config.Variable{
Name: path + field,
Provider: p.Name(),
Value: value.JBytes(md),
}, nil
return value.JBytes(md), nil
}

View File

@@ -15,12 +15,12 @@ func TestProvider(t *testing.T) {
cl, err := test.NewVault()
require.NoError(t, err)
provider := vault.NewSecretKV2(cl)
provider := vault.NewSecretKV2("fdevs", "config", cl)
read := []test.Read{
test.NewReadConfig("database"),
test.NewRead("db:dsn", test.DSN),
test.NewRead("db:timeout", time.Minute),
test.NewRead(test.DSN, "db", "dsn"),
test.NewRead(time.Minute, "db", "timeout"),
}
test.Run(t, provider, read)
}