/
permission.go
335 lines (308 loc) · 12.1 KB
/
permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package keeper
import (
"fmt"
"strings"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
types2 "github.com/bnb-chain/greenfield/types"
gnfderrors "github.com/bnb-chain/greenfield/types/errors"
gnfdresource "github.com/bnb-chain/greenfield/types/resource"
permtypes "github.com/bnb-chain/greenfield/x/permission/types"
"github.com/bnb-chain/greenfield/x/storage/types"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)
var (
PublicReadBucketAllowedActions = map[permtypes.ActionType]bool{
permtypes.ACTION_GET_OBJECT: true,
permtypes.ACTION_COPY_OBJECT: true,
permtypes.ACTION_EXECUTE_OBJECT: true,
permtypes.ACTION_LIST_OBJECT: true,
}
PublicReadObjectAllowedActions = map[permtypes.ActionType]bool{
permtypes.ACTION_GET_OBJECT: true,
permtypes.ACTION_COPY_OBJECT: true,
permtypes.ACTION_EXECUTE_OBJECT: true,
}
)
// VerifyBucketPermission Bucket permissions checks are divided into three steps:
// First, if the bucket is a public bucket and the action is a read-only action, it returns "allow".
// Second, if the operator is the owner of the bucket, it returns "allow", as the owner has the highest permission.
// Third, verify the policy corresponding to the bucket and the operator.
// 1. If the policy is evaluated as "allow", return "allow" to the user.
// 2. If it is evaluated as "deny" or "unspecified", return "deny".
func (k Keeper) VerifyBucketPermission(ctx sdk.Context, bucketInfo *types.BucketInfo, operator sdk.AccAddress,
action permtypes.ActionType, options *permtypes.VerifyOptions,
) permtypes.Effect {
// if bucket is public, anyone can read but can not write it.
if bucketInfo.Visibility == storagetypes.VISIBILITY_TYPE_PUBLIC_READ && PublicReadBucketAllowedActions[action] {
return permtypes.EFFECT_ALLOW
}
// if the operator is empty(may anonymous user), don't need check policy
if operator.Empty() {
return permtypes.EFFECT_DENY
}
// The owner has full permissions
if operator.Equals(sdk.MustAccAddressFromHex(bucketInfo.Owner)) {
return permtypes.EFFECT_ALLOW
}
// verify policy
effect := k.VerifyPolicy(ctx, bucketInfo.Id, gnfdresource.RESOURCE_TYPE_BUCKET, operator, action, options)
if effect == permtypes.EFFECT_ALLOW {
return permtypes.EFFECT_ALLOW
}
return permtypes.EFFECT_DENY
}
// VerifyObjectPermission Object permission checks are divided into four steps:
// First, if the object is a public object and the action is a read-only action, it returns "allow".
// Second, if the operator is the owner of the bucket, it returns "allow"
// Third, verify the policy corresponding to the bucket and the operator
// 1. If it is evaluated as "deny", return "deny"
// 2. If it is evaluated as "allow" or "unspecified", go ahead (Noted as EffectBucket)
//
// Four, verify the policy corresponding to the object and the operator
// 1. If it is evaluated as "deny", return "deny".
// 2. If it is evaluated as "allow", return "allow".
// 3. If it is evaluated as "unspecified", then if the EffectBucket is "allow", return allow
// 4. If it is evaluated as "unspecified", then if the EffectBucket is "unspecified", return deny
func (k Keeper) VerifyObjectPermission(ctx sdk.Context, bucketInfo *types.BucketInfo, objectInfo *types.ObjectInfo,
operator sdk.AccAddress, action permtypes.ActionType,
) permtypes.Effect {
// anyone can read but can not write it when the following case: 1) object is public 2) object is inherit, only when bucket is public
visibility := false
if objectInfo.Visibility == storagetypes.VISIBILITY_TYPE_PUBLIC_READ ||
(objectInfo.Visibility == storagetypes.VISIBILITY_TYPE_INHERIT && bucketInfo.Visibility == storagetypes.VISIBILITY_TYPE_PUBLIC_READ) {
visibility = true
}
if visibility && PublicReadObjectAllowedActions[action] {
return permtypes.EFFECT_ALLOW
}
// if the operator is empty(may anonymous user), don't need check policy
if operator.Empty() {
return permtypes.EFFECT_DENY
}
// The owner has full permissions
ownerAcc := sdk.MustAccAddressFromHex(objectInfo.Owner)
if ownerAcc.Equals(operator) {
return permtypes.EFFECT_ALLOW
}
// verify policy
opts := &permtypes.VerifyOptions{
Resource: types2.NewObjectGRN(objectInfo.BucketName, objectInfo.ObjectName).String(),
}
bucketEffect := k.VerifyPolicy(ctx, bucketInfo.Id, gnfdresource.RESOURCE_TYPE_BUCKET, operator, action, opts)
if bucketEffect == permtypes.EFFECT_DENY {
return permtypes.EFFECT_DENY
}
objectEffect := k.VerifyPolicy(ctx, objectInfo.Id, gnfdresource.RESOURCE_TYPE_OBJECT, operator, action,
nil)
if objectEffect == permtypes.EFFECT_DENY {
return permtypes.EFFECT_DENY
}
if bucketEffect == permtypes.EFFECT_ALLOW || objectEffect == permtypes.EFFECT_ALLOW {
return permtypes.EFFECT_ALLOW
}
return permtypes.EFFECT_DENY
}
func (k Keeper) VerifyGroupPermission(ctx sdk.Context, groupInfo *types.GroupInfo, operator sdk.AccAddress,
action permtypes.ActionType,
) permtypes.Effect {
// The owner has full permissions
if strings.EqualFold(groupInfo.Owner, operator.String()) {
return permtypes.EFFECT_ALLOW
}
// verify policy
effect := k.VerifyPolicy(ctx, groupInfo.Id, gnfdresource.RESOURCE_TYPE_GROUP, operator, action, nil)
if effect == permtypes.EFFECT_ALLOW {
return permtypes.EFFECT_ALLOW
}
return permtypes.EFFECT_DENY
}
func (k Keeper) VerifyPolicy(ctx sdk.Context, resourceID math.Uint, resourceType gnfdresource.ResourceType,
operator sdk.AccAddress, action permtypes.ActionType, opts *permtypes.VerifyOptions,
) permtypes.Effect {
// verify policy which grant permission to account
policy, found := k.permKeeper.GetPolicyForAccount(ctx, resourceID, resourceType, operator)
if found {
effect, newPolicy := policy.Eval(action, ctx.BlockTime(), opts)
if effect != permtypes.EFFECT_UNSPECIFIED {
if effect == permtypes.EFFECT_ALLOW && action == permtypes.ACTION_CREATE_OBJECT && newPolicy != nil && ctx.TxBytes() != nil {
_, err := k.permKeeper.PutPolicy(ctx, newPolicy)
if err != nil {
panic(fmt.Sprintf("Update policy error, %s", err))
}
}
return effect
}
}
// verify policy which grant permission to group
policyGroup, found := k.permKeeper.GetPolicyGroupForResource(ctx, resourceID, resourceType)
if found {
allowed := false
var allowedPolicy *permtypes.Policy
for _, item := range policyGroup.Items {
if !k.hasGroup(ctx, item.GroupId) {
continue
}
// check the group has the right permission of this resource
p := k.permKeeper.MustGetPolicyByID(ctx, item.PolicyId)
effect, newPolicy := p.Eval(action, ctx.BlockTime(), opts)
if effect != permtypes.EFFECT_UNSPECIFIED {
// check the operator is the member of this group
groupMember, memberFound := k.permKeeper.GetGroupMember(ctx, item.GroupId, operator)
if memberFound && (groupMember.ExpirationTime == nil || groupMember.ExpirationTime.After(ctx.BlockTime())) {
if effect == permtypes.EFFECT_ALLOW {
allowed = true
allowedPolicy = newPolicy
} else if effect == permtypes.EFFECT_DENY {
return permtypes.EFFECT_DENY
}
}
}
}
if allowed {
if action == permtypes.ACTION_CREATE_OBJECT && allowedPolicy != nil && ctx.TxBytes() != nil {
_, err := k.permKeeper.PutPolicy(ctx, allowedPolicy)
if err != nil {
panic(fmt.Sprintf("Update policy error, %s", err))
}
}
return permtypes.EFFECT_ALLOW
}
}
return permtypes.EFFECT_UNSPECIFIED
}
func (k Keeper) GetPolicy(ctx sdk.Context, grn types2.GRN, principal *permtypes.Principal) (*permtypes.Policy, error) {
_, resID, err := k.getResourceOwnerAndIdFromGRN(ctx, grn)
if err != nil {
return nil, err
}
var policy *permtypes.Policy
var found bool
if principal.Type == permtypes.PRINCIPAL_TYPE_GNFD_ACCOUNT {
policy, found = k.permKeeper.GetPolicyForAccount(ctx, resID, grn.ResourceType(),
principal.MustGetAccountAddress())
} else if principal.Type == permtypes.PRINCIPAL_TYPE_GNFD_GROUP {
policy, found = k.permKeeper.GetPolicyForGroup(ctx, resID, grn.ResourceType(), principal.MustGetGroupID())
} else {
return nil, permtypes.ErrInvalidPrincipal
}
if !found {
return nil, types.ErrNoSuchPolicy.Wrapf("GRN: %s, principal:%s", grn.String(), principal.String())
}
return policy, nil
}
func (k Keeper) PutPolicy(ctx sdk.Context, operator sdk.AccAddress, grn types2.GRN,
policy *permtypes.Policy,
) (math.Uint, error) {
resOwner, resID, err := k.getResourceOwnerAndIdFromGRN(ctx, grn)
if err != nil {
return math.ZeroUint(), err
}
if !operator.Equals(resOwner) {
return math.ZeroUint(), types.ErrAccessDenied.Wrapf(
"Only resource owner can put policy, operator (%s), owner(%s)",
operator.String(), resOwner.String())
}
k.NormalizePrincipal(ctx, policy.Principal)
err = k.ValidatePrincipal(ctx, resOwner, policy.Principal)
if err != nil {
return math.ZeroUint(), err
}
policy.ResourceId = resID
return k.permKeeper.PutPolicy(ctx, policy)
}
func (k Keeper) DeletePolicy(
ctx sdk.Context, operator sdk.AccAddress, principal *permtypes.Principal, grn types2.GRN,
) (math.Uint, error) {
resOwner, resID, err := k.getResourceOwnerAndIdFromGRN(ctx, grn)
if err != nil {
return math.ZeroUint(), err
}
if !operator.Equals(resOwner) {
return math.ZeroUint(), types.ErrAccessDenied.Wrapf(
"Only resource owner can delete bucket policy, operator (%s), owner(%s)",
operator.String(), resOwner.String())
}
return k.permKeeper.DeletePolicy(ctx, principal, grn.ResourceType(), resID)
}
func (k Keeper) NormalizePrincipal(ctx sdk.Context, principal *permtypes.Principal) {
if principal.Type == permtypes.PRINCIPAL_TYPE_GNFD_GROUP {
if _, err := math.ParseUint(principal.Value); err == nil {
return
}
var grn types2.GRN
if err := grn.ParseFromString(principal.Value, false); err != nil {
return
}
groupOwner, groupName, err := grn.GetGroupOwnerAndAccount()
if err != nil {
return
}
if groupInfo, found := k.GetGroupInfo(ctx, groupOwner, groupName); found {
principal.Value = groupInfo.Id.String()
}
}
}
func (k Keeper) ValidatePrincipal(ctx sdk.Context, resOwner sdk.AccAddress, principal *permtypes.Principal) error {
if principal.Type == permtypes.PRINCIPAL_TYPE_GNFD_ACCOUNT {
principalAccAddress, err := principal.GetAccountAddress()
if err != nil {
return err
}
if principalAccAddress.Equals(resOwner) {
return gnfderrors.ErrInvalidPrincipal.Wrapf("principal account can not be the bucket owner")
}
} else if principal.Type == permtypes.PRINCIPAL_TYPE_GNFD_GROUP {
groupID, err := math.ParseUint(principal.Value)
if err != nil {
return err
}
_, found := k.GetGroupInfoById(ctx, groupID)
if !found {
return types.ErrNoSuchGroup
}
} else {
return permtypes.ErrInvalidPrincipal.Wrapf("Unknown principal type.")
}
return nil
}
func (k Keeper) getResourceOwnerAndIdFromGRN(ctx sdk.Context, grn types2.GRN) (resOwner sdk.AccAddress, resID math.Uint, err error) {
switch grn.ResourceType() {
case gnfdresource.RESOURCE_TYPE_BUCKET:
bucketName, grnErr := grn.GetBucketName()
if grnErr != nil {
return resOwner, resID, grnErr
}
bucketInfo, found := k.GetBucketInfo(ctx, bucketName)
if !found {
return resOwner, resID, types.ErrNoSuchBucket.Wrapf("bucketName: %s", bucketName)
}
resOwner = sdk.MustAccAddressFromHex(bucketInfo.Owner)
resID = bucketInfo.Id
case gnfdresource.RESOURCE_TYPE_OBJECT:
bucketName, objectName, grnErr := grn.GetBucketAndObjectName()
if grnErr != nil {
return resOwner, resID, grnErr
}
objectInfo, found := k.GetObjectInfo(ctx, bucketName, objectName)
if !found {
return resOwner, resID, types.ErrNoSuchObject.Wrapf("BucketName: %s, objectName: %s", bucketName, objectName)
}
resOwner = sdk.MustAccAddressFromHex(objectInfo.Owner)
resID = objectInfo.Id
case gnfdresource.RESOURCE_TYPE_GROUP:
groupOwner, groupName, grnErr := grn.GetGroupOwnerAndAccount()
if grnErr != nil {
return resOwner, resID, grnErr
}
groupInfo, found := k.GetGroupInfo(ctx, groupOwner, groupName)
if !found {
return resOwner, resID, types.ErrNoSuchBucket.Wrapf("groupOwner: %s, groupName: %s", groupOwner.String(), groupName)
}
resOwner = sdk.MustAccAddressFromHex(groupInfo.Owner)
resID = groupInfo.Id
default:
return resOwner, resID, gnfderrors.ErrInvalidGRN.Wrap("Unknown resource type in greenfield resource name")
}
return resOwner, resID, nil
}