Skip to content

Commit

Permalink
fix: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Aug 7, 2023
1 parent 33fd96b commit 3e54afd
Show file tree
Hide file tree
Showing 18 changed files with 552 additions and 691 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/buf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:

jobs:
protobuf-check:
strategy:
matrix:
go-version: [1.20.x]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/stretchr/testify v1.8.4
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.30.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/yaml v1.3.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,8 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a/go.mod h1:KF9sEfUPAXdG8Oev9e99iLGnl2uJMjc5B+4y3O7x610=
gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI=
Expand Down
7 changes: 2 additions & 5 deletions proto/greenfield/permission/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ message GroupMember {
];
// member is the account address of the member
string member = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message GroupMemberExtra {
// expiration_time defines the expiration time of the group member
google.protobuf.Timestamp expiration_time = 1 [
google.protobuf.Timestamp expiration_time = 4 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
(gogoproto.nullable) = true
];
}
4 changes: 4 additions & 0 deletions proto/greenfield/storage/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ message EventCreateGroup {
repeated string members = 5 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// extra defines extra info for the group
string extra = 6;
// added_members_detail defines the all the members detail of the group.
repeated EventGroupMemberDetail added_members_detail = 7;
}

// EventDeleteGroup is emitted on MsgDeleteGroup
Expand Down Expand Up @@ -339,6 +341,8 @@ message EventUpdateGroupMember {
repeated string members_to_add = 5 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// members_to_add defines all the members to be deleted from the group
repeated string members_to_delete = 6 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// added_members_detail defines the all the members detail of the group.
repeated EventGroupMemberDetail added_members_detail = 7;
}

