mrok 0.4.0__tar.gz → 0.4.2__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.
- {mrok-0.4.0 → mrok-0.4.2}/PKG-INFO +2 -1
- {mrok-0.4.0 → mrok-0.4.2}/mrok/master.py +4 -1
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/app.py +15 -19
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/streams.py +3 -3
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/types.py +2 -3
- mrok-0.4.2/mrok/proxy/ziti.py +117 -0
- {mrok-0.4.0 → mrok-0.4.2}/pyproject.toml +3 -1
- {mrok-0.4.0 → mrok-0.4.2}/tests/proxy/test_app.py +2 -0
- {mrok-0.4.0 → mrok-0.4.2}/uv.lock +507 -0
- mrok-0.4.0/mrok/proxy/ziti.py +0 -173
- {mrok-0.4.0 → mrok-0.4.2}/.github/actions/setup-python-env/action.yml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.github/workflows/assets/turing_team_pr_bot.png +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.github/workflows/notify-pr-closed.yaml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.github/workflows/notify-pr-reviewed.yml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.github/workflows/pr-build-merge.yaml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.github/workflows/release.yml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.gitignore +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.pre-commit-config.yaml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/.python-version +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/LICENSE.txt +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/README.md +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/dev.Dockerfile +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/docker-compose.yaml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/entrypoint.sh +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/__main__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/inspector/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/inspector/__main__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/inspector/app.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/devtools/inspector/server.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/sidecar/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/sidecar/app.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/sidecar/main.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/agent/ziticorn.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/bootstrap.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/list/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/list/extensions.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/list/instances.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/register/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/register/extensions.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/register/instances.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/unregister/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/unregister/extensions.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/unregister/instances.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/admin/utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/dev/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/dev/console.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/dev/web.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/run/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/run/asgi.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/run/sidecar.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/agent/utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/controller/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/controller/openapi.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/controller/run.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/proxy/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/commands/proxy/run.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/main.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/rich.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/cli/utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/conf.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/app.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/auth.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/dependencies/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/dependencies/conf.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/dependencies/ziti.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/openapi/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/openapi/examples.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/openapi/utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/pagination.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/routes/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/routes/extensions.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/routes/instances.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/controller/schemas.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/datastructures.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/errors.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/config.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/constants.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/forwarder.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/lifespan.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/middlewares.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/protocol.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/server.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/types.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/http/utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/logging.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/metrics.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/dataclasses.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/proxy/main.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/api.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/bootstrap.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/constants.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/errors.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/identities.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/pki.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/mrok/ziti/services.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/prod.Dockerfile +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/scripts/ziti.sh +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/settings.yaml +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/snapshot_report.html +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/sonar-project.properties +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/__snapshots__/test_inspector/test_inspector_app.svg +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/__snapshots__/test_inspector/test_inspector_app_empty_card.svg +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/__snapshots__/test_inspector/test_inspector_app_filed_store_connection.svg +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/__snapshots__/test_inspector/test_inspector_app_open_card.svg +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/test_app.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/sidecar/test_main.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/agent/test_ziticorn.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/test_bootstrap.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/test_list.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/test_register.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/test_unregister.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/admin/test_utils.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/agent/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/agent/test_run.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/controller/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/controller/test_openapi.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/controller/test_run.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/proxy/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/proxy/test_run.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/cli/test_main.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/conftest.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/controller/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/controller/test_auth.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/controller/test_extensions.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/controller/test_instances.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/controller/test_openapi.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_config.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_forwarder.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_lifespan.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_master.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_protocol.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/http/test_server.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/proxy/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/proxy/test_ziti.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/proxy/test_ziti_branches.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/types.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/__init__.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/test_api.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/test_bootstrap.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/test_identities.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/test_pki.py +0 -0
- {mrok-0.4.0 → mrok-0.4.2}/tests/ziti/test_services.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mrok
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.2
|
|
4
4
|
Summary: MPT Extensions OpenZiti Orchestrator
|
|
5
5
|
Author: SoftwareOne AG
|
|
6
6
|
License: Apache License
|
|
@@ -206,6 +206,7 @@ License: Apache License
|
|
|
206
206
|
limitations under the License.
|
|
207
207
|
License-File: LICENSE.txt
|
|
208
208
|
Requires-Python: <4,>=3.12
|
|
209
|
+
Requires-Dist: aiocache<0.13.0,>=0.12.3
|
|
209
210
|
Requires-Dist: asn1crypto<2.0.0,>=1.5.1
|
|
210
211
|
Requires-Dist: cryptography<46.0.0,>=45.0.7
|
|
211
212
|
Requires-Dist: dynaconf<4.0.0,>=3.2.11
|
|
@@ -12,6 +12,7 @@ from uuid import uuid4
|
|
|
12
12
|
|
|
13
13
|
import zmq
|
|
14
14
|
import zmq.asyncio
|
|
15
|
+
from uvicorn.importer import import_from_string
|
|
15
16
|
from watchfiles import watch
|
|
16
17
|
from watchfiles.filters import PythonFilter
|
|
17
18
|
from watchfiles.run import CombinedProcess, start_process
|
|
@@ -69,6 +70,9 @@ def start_uvicorn_worker(
|
|
|
69
70
|
import sys
|
|
70
71
|
|
|
71
72
|
sys.path.insert(0, os.getcwd())
|
|
73
|
+
if isinstance(app, str):
|
|
74
|
+
app = import_from_string(app)
|
|
75
|
+
|
|
72
76
|
setup_logging(get_settings())
|
|
73
77
|
identity = json.load(open(identity_file))
|
|
74
78
|
meta = Meta(**identity["mrok"])
|
|
@@ -82,7 +86,6 @@ def start_uvicorn_worker(
|
|
|
82
86
|
async def status_sender():
|
|
83
87
|
while True:
|
|
84
88
|
snap = await metrics.snapshot()
|
|
85
|
-
logger.info(f"New metrics snapshot taken: {snap}")
|
|
86
89
|
event = Event(type="status", data=Status(meta=meta, metrics=snap))
|
|
87
90
|
await pub.send_string(event.model_dump_json())
|
|
88
91
|
await asyncio.sleep(metrics_interval)
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import logging
|
|
2
|
-
import os
|
|
3
2
|
from pathlib import Path
|
|
4
3
|
|
|
5
4
|
from mrok.conf import get_settings
|
|
@@ -35,33 +34,30 @@ class ProxyApp(ForwardAppBase):
|
|
|
35
34
|
self._conn_manager = ZitiConnectionManager(
|
|
36
35
|
identity_file,
|
|
37
36
|
ttl_seconds=ziti_connection_ttl_seconds,
|
|
38
|
-
|
|
37
|
+
cleanup_interval=ziti_conn_cache_purge_interval_seconds,
|
|
39
38
|
)
|
|
40
39
|
|
|
41
|
-
def
|
|
42
|
-
header_value = headers.get(
|
|
43
|
-
if
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
48
|
-
if ":" in header_value:
|
|
49
|
-
header_value, _ = header_value.split(":", 1)
|
|
50
|
-
if not header_value.endswith(self._proxy_wildcard_domain):
|
|
51
|
-
raise ProxyError(
|
|
52
|
-
f"Unexpected value for Host or X-Forwarded-For header: `{header_value}`."
|
|
53
|
-
)
|
|
40
|
+
def get_target_from_header(self, headers: dict[str, str], name: str) -> str | None:
|
|
41
|
+
header_value = headers.get(name, "")
|
|
42
|
+
if self._proxy_wildcard_domain in header_value:
|
|
43
|
+
if ":" in header_value:
|
|
44
|
+
header_value, _ = header_value.split(":", 1)
|
|
45
|
+
return header_value[: -len(self._proxy_wildcard_domain)]
|
|
54
46
|
|
|
55
|
-
|
|
47
|
+
def get_target_name(self, headers: dict[str, str]) -> str:
|
|
48
|
+
target = self.get_target_from_header(headers, "x-forwarded-host")
|
|
49
|
+
if not target:
|
|
50
|
+
target = self.get_target_from_header(headers, "host")
|
|
51
|
+
if not target:
|
|
52
|
+
raise ProxyError("Neither Host nor X-Forwarded-Host contain a valid target name")
|
|
53
|
+
return target
|
|
56
54
|
|
|
57
55
|
async def startup(self):
|
|
58
56
|
setup_logging(get_settings())
|
|
59
57
|
await self._conn_manager.start()
|
|
60
|
-
logger.info(f"Proxy app startup completed: {os.getpid()}")
|
|
61
58
|
|
|
62
59
|
async def shutdown(self):
|
|
63
60
|
await self._conn_manager.stop()
|
|
64
|
-
logger.info(f"Proxy app shutdown completed: {os.getpid()}")
|
|
65
61
|
|
|
66
62
|
async def select_backend(
|
|
67
63
|
self,
|
|
@@ -70,4 +66,4 @@ class ProxyApp(ForwardAppBase):
|
|
|
70
66
|
) -> tuple[StreamReader, StreamWriter] | tuple[None, None]:
|
|
71
67
|
target_name = self.get_target_name(headers)
|
|
72
68
|
|
|
73
|
-
return await self._conn_manager.
|
|
69
|
+
return await self._conn_manager.get_or_create(target_name)
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
|
|
3
|
-
from mrok.proxy.types import ConnectionCache
|
|
3
|
+
from mrok.proxy.types import ConnectionCache
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class CachedStreamReader:
|
|
7
7
|
def __init__(
|
|
8
8
|
self,
|
|
9
9
|
reader: asyncio.StreamReader,
|
|
10
|
-
key:
|
|
10
|
+
key: str,
|
|
11
11
|
manager: ConnectionCache,
|
|
12
12
|
):
|
|
13
13
|
self._reader = reader
|
|
@@ -77,7 +77,7 @@ class CachedStreamWriter:
|
|
|
77
77
|
def __init__(
|
|
78
78
|
self,
|
|
79
79
|
writer: asyncio.StreamWriter,
|
|
80
|
-
key:
|
|
80
|
+
key: str,
|
|
81
81
|
manager: ConnectionCache,
|
|
82
82
|
):
|
|
83
83
|
self._writer = writer
|
|
@@ -4,9 +4,8 @@ from typing import Protocol
|
|
|
4
4
|
|
|
5
5
|
from mrok.http.types import StreamReader, StreamWriter
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
CachedStream = tuple[StreamReader, StreamWriter]
|
|
7
|
+
StreamPair = tuple[StreamReader, StreamWriter]
|
|
9
8
|
|
|
10
9
|
|
|
11
10
|
class ConnectionCache(Protocol):
|
|
12
|
-
async def invalidate(self, key:
|
|
11
|
+
async def invalidate(self, key: str) -> None: ...
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import contextlib
|
|
3
|
+
import logging
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import openziti
|
|
7
|
+
from aiocache import Cache
|
|
8
|
+
|
|
9
|
+
from mrok.proxy.streams import CachedStreamReader, CachedStreamWriter
|
|
10
|
+
from mrok.proxy.types import StreamPair
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger("mrok.proxy")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ZitiConnectionManager:
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
identity_file: str | Path,
|
|
19
|
+
ziti_timeout_ms: int = 10000,
|
|
20
|
+
ttl_seconds: float = 60.0,
|
|
21
|
+
cleanup_interval: float = 10.0,
|
|
22
|
+
):
|
|
23
|
+
self.identity_file = identity_file
|
|
24
|
+
self.ziti_timeout_ms = ziti_timeout_ms
|
|
25
|
+
self.ttl_seconds = ttl_seconds
|
|
26
|
+
self.cleanup_interval = cleanup_interval
|
|
27
|
+
|
|
28
|
+
self.cache = Cache(Cache.MEMORY)
|
|
29
|
+
|
|
30
|
+
self._active_pairs: dict[str, StreamPair] = {}
|
|
31
|
+
|
|
32
|
+
self._cleanup_task: asyncio.Task | None = None
|
|
33
|
+
self._ziti_ctx: openziti.context.ZitiContext | None = None
|
|
34
|
+
|
|
35
|
+
async def create_stream_pair(self, key: str) -> StreamPair:
|
|
36
|
+
if not self._ziti_ctx:
|
|
37
|
+
raise Exception("ZitiConnectionManager is not started")
|
|
38
|
+
sock = self._ziti_ctx.connect(key)
|
|
39
|
+
orig_reader, orig_writer = await asyncio.open_connection(sock=sock)
|
|
40
|
+
|
|
41
|
+
reader = CachedStreamReader(orig_reader, key, self)
|
|
42
|
+
writer = CachedStreamWriter(orig_writer, key, self)
|
|
43
|
+
return (reader, writer)
|
|
44
|
+
|
|
45
|
+
async def get_or_create(self, key: str) -> StreamPair:
|
|
46
|
+
pair = await self.cache.get(key)
|
|
47
|
+
|
|
48
|
+
if pair:
|
|
49
|
+
logger.info(f"return cached connection for {key}")
|
|
50
|
+
await self.cache.set(key, pair, ttl=self.ttl_seconds)
|
|
51
|
+
self._active_pairs[key] = pair
|
|
52
|
+
return pair
|
|
53
|
+
|
|
54
|
+
pair = await self.create_stream_pair(key)
|
|
55
|
+
await self.cache.set(key, pair, ttl=self.ttl_seconds)
|
|
56
|
+
self._active_pairs[key] = pair
|
|
57
|
+
logger.info(f"return new connection for {key}")
|
|
58
|
+
return pair
|
|
59
|
+
|
|
60
|
+
async def invalidate(self, key: str) -> None:
|
|
61
|
+
logger.info(f"invalidating connection for {key}")
|
|
62
|
+
pair = await self.cache.get(key)
|
|
63
|
+
if pair:
|
|
64
|
+
await self._close_pair(pair)
|
|
65
|
+
|
|
66
|
+
await self.cache.delete(key)
|
|
67
|
+
self._active_pairs.pop(key, None)
|
|
68
|
+
|
|
69
|
+
async def start(self) -> None:
|
|
70
|
+
if self._cleanup_task is None:
|
|
71
|
+
self._cleanup_task = asyncio.create_task(self._periodic_cleanup())
|
|
72
|
+
if self._ziti_ctx is None:
|
|
73
|
+
ctx, err = openziti.load(str(self.identity_file), timeout=self.ziti_timeout_ms)
|
|
74
|
+
if err != 0:
|
|
75
|
+
raise Exception(f"Cannot create a Ziti context from the identity file: {err}")
|
|
76
|
+
self._ziti_ctx = ctx
|
|
77
|
+
|
|
78
|
+
async def stop(self) -> None:
|
|
79
|
+
if self._cleanup_task:
|
|
80
|
+
self._cleanup_task.cancel()
|
|
81
|
+
with contextlib.suppress(Exception):
|
|
82
|
+
await self._cleanup_task
|
|
83
|
+
|
|
84
|
+
for pair in list(self._active_pairs.values()):
|
|
85
|
+
await self._close_pair(pair)
|
|
86
|
+
|
|
87
|
+
self._active_pairs.clear()
|
|
88
|
+
await self.cache.clear()
|
|
89
|
+
openziti.shutdown()
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
async def _close_pair(pair: StreamPair) -> None:
|
|
93
|
+
reader, writer = pair
|
|
94
|
+
writer.close()
|
|
95
|
+
with contextlib.suppress(Exception):
|
|
96
|
+
await writer.wait_closed()
|
|
97
|
+
|
|
98
|
+
async def _periodic_cleanup(self) -> None:
|
|
99
|
+
try:
|
|
100
|
+
while True:
|
|
101
|
+
await asyncio.sleep(self.cleanup_interval)
|
|
102
|
+
await self._cleanup_once()
|
|
103
|
+
except asyncio.CancelledError:
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
async def _cleanup_once(self) -> None:
|
|
107
|
+
# Keys currently stored in aiocache
|
|
108
|
+
keys_in_cache = set(await self.cache.keys())
|
|
109
|
+
# Keys we think are alive
|
|
110
|
+
known_keys = set(self._active_pairs.keys())
|
|
111
|
+
|
|
112
|
+
expired_keys = known_keys - keys_in_cache
|
|
113
|
+
|
|
114
|
+
for key in expired_keys:
|
|
115
|
+
pair = self._active_pairs.pop(key, None)
|
|
116
|
+
if pair:
|
|
117
|
+
await self._close_pair(pair)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "mrok"
|
|
3
|
-
version = "0.4.
|
|
3
|
+
version = "0.4.2"
|
|
4
4
|
description = "MPT Extensions OpenZiti Orchestrator"
|
|
5
5
|
readme = { file = "README.md", content-type = "text/markdown" }
|
|
6
6
|
authors = [
|
|
@@ -9,6 +9,7 @@ authors = [
|
|
|
9
9
|
license = { file = "LICENSE.txt" }
|
|
10
10
|
requires-python = ">=3.12,<4"
|
|
11
11
|
dependencies = [
|
|
12
|
+
"aiocache>=0.12.3,<0.13.0",
|
|
12
13
|
"asn1crypto>=1.5.1,<2.0.0",
|
|
13
14
|
"cryptography>=45.0.7,<46.0.0",
|
|
14
15
|
"dynaconf>=3.2.11,<4.0.0",
|
|
@@ -38,6 +39,7 @@ dev = [
|
|
|
38
39
|
"asgi-lifespan>=2.1.0,<3.0.0",
|
|
39
40
|
"bandit>=1.8.6,<2.0.0",
|
|
40
41
|
"ipython>=9.4.0,<10.0.0",
|
|
42
|
+
"locust>=2.42.6,<3.0.0",
|
|
41
43
|
"mypy>=1.17.1,<2.0.0",
|
|
42
44
|
"pre-commit>=4.3.0,<5.0.0",
|
|
43
45
|
"pytest>=8.4.2,<9.0.0",
|
|
@@ -4,6 +4,8 @@ from pytest_mock import MockerFixture
|
|
|
4
4
|
from mrok.proxy.app import ProxyApp, ProxyError
|
|
5
5
|
from tests.conftest import SettingsFactory
|
|
6
6
|
|
|
7
|
+
pytestmark = pytest.mark.skipif()
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
def test_init(mocker: MockerFixture):
|
|
9
11
|
ziti_conn_mgr_ctor = mocker.patch("mrok.proxy.app.ZitiConnectionManager")
|