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 85aea94
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
26 changes: 25 additions & 1 deletion authzed/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@
)
from authzed.api.v1.schema_service_pb2_grpc import SchemaServiceStub

from authzed.api.v1.experimental_service_pb2_grpc import ExperimentalServiceStub
from authzed.api.v1.experimental_service_pb2 import (
BulkCheckPermissionRequest,
BulkCheckPermissionResponse,
BulkCheckPermissionPair,
BulkCheckPermissionRequestItem,
BulkCheckPermissionResponseItem,
BulkImportRelationshipsRequest,
BulkImportRelationshipsResponse,
BulkExportRelationshipsRequest,
BulkExportRelationshipsResponse
)


class Client(SchemaServiceStub, PermissionsServiceStub):
class Client(SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub):
"""
v1 Authzed gRPC API client.
"""
Expand All @@ -60,6 +73,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 +114,14 @@ def __init__(self, target, credentials, options=None, compression=None):
"ReadSchemaResponse",
"WriteSchemaRequest",
"WriteSchemaResponse",
# Experimental Service
"BulkCheckPermissionRequest",
"BulkCheckPermissionResponse",
"BulkCheckPermissionPair",
"BulkCheckPermissionRequestItem",
"BulkCheckPermissionResponseItem",
"BulkImportRelationshipsRequest",
"BulkImportRelationshipsResponse",
"BulkExportRelationshipsRequest",
"BulkExportRelationshipsResponse"
]
48 changes: 48 additions & 0 deletions examples/v1/bulk_check_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from authzed.api.v1 import (
BulkCheckPermissionRequest,
CheckPermissionResponse,
BulkCheckPermissionRequestItem,
Consistency,
Client,
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
28 changes: 28 additions & 0 deletions tests/v1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from authzed.api.v1 import (
CheckPermissionRequest,
CheckPermissionResponse,
BulkCheckPermissionRequest,
BulkCheckPermissionRequestItem,
Client,
Consistency,
ContextualizedCaveat,
Expand Down Expand Up @@ -184,6 +186,32 @@ 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 85aea94

Please sign in to comment.