Skip to content

Commit

Permalink
Add test for immutable grants in cycles. This behavior doesn't curren…
Browse files Browse the repository at this point in the history
…tly work :(
  • Loading branch information
ggreer committed Dec 31, 2024
1 parent 3aad9bf commit f7bd344
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion pkg/sync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func TestExpandGrantImmutable(t *testing.T) {

req := &reader_v2.GrantsReaderServiceListGrantsForEntitlementRequest{
Entitlement: group1Ent,
// PrincipalId: user1.Id,
PageToken: "",
Annotations: nil,
}
Expand Down Expand Up @@ -182,6 +181,119 @@ func TestExpandGrantImmutable(t *testing.T) {
_ = os.Remove(c1zpath)
}

func TestExpandGrantImmutableCycle(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

mc := newMockConnector()

mc.rtDB = append(mc.rtDB, groupResourceType, userResourceType)

group1, group1Ent, err := mc.AddGroup(ctx, "test_group_1")
require.NoError(t, err)
group2, group2Ent, err := mc.AddGroup(ctx, "test_group_2")
require.NoError(t, err)
group3, group3Ent, err := mc.AddGroup(ctx, "test_group_3")
require.NoError(t, err)

user1, err := mc.AddUser(ctx, "user_1")
require.NoError(t, err)
user2, err := mc.AddUser(ctx, "user_2")
require.NoError(t, err)

// Add all users to group 2
_ = mc.AddGroupMember(ctx, group2, user1)
_ = mc.AddGroupMember(ctx, group2, user2)

// Add group 2 to group 1
_ = mc.AddGroupMember(ctx, group1, group2, group2Ent)
// Add group 3 to group 2
_ = mc.AddGroupMember(ctx, group2, group3, group3Ent)
// Add group 1 to group 3
_ = mc.AddGroupMember(ctx, group3, group1, group1Ent)

// Directly add user 1 to group 1 (this grant should not be immutable after expansion)
_ = mc.AddGroupMember(ctx, group1, user1)

tempDir, err := os.MkdirTemp("", "baton-expand-grant-immutable-cycle")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
c1zpath := filepath.Join(tempDir, "expand-grants.c1z")
syncer, err := NewSyncer(ctx, mc, WithC1ZPath(c1zpath), WithTmpDir(tempDir))
require.NoError(t, err)
err = syncer.Sync(ctx)
require.NoError(t, err)
err = syncer.Close(ctx)
require.NoError(t, err)

c1zManager, err := manager.New(ctx, c1zpath)
require.NoError(t, err)

store, err := c1zManager.LoadC1Z(ctx)
require.NoError(t, err)

allGrantsReq := &v2.GrantsServiceListGrantsRequest{}
allGrants, err := store.ListGrants(ctx, allGrantsReq)
require.NoError(t, err)
require.Len(t, allGrants.List, 6)

req := &reader_v2.GrantsReaderServiceListGrantsForEntitlementRequest{
Entitlement: group1Ent,
PageToken: "",
Annotations: nil,
}
resp, err := store.ListGrantsForEntitlement(ctx, req)
require.NoError(t, err)
require.Len(t, resp.List, 2) // both users and group2 should have group1 membership

req = &reader_v2.GrantsReaderServiceListGrantsForEntitlementRequest{
// Entitlement: group1Ent,
PrincipalId: user1.Id,
PageToken: "",
Annotations: nil,
}
resp, err = store.ListGrantsForPrincipal(ctx, req)
require.NoError(t, err)
require.Len(t, resp.List, 2)

grant := resp.List[0]
for _, g := range resp.List {
if g.Entitlement.Id == group1Ent.Id {
grant = g
break
}
}
require.Equal(t, grant.Entitlement.Id, group1Ent.Id)

annos := annotations.Annotations(grant.Annotations)
immutable := &v2.GrantImmutable{}
hasImmutable, err := annos.Pick(immutable)
require.NoError(t, err)

require.False(t, hasImmutable) // Direct grant should not be immutable

req = &reader_v2.GrantsReaderServiceListGrantsForEntitlementRequest{
PrincipalId: user2.Id,
PageToken: "",
Annotations: nil,
}
resp, err = store.ListGrantsForPrincipal(ctx, req)
require.NoError(t, err)
require.Len(t, resp.List, 1)

grant = resp.List[0]

annos = annotations.Annotations(grant.Annotations)
immutable = &v2.GrantImmutable{}
hasImmutable, err = annos.Pick(immutable)
require.NoError(t, err)

// TODO: Make this pass. It would require modifying an existing grant after fixing the cycle
// require.True(t, hasImmutable) // Expanded indirect grant should be immutable
require.False(t, hasImmutable) // TODO: delete this and fix the code so the above line passes

_ = os.Remove(c1zpath)
}
func BenchmarkExpandCircle(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down

0 comments on commit f7bd344

Please sign in to comment.