message EventRenewGroupMember {
Expand Down
1 change: 0 additions & 1 deletion proto/greenfield/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ message QueryHeadGroupMemberRequest {

message QueryHeadGroupMemberResponse {
permission.GroupMember group_member = 1;
permission.GroupMemberExtra group_member_extra = 2;
}

message QueryPolicyForGroupRequest {
Expand Down
78 changes: 13 additions & 65 deletions x/permission/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,31 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) AddGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (math.Uint, error) {
func (k Keeper) AddGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, expiration *time.Time) error {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberKey(groupID, member)
if store.Has(memberKey) {
return math.ZeroUint(), storagetypes.ErrGroupMemberAlreadyExists
return storagetypes.ErrGroupMemberAlreadyExists
}
groupMember := types.GroupMember{
GroupId: groupID,
Member: member.String(),
GroupId: groupID,
Member: member.String(),
ExpirationTime: expiration,
}
id := k.groupMemberSeq.NextVal(store)
store.Set(memberKey, id.Bytes())
store.Set(types.GetGroupMemberByIDKey(id), k.cdc.MustMarshal(&groupMember))
return id, nil
}

func (k Keeper) AddGroupMemberWithExpiration(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, expiration time.Time) error {
id, err := k.AddGroupMember(ctx, groupID, member)
if err != nil {
return err
}

store := ctx.KVStore(k.storeKey)
// We can simply override the whole value here, because the expiration time is the only field in the value.
// If there are more fields in the future, we should use a more sophisticated way to update the value.
memberExtra := types.GroupMemberExtra{
ExpirationTime: expiration,
}
store.Set(types.GetGroupMemberExtraKey(groupID, member), id.Bytes())
store.Set(types.GetGroupMemberExtraByIDKey(id), k.cdc.MustMarshal(&memberExtra))
return nil
}

func (k Keeper) UpdateGroupMemberExpiration(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, memberID math.Uint, expiration time.Time) {
func (k Keeper) UpdateGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, memberID math.Uint, expiration *time.Time) {
store := ctx.KVStore(k.storeKey)
// We can simply override the whole value here, because the expiration time is the only field in the value.
// If there are more fields in the future, we should use a more sophisticated way to update the value.
memberExtra := types.GroupMemberExtra{
groupMember := types.GroupMember{
GroupId: groupID,
Member: member.String(),
ExpirationTime: expiration,
}
store.Set(types.GetGroupMemberExtraKey(groupID, member), memberID.Bytes())
store.Set(types.GetGroupMemberExtraByIDKey(memberID), k.cdc.MustMarshal(&memberExtra))
store.Set(types.GetGroupMemberByIDKey(memberID), k.cdc.MustMarshal(&groupMember))
}

func (k Keeper) RemoveGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) error {
Expand All @@ -115,18 +98,6 @@ func (k Keeper) RemoveGroupMember(ctx sdk.Context, groupID math.Uint, member sdk
return nil
}

func (k Keeper) RemoveGroupMemberExtra(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) error {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberExtraKey(groupID, member)
bz := store.Get(memberKey)
if bz == nil {
return storagetypes.ErrNoSuchGroupMember
}
store.Delete(memberKey)
store.Delete(types.GetGroupMemberExtraByIDKey(k.groupMemberSeq.DecodeSequence(bz)))
return nil
}

func (k Keeper) GetGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (*types.GroupMember, bool) {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberKey(groupID, member)
Expand All @@ -150,28 +121,6 @@ func (k Keeper) GetGroupMemberByID(ctx sdk.Context, groupMemberID math.Uint) (*t
return &groupMember, true
}

func (k Keeper) GetGroupMemberExtra(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (*types.GroupMemberExtra, bool) {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberExtraKey(groupID, member)
bz := store.Get(memberKey)
if bz == nil {
return nil, false
}

return k.GetGroupMemberExtraByID(ctx, k.groupMemberSeq.DecodeSequence(bz))
}

func (k Keeper) GetGroupMemberExtraByID(ctx sdk.Context, groupMemberID math.Uint) (*types.GroupMemberExtra, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetGroupMemberExtraByIDKey(groupMemberID))
if bz == nil {
return nil, false
}
var groupMember types.GroupMemberExtra
k.cdc.MustUnmarshal(bz, &groupMember)
return &groupMember, true
}

func (k Keeper) updatePolicy(ctx sdk.Context, policy *types.Policy, newPolicy *types.Policy) *types.Policy {
store := ctx.KVStore(k.storeKey)
policy.Statements = newPolicy.Statements
Expand Down Expand Up @@ -347,7 +296,7 @@ func (k Keeper) VerifyPolicy(ctx sdk.Context, resourceID math.Uint, resourceType
effect, newPolicy = p.Eval(action, ctx.BlockTime(), opts)
if effect != types.EFFECT_UNSPECIFIED {
// check the operator is the member of this group
_, memberFound := k.GetGroupMember(ctx, item.GroupId, operator)
groupMember, memberFound := k.GetGroupMember(ctx, item.GroupId, operator)
if memberFound {
if effect == types.EFFECT_ALLOW {
allowed = true
Expand All @@ -357,9 +306,8 @@ func (k Keeper) VerifyPolicy(ctx sdk.Context, resourceID math.Uint, resourceType
}

// post check if the operator has not been expired
groupMemberExtra, memberExtraFound := k.GetGroupMemberExtra(ctx, item.GroupId, operator)
if allowed && memberExtraFound {
if groupMemberExtra.ExpirationTime.Before(ctx.BlockTime()) {
if allowed && memberFound && groupMember.ExpirationTime != nil {
if groupMember.ExpirationTime.Before(ctx.BlockTime()) {
return types.EFFECT_DENY
}
}
Expand Down
18 changes: 2 additions & 16 deletions x/permission/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ var (
ObjectPolicyForAccountPrefix = []byte{0x12}
GroupPolicyForAccountPrefix = []byte{0x13}
GroupMemberPrefix = []byte{0x14}
GroupMemberExtraPrefix = []byte{0x15}

BucketPolicyForGroupPrefix = []byte{0x21}
ObjectPolicyForGroupPrefix = []byte{0x22}

PolicyByIDPrefix = []byte{0x31}
GroupMemberByIDPrefix = []byte{0x32}
GroupMemberExtraByIDPrefix = []byte{0x33}
PolicyByIDPrefix = []byte{0x31}
GroupMemberByIDPrefix = []byte{0x32}

PolicySequencePrefix = []byte{0x41}
GroupMemberSequencePrefix = []byte{0x42}
Expand Down Expand Up @@ -94,15 +92,3 @@ func GetGroupMemberKey(groupID math.Uint, member sdk.AccAddress) []byte {
func GetGroupMemberByIDKey(memberID math.Uint) []byte {
return append(GroupMemberByIDPrefix, memberID.Bytes()...)
}

func GroupMembersExtraPrefix(groupID math.Uint) []byte {
return append(GroupMemberExtraPrefix, groupID.Bytes()...)
}

func GetGroupMemberExtraKey(groupID math.Uint, member sdk.AccAddress) []byte {
return append(GroupMemberExtraPrefix, append(groupID.Bytes(), member.Bytes()...)...)
}

func GetGroupMemberExtraByIDKey(memberID math.Uint) []byte {
return append(GroupMemberExtraByIDPrefix, memberID.Bytes()...)
}
Loading

0 comments on commit 3e54afd

Please sign in to comment.