Skip to content

Commit 2e50500

Browse files
author
Ronen Hilewicz
committed
Latest grpc bindings
1 parent 1b9ed94 commit 2e50500

File tree

15 files changed

+179
-116
lines changed

15 files changed

+179
-116
lines changed

poetry.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ packages = [
3131
[tool.poetry.dependencies]
3232
python = "^3.9"
3333
aiohttp = "^3.10.2"
34-
aserto-directory = "^0.33.6"
35-
aserto-authorizer = "^0.20.5"
34+
aserto-directory = "^0.33.8"
35+
aserto-authorizer = "^0.20.7"
3636

3737
[tool.poetry.group.dev.dependencies]
3838
black = "^25.1.0"
@@ -49,6 +49,16 @@ target-version = ["py38"]
4949
[tool.isort]
5050
profile = "black"
5151

52+
[tool.pylint]
53+
max-line-length = 100
54+
disable = [
55+
"missing-module-docstring",
56+
"missing-class-docstring",
57+
"missing-function-docstring",
58+
"too-many-arguments",
59+
"too-many-positional-arguments",
60+
"too-many-public-methods",
61+
]
5262

5363
[build-system]
5464
requires = ["poetry-core>=1.0.0"]

src/aserto/client/_typing.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/aserto/client/authorizer/__init__.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import datetime
22
import typing
33

4-
import aserto.authorizer.v2 as authorizer
5-
import aserto.authorizer.v2.api as api
64
import grpc
7-
import grpc.aio as grpcaio
5+
6+
import aserto.authorizer.v2 as authorizer
87
from aserto.authorizer.v2 import (
98
CompileResponse,
109
GetPolicyResponse,
1110
ListPoliciesResponse,
1211
QueryOptions,
1312
QueryResponse,
1413
)
14+
15+
from aserto.authorizer.v2 import api
1516
from aserto.authorizer.v2.api import IdentityContext, IdentityType
1617

1718
import aserto.client._deadline as timeout
18-
import aserto.client.authorizer.helpers as helpers
1919
import aserto.client.resource_context as res_ctx
20+
from aserto.client.authorizer import helpers
2021
from aserto.client.authorizer.helpers import DecisionTree
2122
from aserto.client.identity import Identity
2223
from aserto.client.options import AuthorizerOptions
2324
from aserto.client.resource_context import ResourceContext
2425

26+
if typing.TYPE_CHECKING:
27+
Metadata = grpc.Metadata
28+
else:
29+
Metadata = typing.NewType("Metadata", typing.Tuple)
30+
2531

2632
class AuthorizerClient:
2733
def __init__(
@@ -48,16 +54,16 @@ def _headers(self) -> typing.Mapping[str, str]:
4854
return self._options.auth_headers
4955

5056
@property
51-
def _metadata(self) -> grpcaio.Metadata:
52-
return grpcaio.Metadata(*tuple(self._headers.items()))
57+
def _metadata(self) -> Metadata:
58+
return tuple(self._headers.items())
5359

5460
def decision_tree(
5561
self,
5662
*,
5763
policy_path_root: str,
5864
decisions: typing.Sequence[str],
59-
policy_instance_name: typing.Optional[str] = None,
60-
policy_instance_label: typing.Optional[str] = None,
65+
policy_instance_name: str = "",
66+
policy_instance_label: str = "",
6167
resource_context: typing.Optional[ResourceContext] = None,
6268
policy_path_separator: typing.Optional[typing.Literal["DOT", "SLASH"]] = None,
6369
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -93,8 +99,8 @@ def decisions(
9399
*,
94100
policy_path: str,
95101
decisions: typing.Sequence[str],
96-
policy_instance_name: typing.Optional[str],
97-
policy_instance_label: typing.Optional[str] = None,
102+
policy_instance_name: str = "",
103+
policy_instance_label: str = "",
98104
resource_context: typing.Optional[ResourceContext] = None,
99105
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
100106
) -> typing.Dict[str, bool]:
@@ -129,8 +135,8 @@ def query(
129135
input: str,
130136
policy_path: str,
131137
decisions: typing.Sequence[str],
132-
policy_instance_name: typing.Optional[str],
133-
policy_instance_label: typing.Optional[str] = None,
138+
policy_instance_name: str = "",
139+
policy_instance_label: str = "",
134140
resource_context: typing.Optional[ResourceContext] = None,
135141
options: typing.Optional[QueryOptions] = None,
136142
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -168,8 +174,8 @@ def compile(
168174
disable_inlining: typing.Sequence[str],
169175
policy_path: str,
170176
decisions: typing.Sequence[str],
171-
policy_instance_name: typing.Optional[str],
172-
policy_instance_label: typing.Optional[str] = None,
177+
policy_instance_name: str,
178+
policy_instance_label: str = "",
173179
resource_context: typing.Optional[ResourceContext] = None,
174180
options: typing.Optional[QueryOptions] = None,
175181
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -203,8 +209,8 @@ def compile(
203209
def list_policies(
204210
self,
205211
*,
206-
policy_instance_name: typing.Optional[str],
207-
policy_instance_label: typing.Optional[str] = None,
212+
policy_instance_name: str,
213+
policy_instance_label: str = "",
208214
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
209215
) -> ListPoliciesResponse:
210216
response = self.client.ListPolicies(
@@ -226,8 +232,8 @@ def get_policy(
226232
self,
227233
*,
228234
id: str,
229-
policy_instance_name: typing.Optional[str],
230-
policy_instance_label: typing.Optional[str] = None,
235+
policy_instance_name: str,
236+
policy_instance_label: str = "",
231237
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
232238
) -> GetPolicyResponse:
233239
response = self.client.GetPolicy(

src/aserto/client/authorizer/aio/__init__.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
import datetime
22
import typing
33

4-
import aserto.authorizer.v2 as authorizer
5-
import aserto.authorizer.v2.api as api
64
import grpc.aio as grpc
5+
from grpc import ssl_channel_credentials
6+
7+
import aserto.authorizer.v2 as authorizer
78
from aserto.authorizer.v2 import (
89
CompileResponse,
910
GetPolicyResponse,
1011
ListPoliciesResponse,
1112
QueryOptions,
1213
QueryResponse,
1314
)
15+
16+
import aserto.authorizer.v2.api as api
1417
from aserto.authorizer.v2.api import IdentityContext, IdentityType
15-
from grpc import ssl_channel_credentials
1618

1719
import aserto.client._deadline as timeout
18-
import aserto.client.authorizer.helpers as helpers
1920
import aserto.client.resource_context as res_ctx
21+
from aserto.client.authorizer import helpers
2022
from aserto.client.authorizer.helpers import DecisionTree
2123
from aserto.client.identity import Identity
2224
from aserto.client.options import AuthorizerOptions
2325
from aserto.client.resource_context import ResourceContext
2426

27+
if typing.TYPE_CHECKING:
28+
AuthorizerAsyncStub = authorizer.AuthorizerAsyncStub
29+
else:
30+
AuthorizerAsyncStub = authorizer.AuthorizerStub
31+
2532

2633
class AuthorizerClient:
2734
def __init__(
@@ -41,7 +48,7 @@ def __init__(
4148
target=self._options.url,
4249
credentials=ssl_channel_credentials(self._options.cert),
4350
)
44-
self.client = authorizer.AuthorizerStub(self._channel)
51+
self.client = typing.cast(AuthorizerAsyncStub, authorizer.AuthorizerStub(self._channel))
4552

4653
@property
4754
def _headers(self) -> typing.Mapping[str, str]:
@@ -56,8 +63,8 @@ async def decision_tree(
5663
*,
5764
policy_path_root: str,
5865
decisions: typing.Sequence[str],
59-
policy_instance_name: typing.Optional[str] = None,
60-
policy_instance_label: typing.Optional[str] = None,
66+
policy_instance_name: str = "",
67+
policy_instance_label: str = "",
6168
resource_context: typing.Optional[ResourceContext] = None,
6269
policy_path_separator: typing.Optional[typing.Literal["DOT", "SLASH"]] = None,
6370
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -93,8 +100,8 @@ async def decisions(
93100
*,
94101
policy_path: str,
95102
decisions: typing.Sequence[str],
96-
policy_instance_name: typing.Optional[str],
97-
policy_instance_label: typing.Optional[str] = None,
103+
policy_instance_name: str = "",
104+
policy_instance_label: str = "",
98105
resource_context: typing.Optional[ResourceContext] = None,
99106
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
100107
) -> typing.Dict[str, bool]:
@@ -129,8 +136,8 @@ async def query(
129136
input: str,
130137
policy_path: str,
131138
decisions: typing.Sequence[str],
132-
policy_instance_name: typing.Optional[str],
133-
policy_instance_label: typing.Optional[str] = None,
139+
policy_instance_name: str = "",
140+
policy_instance_label: str = "",
134141
resource_context: typing.Optional[ResourceContext] = None,
135142
options: typing.Optional[QueryOptions] = None,
136143
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -168,8 +175,8 @@ async def compile(
168175
disable_inlining: typing.Sequence[str],
169176
policy_path: str,
170177
decisions: typing.Sequence[str],
171-
policy_instance_name: typing.Optional[str],
172-
policy_instance_label: typing.Optional[str] = None,
178+
policy_instance_name: str = "",
179+
policy_instance_label: str = "",
173180
resource_context: typing.Optional[ResourceContext] = None,
174181
options: typing.Optional[QueryOptions] = None,
175182
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
@@ -203,8 +210,8 @@ async def compile(
203210
async def list_policies(
204211
self,
205212
*,
206-
policy_instance_name: typing.Optional[str],
207-
policy_instance_label: typing.Optional[str] = None,
213+
policy_instance_name: str = "",
214+
policy_instance_label: str = "",
208215
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
209216
) -> ListPoliciesResponse:
210217
response = await self.client.ListPolicies(
@@ -226,8 +233,8 @@ async def get_policy(
226233
self,
227234
*,
228235
id: str,
229-
policy_instance_name: typing.Optional[str],
230-
policy_instance_label: typing.Optional[str] = None,
236+
policy_instance_name: str = "",
237+
policy_instance_label: str = "",
231238
deadline: typing.Optional[typing.Union[datetime.datetime, datetime.timedelta]] = None,
232239
) -> GetPolicyResponse:
233240
return await self.client.GetPolicy(
@@ -244,7 +251,13 @@ async def get_policy(
244251
),
245252
)
246253

247-
async def close(self) -> None:
248-
"""Closes the gRPC channel"""
254+
async def close(self, grace: typing.Optional[float] = None) -> None:
255+
"""Closes the authorizer client's connection to the server.
249256
250-
await self._channel.close()
257+
If a grace period is specified, this method waits until all active
258+
requests are finished or until the grace period is reached. Requests that haven't
259+
been terminated within the grace period are aborted.
260+
If a grace period is not specified (by passing None for grace),
261+
all existing requests are cancelled immediately.
262+
"""
263+
await self._channel.close(grace)

src/aserto/client/authorizer/helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
from aserto.authorizer.v2 import DecisionTreeResponse, PathSeparator
44

5-
from aserto.client._typing import assert_unreachable
6-
75
DecisionTree = Dict[str, Dict[str, bool]]
86

97

10-
def policy_path_separator_field(policy_path_separator: Literal["DOT", "SLASH"]) -> PathSeparator:
8+
def policy_path_separator_field(
9+
policy_path_separator: Literal["DOT", "SLASH"],
10+
) -> PathSeparator.ValueType:
1111
if policy_path_separator == "DOT":
1212
return PathSeparator.PATH_SEPARATOR_DOT
13-
elif policy_path_separator == "SLASH":
13+
if policy_path_separator == "SLASH":
1414
return PathSeparator.PATH_SEPARATOR_SLASH
15-
else:
16-
assert_unreachable(policy_path_separator)
15+
16+
raise ValueError(f"Invalid PathSeparator: {policy_path_separator}")
1717

1818

1919
def validate_decision_tree(response: DecisionTreeResponse) -> DecisionTree:

src/aserto/client/directory/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ def get_metadata(api_key, tenant_id) -> Tuple[Tuple[str, str], ...]:
3030

3131
def translate_rpc_error(err: RpcError) -> None:
3232
if err.code() == StatusCode.NOT_FOUND:
33-
raise NotFoundError from err
33+
raise NotFoundError(err.details()) from err
3434
if err.code() == StatusCode.INVALID_ARGUMENT:
35-
raise InvalidArgumentError from err
35+
raise InvalidArgumentError(err.details()) from err

src/aserto/client/directory/aio/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import grpc.aio as grpc_aio
21
from typing import Optional
2+
3+
import grpc.aio as grpc_aio
4+
35
from aserto.client.directory.channels import channel_credentials, validate_addresses
46

57

@@ -41,7 +43,7 @@ def __init__(
4143
exporter_address,
4244
model_address,
4345
]
44-
self._channels = dict()
46+
self._channels = {}
4547
for x in self._addresses:
4648
if x and x not in self._channels:
4749
self._channels[x] = build_grpc_channel(x, ca_cert_path=ca_cert_path)

0 commit comments

Comments
 (0)