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: policy key is not GC while a expired policy is GC #511

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
53 changes: 53 additions & 0 deletions e2e/tests/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,3 +1910,56 @@ func (s *StorageTestSuite) TestGrantsPermissionToObjectWithWildcardInName() {
_, err := s.Client.HeadObject(context.Background(), &storagetypes.QueryHeadObjectRequest{BucketName: bucketName, ObjectName: objectName})
s.Require().True(strings.Contains(err.Error(), "No such object"))
}

func (s *StorageTestSuite) TestExpiredPolicyGCAndRePut() {
var err error
ctx := context.Background()
user1 := s.GenAndChargeAccounts(1, 1000000)[0]

_, owner, bucketName, _, _, objectId := s.createObjectWithVisibility(storagetypes.VISIBILITY_TYPE_PUBLIC_READ)

principal := types.NewPrincipalWithAccount(user1.GetAddr())

// Put bucket policy
bucketStatement := &types.Statement{
Actions: []types.ActionType{types.ACTION_DELETE_BUCKET},
Effect: types.EFFECT_ALLOW,
}
expirationTime := time.Now().Add(5 * time.Second)

msgPutBucketPolicy := storagetypes.NewMsgPutPolicy(owner.GetAddr(), types2.NewBucketGRN(bucketName).String(),
principal, []*types.Statement{bucketStatement}, &expirationTime)
s.SendTxBlock(owner, msgPutBucketPolicy)

// Query the policy which is enforced on bucket
grn1 := types2.NewBucketGRN(bucketName)
queryPolicyForAccountResp, err := s.Client.QueryPolicyForAccount(ctx, &storagetypes.QueryPolicyForAccountRequest{
Resource: grn1.String(),
PrincipalAddress: user1.GetAddr().String(),
})
s.Require().NoError(err)
s.Require().Equal(objectId, queryPolicyForAccountResp.Policy.ResourceId)

// wait for policy expired
time.Sleep(5 * time.Second)

// query the policy, which is already GC, should get err.
_, err = s.Client.QueryPolicyForAccount(ctx, &storagetypes.QueryPolicyForAccountRequest{
Resource: grn1.String(),
PrincipalAddress: user1.GetAddr().String(),
})
s.Require().Error(err)

// the user should be able to re-put policy for the bucket.
msgPutBucketPolicy = storagetypes.NewMsgPutPolicy(owner.GetAddr(), types2.NewBucketGRN(bucketName).String(),
principal, []*types.Statement{bucketStatement}, nil)
s.SendTxBlock(owner, msgPutBucketPolicy)

// Query the policy which is enforced on bucket.
queryPolicyForAccountResp, err = s.Client.QueryPolicyForAccount(ctx, &storagetypes.QueryPolicyForAccountRequest{
Resource: grn1.String(),
PrincipalAddress: user1.GetAddr().String(),
})
s.Require().NoError(err)
s.Require().Equal(objectId, queryPolicyForAccountResp.Policy.ResourceId)
}
11 changes: 11 additions & 0 deletions x/permission/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,19 @@ func (k Keeper) RemoveExpiredPolicies(ctx sdk.Context) {
}
store.Delete(iterator.Key())

// delete policyId -> policy
policyId := types.ParsePolicyIdFromQueueKey(iterator.Key())
var policy types.Policy
k.cdc.MustUnmarshal(store.Get(types.GetPolicyByIDKey(policyId)), &policy)
store.Delete(types.GetPolicyByIDKey(policyId))

// delete policyKey -> policyId
if ctx.IsUpgraded(upgradetypes.Pampas) {
policyKey := types.GetPolicyForAccountKey(policy.ResourceId, policy.ResourceType,
policy.Principal.MustGetAccountAddress())
store.Delete(policyKey)
}

ctx.EventManager().EmitTypedEvents(&types.EventDeletePolicy{PolicyId: policyId}) //nolint: errcheck

count++
Expand Down
Loading