-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add identity templating helper to sdk/framework (#8088)
* Add identity templating helper to sdk/framework * Cleanup a bit * Fix length issue when groups/aliases are filtered due to ns * review feedback
- Loading branch information
1 parent
e8c1ef5
commit 5fc424d
Showing
26 changed files
with
1,827 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package framework | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/vault/sdk/helper/identitytpl" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
// PopulateIdentityTemplate takes a template string, an entity ID, and an | ||
// instance of system view. It will query system view for information about the | ||
// entity and use the resulting identity information to populate the template | ||
// string. | ||
func PopulateIdentityTemplate(tpl string, entityID string, sysView logical.SystemView) (string, error) { | ||
entity, err := sysView.EntityInfo(entityID) | ||
if err != nil { | ||
return "", err | ||
} | ||
if entity == nil { | ||
return "", errors.New("no entity found") | ||
} | ||
|
||
groups, err := sysView.GroupsForEntity(entityID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
input := identitytpl.PopulateStringInput{ | ||
String: tpl, | ||
Entity: entity, | ||
Groups: groups, | ||
Mode: identitytpl.ACLTemplating, | ||
} | ||
|
||
_, out, err := identitytpl.PopulateString(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
// ValidateIdentityTemplate takes a template string and returns if the string is | ||
// a valid identity template. | ||
func ValidateIdentityTemplate(tpl string) (bool, error) { | ||
hasTemplating, _, err := identitytpl.PopulateString(identitytpl.PopulateStringInput{ | ||
Mode: identitytpl.ACLTemplating, | ||
ValidityCheckOnly: true, | ||
String: tpl, | ||
}) | ||
if err != nil { | ||
return false, errwrap.Wrapf("failed to validate policy templating: {{err}}", err) | ||
} | ||
|
||
return hasTemplating, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package framework | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
func TestIdentityTemplating(t *testing.T) { | ||
sysView := &logical.StaticSystemView{ | ||
EntityVal: &logical.Entity{ | ||
ID: "test-id", | ||
Name: "test", | ||
Aliases: []*logical.Alias{ | ||
{ | ||
ID: "alias-id", | ||
Name: "test alias", | ||
MountAccessor: "test_mount", | ||
MountType: "secret", | ||
Metadata: map[string]string{ | ||
"alias-metadata": "alias-metadata-value", | ||
}, | ||
}, | ||
}, | ||
Metadata: map[string]string{ | ||
"entity-metadata": "entity-metadata-value", | ||
}, | ||
}, | ||
GroupsVal: []*logical.Group{ | ||
{ | ||
ID: "group1-id", | ||
Name: "group1", | ||
Metadata: map[string]string{ | ||
"group-metadata": "group-metadata-value", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tCases := []struct { | ||
tpl string | ||
expected string | ||
}{ | ||
{ | ||
tpl: "{{identity.entity.id}}", | ||
expected: "test-id", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.name}}", | ||
expected: "test", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.metadata.entity-metadata}}", | ||
expected: "entity-metadata-value", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.aliases.test_mount.id}}", | ||
expected: "alias-id", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.aliases.test_mount.id}}", | ||
expected: "alias-id", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.aliases.test_mount.name}}", | ||
expected: "test alias", | ||
}, | ||
{ | ||
tpl: "{{identity.entity.aliases.test_mount.metadata.alias-metadata}}", | ||
expected: "alias-metadata-value", | ||
}, | ||
{ | ||
tpl: "{{identity.groups.ids.group1-id.name}}", | ||
expected: "group1", | ||
}, | ||
{ | ||
tpl: "{{identity.groups.names.group1.id}}", | ||
expected: "group1-id", | ||
}, | ||
{ | ||
tpl: "{{identity.groups.names.group1.metadata.group-metadata}}", | ||
expected: "group-metadata-value", | ||
}, | ||
{ | ||
tpl: "{{identity.groups.ids.group1-id.metadata.group-metadata}}", | ||
expected: "group-metadata-value", | ||
}, | ||
} | ||
|
||
for _, tCase := range tCases { | ||
out, err := PopulateIdentityTemplate(tCase.tpl, "test", sysView) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if out != tCase.expected { | ||
t.Fatalf("got %q, expected %q", out, tCase.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.