2015-04-01 21:51:13 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
|
|
|
"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:24:39 +00:00
|
|
|
Prefix string
|
|
|
|
Name string
|
|
|
|
Schema map[string]*FieldSchema
|
|
|
|
CaseInsensitive bool
|
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
|
|
|
|
func (p *PathMap) pathStruct(k string) *PathStruct {
|
|
|
|
p.once.Do(p.init)
|
|
|
|
|
2015-05-11 17:24:39 +00:00
|
|
|
// If we don't care about casing, store everything lowercase
|
|
|
|
if p.CaseInsensitive {
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
}
|
|
|
|
|
2015-04-17 16:35:49 +00:00
|
|
|
return &PathStruct{
|
|
|
|
Name: fmt.Sprintf("map/%s/%s", p.Name, k),
|
|
|
|
Schema: p.Schema,
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
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
|
|
|
|
func (p *PathMap) Get(s logical.Storage, k string) (map[string]interface{}, error) {
|
|
|
|
return p.pathStruct(k).Get(s)
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put writes a value into the mapping
|
2015-04-17 16:35:49 +00:00
|
|
|
func (p *PathMap) Put(s logical.Storage, k string, v map[string]interface{}) error {
|
|
|
|
return p.pathStruct(k).Put(s, v)
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 01:04:26 +00:00
|
|
|
// List reads the keys under a given path
|
|
|
|
func (p *PathMap) List(s logical.Storage, prefix string) ([]string, error) {
|
|
|
|
stripPrefix := fmt.Sprintf("struct/map/%s/", p.Name)
|
|
|
|
fullPrefix := fmt.Sprintf("%s%s", stripPrefix, prefix)
|
|
|
|
out, err := s.List(fullPrefix)
|
|
|
|
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{
|
2015-04-17 16:35:49 +00:00
|
|
|
Pattern: fmt.Sprintf("%s/%s$", p.Prefix, p.Name),
|
2015-04-01 21:51:13 +00:00
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ListOperation: p.pathList,
|
|
|
|
logical.ReadOperation: p.pathList,
|
|
|
|
},
|
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{
|
|
|
|
logical.WriteOperation: p.pathSingleWrite,
|
|
|
|
logical.ReadOperation: p.pathSingleRead,
|
|
|
|
},
|
2015-04-04 19:18:33 +00:00
|
|
|
|
|
|
|
HelpSynopsis: fmt.Sprintf("Read/write a single %s mapping", p.Name),
|
2015-04-01 21:51:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PathMap) pathList(
|
|
|
|
req *logical.Request, d *FieldData) (*logical.Response, error) {
|
|
|
|
keys, err := req.Storage.List(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return logical.ListResponse(keys), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PathMap) pathSingleRead(
|
|
|
|
req *logical.Request, d *FieldData) (*logical.Response, error) {
|
|
|
|
v, err := p.Get(req.Storage, d.Get("key").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
2015-04-17 16:35:49 +00:00
|
|
|
Data: v,
|
2015-04-01 21:51:13 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PathMap) pathSingleWrite(
|
|
|
|
req *logical.Request, d *FieldData) (*logical.Response, error) {
|
2015-04-17 16:35:49 +00:00
|
|
|
err := p.Put(req.Storage, d.Get("key").(string), d.Raw)
|
2015-04-01 21:51:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|