/
vault_list.go
156 lines (127 loc) · 3.67 KB
/
vault_list.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dependency
import (
"fmt"
"log"
"net/url"
"path"
"sort"
"strings"
"time"
"github.com/pkg/errors"
)
// Ensure implements
var _ Dependency = (*VaultListQuery)(nil)
// VaultListQuery is the dependency to Vault for a secret
type VaultListQuery struct {
stopCh chan struct{}
path string
}
// NewVaultListQuery creates a new datacenter dependency.
func NewVaultListQuery(s string) (*VaultListQuery, error) {
s = strings.TrimSpace(s)
s = strings.Trim(s, "/")
if s == "" {
return nil, fmt.Errorf("vault.list: invalid format: %q", s)
}
return &VaultListQuery{
stopCh: make(chan struct{}, 1),
path: s,
}, nil
}
// Fetch queries the Vault API
func (d *VaultListQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-d.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{})
// If this is not the first query, poll to simulate blocking-queries.
if opts.WaitIndex != 0 {
dur := VaultDefaultLeaseDuration
log.Printf("[TRACE] %s: long polling for %s", d, dur)
select {
case <-d.stopCh:
return nil, nil, ErrStopped
case <-time.After(dur):
}
}
secretsPath := d.path
// Checking secret engine version. If it's v2, we should shim /metadata/
// to secret path if necessary.
mountPath, isV2, _ := isKVv2(clients.Vault(), secretsPath)
if isV2 {
secretsPath = shimKvV2ListPath(secretsPath, mountPath)
}
// If we got this far, we either didn't have a secret to renew, the secret was
// not renewable, or the renewal failed, so attempt a fresh list.
log.Printf("[TRACE] %s: LIST %s", d, &url.URL{
Path: "/v1/" + secretsPath,
RawQuery: opts.String(),
})
secret, err := clients.Vault().Logical().List(secretsPath)
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
var result []string
// The secret could be nil if it does not exist.
if secret == nil || secret.Data == nil {
log.Printf("[TRACE] %s: no data", d)
return respWithMetadata(result)
}
// This is a weird thing that happened once...
keys, ok := secret.Data["keys"]
if !ok {
log.Printf("[TRACE] %s: no keys", d)
return respWithMetadata(result)
}
list, ok := keys.([]interface{})
if !ok {
log.Printf("[TRACE] %s: not list", d)
return nil, nil, fmt.Errorf("%s: unexpected response", d)
}
for _, v := range list {
typed, ok := v.(string)
if !ok {
return nil, nil, fmt.Errorf("%s: non-string in list", d)
}
result = append(result, typed)
}
sort.Strings(result)
log.Printf("[TRACE] %s: returned %d results", d, len(result))
return respWithMetadata(result)
}
// CanShare returns if this dependency is shareable.
func (d *VaultListQuery) CanShare() bool {
return false
}
// Stop halts the given dependency's fetch.
func (d *VaultListQuery) Stop() {
close(d.stopCh)
}
// String returns the human-friendly version of this dependency.
func (d *VaultListQuery) String() string {
return fmt.Sprintf("vault.list(%s)", d.path)
}
// Type returns the type of this dependency.
func (d *VaultListQuery) Type() Type {
return TypeVault
}
// shimKvV2ListPath aligns the supported legacy path to KV v2 specs by inserting
// /metadata/ into the path for listing secrets. Paths with /metadata/ are not modified.
func shimKvV2ListPath(rawPath, mountPath string) string {
mountPath = strings.TrimSuffix(mountPath, "/")
if strings.HasPrefix(rawPath, path.Join(mountPath, "metadata")) {
// It doesn't need modifying.
return rawPath
}
switch {
case rawPath == mountPath:
return path.Join(mountPath, "metadata")
default:
rawPath = strings.TrimPrefix(rawPath, mountPath)
return path.Join(mountPath, "metadata", rawPath)
}
}