swarmauri_transport_tls_unicast 0.1.0.dev9__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.
@@ -0,0 +1,87 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_transport_tls_unicast
3
+ Version: 0.1.0.dev9
4
+ Summary: TLS / mTLS unicast transport for Swarmauri.
5
+ License-Expression: Apache-2.0
6
+ Keywords: swarmauri,sdk,standards,transport,tls,unicast
7
+ Author: Jacob Stewart
8
+ Author-email: jacob@swarmauri.com
9
+ Requires-Python: >=3.10,<3.13
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Requires-Dist: swarmauri-base
19
+ Requires-Dist: swarmauri_core
20
+ Description-Content-Type: text/markdown
21
+
22
+ # Swarmauri TLS Unicast Transport
23
+
24
+ ![Transport Icon](https://img.shields.io/badge/transport-tls_unicast-4B8BF4.svg)
25
+ ![Security](https://img.shields.io/badge/security-TLS%2Fmtls-2ECC71.svg)
26
+ ![Lifecycle](https://img.shields.io/badge/lifecycle-async%20contexts-1E90FF.svg)
27
+
28
+ The **Swarmauri TLS Unicast Transport** brings encrypted, authenticated
29
+ connections to the unified transport lifecycle. It wraps asyncio's TLS streams
30
+ with the `.server(...)` / `.client(...)` contexts so you can safely manage
31
+ secure sockets directly from the transport.
32
+
33
+ ## Installation
34
+
35
+ ### Using `uv`
36
+
37
+ ```bash
38
+ uv add --directory pkgs/standards/swarmauri_transport_tls_unicast swarmauri_transport_tls_unicast
39
+ ```
40
+
41
+ ### Using `pip`
42
+
43
+ ```bash
44
+ pip install swarmauri_transport_tls_unicast
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ The snippet below demonstrates creating a mutually-authenticated TLS echo
50
+ server and client.
51
+
52
+ ```python
53
+ import asyncio
54
+ import ssl
55
+ from swarmauri_transport_tls_unicast import TlsUnicastTransport
56
+
57
+ srv_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
58
+ srv_ctx.load_cert_chain("srv.pem", "srv.key")
59
+ # srv_ctx.verify_mode = ssl.CERT_REQUIRED
60
+ # srv_ctx.load_verify_locations("ca.pem")
61
+
62
+ cli_ctx = ssl.create_default_context()
63
+ # cli_ctx.load_cert_chain("cli.pem", "cli.key")
64
+
65
+ async def main():
66
+ server = TlsUnicastTransport(srv_ctx)
67
+
68
+ async def run_server():
69
+ async with server.server(host="0.0.0.0", port=8443):
70
+ data = await server.recv()
71
+ await server.send("peer", b"tls:" + data)
72
+
73
+ async def run_client():
74
+ client = TlsUnicastTransport(cli_ctx, sni="localhost")
75
+ async with client.client(host="127.0.0.1", port=8443):
76
+ await client.send("server", b"hello")
77
+ response = await client.recv()
78
+ print(response.decode())
79
+
80
+ await asyncio.gather(run_server(), run_client())
81
+
82
+ asyncio.run(main())
83
+ ```
84
+
85
+ Configure the SSL contexts with your own certificates (and CA trust) to enable
86
+ TLS or full mTLS verification.
87
+
@@ -0,0 +1,65 @@
1
+ # Swarmauri TLS Unicast Transport
2
+
3
+ ![Transport Icon](https://img.shields.io/badge/transport-tls_unicast-4B8BF4.svg)
4
+ ![Security](https://img.shields.io/badge/security-TLS%2Fmtls-2ECC71.svg)
5
+ ![Lifecycle](https://img.shields.io/badge/lifecycle-async%20contexts-1E90FF.svg)
6
+
7
+ The **Swarmauri TLS Unicast Transport** brings encrypted, authenticated
8
+ connections to the unified transport lifecycle. It wraps asyncio's TLS streams
9
+ with the `.server(...)` / `.client(...)` contexts so you can safely manage
10
+ secure sockets directly from the transport.
11
+
12
+ ## Installation
13
+
14
+ ### Using `uv`
15
+
16
+ ```bash
17
+ uv add --directory pkgs/standards/swarmauri_transport_tls_unicast swarmauri_transport_tls_unicast
18
+ ```
19
+
20
+ ### Using `pip`
21
+
22
+ ```bash
23
+ pip install swarmauri_transport_tls_unicast
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ The snippet below demonstrates creating a mutually-authenticated TLS echo
29
+ server and client.
30
+
31
+ ```python
32
+ import asyncio
33
+ import ssl
34
+ from swarmauri_transport_tls_unicast import TlsUnicastTransport
35
+
36
+ srv_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
37
+ srv_ctx.load_cert_chain("srv.pem", "srv.key")
38
+ # srv_ctx.verify_mode = ssl.CERT_REQUIRED
39
+ # srv_ctx.load_verify_locations("ca.pem")
40
+
41
+ cli_ctx = ssl.create_default_context()
42
+ # cli_ctx.load_cert_chain("cli.pem", "cli.key")
43
+
44
+ async def main():
45
+ server = TlsUnicastTransport(srv_ctx)
46
+
47
+ async def run_server():
48
+ async with server.server(host="0.0.0.0", port=8443):
49
+ data = await server.recv()
50
+ await server.send("peer", b"tls:" + data)
51
+
52
+ async def run_client():
53
+ client = TlsUnicastTransport(cli_ctx, sni="localhost")
54
+ async with client.client(host="127.0.0.1", port=8443):
55
+ await client.send("server", b"hello")
56
+ response = await client.recv()
57
+ print(response.decode())
58
+
59
+ await asyncio.gather(run_server(), run_client())
60
+
61
+ asyncio.run(main())
62
+ ```
63
+
64
+ Configure the SSL contexts with your own certificates (and CA trust) to enable
65
+ TLS or full mTLS verification.
@@ -0,0 +1,71 @@
1
+ [project]
2
+ name = "swarmauri_transport_tls_unicast"
3
+ version = "0.1.0.dev9"
4
+ authors = [
5
+ { name = "Jacob Stewart", email = "jacob@swarmauri.com" }
6
+ ]
7
+ description = "TLS / mTLS unicast transport for Swarmauri."
8
+ readme = "README.md"
9
+ license = "Apache-2.0"
10
+ repository = "https://github.com/swarmauri/swarmauri-sdk"
11
+ requires-python = ">=3.10,<3.13"
12
+ classifiers = [
13
+ "License :: OSI Approved :: Apache Software License",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3.10",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ ]
22
+ dependencies = [
23
+ "swarmauri-base",
24
+ "swarmauri_core",
25
+ ]
26
+ keywords = [
27
+ 'swarmauri',
28
+ 'sdk',
29
+ 'standards',
30
+ 'transport',
31
+ 'tls',
32
+ 'unicast',
33
+ ]
34
+
35
+ [tool.uv.sources]
36
+ swarmauri-base = { workspace = true }
37
+ swarmauri_core = { workspace = true }
38
+
39
+ [build-system]
40
+ requires = ["poetry-core>=1.0.0"]
41
+ build-backend = "poetry.core.masonry.api"
42
+
43
+ [tool.pytest.ini_options]
44
+ markers = [
45
+ "test: standard test",
46
+ "unit: Unit tests",
47
+ "i9n: Integration tests",
48
+ "r8n: Regression tests",
49
+ "acceptance: Acceptance tests",
50
+ "perf: Performance tests",
51
+ "example: Documentation examples",
52
+ ]
53
+ timeout = 300
54
+ log_cli = true
55
+ log_cli_level = "INFO"
56
+ log_cli_format = "%(asctime)s [%(levelname)s] %(message)s"
57
+ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
58
+ asyncio_default_fixture_loop_scope = "function"
59
+ addopts = "--pylicense-accept-deps=sniffio"
60
+
61
+ [dependency-groups]
62
+ dev = [
63
+ "pytest>=8.0",
64
+ "pytest-asyncio>=0.24.0",
65
+ "pytest-xdist>=3.6.1",
66
+ "pytest-json-report>=1.5.0",
67
+ "pytest-timeout>=2.3.1",
68
+ "pytest-benchmark>=4.0.0",
69
+ "ruff>=0.9.9",
70
+ "swarmauri_tests_pylicense",
71
+ ]
@@ -0,0 +1,5 @@
1
+ """TLS-secured unicast transport."""
2
+
3
+ from .tls_unicast import TlsUnicastTransport
4
+
5
+ __all__ = ["TlsUnicastTransport"]
@@ -0,0 +1,110 @@
1
+ """TLS (and optional mutual TLS) unicast transport implementation."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import ssl
7
+ from typing import AsyncIterator, Optional, Tuple
8
+
9
+ from swarmauri_base.transports import (
10
+ TransportBase,
11
+ )
12
+ from swarmauri_core.transports import (
13
+ AddressScheme,
14
+ Cast,
15
+ Feature,
16
+ IOModel,
17
+ Protocol,
18
+ SecurityMode,
19
+ TransportCapabilities,
20
+ )
21
+
22
+
23
+ class TlsUnicastTransport(TransportBase):
24
+ """TLS-secured unicast transport built on top of asyncio streams."""
25
+
26
+ def __init__(self, ssl_ctx: ssl.SSLContext, sni: Optional[str] = None):
27
+ super().__init__(name="TLS")
28
+ self._ssl_ctx = ssl_ctx
29
+ self._sni = sni
30
+ self._server: asyncio.AbstractServer | None = None
31
+ self._reader: asyncio.StreamReader | None = None
32
+ self._writer: asyncio.StreamWriter | None = None
33
+ self._accept_queue: asyncio.Queue[
34
+ Tuple[asyncio.StreamReader, asyncio.StreamWriter]
35
+ ] = asyncio.Queue()
36
+
37
+ def supports(self) -> TransportCapabilities:
38
+ security = (
39
+ SecurityMode.MTLS
40
+ if self._ssl_ctx.verify_mode == ssl.CERT_REQUIRED
41
+ else SecurityMode.TLS
42
+ )
43
+ features = {
44
+ Feature.RELIABLE,
45
+ Feature.ORDERED,
46
+ Feature.ENCRYPTED,
47
+ Feature.AUTHENTICATED,
48
+ }
49
+ if security is SecurityMode.MTLS:
50
+ features.add(Feature.MUTUAL_AUTH)
51
+ return TransportCapabilities(
52
+ protocols=frozenset({Protocol.TLS}),
53
+ io=IOModel.STREAM,
54
+ casts=frozenset({Cast.UNICAST}),
55
+ features=frozenset(features),
56
+ security=security,
57
+ schemes=frozenset({AddressScheme.TLS}),
58
+ )
59
+
60
+ async def _start_server(self, host: str = "0.0.0.0", port: int = 8443) -> None:
61
+ async def _handler(
62
+ reader: asyncio.StreamReader, writer: asyncio.StreamWriter
63
+ ) -> None:
64
+ await self._accept_queue.put((reader, writer))
65
+
66
+ self._server = await asyncio.start_server(
67
+ _handler, host, port, ssl=self._ssl_ctx
68
+ )
69
+
70
+ async def _stop_server(self) -> None:
71
+ if self._server:
72
+ self._server.close()
73
+ await self._server.wait_closed()
74
+ self._server = None
75
+ self._accept_queue = asyncio.Queue()
76
+
77
+ async def _open_client(self, host: str = "127.0.0.1", port: int = 8443) -> None:
78
+ self._reader, self._writer = await asyncio.open_connection(
79
+ host, port, ssl=self._ssl_ctx, server_hostname=self._sni
80
+ )
81
+
82
+ async def _close_client(self) -> None:
83
+ if self._writer:
84
+ self._writer.close()
85
+ await self._writer.wait_closed()
86
+ self._reader = None
87
+ self._writer = None
88
+
89
+ async def accept(
90
+ self,
91
+ ) -> AsyncIterator[Tuple[asyncio.StreamReader, asyncio.StreamWriter]]:
92
+ if not self._server:
93
+ raise RuntimeError("server not started")
94
+ while True:
95
+ reader, writer = await self._accept_queue.get()
96
+ self._reader, self._writer = reader, writer
97
+ yield reader, writer
98
+
99
+ async def send(
100
+ self, target: str, data: bytes, *, timeout: Optional[float] = None
101
+ ) -> None:
102
+ if not self._writer:
103
+ raise RuntimeError("not connected")
104
+ self._writer.write(data)
105
+ await asyncio.wait_for(self._writer.drain(), timeout)
106
+
107
+ async def recv(self, *, timeout: Optional[float] = None) -> bytes:
108
+ if not self._reader:
109
+ raise RuntimeError("not connected")
110
+ return await asyncio.wait_for(self._reader.read(65536), timeout)