2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-01 22:46:13 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-04-01 22:46:13 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-01 22:46:13 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 14:17:05 +00:00
|
|
|
// DEPRECATED: Don't use this. It's too inflexible, nearly impossible to use
|
|
|
|
// with some modern Vault features, and imposes specific API designs.
|
|
|
|
//
|
2015-04-01 22:46:13 +00:00
|
|
|
// 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 {
|
2015-04-17 16:35:49 +00:00
|
|
|
PathMap
|
2015-04-01 22:46:13 +00:00
|
|
|
|
|
|
|
DefaultKey string
|
2015-04-17 16:35:49 +00:00
|
|
|
PolicyKey string
|
2015-04-01 22:46:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PolicyMap) Policies(ctx context.Context, s logical.Storage, names ...string) ([]string, error) {
|
2015-04-17 16:35:49 +00:00
|
|
|
policyKey := "value"
|
|
|
|
if p.PolicyKey != "" {
|
|
|
|
policyKey = p.PolicyKey
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:46:13 +00:00
|
|
|
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 {
|
2018-01-19 06:44:44 +00:00
|
|
|
v, err := p.Get(ctx, s, name)
|
2015-04-01 22:46:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-17 16:35:49 +00:00
|
|
|
valuesRaw, ok := v[policyKey]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
values, ok := valuesRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range strings.Split(values, ",") {
|
2015-04-01 22:46:13 +00:00
|
|
|
if p = strings.TrimSpace(p); p != "" {
|
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(set))
|
2021-04-08 16:43:39 +00:00
|
|
|
for k := range set {
|
2015-04-01 22:46:13 +00:00
|
|
|
list = append(list, k)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|