forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_map.go
65 lines (53 loc) · 1.16 KB
/
policy_map.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
package framework
import (
"context"
"sort"
"strings"
"github.com/hashicorp/vault/logical"
)
// PolicyMap is a specialization of PathMap that expects the values to
// be lists of policies. This assists in querying and loading policies
// from the PathMap.
type PolicyMap struct {
PathMap
DefaultKey string
PolicyKey string
}
func (p *PolicyMap) Policies(ctx context.Context, s logical.Storage, names ...string) ([]string, error) {
policyKey := "value"
if p.PolicyKey != "" {
policyKey = p.PolicyKey
}
if p.DefaultKey != "" {
newNames := make([]string, len(names)+1)
newNames[0] = p.DefaultKey
copy(newNames[1:], names)
names = newNames
}
set := make(map[string]struct{})
for _, name := range names {
v, err := p.Get(ctx, s, name)
if err != nil {
return nil, err
}
valuesRaw, ok := v[policyKey]
if !ok {
continue
}
values, ok := valuesRaw.(string)
if !ok {
continue
}
for _, p := range strings.Split(values, ",") {
if p = strings.TrimSpace(p); p != "" {
set[p] = struct{}{}
}
}
}
list := make([]string, 0, len(set))
for k, _ := range set {
list = append(list, k)
}
sort.Strings(list)
return list, nil
}