-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add identity templating helper to sdk/framework #8088
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "metadata-value", | ||
}, | ||
}, | ||
}, | ||
Metadata: map[string]string{ | ||
"entity-metadata": "metadata-value", | ||
}, | ||
}, | ||
GroupsVal: []*logical.Group{ | ||
{ | ||
ID: "group1-id", | ||
Name: "group1", | ||
Metadata: map[string]string{ | ||
"group-metadata": "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: "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: "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: "metadata-value", | ||
}, | ||
{ | ||
tpl: "{{identity.groups.ids.group1-id.metadata.group-metadata}}", | ||
expected: "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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use a different value than "metadata-value" in the three places it occurs? Would reduce ambiguity in test case expected values.