Skip to content
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

Return group memberships of entity during read #3526

Merged
merged 9 commits into from
Nov 6, 2017
12 changes: 12 additions & 0 deletions vault/identity_store_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ func (i *IdentityStore) handleEntityReadCommon(entity *identity.Entity) (*logica
// formats
respData["aliases"] = aliasesToReturn

// Fetch the groups this entity belongs to and return their identifiers
groups, err := i.MemDBGroupsByMemberEntityID(entity.ID, false, false)
if err != nil {
return nil, err
}

groupIDs := make([]string, len(groups))
for i, group := range groups {
groupIDs[i] = group.ID
}
respData["group_ids"] = groupIDs

return &logical.Response{
Data: respData,
}, nil
Expand Down
56 changes: 56 additions & 0 deletions vault/identity_store_entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,62 @@ import (
"github.com/hashicorp/vault/logical"
)

func TestIdentityStore_EntityReadGroupIDs(t *testing.T) {
var err error
var resp *logical.Response

i, _, _ := testIdentityStoreWithGithubAuth(t)

entityReq := &logical.Request{
Path: "entity",
Operation: logical.UpdateOperation,
}

resp, err = i.HandleRequest(entityReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\nerr: %v", resp, err)
}

entityID := resp.Data["id"].(string)

groupReq := &logical.Request{
Path: "group",
Operation: logical.UpdateOperation,
Data: map[string]interface{}{
"member_entity_ids": []string{
entityID,
},
},
}

resp, err = i.HandleRequest(groupReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\nerr: %v", resp, err)
}

groupID := resp.Data["id"].(string)

lookupReq := &logical.Request{
Path: "lookup/entity",
Operation: logical.UpdateOperation,
Data: map[string]interface{}{
"type": "id",
"id": entityID,
},
}

resp, err = i.HandleRequest(lookupReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\nerr: %v", resp, err)
}

expected := []string{groupID}
actual := resp.Data["group_ids"].([]string)
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("bad: group_ids; expected: %#v\nactual: %#v\n", expected, actual)
}
}

func TestIdentityStore_EntityCreateUpdate(t *testing.T) {
var err error
var resp *logical.Response
Expand Down