-
Notifications
You must be signed in to change notification settings - Fork 1
/
vault.go
51 lines (40 loc) · 1.04 KB
/
vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package vault
import (
"context"
"github.com/alexfalkowski/konfig/provider/vault/otel"
"github.com/hashicorp/vault/api"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// Transformer for vault.
type Transformer struct {
client *api.Client
tracer otel.Tracer
}
// NewTransformer for vault.
func NewTransformer(client *api.Client, tracer otel.Tracer) *Transformer {
return &Transformer{client: client, tracer: tracer}
}
// Transform for vault.
func (t *Transformer) Transform(ctx context.Context, value string) (any, error) {
ctx, span := t.tracer.Start(ctx, "transform", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
sec, err := t.client.Logical().ReadWithContext(ctx, value)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return value, err
}
if sec == nil || sec.Data == nil {
return value, nil
}
d := sec.Data["data"]
if d == nil {
return value, nil
}
md, ok := d.(map[string]any)
if !ok || md["value"] == nil {
return value, nil
}
return md["value"], nil
}