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

Fix handling member group IDs #6527

Merged
merged 4 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions vault/identity_store_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,66 @@ import (
"github.com/hashicorp/vault/logical"
)

func TestIdentityStore_FixOverwrittenMemberGroupIDs(t *testing.T) {
c, _, _ := TestCoreUnsealed(t)
ctx := namespace.RootContext(nil)

// Create a group
resp, err := c.identityStore.HandleRequest(ctx, &logical.Request{
Path: "group",
Operation: logical.UpdateOperation,
Data: map[string]interface{}{
"name": "group1",
},
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
}

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

expectedMemberGroupIDs := []string{groupID}

// Create another group and add the above created group as its member
resp, err = c.identityStore.HandleRequest(ctx, &logical.Request{
Path: "group",
Operation: logical.UpdateOperation,
Data: map[string]interface{}{
"name": "group2",
"policies": "default",
"member_group_ids": expectedMemberGroupIDs,
},
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
}

// Create another group and add the above created group as its member
resp, err = c.identityStore.HandleRequest(ctx, &logical.Request{
Path: "group/name/group2",
Operation: logical.UpdateOperation,
Data: map[string]interface{}{
"policies": "default,another-policy",
},
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
}

// Read the group and check if the member_group_ids is intact
resp, err = c.identityStore.HandleRequest(ctx, &logical.Request{
Path: "group/name/group2",
Operation: logical.ReadOperation,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
}

if !reflect.DeepEqual(resp.Data["member_group_ids"], expectedMemberGroupIDs) {
t.Fatalf("bad: member_group_ids; expected: %#v\n, actual: %#v", expectedMemberGroupIDs, resp.Data["member_group_ids"])
}
}

func TestIdentityStore_GroupEntityMembershipUpgrade(t *testing.T) {
c, keys, rootToken := TestCoreUnsealed(t)

Expand Down
14 changes: 12 additions & 2 deletions vault/identity_store_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,14 +1085,23 @@ func (i *IdentityStore) sanitizeAndUpsertGroup(ctx context.Context, group *ident
txn := i.db.Txn(true)
defer txn.Abort()

var currentMemberGroupIDs []string
var currentMemberGroups []*identity.Group

// If there are no member group IDs supplied, then it shouldn't be
// processed. If an empty set of member group IDs are supplied, then it
// should be processed. Hence the nil check instead of the length check.
if memberGroupIDs == nil {
goto ALIAS
}

memberGroupIDs = strutil.RemoveDuplicates(memberGroupIDs, false)

// For those group member IDs that are removed from the list, remove current
// group ID as their respective ParentGroupID.

// Get the current MemberGroups IDs for this group
var currentMemberGroupIDs []string
currentMemberGroups, err := i.MemDBGroupsByParentGroupID(group.ID, false)
currentMemberGroups, err = i.MemDBGroupsByParentGroupID(group.ID, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -1183,6 +1192,7 @@ func (i *IdentityStore) sanitizeAndUpsertGroup(ctx context.Context, group *ident
}
}

ALIAS:
// Sanitize the group alias
if group.Alias != nil {
group.Alias.CanonicalID = group.ID
Expand Down