2015-04-01 21:51:13 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-01 21:51:13 +00:00
|
|
|
"fmt"
|
2015-04-24 01:04:26 +00:00
|
|
|
"strings"
|
2015-04-17 16:35:49 +00:00
|
|
|
"sync"
|
2015-04-01 21:51:13 +00:00
|
|
|
|
2018-01-18 00:48:32 +00:00
|
|
|
saltpkg "github.com/hashicorp/vault/helper/salt"
|
2015-04-01 21:51:13 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PathMap can be used to generate a path that stores mappings in the
|
|
|
|
// storage. It is a structure that also exports functions for querying the
|
|
|
|
// mappings.
|
|
|
|
//
|
|
|
|
// The primary use case for this is for credential providers to do their
|
|
|
|
// mapping to policies.
|
|
|
|
type PathMap struct {
|
2015-05-11 17:27:04 +00:00
|
|
|
Prefix string
|
|
|
|
Name string
|
|
|
|
Schema map[string]*FieldSchema
|
|
|
|
CaseSensitive bool
|
2018-01-18 00:48:32 +00:00
|
|
|
Salt *saltpkg.Salt
|
2018-03-08 19:21:11 +00:00
|
|
|
SaltFunc func(context.Context) (*saltpkg.Salt, error)
|
2015-04-17 16:35:49 +00:00
|
|
|
|
|
|
|
once sync.Once
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 16:35:49 +00:00
|
|
|
func (p *PathMap) init() {
|
|
|
|
if p.Prefix == "" {
|
|
|
|
p.Prefix = "map"
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
2015-04-17 16:35:49 +00:00
|
|
|
|
|
|
|
if p.Schema == nil {
|
|
|
|
p.Schema = map[string]*FieldSchema{
|
|
|
|
"value": &FieldSchema{
|
|
|
|
Type: TypeString,
|
|
|
|
Description: fmt.Sprintf("Value for %s mapping", p.Name),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathStruct returns the pathStruct for this mapping
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PathMap) pathStruct(ctx context.Context, s logical.Storage, k string) (*PathStruct, error) {
|
2015-04-17 16:35:49 +00:00
|
|
|
p.once.Do(p.init)
|
|
|
|
|
2015-05-11 17:24:39 +00:00
|
|
|
// If we don't care about casing, store everything lowercase
|
2015-05-11 17:27:04 +00:00
|
|
|
if !p.CaseSensitive {
|
2015-05-11 17:24:39 +00:00
|
|
|
k = strings.ToLower(k)
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:22:08 +00:00
|
|
|
// The original key before any salting
|
|
|
|
origKey := k
|
|
|
|
|
2015-06-30 21:28:45 +00:00
|
|
|
// If we have a salt, apply it before lookup
|
2017-05-09 21:51:09 +00:00
|
|
|
salt := p.Salt
|
2017-06-07 19:22:08 +00:00
|
|
|
var err error
|
2017-05-09 21:51:09 +00:00
|
|
|
if p.SaltFunc != nil {
|
2018-03-08 19:21:11 +00:00
|
|
|
salt, err = p.SaltFunc(ctx)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
if salt != nil {
|
2018-01-18 00:48:32 +00:00
|
|
|
k = "s" + salt.SaltIDHashFunc(k, saltpkg.SHA256Hash)
|
2015-06-30 21:28:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:22:08 +00:00
|
|
|
finalName := fmt.Sprintf("map/%s/%s", p.Name, k)
|
|
|
|
ps := &PathStruct{
|
|
|
|
Name: finalName,
|
2015-04-17 16:35:49 +00:00
|
|
|
Schema: p.Schema,
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
2017-06-07 19:22:08 +00:00
|
|
|
|
2018-01-18 00:48:32 +00:00
|
|
|
if !strings.HasPrefix(origKey, "s") && k != origKey {
|
2017-06-07 19:22:08 +00:00
|
|
|
// Ensure that no matter what happens what is returned is the final
|
|
|
|
// path
|
|
|
|
defer func() {
|
|
|
|
ps.Name = finalName
|
|
|
|
}()
|
2018-01-18 00:48:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Check for unsalted version and upgrade if so
|
|
|
|
//
|
|
|
|
|
|
|
|
// Generate the unsalted name
|
|
|
|
unsaltedName := fmt.Sprintf("map/%s/%s", p.Name, origKey)
|
|
|
|
// Set the path struct to use the unsalted name
|
|
|
|
ps.Name = unsaltedName
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
val, err := ps.Get(ctx, s)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If not nil, we have an unsalted entry -- upgrade it
|
|
|
|
if val != nil {
|
|
|
|
// Set the path struct to use the desired final name
|
|
|
|
ps.Name = finalName
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.Put(ctx, s, val)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Set it back to the old path and delete
|
|
|
|
ps.Name = unsaltedName
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.Delete(ctx, s)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// We'll set this in the deferred function but doesn't hurt here
|
|
|
|
ps.Name = finalName
|
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Check for SHA1 hashed version and upgrade if so
|
|
|
|
//
|
|
|
|
|
|
|
|
// Generate the SHA1 hash suffixed path name
|
|
|
|
sha1SuffixedName := fmt.Sprintf("map/%s/%s", p.Name, salt.SaltID(origKey))
|
|
|
|
|
|
|
|
// Set the path struct to use the SHA1 hash suffixed path name
|
|
|
|
ps.Name = sha1SuffixedName
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
val, err = ps.Get(ctx, s)
|
2018-01-18 00:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If not nil, we have an SHA1 hash suffixed entry -- upgrade it
|
|
|
|
if val != nil {
|
|
|
|
// Set the path struct to use the desired final name
|
|
|
|
ps.Name = finalName
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.Put(ctx, s, val)
|
2018-01-18 00:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Set it back to the old path and delete
|
|
|
|
ps.Name = sha1SuffixedName
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.Delete(ctx, s)
|
2018-01-18 00:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// We'll set this in the deferred function but doesn't hurt here
|
|
|
|
ps.Name = finalName
|
|
|
|
}
|
2017-06-07 19:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ps, nil
|
2015-04-17 16:35:49 +00:00
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
|
2015-04-17 16:35:49 +00:00
|
|
|
// Get reads a value out of the mapping
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PathMap) Get(ctx context.Context, s logical.Storage, k string) (map[string]interface{}, error) {
|
|
|
|
ps, err := p.pathStruct(ctx, s, k)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return ps.Get(ctx, s)
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put writes a value into the mapping
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PathMap) Put(ctx context.Context, s logical.Storage, k string, v map[string]interface{}) error {
|
|
|
|
ps, err := p.pathStruct(ctx, s, k)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return ps.Put(ctx, s, v)
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 12:28:33 +00:00
|
|
|
// Delete removes a value from the mapping
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PathMap) Delete(ctx context.Context, s logical.Storage, k string) error {
|
|
|
|
ps, err := p.pathStruct(ctx, s, k)
|
2017-06-07 19:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return ps.Delete(ctx, s)
|
2015-05-14 12:28:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 01:04:26 +00:00
|
|
|
// List reads the keys under a given path
|
2018-01-19 06:44:44 +00:00
|
|
|
func (p *PathMap) List(ctx context.Context, s logical.Storage, prefix string) ([]string, error) {
|
2015-04-24 01:04:26 +00:00
|
|
|
stripPrefix := fmt.Sprintf("struct/map/%s/", p.Name)
|
|
|
|
fullPrefix := fmt.Sprintf("%s%s", stripPrefix, prefix)
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := s.List(ctx, fullPrefix)
|
2015-04-24 01:04:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stripped := make([]string, len(out))
|
|
|
|
for idx, k := range out {
|
|
|
|
stripped[idx] = strings.TrimPrefix(k, stripPrefix)
|
|
|
|
}
|
|
|
|
return stripped, nil
|
|
|
|
}
|
|
|
|
|
2015-04-01 21:51:13 +00:00
|
|
|
// Paths are the paths to append to the Backend paths.
|
|
|
|
func (p *PathMap) Paths() []*Path {
|
2015-04-17 16:35:49 +00:00
|
|
|
p.once.Do(p.init)
|
|
|
|
|
|
|
|
// Build the schema by simply adding the "key"
|
|
|
|
schema := make(map[string]*FieldSchema)
|
|
|
|
for k, v := range p.Schema {
|
|
|
|
schema[k] = v
|
|
|
|
}
|
|
|
|
schema["key"] = &FieldSchema{
|
|
|
|
Type: TypeString,
|
|
|
|
Description: fmt.Sprintf("Key for the %s mapping", p.Name),
|
|
|
|
}
|
|
|
|
|
2015-04-01 21:51:13 +00:00
|
|
|
return []*Path{
|
|
|
|
&Path{
|
2017-01-11 21:09:01 +00:00
|
|
|
Pattern: fmt.Sprintf("%s/%s/?$", p.Prefix, p.Name),
|
2015-04-01 21:51:13 +00:00
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.ListOperation: p.pathList(),
|
|
|
|
logical.ReadOperation: p.pathList(),
|
2015-04-01 21:51:13 +00:00
|
|
|
},
|
2015-04-04 19:18:33 +00:00
|
|
|
|
|
|
|
HelpSynopsis: fmt.Sprintf("Read mappings for %s", p.Name),
|
2015-04-01 21:51:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
&Path{
|
2015-05-02 20:17:42 +00:00
|
|
|
Pattern: fmt.Sprintf(`%s/%s/(?P<key>[-\w]+)`, p.Prefix, p.Name),
|
2015-04-17 16:35:49 +00:00
|
|
|
|
|
|
|
Fields: schema,
|
2015-04-01 21:51:13 +00:00
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.CreateOperation: p.pathSingleWrite(),
|
|
|
|
logical.ReadOperation: p.pathSingleRead(),
|
|
|
|
logical.UpdateOperation: p.pathSingleWrite(),
|
|
|
|
logical.DeleteOperation: p.pathSingleDelete(),
|
2015-04-01 21:51:13 +00:00
|
|
|
},
|
2015-04-04 19:18:33 +00:00
|
|
|
|
2015-05-14 21:56:32 +00:00
|
|
|
HelpSynopsis: fmt.Sprintf("Read/write/delete a single %s mapping", p.Name),
|
2016-01-07 20:10:05 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
ExistenceCheck: p.pathSingleExistenceCheck(),
|
2015-04-01 21:51:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (p *PathMap) pathList() OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := p.List(ctx, req.Storage, "")
|
2018-01-08 18:31:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return logical.ListResponse(keys), nil
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (p *PathMap) pathSingleRead() OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
v, err := p.Get(ctx, req.Storage, d.Get("key").(string))
|
2018-01-08 18:31:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return &logical.Response{
|
|
|
|
Data: v,
|
|
|
|
}, nil
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (p *PathMap) pathSingleWrite() OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
err := p.Put(ctx, req.Storage, d.Get("key").(string), d.Raw)
|
2018-01-08 18:31:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
2015-05-14 12:28:33 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (p *PathMap) pathSingleDelete() OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
err := p.Delete(ctx, req.Storage, d.Get("key").(string))
|
2018-01-08 18:31:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-14 12:28:33 +00:00
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (p *PathMap) pathSingleExistenceCheck() ExistenceFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *FieldData) (bool, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
v, err := p.Get(ctx, req.Storage, d.Get("key").(string))
|
2018-01-08 18:31:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return v != nil, nil
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
|
|
|
}
|