Skip to content

Commit

Permalink
wire up BulkCheck from ExperimentalService
Browse files Browse the repository at this point in the history
  • Loading branch information
vroldanbet committed Oct 16, 2023
1 parent 688fd2d commit bda24dc
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
25 changes: 24 additions & 1 deletion authzed/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
ZedToken,
)
from authzed.api.v1.error_reason_pb2 import ErrorReason
from authzed.api.v1.experimental_service_pb2 import (
BulkCheckPermissionPair,
BulkCheckPermissionRequest,
BulkCheckPermissionRequestItem,
BulkCheckPermissionResponse,
BulkCheckPermissionResponseItem,
BulkExportRelationshipsRequest,
BulkExportRelationshipsResponse,
BulkImportRelationshipsRequest,
BulkImportRelationshipsResponse,
)
from authzed.api.v1.experimental_service_pb2_grpc import ExperimentalServiceStub
from authzed.api.v1.permission_service_pb2 import (
CheckPermissionRequest,
CheckPermissionResponse,
Expand Down Expand Up @@ -45,7 +57,7 @@
from authzed.api.v1.schema_service_pb2_grpc import SchemaServiceStub


class Client(SchemaServiceStub, PermissionsServiceStub):
class Client(SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub):
"""
v1 Authzed gRPC API client.
"""
Expand All @@ -60,6 +72,7 @@ def __init__(self, target, credentials, options=None, compression=None):
channel = channelfn(target, credentials, options, compression)
SchemaServiceStub.__init__(self, channel)
PermissionsServiceStub.__init__(self, channel)
ExperimentalServiceStub.__init__(self, channel)


__all__ = [
Expand Down Expand Up @@ -100,4 +113,14 @@ def __init__(self, target, credentials, options=None, compression=None):
"ReadSchemaResponse",
"WriteSchemaRequest",
"WriteSchemaResponse",
# Experimental Service
"BulkCheckPermissionRequest",
"BulkCheckPermissionResponse",
"BulkCheckPermissionPair",
"BulkCheckPermissionRequestItem",
"BulkCheckPermissionResponseItem",
"BulkImportRelationshipsRequest",
"BulkImportRelationshipsResponse",
"BulkExportRelationshipsRequest",
"BulkExportRelationshipsResponse",
]
51 changes: 51 additions & 0 deletions examples/v1/bulk_check_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from authzed.api.v1 import (
BulkCheckPermissionRequest,
BulkCheckPermissionRequestItem,
CheckPermissionResponse,
Client,
Consistency,
ObjectReference,
SubjectReference,
)
from grpcutil import bearer_token_credentials

emilia = SubjectReference(
object=ObjectReference(
object_type="blog/user",
object_id="emilia",
)
)
beatrice = SubjectReference(
object=ObjectReference(
object_type="blog/user",
object_id="beatrice",
)
)

post_one = ObjectReference(object_type="blog/post", object_id="1")

client = Client(
"grpc.authzed.com:443",
bearer_token_credentials("t_your_token_here_1234567deadbeef"),
)

resp = client.BulkCheckPermission(
BulkCheckPermissionRequest(
consistency=Consistency(fully_consistent=True),
items=[
BulkCheckPermissionRequestItem(
resource=post_one,
permission="view",
subject=beatrice,
),
BulkCheckPermissionRequestItem(
resource=post_one,
permission="write",
subject=beatrice,
),
],
)
)
assert len(resp.pairs) == 2
assert resp.pairs[0].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
assert resp.pairs[1].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
35 changes: 35 additions & 0 deletions tests/v1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from google.protobuf.struct_pb2 import Struct

from authzed.api.v1 import (
BulkCheckPermissionRequest,
BulkCheckPermissionRequestItem,
CheckPermissionRequest,
CheckPermissionResponse,
Client,
Expand Down Expand Up @@ -184,6 +186,39 @@ def test_lookup_subjects(client):
assert responses.count(beatrice.object.object_id) == 1


def test_bulk_check(client):
# Write a basic schema.
write_test_schema(client)
beatrice, emilia, post_one, post_two = write_test_tuples(client)

# Issue some checks.
resp = client.BulkCheckPermission(
BulkCheckPermissionRequest(
consistency=Consistency(fully_consistent=True),
items=[
BulkCheckPermissionRequestItem(
resource=post_one,
permission="view",
subject=emilia,
),
BulkCheckPermissionRequestItem(
resource=post_one,
permission="write",
subject=emilia,
),
],
)
)

assert len(resp.pairs) == 2
assert (
resp.pairs[0].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
)
assert (
resp.pairs[1].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
)


def write_test_tuples(client):
emilia = SubjectReference(
object=ObjectReference(
Expand Down

0 comments on commit bda24dc

Please sign in to comment.