h2o-audit-trail 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- h2o_audit_trail-0.1.0/.gitignore +4 -0
- h2o_audit_trail-0.1.0/Makefile +17 -0
- h2o_audit_trail-0.1.0/PKG-INFO +14 -0
- h2o_audit_trail-0.1.0/PUBLIC_README.md +1 -0
- h2o_audit_trail-0.1.0/local/example.py +135 -0
- h2o_audit_trail-0.1.0/pyproject.toml +101 -0
- h2o_audit_trail-0.1.0/requirements.txt +8 -0
- h2o_audit_trail-0.1.0/src/.openapi-generator-ignore +4 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/__init__.py +3 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/_version.py +3 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/auth/__init__.py +0 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/auth/token_api_client.py +25 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/connection_config.py +116 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/event/__init__.py +0 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/event/client.py +144 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/event/create.py +72 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/event/event.py +136 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/event/search.py +150 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/exception.py +36 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/__init__.py +27 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/api/__init__.py +3 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/api/event_service_api.py +451 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/api_client.py +896 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/apis/__init__.py +17 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/configuration.py +446 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/exceptions.py +158 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/__init__.py +5 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/audittrailv1_status.py +273 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/googlerpc_status.py +277 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/protobuf_any.py +263 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/search_events_request_filter.py +323 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_batch_create_events_request.py +275 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_batch_create_events_response.py +275 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_create_event_request.py +279 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_create_event_response.py +269 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_event.py +340 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_search_events_request.py +277 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model/v1_search_events_response.py +277 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/model_utils.py +2056 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/models/__init__.py +22 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/gen/rest.py +352 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/login.py +136 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/model/__init__.py +0 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/model/rpc_status.py +33 -0
- h2o_audit_trail-0.1.0/src/h2o_audit_trail/model/status.py +34 -0
- h2o_audit_trail-0.1.0/tests/conftest.py +22 -0
- h2o_audit_trail-0.1.0/tests/e2e/event/test_batch_create_events.py +83 -0
- h2o_audit_trail-0.1.0/tests/e2e/event/test_create_event.py +60 -0
- h2o_audit_trail-0.1.0/tests/e2e/event/test_search_events.py +70 -0
- h2o_audit_trail-0.1.0/tests/testutil/eventutil/event.py +114 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: h2o-audit-trail
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for H2O Audit Trail
|
|
5
|
+
Project-URL: Documentation, https://docs.h2o.ai/audit-trail/py/initialization
|
|
6
|
+
Author-email: "H2O.ai" <support@h2o.ai>
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Requires-Dist: h2o-authn>=1.0.0
|
|
9
|
+
Requires-Dist: h2o-cloud-discovery>=2.1.0
|
|
10
|
+
Requires-Dist: python-dateutil>=2.8.2
|
|
11
|
+
Requires-Dist: requests>=2.32.4
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# `h2o-audit-trail`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# `h2o-audit-trail`
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from datetime import timezone
|
|
4
|
+
|
|
5
|
+
import urllib3
|
|
6
|
+
from urllib3.exceptions import InsecureRequestWarning
|
|
7
|
+
|
|
8
|
+
import h2o_audit_trail
|
|
9
|
+
from h2o_audit_trail.event.create import CreateEventRequest
|
|
10
|
+
from h2o_audit_trail.event.event import Event
|
|
11
|
+
from h2o_audit_trail.event.search import SearchEventsRequestFilter
|
|
12
|
+
from h2o_audit_trail.model.status import Status
|
|
13
|
+
|
|
14
|
+
# We don't want to display warning messages:
|
|
15
|
+
# "InsecureRequestWarning: Unverified HTTPS request is being made to host 'audittrail.cloud-dev.h2o.dev' ... "
|
|
16
|
+
urllib3.disable_warnings(InsecureRequestWarning)
|
|
17
|
+
|
|
18
|
+
event_client = h2o_audit_trail.login_custom(
|
|
19
|
+
endpoint="https://audittrail.cloud-dev.h2o.dev",
|
|
20
|
+
# Pass in a valid platform token for cloud-dev.
|
|
21
|
+
refresh_token="<PLATFORM_TOKEN>",
|
|
22
|
+
issuer_url="https://auth.cloud-dev.h2o.dev/auth/realms/hac",
|
|
23
|
+
client_id="hac-platform-public",
|
|
24
|
+
verify_ssl=False,
|
|
25
|
+
).event_client
|
|
26
|
+
|
|
27
|
+
start_time = datetime.now(timezone.utc)
|
|
28
|
+
event1_id = str(uuid.uuid4())
|
|
29
|
+
event2_id = str(uuid.uuid4())
|
|
30
|
+
event3_id = str(uuid.uuid4())
|
|
31
|
+
event4_id = str(uuid.uuid4())
|
|
32
|
+
|
|
33
|
+
# Create one event.
|
|
34
|
+
event = event_client.create_event(
|
|
35
|
+
event_id=event1_id,
|
|
36
|
+
event=Event(
|
|
37
|
+
event_time=datetime.now(timezone.utc),
|
|
38
|
+
event_source="h2oai-enginemanager-server",
|
|
39
|
+
action="actions/enginemanager/daiEngines/CREATE",
|
|
40
|
+
read_only=False,
|
|
41
|
+
status=Status(
|
|
42
|
+
code=0,
|
|
43
|
+
),
|
|
44
|
+
principal="users/jans",
|
|
45
|
+
source_ip_address="1.1.1.1",
|
|
46
|
+
metadata={"display_name": "event1"},
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
print(f"created {event.metadata["display_name"]} ({event.name})")
|
|
50
|
+
|
|
51
|
+
# Create multiple events in one request. Supports partial failure.
|
|
52
|
+
batch_resp = event_client.batch_create_events(
|
|
53
|
+
requests=[
|
|
54
|
+
CreateEventRequest(
|
|
55
|
+
event_id=event2_id,
|
|
56
|
+
event=Event(
|
|
57
|
+
event_time=datetime.now(timezone.utc),
|
|
58
|
+
event_source="h2oai-enginemanager-server",
|
|
59
|
+
action="actions/enginemanager/daiEngines/CREATE",
|
|
60
|
+
read_only=False,
|
|
61
|
+
status=Status(
|
|
62
|
+
code=0,
|
|
63
|
+
),
|
|
64
|
+
principal="users/jans",
|
|
65
|
+
source_ip_address="1.1.1.1",
|
|
66
|
+
metadata={"display_name": "event2"},
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
CreateEventRequest(
|
|
70
|
+
event_id=event3_id,
|
|
71
|
+
event=Event(
|
|
72
|
+
event_time=datetime.now(timezone.utc),
|
|
73
|
+
event_source="h2oai-enginemanager-server",
|
|
74
|
+
action="actions/enginemanager/daiEngines/CREATE",
|
|
75
|
+
read_only=False,
|
|
76
|
+
status=Status(
|
|
77
|
+
code=0,
|
|
78
|
+
),
|
|
79
|
+
principal="users/foo",
|
|
80
|
+
source_ip_address="1.1.1.1",
|
|
81
|
+
metadata={"display_name": "event3"},
|
|
82
|
+
),
|
|
83
|
+
),
|
|
84
|
+
CreateEventRequest(
|
|
85
|
+
event_id=event4_id,
|
|
86
|
+
event=Event(
|
|
87
|
+
event_time=datetime.now(timezone.utc),
|
|
88
|
+
event_source="h2oai-enginemanager-server",
|
|
89
|
+
action="actions/enginemanager/daiEngines/CREATE",
|
|
90
|
+
read_only=False,
|
|
91
|
+
status=Status(
|
|
92
|
+
code=0,
|
|
93
|
+
),
|
|
94
|
+
principal="users/jans",
|
|
95
|
+
source_ip_address="1.1.1.1",
|
|
96
|
+
metadata={"display_name": "event4"},
|
|
97
|
+
),
|
|
98
|
+
),
|
|
99
|
+
],
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
print(f"batch created {len(batch_resp.events)} event(s)")
|
|
103
|
+
for event in batch_resp.events:
|
|
104
|
+
print(f"\tcreated {event.metadata["display_name"]} ({event.name})")
|
|
105
|
+
|
|
106
|
+
print(f"failed to batch create {len(batch_resp.failed_requests)} event(s)")
|
|
107
|
+
for idx, rpcStatus in batch_resp.failed_requests:
|
|
108
|
+
print(f"\tfailed request on index {idx}: {rpcStatus.code} | {rpcStatus.message}")
|
|
109
|
+
|
|
110
|
+
# Search for events with principal "users/jans" within a time window [start_time, end_time).
|
|
111
|
+
# Using pagination (2 events per page).
|
|
112
|
+
end_time = datetime.now(timezone.utc)
|
|
113
|
+
page_token = ""
|
|
114
|
+
max_pages = 5
|
|
115
|
+
|
|
116
|
+
for i in range(max_pages):
|
|
117
|
+
search_resp = event_client.search_events(
|
|
118
|
+
filter_=SearchEventsRequestFilter(
|
|
119
|
+
start_event_time=start_time,
|
|
120
|
+
end_event_time=end_time,
|
|
121
|
+
principal_exact="users/jans",
|
|
122
|
+
),
|
|
123
|
+
page_size=2,
|
|
124
|
+
page_token=page_token,
|
|
125
|
+
)
|
|
126
|
+
print(f"page {i + 1}, got {len(search_resp.events)} event(s):")
|
|
127
|
+
for event in search_resp.events:
|
|
128
|
+
print(f"\t{event.metadata["display_name"]} ({event.name})")
|
|
129
|
+
|
|
130
|
+
page_token = search_resp.next_page_token
|
|
131
|
+
|
|
132
|
+
print(f"next page available: {'Yes' if page_token else 'No'}")
|
|
133
|
+
|
|
134
|
+
if not page_token:
|
|
135
|
+
break
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-requirements-txt"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "h2o-audit-trail"
|
|
7
|
+
dynamic = ["version", "dependencies"]
|
|
8
|
+
description = 'Python client for H2O Audit Trail'
|
|
9
|
+
readme = "PUBLIC_README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
keywords = []
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "H2O.ai", email = "support@h2o.ai" },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[tool.hatch.metadata.hooks.requirements_txt]
|
|
17
|
+
files = ["requirements.txt"]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Documentation = "https://docs.h2o.ai/audit-trail/py/initialization"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/h2o_audit_trail"]
|
|
24
|
+
|
|
25
|
+
[tool.hatch.version]
|
|
26
|
+
path = "../../VERSION"
|
|
27
|
+
pattern = "(?P<version>.*)"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.envs.test]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"pytest==8.1.1",
|
|
32
|
+
"pytest-timeout==2.3.1",
|
|
33
|
+
"pytest-xdist==3.5.0",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[tool.hatch.envs.test.scripts]
|
|
37
|
+
e2e = [
|
|
38
|
+
"python -m pytest -n 1 tests/e2e",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
timeout = 10
|
|
43
|
+
|
|
44
|
+
[tool.hatch.envs.lint]
|
|
45
|
+
python = "3.10"
|
|
46
|
+
dependencies = [
|
|
47
|
+
"black==23.1.0",
|
|
48
|
+
"isort==5.12.0",
|
|
49
|
+
"mypy==1.6.0",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.envs.lint.scripts]
|
|
53
|
+
# TODO use flake8 and mypy
|
|
54
|
+
check = [
|
|
55
|
+
"black --check --diff .",
|
|
56
|
+
"isort --check --diff .",
|
|
57
|
+
# In case a third-party library does not have type hints, we can "--install-types"
|
|
58
|
+
# which installs stub packages that contain type hints for another library (but not actual code)
|
|
59
|
+
# so mypy can check correctly passed types to these libraries.
|
|
60
|
+
"mypy --install-types --non-interactive src"
|
|
61
|
+
]
|
|
62
|
+
fix = [
|
|
63
|
+
"black .",
|
|
64
|
+
"isort ."
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.black]
|
|
68
|
+
exclude = ["src/h2o_audit_trail/gen"]
|
|
69
|
+
skip-magic-trailing-comma = true
|
|
70
|
+
|
|
71
|
+
[tool.isort]
|
|
72
|
+
skip = ["src/h2o_audit_trail/gen"]
|
|
73
|
+
force_single_line = true
|
|
74
|
+
known_application = ["h2o_audit_trail"]
|
|
75
|
+
profile = "black"
|
|
76
|
+
|
|
77
|
+
[tool.mypy]
|
|
78
|
+
exclude = [
|
|
79
|
+
"src/h2o_audit_trail/gen",
|
|
80
|
+
"src/testing",
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
# Even if the gen folder is excluded, mypy will still check it because gen
|
|
84
|
+
# is imported into our other src code and mypy by default checks all imports as well.
|
|
85
|
+
# This is telling mypy to not check the imported code (so we check only our src code).
|
|
86
|
+
follow_imports = "silent"
|
|
87
|
+
|
|
88
|
+
check_untyped_defs = true
|
|
89
|
+
|
|
90
|
+
[tool.hatch.envs.docs]
|
|
91
|
+
# pdoc3 generates different results for different python versions
|
|
92
|
+
# use fixed python version for stable docs output
|
|
93
|
+
python = "3.10"
|
|
94
|
+
dependencies = [
|
|
95
|
+
"pdoc3==0.10.0",
|
|
96
|
+
]
|
|
97
|
+
|
|
98
|
+
[tool.hatch.envs.docs.scripts]
|
|
99
|
+
generate = [
|
|
100
|
+
"pdoc3 --force --template-dir docs/templates --output-dir ../../docs/docs/py src/h2o_audit_trail/clients"
|
|
101
|
+
]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Snyk for python does not support hatch package manager.
|
|
2
|
+
# Workaround: defining runtime project dependencies in requirements.txt file
|
|
3
|
+
# and adding this file dynamically to hatch via hatch-requirements-txt plugin.
|
|
4
|
+
|
|
5
|
+
python-dateutil>=2.8.2
|
|
6
|
+
h2o-authn>=1.0.0
|
|
7
|
+
h2o-cloud-discovery>=2.1.0
|
|
8
|
+
requests>=2.32.4
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
|
|
3
|
+
from h2o_audit_trail.gen.api_client import ApiClient
|
|
4
|
+
from h2o_audit_trail.gen.api_client import Configuration
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TokenApiClient(ApiClient):
|
|
8
|
+
"""Overrides update_params_for_auth method of the generated ApiClient classes"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, configuration: Configuration, token_provider: Callable[[], str]):
|
|
11
|
+
self._token_provider = token_provider
|
|
12
|
+
super().__init__(configuration=configuration)
|
|
13
|
+
|
|
14
|
+
def update_params_for_auth(
|
|
15
|
+
self,
|
|
16
|
+
headers,
|
|
17
|
+
queries,
|
|
18
|
+
auth_settings,
|
|
19
|
+
resource_path,
|
|
20
|
+
method,
|
|
21
|
+
body,
|
|
22
|
+
request_auths=None,
|
|
23
|
+
):
|
|
24
|
+
token = self._token_provider()
|
|
25
|
+
headers["Authorization"] = f"Bearer {token}"
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import ssl
|
|
2
|
+
from typing import NamedTuple
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
import h2o_authn
|
|
6
|
+
import h2o_discovery
|
|
7
|
+
from h2o_authn import TokenProvider
|
|
8
|
+
|
|
9
|
+
# Name of the platform client in the discovery response.
|
|
10
|
+
PLATFORM_CLIENT_NAME = "platform"
|
|
11
|
+
# Name of the Audit Trail service in the discovery response.
|
|
12
|
+
AUDIT_TRAIL_SERVICE_NAME = "audit-trail"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ConnectionConfig(NamedTuple):
|
|
16
|
+
"""Object holding connection configuration for the Audit Trail server."""
|
|
17
|
+
|
|
18
|
+
audit_trail_url: str
|
|
19
|
+
token_provider: TokenProvider
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_connection(
|
|
23
|
+
audit_trail_url: str,
|
|
24
|
+
refresh_token: str,
|
|
25
|
+
issuer_url: str,
|
|
26
|
+
client_id: str,
|
|
27
|
+
client_secret: Optional[str],
|
|
28
|
+
verify_ssl: bool,
|
|
29
|
+
ssl_ca_cert: Optional[str],
|
|
30
|
+
) -> ConnectionConfig:
|
|
31
|
+
"""Creates ConnectionConfig object. Initializes and tests token provider."""
|
|
32
|
+
ssl_context = ssl.create_default_context(cafile=ssl_ca_cert) # Will use system store if None
|
|
33
|
+
if not verify_ssl:
|
|
34
|
+
ssl_context.check_hostname = False
|
|
35
|
+
ssl_context.verify_mode = ssl.CERT_NONE
|
|
36
|
+
|
|
37
|
+
# init token provider
|
|
38
|
+
tp = h2o_authn.TokenProvider(
|
|
39
|
+
issuer_url=issuer_url,
|
|
40
|
+
refresh_token=refresh_token,
|
|
41
|
+
client_id=client_id,
|
|
42
|
+
client_secret=client_secret,
|
|
43
|
+
http_ssl_context=ssl_context,
|
|
44
|
+
)
|
|
45
|
+
# test token refresh
|
|
46
|
+
tp()
|
|
47
|
+
|
|
48
|
+
return ConnectionConfig(audit_trail_url=audit_trail_url, token_provider=tp)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def discover_platform_connection(
|
|
52
|
+
environment_url: Optional[str] = None,
|
|
53
|
+
config_path: Optional[str] = None,
|
|
54
|
+
platform_token: Optional[str] = None,
|
|
55
|
+
token_provider: Optional[TokenProvider] = None,
|
|
56
|
+
ssl_context: Optional[ssl.SSLContext] = None,
|
|
57
|
+
) -> ConnectionConfig:
|
|
58
|
+
"""Creates ConnectionConfig object by discovering platform connection configuration using h2o_discovery.
|
|
59
|
+
|
|
60
|
+
:param environment_url: Override for the URL of the environment passed to the discovery service.
|
|
61
|
+
:param config_path: Override path to the h2o cli config file passed to the discovery service.
|
|
62
|
+
:param platform_token: Platform token. If not provided, the token will be discovered.
|
|
63
|
+
:param token_provider: Token provider. If not provided, the provider will be constructed from the discovered config.
|
|
64
|
+
:param ssl_context: SSL context to use for the discovery client.
|
|
65
|
+
"""
|
|
66
|
+
# Discover the Audit Trail server URL
|
|
67
|
+
d = h2o_discovery.discover(environment=environment_url, config_path=config_path, ssl_context=ssl_context)
|
|
68
|
+
|
|
69
|
+
audit_trail_service = d.services.get(AUDIT_TRAIL_SERVICE_NAME)
|
|
70
|
+
if audit_trail_service is None:
|
|
71
|
+
raise Exception("Audit Trail service is not registered in discovery service")
|
|
72
|
+
|
|
73
|
+
audit_trail_url = audit_trail_service.uri
|
|
74
|
+
if not audit_trail_url:
|
|
75
|
+
raise ConnectionError("Unable to discover Audit Trail server URL connection value.")
|
|
76
|
+
|
|
77
|
+
# If the token provider is provided, use it to construct the connection config.
|
|
78
|
+
if token_provider is not None:
|
|
79
|
+
# Test token refresh
|
|
80
|
+
token_provider()
|
|
81
|
+
return ConnectionConfig(audit_trail_url=audit_trail_url, token_provider=token_provider)
|
|
82
|
+
|
|
83
|
+
# If the token provider is not provided, construct it from the discovered config.
|
|
84
|
+
if not platform_token:
|
|
85
|
+
platform_client_credentials = d.credentials[PLATFORM_CLIENT_NAME]
|
|
86
|
+
if platform_client_credentials is None:
|
|
87
|
+
raise Exception("Platform client credentials are not available in discovery service")
|
|
88
|
+
|
|
89
|
+
platform_token = platform_client_credentials.refresh_token
|
|
90
|
+
|
|
91
|
+
if not platform_token:
|
|
92
|
+
raise ValueError(
|
|
93
|
+
"Please set the 'platform_token' argument or configure the H2O CLI."
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Discover client id
|
|
97
|
+
platform_client = d.clients.get(PLATFORM_CLIENT_NAME)
|
|
98
|
+
if platform_client is None:
|
|
99
|
+
raise Exception("platform client is not registered in discovery service")
|
|
100
|
+
|
|
101
|
+
client_id = platform_client.oauth2_client_id
|
|
102
|
+
if not client_id:
|
|
103
|
+
raise ConnectionError(
|
|
104
|
+
"Unable to discover platform oauth2_client_id connection value."
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
tp = h2o_authn.TokenProvider(
|
|
108
|
+
issuer_url=d.environment.issuer_url,
|
|
109
|
+
client_id=client_id,
|
|
110
|
+
refresh_token=platform_token,
|
|
111
|
+
http_ssl_context=ssl_context,
|
|
112
|
+
)
|
|
113
|
+
# Test token refresh
|
|
114
|
+
tp()
|
|
115
|
+
|
|
116
|
+
return ConnectionConfig(audit_trail_url=audit_trail_url, token_provider=tp)
|
|
File without changes
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from h2o_audit_trail.auth.token_api_client import TokenApiClient
|
|
5
|
+
from h2o_audit_trail.connection_config import ConnectionConfig
|
|
6
|
+
from h2o_audit_trail.event.create import BatchCreateEventsResponse
|
|
7
|
+
from h2o_audit_trail.event.create import CreateEventRequest
|
|
8
|
+
from h2o_audit_trail.event.create import batch_create_events_response_from_api
|
|
9
|
+
from h2o_audit_trail.event.create import create_event_requests_to_api_requests
|
|
10
|
+
from h2o_audit_trail.event.event import Event
|
|
11
|
+
from h2o_audit_trail.event.event import event_from_api_event
|
|
12
|
+
from h2o_audit_trail.event.event import event_to_api_event
|
|
13
|
+
from h2o_audit_trail.event.event import events_from_api_events
|
|
14
|
+
from h2o_audit_trail.event.search import SearchEventsRequestFilter
|
|
15
|
+
from h2o_audit_trail.event.search import SearchEventsResponse
|
|
16
|
+
from h2o_audit_trail.event.search import search_events_request_filter_to_api
|
|
17
|
+
from h2o_audit_trail.exception import CustomApiException
|
|
18
|
+
from h2o_audit_trail.gen import ApiException
|
|
19
|
+
from h2o_audit_trail.gen import Configuration
|
|
20
|
+
from h2o_audit_trail.gen.api.event_service_api import EventServiceApi
|
|
21
|
+
from h2o_audit_trail.gen.model.v1_batch_create_events_request import (
|
|
22
|
+
V1BatchCreateEventsRequest,
|
|
23
|
+
)
|
|
24
|
+
from h2o_audit_trail.gen.model.v1_batch_create_events_response import (
|
|
25
|
+
V1BatchCreateEventsResponse,
|
|
26
|
+
)
|
|
27
|
+
from h2o_audit_trail.gen.model.v1_event import V1Event
|
|
28
|
+
from h2o_audit_trail.gen.model.v1_search_events_request import V1SearchEventsRequest
|
|
29
|
+
from h2o_audit_trail.gen.model.v1_search_events_response import V1SearchEventsResponse
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class EventClient:
|
|
33
|
+
"""EventClient manages Events."""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
connection_config: ConnectionConfig,
|
|
38
|
+
verify_ssl: bool = True,
|
|
39
|
+
ssl_ca_cert: Optional[str] = None,
|
|
40
|
+
):
|
|
41
|
+
"""Initializes EventClient.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
connection_config (ConnectionConfig): Audit Trail connection configuration object.
|
|
45
|
+
verify_ssl (bool): Set to False to disable SSL certificate verification.
|
|
46
|
+
ssl_ca_cert (Optional[str]): Path to a CA cert bundle with certificates of trusted CAs.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
configuration = Configuration(host=connection_config.audit_trail_url)
|
|
50
|
+
configuration.verify_ssl = verify_ssl
|
|
51
|
+
configuration.ssl_ca_cert = ssl_ca_cert
|
|
52
|
+
|
|
53
|
+
with TokenApiClient(
|
|
54
|
+
configuration, connection_config.token_provider
|
|
55
|
+
) as api_client:
|
|
56
|
+
self.service_api = EventServiceApi(api_client)
|
|
57
|
+
|
|
58
|
+
def create_event(
|
|
59
|
+
self,
|
|
60
|
+
event_id: str,
|
|
61
|
+
event: Event,
|
|
62
|
+
) -> Event:
|
|
63
|
+
"""Creates an Event.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
event_id (str): The ID of the Event resource to create.
|
|
67
|
+
Must be a UUID4, for example: "b096430b-c7a8-47f2-a129-d9ae58cf454f".
|
|
68
|
+
event (Event): The Event resource to create.
|
|
69
|
+
Returns:
|
|
70
|
+
Event: created Event.
|
|
71
|
+
"""
|
|
72
|
+
created_api_object: V1Event
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
created_api_object = self.service_api.event_service_create_event(
|
|
76
|
+
event_id=event_id,
|
|
77
|
+
event=event_to_api_event(event=event),
|
|
78
|
+
).event
|
|
79
|
+
except ApiException as e:
|
|
80
|
+
raise CustomApiException(e)
|
|
81
|
+
|
|
82
|
+
return event_from_api_event(created_api_object)
|
|
83
|
+
|
|
84
|
+
def batch_create_events(
|
|
85
|
+
self,
|
|
86
|
+
requests: List[CreateEventRequest],
|
|
87
|
+
) -> BatchCreateEventsResponse:
|
|
88
|
+
"""Create multiple Events.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
requests (List[CreateEventRequest]): A list of objects, where each object describes an event to be created.
|
|
92
|
+
A maximum of 1000 events can be created in a batch.
|
|
93
|
+
Returns:
|
|
94
|
+
BatchCreateEventsResponse: A response object containing successfully created events
|
|
95
|
+
and failed create requests.
|
|
96
|
+
"""
|
|
97
|
+
api_requests = create_event_requests_to_api_requests(requests=requests)
|
|
98
|
+
api_resp: V1BatchCreateEventsResponse
|
|
99
|
+
try:
|
|
100
|
+
api_resp = self.service_api.event_service_batch_create_events(
|
|
101
|
+
body=V1BatchCreateEventsRequest(requests=api_requests),
|
|
102
|
+
)
|
|
103
|
+
except ApiException as e:
|
|
104
|
+
raise CustomApiException(e)
|
|
105
|
+
|
|
106
|
+
return batch_create_events_response_from_api(api_response=api_resp)
|
|
107
|
+
|
|
108
|
+
def search_events(
|
|
109
|
+
self,
|
|
110
|
+
filter_: Optional[SearchEventsRequestFilter] = None,
|
|
111
|
+
page_size: int = 0,
|
|
112
|
+
page_token: str = "",
|
|
113
|
+
) -> SearchEventsResponse:
|
|
114
|
+
"""Search Events.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
filter_: Filter for events. When unset, default filter values will be used.
|
|
118
|
+
page_size: Maximum number of Events to return in a response.
|
|
119
|
+
When unset, at most 50 Events will be returned.
|
|
120
|
+
The maximum value is 1000; values above 1000 will be coerced to 1000.
|
|
121
|
+
page_token: Leave unset to receive the initial page.
|
|
122
|
+
To list any subsequent pages, use the value of 'next_page_token'
|
|
123
|
+
returned from the SearchEventsResponse.
|
|
124
|
+
Returns:
|
|
125
|
+
SearchEventsResponse: A response object containing searched events with next_page_token.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
api_resp: V1SearchEventsResponse
|
|
129
|
+
try:
|
|
130
|
+
api_resp = self.service_api.event_service_search_events(
|
|
131
|
+
body=V1SearchEventsRequest(
|
|
132
|
+
filter=search_events_request_filter_to_api(filter_=filter_),
|
|
133
|
+
page_size=page_size,
|
|
134
|
+
page_token=page_token,
|
|
135
|
+
),
|
|
136
|
+
)
|
|
137
|
+
except ApiException as e:
|
|
138
|
+
raise CustomApiException(e)
|
|
139
|
+
|
|
140
|
+
return SearchEventsResponse(
|
|
141
|
+
events=events_from_api_events(api_events=api_resp.events),
|
|
142
|
+
next_page_token=api_resp.next_page_token,
|
|
143
|
+
searched_until_time=api_resp.searched_until_time,
|
|
144
|
+
)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Dict
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from h2o_audit_trail.event.event import Event
|
|
6
|
+
from h2o_audit_trail.event.event import event_to_api_event
|
|
7
|
+
from h2o_audit_trail.event.event import events_from_api_events
|
|
8
|
+
from h2o_audit_trail.gen.model.googlerpc_status import GooglerpcStatus
|
|
9
|
+
from h2o_audit_trail.gen.model.v1_batch_create_events_response import (
|
|
10
|
+
V1BatchCreateEventsResponse,
|
|
11
|
+
)
|
|
12
|
+
from h2o_audit_trail.gen.model.v1_create_event_request import V1CreateEventRequest
|
|
13
|
+
from h2o_audit_trail.model.rpc_status import RPCStatus
|
|
14
|
+
from h2o_audit_trail.model.rpc_status import rpc_status_from_google_rpc_status
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class CreateEventRequest:
|
|
19
|
+
"""Request message for CreateEvent."""
|
|
20
|
+
|
|
21
|
+
event_id: str
|
|
22
|
+
"""
|
|
23
|
+
The ID of the Event resource to create.
|
|
24
|
+
Must be a UUID4, for example: "b096430b-c7a8-47f2-a129-d9ae58cf454f".
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
event: Event
|
|
28
|
+
"""The Event resource to create."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def create_event_requests_to_api_requests(requests: List[CreateEventRequest]) -> List[V1CreateEventRequest]:
|
|
32
|
+
return [create_event_request_to_api_request(request=request) for request in requests]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def create_event_request_to_api_request(request: CreateEventRequest) -> V1CreateEventRequest:
|
|
36
|
+
return V1CreateEventRequest(
|
|
37
|
+
event_id=request.event_id,
|
|
38
|
+
event=event_to_api_event(event=request.event),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class BatchCreateEventsResponse:
|
|
44
|
+
"""Response message for BatchCreateEvents."""
|
|
45
|
+
|
|
46
|
+
events: List[Event]
|
|
47
|
+
"""Successfully created Events."""
|
|
48
|
+
|
|
49
|
+
failed_requests: Dict[int, RPCStatus]
|
|
50
|
+
"""
|
|
51
|
+
A dictionary for failed requests, mapping the original
|
|
52
|
+
request's index to an `RPCStatus` object detailing the error.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def batch_create_events_response_from_api(api_response: V1BatchCreateEventsResponse) -> BatchCreateEventsResponse:
|
|
57
|
+
created_events = events_from_api_events(api_events=api_response.events)
|
|
58
|
+
failed_requests = failed_requests_from_api(api_failed_requests=api_response.failed_requests)
|
|
59
|
+
|
|
60
|
+
return BatchCreateEventsResponse(
|
|
61
|
+
events=created_events,
|
|
62
|
+
failed_requests=failed_requests,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def failed_requests_from_api(
|
|
67
|
+
api_failed_requests: Dict[str, GooglerpcStatus]
|
|
68
|
+
) -> Dict[int, RPCStatus]:
|
|
69
|
+
return {
|
|
70
|
+
int(index_str): rpc_status_from_google_rpc_status(google_rpc_status=google_rpc_status)
|
|
71
|
+
for index_str, google_rpc_status in api_failed_requests.items()
|
|
72
|
+
}
|