-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathopa_async.py
More file actions
605 lines (506 loc) · 15.4 KB
/
opa_async.py
File metadata and controls
605 lines (506 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import asyncio
import warnings
import os
import ssl
from typing import Dict, Optional, Union
from urllib.parse import urlencode
import aiofiles
import aiohttp
from aiohttp import ClientSession, TCPConnector
from .errors import (
CheckPermissionError,
ConnectionsError,
DeleteDataError,
DeletePolicyError,
FileError,
PathNotFoundError,
PolicyNotFoundError,
RegoParseError,
TypeException,
)
class AsyncOpaClient:
"""
AsyncOpaClient client object to connect and manipulate OPA service asynchronously.
Parameters:
host (str): Host to connect to OPA service, defaults to 'localhost'.
port (int): Port to connect to OPA service, defaults to 8181.
version (str): REST API version provided by OPA, defaults to 'v1'.
ssl (bool): Verify SSL certificates for HTTPS requests, defaults to False.
cert (Optional[str] or Tuple[str, str]): Path to client certificate or a tuple of (cert_file, key_file).
headers (Optional[dict]): Dictionary of headers to send, defaults to None.
timeout (float): Timeout for requests in seconds, defaults to 1.5.
Example:
async with AsyncOpaClient(host='opa.example.com', ssl=True, cert='/path/to/cert.pem') as client:
await client.check_connection()
"""
def __init__(
self,
host: str = "localhost",
port: int = 8181,
version: str = "v1",
ssl: bool = False,
cert: Optional[Union[str, tuple]] = None,
headers: Optional[dict] = None,
timeout: float = 1.5,
):
self.host = host.strip()
self.port = port
self.version = version
self.ssl = ssl
self.cert = cert
self.timeout = timeout
self.schema = "https://" if ssl else "http://"
self.root_url = f"{self.schema}{self.host}:{self.port}/{self.version}"
self.headers = headers
# Initialize the session attributes
self._session: Optional[ClientSession] = None
self._connector = None # Will be initialized in _init_session
async def __aenter__(self):
await self._init_session()
return self
async def __aexit__(self, exc_type, exc_value, traceback):
await self.close_connection()
async def _init_session(self):
ssl_context = None
if self.ssl:
ssl_context = ssl.create_default_context()
# If cert is provided, load the client certificate
if self.cert:
if isinstance(self.cert, tuple):
# Tuple of (cert_file, key_file)
ssl_context.load_cert_chain(*self.cert)
else:
# Single cert file (might contain both cert and key)
ssl_context.load_cert_chain(self.cert)
else:
# Verify default CA certificates
ssl_context.load_default_certs()
self._connector = TCPConnector(ssl=ssl_context)
self._session = aiohttp.ClientSession(
headers=self.headers,
connector=self._connector,
timeout=aiohttp.ClientTimeout(total=self.timeout),
)
async def close_connection(self):
"""Close the session and release any resources."""
if self._session and not self._session.closed:
await self._session.close()
self._session = None
async def check_connection(self) -> str:
"""
Checks whether the established connection is configured properly.
If not, raises a ConnectionsError.
Returns:
str: Confirmation message if the connection is successful.
"""
url = f"{self.root_url}/policies/"
try:
async with self._session.get(url) as response:
if response.status == 200:
return True
else:
raise ConnectionsError(
"Service unreachable", "Check config and try again"
)
except Exception as e:
raise ConnectionsError(
"Service unreachable", "Check config and try again"
) from e
async def check_health(
self, query: Dict[str, bool] = None, diagnostic_url: str = None
) -> bool:
"""
Check if OPA is healthy.
Parameters:
query (Dict[str, bool], optional): Query parameters for health check.
diagnostic_url (str, optional): Custom diagnostic URL.
Returns:
bool: True if OPA is healthy, False otherwise.
"""
url = diagnostic_url or f"{self.schema}{self.host}:{self.port}/health"
if query:
url = f"{url}?{urlencode(query)}"
try:
async with self._session.get(url) as response:
return response.status == 200
except Exception:
return False
async def get_policies_list(self) -> list:
"""Returns all OPA policies in the service."""
url = f"{self.root_url}/policies/"
async with self._session.get(url) as response:
response.raise_for_status()
policies = await response.json()
result = policies.get("result", [])
return [policy.get("id") for policy in result if policy.get("id")]
async def get_policies_info(self) -> dict:
"""
Returns information about each policy, including
policy path and policy rules.
"""
url = f"{self.root_url}/policies/"
async with self._session.get(url) as response:
response.raise_for_status()
policies = await response.json()
result = policies.get("result", [])
policies_info = {}
for policy in result:
policy_id = policy.get("id")
ast = policy.get("ast", {})
package_path = "/".join(
[
p.get("value")
for p in ast.get("package", {}).get("path", [])
]
)
rules = list(
set(
rule.get("head", {}).get("name")
for rule in ast.get("rules", [])
)
)
policy_url = f"{self.root_url}/{package_path}"
rules_urls = [f"{policy_url}/{rule}" for rule in rules if rule]
policies_info[policy_id] = {
"path": policy_url,
"rules": rules_urls,
}
return policies_info
async def update_policy_from_string(
self, new_policy: str, endpoint: str
) -> bool:
"""
Update OPA policy using a policy string.
Parameters:
new_policy (str): The new policy in Rego language.
endpoint (str): The policy endpoint in OPA.
Returns:
bool: True if the policy was successfully updated.
"""
if not new_policy or not isinstance(new_policy, str):
raise TypeException("new_policy must be a non-empty string")
url = f"{self.root_url}/policies/{endpoint}"
headers = self.headers.copy() if self.headers else {}
headers["Content-Type"] = "text/plain"
async with self._session.put(
url, data=new_policy.encode("utf-8"), headers=self.headers
) as response:
if response.status == 200:
return True
error = await response.json()
raise RegoParseError(error.get("code"), error.get("message"))
async def update_policy_from_file(
self, filepath: str, endpoint: str
) -> bool:
"""
Update OPA policy using a policy file.
Parameters:
filepath (str): Path to the policy file.
endpoint (str): The policy endpoint in OPA.
Returns:
bool: True if the policy was successfully updated.
"""
if not os.path.isfile(filepath):
raise FileError(f"'{filepath}' is not a valid file")
async with aiofiles.open(filepath, "r", encoding="utf-8") as file:
policy_str = await file.read()
return await self.update_policy_from_string(policy_str, endpoint)
async def update_policy_from_url(self, url: str, endpoint: str) -> bool:
"""
Update OPA policy by fetching it from a URL.
Parameters:
url (str): URL to fetch the policy from.
endpoint (str): The policy endpoint in OPA.
Returns:
bool: True if the policy was successfully updated.
"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
policy_str = await response.text()
return await self.update_policy_from_string(policy_str, endpoint)
async def update_or_create_data(
self, new_data: dict, endpoint: str
) -> bool:
"""
Update or create OPA data.
Parameters:
new_data (dict): The data to be updated or created.
endpoint (str): The data endpoint in OPA.
Returns:
bool: True if the data was successfully updated or created.
"""
if not isinstance(new_data, dict):
raise TypeException("new_data must be a dictionary")
url = f"{self.root_url}/data/{endpoint}"
headers = self.headers.copy() if self.headers else {}
headers["Content-Type"] = "application/json"
async with self._session.put(
url, json=new_data, headers=headers
) as response:
if response.status == 204:
return True
else:
error = await response.json()
raise RegoParseError(error.get("code"), error.get("message"))
async def get_data(
self, data_name: str = "", query_params: Dict[str, bool] = None
) -> dict:
"""
Get OPA data.
Parameters:
data_name (str, optional): The name of the data to retrieve.
query_params (Dict[str, bool], optional): Query parameters.
Returns:
dict: The retrieved data.
"""
url = f"{self.root_url}/data/{data_name}"
if query_params:
url = f"{url}?{urlencode(query_params)}"
async with self._session.get(url) as response:
if response.status == 200:
return await response.json()
else:
error = await response.json()
raise PolicyNotFoundError(
error.get("code"), error.get("message")
)
async def policy_to_file(
self,
policy_name: str,
path: Optional[str] = None,
filename: str = "opa_policy.rego",
) -> bool:
"""
Save an OPA policy to a file.
Parameters:
policy_name (str): The name of the policy.
path (Optional[str]): The directory path to save the file.
filename (str): The name of the file.
Returns:
bool: True if the policy was successfully saved.
"""
policy = await self.get_policy(policy_name)
policy_raw = policy.get("result", {}).get("raw", "")
if not policy_raw:
raise PolicyNotFoundError("Policy content is empty")
full_path = os.path.join(path or "", filename)
try:
async with aiofiles.open(full_path, "w", encoding="utf-8") as file:
await file.write(policy_raw)
return True
except OSError as e:
raise PathNotFoundError(f"Failed to write to '{full_path}'") from e
async def get_policy(self, policy_name: str) -> dict:
"""
Get a specific OPA policy.
Parameters:
policy_name (str): The name of the policy.
Returns:
dict: The policy data.
"""
url = f"{self.root_url}/policies/{policy_name}"
async with self._session.get(url) as response:
if response.status == 200:
return await response.json()
else:
error = await response.json()
raise PolicyNotFoundError(
error.get("code"), error.get("message")
)
async def delete_policy(self, policy_name: str) -> bool:
"""
Delete an OPA policy.
Parameters:
policy_name (str): The name of the policy.
Returns:
bool: True if the policy was successfully deleted.
"""
url = f"{self.root_url}/policies/{policy_name}"
async with self._session.delete(url) as response:
if response.status == 200:
return True
else:
error = await response.json()
raise DeletePolicyError(
error.get("code"), error.get("message")
)
async def delete_data(self, data_name: str) -> bool:
"""
Delete OPA data.
Parameters:
data_name (str): The name of the data.
Returns:
bool: True if the data was successfully deleted.
"""
url = f"{self.root_url}/data/{data_name}"
async with self._session.delete(url) as response:
if response.status == 204:
return True
else:
error = await response.json()
raise DeleteDataError(error.get("code"), error.get("message"))
async def check_permission(
self,
input_data: dict,
policy_name: str,
rule_name: str,
query_params: Dict[str, bool] = None,
) -> dict:
"""
Check permissions based on input data, policy name, and rule name.
Parameters:
input_data (dict): The input data to check against the policy.
policy_name (str): The name of the policy.
rule_name (str): The name of the rule in the policy.
query_params (Dict[str, bool], optional): Query parameters.
Returns:
dict: The result of the permission check.
"""
warnings.warn(
"check_permission is deprecated and will be removed in a future release. Use `query_rule` instead.",
DeprecationWarning,
stacklevel=2
)
policy = await self.get_policy(policy_name)
ast = policy.get("result", {}).get("ast", {})
package_path = "/".join(
[p.get("value") for p in ast.get("package", {}).get("path", [])]
)
rules = [
rule.get("head", {}).get("name") for rule in ast.get("rules", [])
]
if rule_name not in rules:
raise CheckPermissionError(
f"Rule '{rule_name}' not found in policy '{policy_name}'"
)
url = f"{self.root_url}/data/{package_path}/{rule_name}"
if query_params:
url = f"{url}?{urlencode(query_params)}"
async with self._session.post(
url, json={"input": input_data}
) as response:
response.raise_for_status()
return await response.json()
async def query_rule(
self,
input_data: dict,
package_path: str,
rule_name: Optional[str] = None,
) -> dict:
"""
Query a specific rule in a package.
Parameters:
input_data (dict): The input data for the query.
package_path (str): The package path.
rule_name (Optional[str]): The rule name.
Returns:
dict: The result of the query.
"""
path = package_path.replace(".", "/")
if rule_name:
path = f"{path}/{rule_name}"
url = f"{self.root_url}/data/{path}"
async with self._session.post(
url, json={"input": input_data}
) as response:
response.raise_for_status()
return await response.json()
async def ad_hoc_query(self, query: str, input_data: dict = None) -> dict:
"""
Execute an ad-hoc query.
Parameters:
query (str): The query string.
input_data (dict, optional): The input data for the query.
Returns:
dict: The result of the query.
"""
url = f"{self.schema}{self.host}:{self.port}/v1/query"
params = {"q": query}
payload = {"input": input_data} if input_data else None
async with self._session.post(
url, params=params, json=payload
) as response:
response.raise_for_status()
return await response.json()
# Property methods for read-only access to certain attributes
@property
def host(self) -> str:
return self._host
@host.setter
def host(self, value: str):
self._host = value
@property
def port(self) -> int:
return self._port
@port.setter
def port(self, value: int):
if not isinstance(value, int):
raise TypeError("Port must be an integer")
self._port = value
@property
def version(self) -> str:
return self._version
@version.setter
def version(self, value: str):
self._version = value
@property
def schema(self) -> str:
return self._schema
@schema.setter
def schema(self, value: str):
self._schema = value
@property
def root_url(self) -> str:
return self._root_url
@root_url.setter
def root_url(self, value: str):
self._root_url = value
@property
def ssl(self) -> bool:
return self._ssl
@ssl.setter
def ssl(self, value: bool):
self._ssl = value
@property
def cert(self) -> Optional[str]:
return self._cert
@cert.setter
def cert(self, value: Optional[str]):
self._cert = value
@property
def headers(self) -> dict:
return self._headers
@headers.setter
def headers(self, value: dict):
self._headers = value
@property
def timeout(self) -> float:
return self._timeout
@timeout.setter
def timeout(self, value: float):
self._timeout = value
# Example usage:
async def main():
async with AsyncOpaClient() as client:
try:
result = await client.check_connection()
print(result)
finally:
await client.close_connection()
# Run the example
if __name__ == "__main__":
asyncio.run(main())
# Example usage:
async def main():
async with AsyncOpaClient(
host="localhost",
port=8181,
ssl=True,
cert=("/path/to/cert.pem", "/path/to/key.pem"),
) as client:
try:
result = await client.check_connection()
print(result)
finally:
await client.close_connection()