Debian Codegraph @

providers/vault/vault.go

raw
1 // Package vault implements a koanf.Provider for Hashicorp Vault KV secrets engine
2 // and provides it to koanf to be parsed by a koanf.Parser.
3 package vault
4
5 import (
6 "context"
7 "errors"
8 "net/http"
9 "time"
10
11 "github.com/hashicorp/vault/api"
12 "github.com/knadh/koanf/maps"
13 )
14
15 type Config struct {
16 // Vault server address
17 Address string
18
19 // AuthMethod the Vault auth method https://developer.hashicorp.com/vault/docs/auth
20 AuthMethod api.AuthMethod
21
22 // Vault static token
23 Token string
24
25 // Secret data path
26 Path string
27
28 // If FlatPaths is true, then the loaded configuration is not split into
29 // hierarchical maps based on the delimiter. The keys including the delimiter,
30 // eg: app.db.name stays as-is in the confmap.
31 FlatPaths bool
32
33 // Delim is the delimiter to use
34 // when specifying config key paths, for instance a . for `parent.child.key`
35 // or a / for `parent/child/key`.
36 Delim string
37
38 // Internal HTTP client timeout
39 Timeout time.Duration
40
41 // Transport the optional HTTP client transport allows you to
42 // customize the settings like InsecureSkipVerify
43 Transport *http.Transport
44
45 // ExcludeMeta states whether the secret should be returned with its metadata.
46 // If ExcludeMeta is true, no metadata will be returned, and the data can be
47 // accessed as `k.String("key")`. If set to false, the value for data `key`
48 // and the metadata `version` can be accessed as `k.String("data.key")` and
49 // `k.Int("metadata.version")`.
50 ExcludeMeta bool
51 }
52
53 type Vault struct {
54 client *api.Client
55 cfg Config
56 }
57
58 // Provider returns a provider that takes a Vault config.
59 func Provider(cfg Config) (*Vault, error) {
60 httpClient := &http.Client{Timeout: cfg.Timeout, Transport: cfg.Transport}
61 client, err := api.NewClient(&api.Config{Address: cfg.Address, HttpClient: httpClient})
62 if err != nil {
63 return nil, err
64 }
65 if cfg.AuthMethod != nil {
66 if _, err := client.Auth().Login(context.Background(), cfg.AuthMethod); err != nil {
67 return nil, err
68 }
69 } else {
70 client.SetToken(cfg.Token)
71 }
72
73 return &Vault{client: client, cfg: cfg}, nil
74 }
75
76 // ReadBytes is not supported by the vault provider.
77 func (r *Vault) ReadBytes() ([]byte, error) {
78 return nil, errors.New("vault provider does not support this method")
79 }
80
81 // Read fetches the configuration from the source and returns a nested config map.
82 func (r *Vault) Read() (map[string]any, error) {
83 secret, err := r.client.Logical().Read(r.cfg.Path)
84 if err != nil {
85 return nil, err
86 }
87
88 if secret == nil {
89 return nil, errors.New("vault provider fetched no data")
90 }
91
92 s := secret.Data
93 if r.cfg.ExcludeMeta {
94 s = secret.Data["data"].(map[string]any)
95 }
96
97 // Unflatten only when a delimiter is specified
98 if !r.cfg.FlatPaths && r.cfg.Delim != "" {
99 data := maps.Unflatten(s, r.cfg.Delim)
100
101 return data, nil
102 }
103
104 return s, nil
105 }