2020-01-06 18:16:52 +00:00
|
|
|
package identitytpl
|
2018-08-15 18:42:56 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-21 17:23:39 +00:00
|
|
|
"encoding/json"
|
2018-08-15 18:42:56 +00:00
|
|
|
"errors"
|
2018-08-23 16:15:02 +00:00
|
|
|
"fmt"
|
2019-06-21 17:23:39 +00:00
|
|
|
"strconv"
|
2018-08-15 18:42:56 +00:00
|
|
|
"strings"
|
2019-06-21 17:23:39 +00:00
|
|
|
"time"
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2020-01-06 18:16:52 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-08-15 18:42:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnbalancedTemplatingCharacter = errors.New("unbalanced templating characters")
|
|
|
|
ErrNoEntityAttachedToToken = errors.New("string contains entity template directives but no entity was provided")
|
|
|
|
ErrNoGroupsAttachedToToken = errors.New("string contains groups template directives but no groups were provided")
|
|
|
|
ErrTemplateValueNotFound = errors.New("no value could be found for one of the template directives")
|
|
|
|
)
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
const (
|
|
|
|
ACLTemplating = iota // must be the first value for backwards compatibility
|
|
|
|
JSONTemplating
|
|
|
|
)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
type PopulateStringInput struct {
|
|
|
|
String string
|
2019-06-21 17:23:39 +00:00
|
|
|
ValidityCheckOnly bool
|
2020-01-06 18:16:52 +00:00
|
|
|
Entity *logical.Entity
|
|
|
|
Groups []*logical.Group
|
|
|
|
NamespaceID string
|
2019-06-21 17:23:39 +00:00
|
|
|
Mode int // processing mode, ACLTemplate or JSONTemplating
|
|
|
|
Now time.Time // optional, defaults to current time
|
|
|
|
|
|
|
|
templateHandler templateHandlerFunc
|
|
|
|
groupIDs []string
|
|
|
|
groupNames []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// templateHandlerFunc allows generating string outputs based on data type, and
|
|
|
|
// different handlers can be used based on mode. For example in ACL mode, strings
|
|
|
|
// are emitted verbatim, but they're wrapped in double quotes for JSON mode. And
|
|
|
|
// some structures, like slices, might be rendered in one mode but prohibited in
|
|
|
|
// another.
|
|
|
|
type templateHandlerFunc func(interface{}, ...string) (string, error)
|
|
|
|
|
|
|
|
// aclTemplateHandler processes known parameter data types when operating
|
|
|
|
// in ACL mode.
|
|
|
|
func aclTemplateHandler(v interface{}, keys ...string) (string, error) {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
if t == "" {
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
case []string:
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
case map[string]string:
|
|
|
|
if len(keys) > 0 {
|
|
|
|
val, ok := t[keys[0]]
|
|
|
|
if ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("unknown type: %T", v)
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
// jsonTemplateHandler processes known parameter data types when operating
|
|
|
|
// in JSON mode.
|
|
|
|
func jsonTemplateHandler(v interface{}, keys ...string) (string, error) {
|
|
|
|
jsonMarshaller := func(v interface{}) (string, error) {
|
|
|
|
enc, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(enc), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
return strconv.Quote(t), nil
|
|
|
|
case []string:
|
|
|
|
return jsonMarshaller(t)
|
|
|
|
case map[string]string:
|
|
|
|
if len(keys) > 0 {
|
|
|
|
return strconv.Quote(t[keys[0]]), nil
|
|
|
|
}
|
|
|
|
if t == nil {
|
|
|
|
return "{}", nil
|
|
|
|
}
|
|
|
|
return jsonMarshaller(t)
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
return "", fmt.Errorf("unknown type: %T", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PopulateString(p PopulateStringInput) (bool, string, error) {
|
2018-08-15 18:42:56 +00:00
|
|
|
if p.String == "" {
|
|
|
|
return false, "", nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
// preprocess groups
|
|
|
|
for _, g := range p.Groups {
|
|
|
|
p.groupNames = append(p.groupNames, g.Name)
|
|
|
|
p.groupIDs = append(p.groupIDs, g.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up mode-specific handler
|
|
|
|
switch p.Mode {
|
|
|
|
case ACLTemplating:
|
|
|
|
p.templateHandler = aclTemplateHandler
|
|
|
|
case JSONTemplating:
|
|
|
|
p.templateHandler = jsonTemplateHandler
|
|
|
|
default:
|
|
|
|
return false, "", fmt.Errorf("unknown mode %q", p.Mode)
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
var subst bool
|
|
|
|
splitStr := strings.Split(p.String, "{{")
|
|
|
|
|
|
|
|
if len(splitStr) >= 1 {
|
2018-11-06 02:40:29 +00:00
|
|
|
if strings.Contains(splitStr[0], "}}") {
|
2018-08-15 18:42:56 +00:00
|
|
|
return false, "", ErrUnbalancedTemplatingCharacter
|
|
|
|
}
|
|
|
|
if len(splitStr) == 1 {
|
|
|
|
return false, p.String, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
if !p.ValidityCheckOnly {
|
|
|
|
b.Grow(2 * len(p.String))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, str := range splitStr {
|
|
|
|
if i == 0 {
|
|
|
|
if !p.ValidityCheckOnly {
|
|
|
|
b.WriteString(str)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
splitPiece := strings.Split(str, "}}")
|
|
|
|
switch len(splitPiece) {
|
|
|
|
case 2:
|
|
|
|
subst = true
|
|
|
|
if !p.ValidityCheckOnly {
|
2019-06-21 17:23:39 +00:00
|
|
|
tmplStr, err := performTemplating(strings.TrimSpace(splitPiece[0]), &p)
|
2018-08-15 18:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
b.WriteString(tmplStr)
|
|
|
|
b.WriteString(splitPiece[1])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false, "", ErrUnbalancedTemplatingCharacter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subst, b.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
func performTemplating(input string, p *PopulateStringInput) (string, error) {
|
2020-01-06 18:16:52 +00:00
|
|
|
performAliasTemplating := func(trimmed string, alias *logical.Alias) (string, error) {
|
2018-08-15 18:42:56 +00:00
|
|
|
switch {
|
|
|
|
case trimmed == "id":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(alias.ID)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case trimmed == "name":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(alias.Name)
|
|
|
|
|
|
|
|
case trimmed == "metadata":
|
|
|
|
return p.templateHandler(alias.Metadata)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case strings.HasPrefix(trimmed, "metadata."):
|
2019-06-21 17:23:39 +00:00
|
|
|
split := strings.SplitN(trimmed, ".", 2)
|
|
|
|
return p.templateHandler(alias.Metadata, split[1])
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
performEntityTemplating := func(trimmed string) (string, error) {
|
|
|
|
switch {
|
|
|
|
case trimmed == "id":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(p.Entity.ID)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case trimmed == "name":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(p.Entity.Name)
|
|
|
|
|
|
|
|
case trimmed == "metadata":
|
|
|
|
return p.templateHandler(p.Entity.Metadata)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case strings.HasPrefix(trimmed, "metadata."):
|
2019-06-21 17:23:39 +00:00
|
|
|
split := strings.SplitN(trimmed, ".", 2)
|
|
|
|
return p.templateHandler(p.Entity.Metadata, split[1])
|
|
|
|
|
2019-07-19 17:08:47 +00:00
|
|
|
case trimmed == "groups.names":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(p.groupNames)
|
|
|
|
|
2019-07-19 17:08:47 +00:00
|
|
|
case trimmed == "groups.ids":
|
2019-06-21 17:23:39 +00:00
|
|
|
return p.templateHandler(p.groupIDs)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case strings.HasPrefix(trimmed, "aliases."):
|
|
|
|
split := strings.SplitN(strings.TrimPrefix(trimmed, "aliases."), ".", 2)
|
|
|
|
if len(split) != 2 {
|
|
|
|
return "", errors.New("invalid alias selector")
|
|
|
|
}
|
2020-01-06 18:16:52 +00:00
|
|
|
var alias *logical.Alias
|
2019-06-21 17:23:39 +00:00
|
|
|
for _, a := range p.Entity.Aliases {
|
|
|
|
if split[0] == a.MountAccessor {
|
|
|
|
alias = a
|
2018-08-15 18:42:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 17:23:39 +00:00
|
|
|
if alias == nil {
|
|
|
|
if p.Mode == ACLTemplating {
|
|
|
|
return "", errors.New("alias not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// An empty alias is sufficient for generating defaults
|
2020-01-06 18:16:52 +00:00
|
|
|
alias = &logical.Alias{Metadata: make(map[string]string)}
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
2019-06-21 17:23:39 +00:00
|
|
|
return performAliasTemplating(split[1], alias)
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
performGroupsTemplating := func(trimmed string) (string, error) {
|
|
|
|
var ids bool
|
|
|
|
|
|
|
|
selectorSplit := strings.SplitN(trimmed, ".", 2)
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
switch {
|
|
|
|
case len(selectorSplit) != 2:
|
|
|
|
return "", errors.New("invalid groups selector")
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case selectorSplit[0] == "ids":
|
|
|
|
ids = true
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case selectorSplit[0] == "names":
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
default:
|
|
|
|
return "", errors.New("invalid groups selector")
|
|
|
|
}
|
|
|
|
trimmed = selectorSplit[1]
|
|
|
|
|
|
|
|
accessorSplit := strings.SplitN(trimmed, ".", 2)
|
|
|
|
if len(accessorSplit) != 2 {
|
|
|
|
return "", errors.New("invalid groups accessor")
|
|
|
|
}
|
2020-01-06 18:16:52 +00:00
|
|
|
var found *logical.Group
|
2019-06-21 17:23:39 +00:00
|
|
|
for _, group := range p.Groups {
|
2018-09-18 03:03:00 +00:00
|
|
|
var compare string
|
2018-08-15 18:42:56 +00:00
|
|
|
if ids {
|
|
|
|
compare = group.ID
|
2018-09-18 03:03:00 +00:00
|
|
|
} else {
|
2020-01-06 18:16:52 +00:00
|
|
|
if p.NamespaceID != "" && group.NamespaceID != p.NamespaceID {
|
2018-09-18 03:03:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-06 18:16:52 +00:00
|
|
|
compare = group.Name
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if compare == accessorSplit[0] {
|
|
|
|
found = group
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found == nil {
|
2018-08-23 16:15:02 +00:00
|
|
|
return "", fmt.Errorf("entity is not a member of group %q", accessorSplit[0])
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trimmed = accessorSplit[1]
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case trimmed == "id":
|
|
|
|
return found.ID, nil
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case trimmed == "name":
|
|
|
|
if found.Name == "" {
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
return found.Name, nil
|
2019-06-21 17:23:39 +00:00
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
case strings.HasPrefix(trimmed, "metadata."):
|
|
|
|
val, ok := found.Metadata[strings.TrimPrefix(trimmed, "metadata.")]
|
|
|
|
if !ok {
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
performTimeTemplating := func(trimmed string) (string, error) {
|
|
|
|
now := p.Now
|
|
|
|
if now.IsZero() {
|
|
|
|
now = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
opsSplit := strings.SplitN(trimmed, ".", 3)
|
|
|
|
|
|
|
|
if opsSplit[0] != "now" {
|
|
|
|
return "", fmt.Errorf("invalid time selector %q", opsSplit[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
result := now
|
|
|
|
switch len(opsSplit) {
|
|
|
|
case 1:
|
|
|
|
// return current time
|
|
|
|
case 2:
|
|
|
|
return "", errors.New("missing time operand")
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
duration, err := time.ParseDuration(opsSplit[2])
|
|
|
|
if err != nil {
|
|
|
|
return "", errwrap.Wrapf("invalid duration: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch opsSplit[1] {
|
|
|
|
case "plus":
|
|
|
|
result = result.Add(duration)
|
|
|
|
case "minus":
|
|
|
|
result = result.Add(-duration)
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("invalid time operator %q", opsSplit[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.FormatInt(result.Unix(), 10), nil
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(input, "identity.entity."):
|
2019-06-21 17:23:39 +00:00
|
|
|
if p.Entity == nil {
|
2018-08-15 18:42:56 +00:00
|
|
|
return "", ErrNoEntityAttachedToToken
|
|
|
|
}
|
|
|
|
return performEntityTemplating(strings.TrimPrefix(input, "identity.entity."))
|
|
|
|
|
|
|
|
case strings.HasPrefix(input, "identity.groups."):
|
2019-06-21 17:23:39 +00:00
|
|
|
if len(p.Groups) == 0 {
|
2018-08-15 18:42:56 +00:00
|
|
|
return "", ErrNoGroupsAttachedToToken
|
|
|
|
}
|
|
|
|
return performGroupsTemplating(strings.TrimPrefix(input, "identity.groups."))
|
2019-06-21 17:23:39 +00:00
|
|
|
|
|
|
|
case strings.HasPrefix(input, "time."):
|
|
|
|
return performTimeTemplating(strings.TrimPrefix(input, "time."))
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrTemplateValueNotFound
|
|
|
|
}
|