From 85aea94a3177ec1cb57e0fe14a92f73647d2fe4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rold=C3=A1n=20Betancort?= Date: Mon, 16 Oct 2023 09:55:05 +0100 Subject: [PATCH] wire up BulkCheck from ExperimentalService --- authzed/api/v1/__init__.py | 26 ++++++++++++++- examples/v1/bulk_check_permissions.py | 48 +++++++++++++++++++++++++++ tests/v1_test.py | 28 ++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 examples/v1/bulk_check_permissions.py diff --git a/authzed/api/v1/__init__.py b/authzed/api/v1/__init__.py index 2a86e7c..1e13b70 100644 --- a/authzed/api/v1/__init__.py +++ b/authzed/api/v1/__init__.py @@ -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. """ @@ -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__ = [ @@ -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" ] diff --git a/examples/v1/bulk_check_permissions.py b/examples/v1/bulk_check_permissions.py new file mode 100644 index 0000000..651e075 --- /dev/null +++ b/examples/v1/bulk_check_permissions.py @@ -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 diff --git a/tests/v1_test.py b/tests/v1_test.py index 1a60a83..194f313 100644 --- a/tests/v1_test.py +++ b/tests/v1_test.py @@ -6,6 +6,8 @@ from authzed.api.v1 import ( CheckPermissionRequest, CheckPermissionResponse, + BulkCheckPermissionRequest, + BulkCheckPermissionRequestItem, Client, Consistency, ContextualizedCaveat, @@ -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(