2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-01-06 18:16:52 +00:00
|
|
|
package identitytpl
|
2018-08-15 18:42:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-21 17:23:39 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
2018-08-15 18:42:56 +00:00
|
|
|
"testing"
|
2019-06-21 17:23:39 +00:00
|
|
|
"time"
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2020-01-06 18:16:52 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-08-15 18:42:56 +00:00
|
|
|
)
|
|
|
|
|
2019-06-21 17:23:39 +00:00
|
|
|
// intentionally != time.Now() to catch latent used of time.Now instead of
|
|
|
|
// passed in values
|
|
|
|
var testNow = time.Now().Add(100 * time.Hour)
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
func TestPopulate_Basic(t *testing.T) {
|
2021-04-08 16:43:39 +00:00
|
|
|
tests := []struct {
|
2021-09-17 18:03:47 +00:00
|
|
|
mode int
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
err error
|
|
|
|
entityName string
|
|
|
|
metadata map[string]string
|
|
|
|
aliasAccessor string
|
|
|
|
aliasID string
|
|
|
|
aliasName string
|
|
|
|
nilEntity bool
|
|
|
|
validityCheckOnly bool
|
|
|
|
aliasMetadata map[string]string
|
|
|
|
aliasCustomMetadata map[string]string
|
|
|
|
groupName string
|
|
|
|
groupMetadata map[string]string
|
|
|
|
groupMemberships []string
|
|
|
|
now time.Time
|
2018-08-15 18:42:56 +00:00
|
|
|
}{
|
2019-06-21 17:23:39 +00:00
|
|
|
// time.* tests. Keep tests with time.Now() at the front to avoid false
|
|
|
|
// positives due to the second changing during the test
|
|
|
|
{
|
|
|
|
name: "time now",
|
|
|
|
input: "{{time.now}}",
|
|
|
|
output: strconv.Itoa(int(testNow.Unix())),
|
|
|
|
now: testNow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "time plus",
|
|
|
|
input: "{{time.now.plus.1h}}",
|
|
|
|
output: strconv.Itoa(int(testNow.Unix() + (60 * 60))),
|
|
|
|
now: testNow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "time plus",
|
|
|
|
input: "{{time.now.minus.5m}}",
|
|
|
|
output: strconv.Itoa(int(testNow.Unix() - (5 * 60))),
|
|
|
|
now: testNow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid operator",
|
|
|
|
input: "{{time.now.divide.5m}}",
|
|
|
|
err: errors.New("invalid time operator \"divide\""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "time missing operand",
|
|
|
|
input: "{{time.now.plus}}",
|
|
|
|
err: errors.New("missing time operand"),
|
|
|
|
},
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
{
|
|
|
|
name: "no_templating",
|
|
|
|
input: "path foobar {",
|
|
|
|
output: "path foobar {",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only_closing",
|
|
|
|
input: "path foobar}} {",
|
|
|
|
err: ErrUnbalancedTemplatingCharacter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "closing_in_front",
|
|
|
|
input: "path }} {{foobar}} {",
|
|
|
|
err: ErrUnbalancedTemplatingCharacter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "closing_in_back",
|
|
|
|
input: "path {{foobar}} }}",
|
|
|
|
err: ErrUnbalancedTemplatingCharacter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic",
|
|
|
|
input: "path /{{identity.entity.id}}/ {",
|
|
|
|
output: "path /entityID/ {",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple",
|
|
|
|
input: "path {{identity.entity.name}} {\n\tval = {{identity.entity.metadata.foo}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
metadata: map[string]string{"foo": "bar"},
|
|
|
|
output: "path entityName {\n\tval = bar\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple_bad_name",
|
|
|
|
input: "path {{identity.entity.name}} {\n\tval = {{identity.entity.metadata.foo}}\n}",
|
|
|
|
metadata: map[string]string{"foo": "bar"},
|
|
|
|
err: ErrTemplateValueNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unbalanced_close",
|
|
|
|
input: "path {{identity.entity.id}} {\n\tval = {{ent}}ity.metadata.foo}}\n}",
|
|
|
|
err: ErrUnbalancedTemplatingCharacter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unbalanced_open",
|
|
|
|
input: "path {{identity.entity.id}} {\n\tval = {{ent{{ity.metadata.foo}}\n}",
|
|
|
|
err: ErrUnbalancedTemplatingCharacter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no_entity_no_directives",
|
|
|
|
input: "path {{identity.entity.id}} {\n\tval = {{ent{{ity.metadata.foo}}\n}",
|
|
|
|
err: ErrNoEntityAttachedToToken,
|
|
|
|
nilEntity: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no_entity_no_diretives",
|
|
|
|
input: "path name {\n\tval = foo\n}",
|
|
|
|
output: "path name {\n\tval = foo\n}",
|
|
|
|
nilEntity: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias_id_name",
|
2019-06-21 17:23:39 +00:00
|
|
|
input: "path {{ identity.entity.name}} {\n\tval = {{identity.entity.aliases.foomount.id}} nval = {{identity.entity.aliases.foomount.name}}\n}",
|
2018-08-15 18:42:56 +00:00
|
|
|
entityName: "entityName",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
aliasID: "aliasID",
|
2019-06-21 17:23:39 +00:00
|
|
|
aliasName: "aliasName",
|
2018-08-15 18:42:56 +00:00
|
|
|
metadata: map[string]string{"foo": "bar"},
|
2019-06-21 17:23:39 +00:00
|
|
|
output: "path entityName {\n\tval = aliasID nval = aliasName\n}",
|
2018-08-15 18:42:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias_id_name_bad_selector",
|
|
|
|
input: "path foobar {\n\tval = {{identity.entity.aliases.foomount}}\n}",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
err: errors.New("invalid alias selector"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias_id_name_bad_accessor",
|
|
|
|
input: "path \"foobar\" {\n\tval = {{identity.entity.aliases.barmount.id}}\n}",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
err: errors.New("alias not found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias_id_name",
|
|
|
|
input: "path \"{{identity.entity.name}}\" {\n\tval = {{identity.entity.aliases.foomount.metadata.zip}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
aliasID: "aliasID",
|
|
|
|
metadata: map[string]string{"foo": "bar"},
|
|
|
|
aliasMetadata: map[string]string{"zip": "zap"},
|
|
|
|
output: "path \"entityName\" {\n\tval = zap\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group_name",
|
|
|
|
input: "path \"{{identity.groups.ids.groupID.name}}\" {\n\tval = {{identity.entity.name}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
groupName: "groupName",
|
|
|
|
output: "path \"groupName\" {\n\tval = entityName\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group_bad_id",
|
|
|
|
input: "path \"{{identity.groups.ids.hroupID.name}}\" {\n\tval = {{identity.entity.name}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
groupName: "groupName",
|
2018-08-23 16:15:02 +00:00
|
|
|
err: errors.New("entity is not a member of group \"hroupID\""),
|
2018-08-15 18:42:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group_id",
|
|
|
|
input: "path \"{{identity.groups.names.groupName.id}}\" {\n\tval = {{identity.entity.name}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
groupName: "groupName",
|
|
|
|
output: "path \"groupID\" {\n\tval = entityName\n}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group_bad_name",
|
|
|
|
input: "path \"{{identity.groups.names.hroupName.id}}\" {\n\tval = {{identity.entity.name}}\n}",
|
|
|
|
entityName: "entityName",
|
|
|
|
groupName: "groupName",
|
2018-08-23 16:15:02 +00:00
|
|
|
err: errors.New("entity is not a member of group \"hroupName\""),
|
2018-08-15 18:42:56 +00:00
|
|
|
},
|
2019-06-21 17:23:39 +00:00
|
|
|
{
|
|
|
|
name: "metadata_object_disallowed",
|
|
|
|
input: "{{identity.entity.metadata}}",
|
|
|
|
metadata: map[string]string{"foo": "bar"},
|
|
|
|
err: ErrTemplateValueNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias_metadata_object_disallowed",
|
|
|
|
input: "{{identity.entity.aliases.foomount.metadata}}",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar"},
|
|
|
|
err: ErrTemplateValueNotFound,
|
|
|
|
},
|
|
|
|
{
|
2019-07-19 17:08:47 +00:00
|
|
|
name: "groups.names_disallowed",
|
|
|
|
input: "{{identity.entity.groups.names}}",
|
2019-06-21 17:23:39 +00:00
|
|
|
groupMemberships: []string{"foo", "bar"},
|
|
|
|
err: ErrTemplateValueNotFound,
|
|
|
|
},
|
|
|
|
{
|
2019-07-19 17:08:47 +00:00
|
|
|
name: "groups.ids_disallowed",
|
|
|
|
input: "{{identity.entity.groups.ids}}",
|
2019-06-21 17:23:39 +00:00
|
|
|
groupMemberships: []string{"foo", "bar"},
|
|
|
|
err: ErrTemplateValueNotFound,
|
|
|
|
},
|
|
|
|
|
|
|
|
// missing selector cases
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "entity id",
|
|
|
|
input: "{{identity.entity.id}}",
|
|
|
|
output: `"entityID"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "entity name",
|
|
|
|
input: "{{identity.entity.name}}",
|
|
|
|
entityName: "entityName",
|
|
|
|
output: `"entityName"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "entity name missing",
|
|
|
|
input: "{{identity.entity.name}}",
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "alias name/id",
|
|
|
|
input: "{{identity.entity.aliases.foomount.id}} {{identity.entity.aliases.foomount.name}}",
|
|
|
|
aliasAccessor: "foomount",
|
|
|
|
aliasID: "aliasID",
|
|
|
|
aliasName: "aliasName",
|
|
|
|
output: `"aliasID" "aliasName"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one metadata key",
|
|
|
|
input: "{{identity.entity.metadata.color}}",
|
|
|
|
metadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `"green"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one metadata key not found",
|
|
|
|
input: "{{identity.entity.metadata.size}}",
|
|
|
|
metadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "all entity metadata",
|
|
|
|
input: "{{identity.entity.metadata}}",
|
|
|
|
metadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `{"color":"green","foo":"bar"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "null entity metadata",
|
|
|
|
input: "{{identity.entity.metadata}}",
|
|
|
|
output: `{}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
2019-07-19 17:08:47 +00:00
|
|
|
name: "groups.names",
|
|
|
|
input: "{{identity.entity.groups.names}}",
|
2019-06-21 17:23:39 +00:00
|
|
|
groupMemberships: []string{"foo", "bar"},
|
|
|
|
output: `["foo","bar"]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
2019-07-19 17:08:47 +00:00
|
|
|
name: "groups.ids",
|
|
|
|
input: "{{identity.entity.groups.ids}}",
|
2019-06-21 17:23:39 +00:00
|
|
|
groupMemberships: []string{"foo", "bar"},
|
|
|
|
output: `["foo_0","bar_1"]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias metadata key",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata.color}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `"green"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias metadata key not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata.size}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias metadata, accessor not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata.size}}",
|
|
|
|
aliasAccessor: "not_gonna_match",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "all alias metadata",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `{"color":"green","foo":"bar"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "null alias metadata",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
output: `{}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "all alias metadata, accessor not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.metadata}}",
|
|
|
|
aliasAccessor: "not_gonna_match",
|
|
|
|
aliasMetadata: map[string]string{"foo": "bar", "color": "green"},
|
|
|
|
output: `{}`,
|
|
|
|
},
|
2021-09-17 18:03:47 +00:00
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias custom metadata key",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata.foo}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasCustomMetadata: map[string]string{"foo": "abc", "bar": "123"},
|
|
|
|
output: `"abc"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias custom metadata key not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata.size}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasCustomMetadata: map[string]string{"foo": "abc", "bar": "123"},
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "one alias custom metadata, accessor not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata.size}}",
|
|
|
|
aliasAccessor: "not_gonna_match",
|
|
|
|
aliasCustomMetadata: map[string]string{"foo": "abc", "bar": "123"},
|
|
|
|
output: `""`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "all alias custom metadata",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
aliasCustomMetadata: map[string]string{"foo": "abc", "bar": "123"},
|
|
|
|
output: `{"bar":"123","foo":"abc"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "null alias custom metadata",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata}}",
|
|
|
|
aliasAccessor: "aws_123",
|
|
|
|
output: `{}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: JSONTemplating,
|
|
|
|
name: "all alias custom metadata, accessor not found",
|
|
|
|
input: "{{identity.entity.aliases.aws_123.custom_metadata}}",
|
|
|
|
aliasAccessor: "not_gonna_match",
|
|
|
|
aliasCustomMetadata: map[string]string{"foo": "abc", "bar": "123"},
|
|
|
|
output: `{}`,
|
|
|
|
},
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2020-01-06 18:16:52 +00:00
|
|
|
var entity *logical.Entity
|
2018-08-15 18:42:56 +00:00
|
|
|
if !test.nilEntity {
|
2020-01-06 18:16:52 +00:00
|
|
|
entity = &logical.Entity{
|
2018-08-15 18:42:56 +00:00
|
|
|
ID: "entityID",
|
|
|
|
Name: test.entityName,
|
|
|
|
Metadata: test.metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if test.aliasAccessor != "" {
|
2020-01-06 18:16:52 +00:00
|
|
|
entity.Aliases = []*logical.Alias{
|
2019-06-21 17:23:39 +00:00
|
|
|
{
|
2021-09-17 18:03:47 +00:00
|
|
|
MountAccessor: test.aliasAccessor,
|
|
|
|
ID: test.aliasID,
|
|
|
|
Name: test.aliasName,
|
|
|
|
Metadata: test.aliasMetadata,
|
|
|
|
CustomMetadata: test.aliasCustomMetadata,
|
2018-08-15 18:42:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 18:16:52 +00:00
|
|
|
var groups []*logical.Group
|
2018-08-15 18:42:56 +00:00
|
|
|
if test.groupName != "" {
|
2020-01-06 18:16:52 +00:00
|
|
|
groups = append(groups, &logical.Group{
|
2018-09-18 03:03:00 +00:00
|
|
|
ID: "groupID",
|
|
|
|
Name: test.groupName,
|
|
|
|
Metadata: test.groupMetadata,
|
2020-01-06 18:16:52 +00:00
|
|
|
NamespaceID: "root",
|
2018-08-15 18:42:56 +00:00
|
|
|
})
|
|
|
|
}
|
2019-06-21 17:23:39 +00:00
|
|
|
|
|
|
|
if test.groupMemberships != nil {
|
|
|
|
for i, groupName := range test.groupMemberships {
|
2020-01-06 18:16:52 +00:00
|
|
|
groups = append(groups, &logical.Group{
|
2019-06-21 17:23:39 +00:00
|
|
|
ID: fmt.Sprintf("%s_%d", groupName, i),
|
|
|
|
Name: groupName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subst, out, err := PopulateString(PopulateStringInput{
|
|
|
|
Mode: test.mode,
|
2018-08-15 18:42:56 +00:00
|
|
|
ValidityCheckOnly: test.validityCheckOnly,
|
|
|
|
String: test.input,
|
|
|
|
Entity: entity,
|
|
|
|
Groups: groups,
|
2020-01-06 18:16:52 +00:00
|
|
|
NamespaceID: "root",
|
2019-06-21 17:23:39 +00:00
|
|
|
Now: test.now,
|
2018-08-15 18:42:56 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if test.err == nil {
|
|
|
|
t.Fatalf("%s: expected success, got error: %v", test.name, err)
|
|
|
|
}
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Fatalf("%s: got error: %v", test.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out != test.output {
|
2019-06-21 17:23:39 +00:00
|
|
|
t.Fatalf("%s: bad output: %s, expected: %s", test.name, out, test.output)
|
2018-08-15 18:42:56 +00:00
|
|
|
}
|
|
|
|
if err == nil && !subst && out != test.input {
|
|
|
|
t.Fatalf("%s: bad subst flag", test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 17:23:39 +00:00
|
|
|
|
|
|
|
func TestPopulate_CurrentTime(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// Test that an unset Now parameter results in current time
|
|
|
|
input := PopulateStringInput{
|
|
|
|
Mode: JSONTemplating,
|
|
|
|
String: `{{time.now}}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, out, err := PopulateString(input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nowPopulated, err := strconv.Atoi(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := math.Abs(float64(int64(nowPopulated) - now.Unix()))
|
|
|
|
if diff > 1 {
|
|
|
|
t.Fatalf("expected time within 1 second. Got diff of: %f", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPopulate_FullObject(t *testing.T) {
|
2021-04-08 16:43:39 +00:00
|
|
|
testEntity := &logical.Entity{
|
2019-06-21 17:23:39 +00:00
|
|
|
ID: "abc-123",
|
|
|
|
Name: "Entity Name",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"color": "green",
|
|
|
|
"size": "small",
|
|
|
|
"non-printable": "\"\n\t",
|
|
|
|
},
|
2020-01-06 18:16:52 +00:00
|
|
|
Aliases: []*logical.Alias{
|
2019-06-21 17:23:39 +00:00
|
|
|
{
|
|
|
|
MountAccessor: "aws_123",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"service": "ec2",
|
|
|
|
"region": "west",
|
|
|
|
},
|
2021-09-17 18:03:47 +00:00
|
|
|
CustomMetadata: map[string]string{
|
|
|
|
"foo": "abc",
|
|
|
|
"bar": "123",
|
|
|
|
},
|
2019-06-21 17:23:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
testGroups := []*logical.Group{
|
2019-06-21 17:23:39 +00:00
|
|
|
{ID: "a08b0c02", Name: "g1"},
|
|
|
|
{ID: "239bef91", Name: "g2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
template := `
|
|
|
|
{
|
|
|
|
"id": {{identity.entity.id}},
|
|
|
|
"name": {{identity.entity.name}},
|
|
|
|
"all metadata": {{identity.entity.metadata}},
|
|
|
|
"one metadata key": {{identity.entity.metadata.color}},
|
|
|
|
"one metadata key not found": {{identity.entity.metadata.asldfk}},
|
|
|
|
"alias metadata": {{identity.entity.aliases.aws_123.metadata}},
|
|
|
|
"alias not found metadata": {{identity.entity.aliases.blahblah.metadata}},
|
|
|
|
"one alias metadata key": {{identity.entity.aliases.aws_123.metadata.service}},
|
|
|
|
"one not found alias metadata key": {{identity.entity.aliases.blahblah.metadata.service}},
|
2019-07-19 17:08:47 +00:00
|
|
|
"group names": {{identity.entity.groups.names}},
|
|
|
|
"group ids": {{identity.entity.groups.ids}},
|
2021-09-17 18:03:47 +00:00
|
|
|
"repeated and": {"nested element": {{identity.entity.name}}},
|
|
|
|
"alias custom metadata": {{identity.entity.aliases.aws_123.custom_metadata}},
|
|
|
|
"alias not found custom metadata": {{identity.entity.aliases.blahblah.custom_metadata}},
|
|
|
|
"one alias custom metadata key": {{identity.entity.aliases.aws_123.custom_metadata.foo}},
|
|
|
|
"one not found alias custom metadata key": {{identity.entity.aliases.blahblah.custom_metadata.foo}},
|
2019-06-21 17:23:39 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"id": "abc-123",
|
|
|
|
"name": "Entity Name",
|
|
|
|
"all metadata": {"color":"green","non-printable":"\"\n\t","size":"small"},
|
|
|
|
"one metadata key": "green",
|
|
|
|
"one metadata key not found": "",
|
|
|
|
"alias metadata": {"region":"west","service":"ec2"},
|
|
|
|
"alias not found metadata": {},
|
|
|
|
"one alias metadata key": "ec2",
|
|
|
|
"one not found alias metadata key": "",
|
|
|
|
"group names": ["g1","g2"],
|
|
|
|
"group ids": ["a08b0c02","239bef91"],
|
2021-09-17 18:03:47 +00:00
|
|
|
"repeated and": {"nested element": "Entity Name"},
|
|
|
|
"alias custom metadata": {"bar":"123","foo":"abc"},
|
|
|
|
"alias not found custom metadata": {},
|
|
|
|
"one alias custom metadata key": "abc",
|
|
|
|
"one not found alias custom metadata key": "",
|
2019-06-21 17:23:39 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
input := PopulateStringInput{
|
|
|
|
Mode: JSONTemplating,
|
|
|
|
String: template,
|
|
|
|
Entity: testEntity,
|
|
|
|
Groups: testGroups,
|
|
|
|
}
|
|
|
|
_, out, err := PopulateString(input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out != expected {
|
|
|
|
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, out)
|
|
|
|
}
|
|
|
|
}
|