forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
4,298 additions
and
1,458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
--- | ||
version: "v1" | ||
version: v1 | ||
plugins: | ||
- remote: "buf.build/library/plugins/go:v1.27.1-1" | ||
out: "proto" | ||
opt: "paths=source_relative" | ||
- remote: "buf.build/library/plugins/go-grpc:v1.1.0-2" | ||
out: "proto" | ||
opt: "paths=source_relative" | ||
- remote: buf.build/jirkad/plugins/protoc-gen-validate:v0.6.7 | ||
- plugin: buf.build/protocolbuffers/go:v1.32.0 | ||
out: proto | ||
opt: paths=source_relative | ||
- plugin: buf.build/grpc/go:v1.3.0 | ||
out: proto | ||
opt: paths=source_relative,require_unimplemented_servers=true | ||
- plugin: buf.build/bufbuild/validate-go:v1.0.4 | ||
out: proto | ||
opt: paths=source_relative | ||
- plugin: buf.build/grpc-ecosystem/gateway:v2.19.0 | ||
out: proto | ||
opt: | ||
- paths=source_relative | ||
- lang=go | ||
- remote: "buf.build/grpc-ecosystem/plugins/grpc-gateway:v2.5.0-1" | ||
out: "proto" | ||
opt: "paths=source_relative" | ||
- remote: "buf.build/grpc-ecosystem/plugins/openapiv2:v2.6.0-1" | ||
out: "proto" | ||
opt: "allow_merge=true" | ||
- allow_repeated_fields_in_body=true | ||
- plugin: buf.build/grpc-ecosystem/openapiv2:v2.19.0 | ||
out: proto | ||
opt: | ||
- allow_repeated_fields_in_body=true | ||
- output_format=yaml | ||
- allow_merge=true | ||
- merge_file_name=siren | ||
- openapi_naming_strategy=simple | ||
- json_names_for_fields=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,3 +255,282 @@ func TestHandler_CheckResourcePermission(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestHandler_CheckResourceUserPermission(t *testing.T) { | ||
var userEmail = "[email protected]" | ||
tests := []struct { | ||
name string | ||
setup func(res *mocks.ResourceService) | ||
request *shieldv1beta1.CheckResourceUserPermissionRequest | ||
want *shieldv1beta1.CheckResourceUserPermissionResponse | ||
wantErr error | ||
}{ | ||
{ | ||
name: "Deprecated check single resource permission: should return internal error if relation service's CheckAuthz function returns some error", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, errors.New("some error")) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: grpcInternalServerError, | ||
}, | ||
{ | ||
name: "Deprecated check single resource permission: should return true when CheckAuthz function returns true bool", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(true, nil) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: &shieldv1beta1.CheckResourceUserPermissionResponse{ | ||
ResourcePermissions: []*shieldv1beta1.CheckResourceUserPermissionResponse_ResourcePermissionResponse{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
Allowed: true, | ||
}, | ||
}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "Deprecated check single resource permission: should return false when CheckAuthz function returns false bool", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, nil) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: &shieldv1beta1.CheckResourceUserPermissionResponse{ | ||
ResourcePermissions: []*shieldv1beta1.CheckResourceUserPermissionResponse_ResourcePermissionResponse{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
Allowed: false, | ||
}, | ||
}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "Deprecated check single resource permission: should return unauthenticated error if relation service's CheckAuthz function returns auth error", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, user.ErrInvalidEmail) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: grpcUnauthenticated, | ||
}, | ||
{ | ||
name: "should return internal error if relation service's CheckAuthz function returns some error", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, errors.New("some error")) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: grpcInternalServerError, | ||
}, | ||
{ | ||
name: "should return unauthenticated error if relation service's CheckAuthz function returns auth error", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, user.ErrInvalidEmail) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: grpcUnauthenticated, | ||
}, | ||
{ | ||
name: "should return validation error if the request has empty resource permission list", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, errors.New("some error")) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{}, | ||
}, | ||
want: nil, | ||
wantErr: fmt.Errorf("%s: %s", ErrRequestBodyValidation, "resource_permissions"), | ||
}, | ||
{ | ||
name: "should return true when CheckAuthz function returns true bool", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(true, nil) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: &shieldv1beta1.CheckResourceUserPermissionResponse{ | ||
ResourcePermissions: []*shieldv1beta1.CheckResourceUserPermissionResponse_ResourcePermissionResponse{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
Allowed: true, | ||
}, | ||
}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return false when CheckAuthz function returns false bool", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, nil) | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
}, | ||
}, | ||
want: &shieldv1beta1.CheckResourceUserPermissionResponse{ | ||
ResourcePermissions: []*shieldv1beta1.CheckResourceUserPermissionResponse_ResourcePermissionResponse{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
Allowed: false, | ||
}, | ||
}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "should return internal error if resource service's CheckAuthz returns some error even with multiple resource check failures", | ||
setup: func(res *mocks.ResourceService) { | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.EditPermission}).Return(false, errors.New("some error")) | ||
res.EXPECT().CheckAuthz(mock.AnythingOfType("*context.valueCtx"), resource.Resource{ | ||
Name: testRelationV2.Object.ID, | ||
NamespaceID: testRelationV2.Object.NamespaceID, | ||
}, action.Action{ID: schema.ViewPermission}).Return(false, errors.New("some error")).Twice() | ||
}, | ||
request: &shieldv1beta1.CheckResourceUserPermissionRequest{ | ||
Id: userEmail, | ||
ResourcePermissions: []*shieldv1beta1.ResourcePermission{ | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.EditPermission, | ||
}, | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.ViewPermission, | ||
}, | ||
{ | ||
ObjectId: testRelationV2.Object.ID, | ||
ObjectNamespace: testRelationV2.Object.NamespaceID, | ||
Permission: schema.ViewPermission, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: grpcInternalServerError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockResourceSrv := new(mocks.ResourceService) | ||
if tt.setup != nil { | ||
tt.setup(mockResourceSrv) | ||
} | ||
|
||
mockDep := Handler{resourceService: mockResourceSrv, checkAPILimit: 5} | ||
resp, err := mockDep.CheckResourceUserPermission(context.Background(), tt.request) | ||
assert.EqualValues(t, tt.want, resp) | ||
assert.EqualValues(t, tt.wantErr, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.