agentconnect-cli 0.1.0a1__tar.gz → 0.1.0a2__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.
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/PKG-INFO +16 -14
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/README.md +11 -10
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/pyproject.toml +6 -5
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/pyproject.toml.orig +6 -5
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/__init__.py +1 -1
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/adapter.py +88 -1
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/adapters.py +18 -4
- agentconnect_cli-0.1.0a2/src/agentconnect/api_tls.py +222 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/apiclient.py +14 -4
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/cli.py +2 -1
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/cli_support.py +4 -1
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/config.py +55 -10
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/isolation.py +21 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/mcp/backend.py +11 -8
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/api.py +74 -4
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/runtime.py +1 -1
- agentconnect_cli-0.1.0a2/src/agentconnect/sockets.py +635 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/webhook.py +14 -1
- agentconnect_cli-0.1.0a1/src/agentconnect/sockets.py +0 -86
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/__init__.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/cards.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/convert.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/facade.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/seen.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/serve.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/adapter.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/backoff.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/callback.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/cli_json.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/clock.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/daemon/__init__.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/deliveries.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/directory_client.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/failpoints.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/identity.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/keystore.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/limits.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/log.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/mcp/__init__.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/mcp/server.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/mcp/session.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/mcp/tools.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/ownerfile.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/payloads.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/policy.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/provision.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/py.typed +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/relay.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/__init__.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/agent.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/engine.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/errors.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/floor.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/groups.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/knocks.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/ledger.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/listing.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/registry.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/runtime/sas.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/sas_emoji.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/schemas.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/shapes.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/sse.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/store.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/tasks.py +0 -0
- {agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/tokens.py +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: agentconnect-cli
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.0a2
|
|
4
4
|
Summary: AgentConnect daemon (agentconnectd) and CLI
|
|
5
5
|
License-Expression: Apache-2.0
|
|
6
|
-
Requires-Dist: a2a-sdk[http-server,signing]>=1.2.
|
|
7
|
-
Requires-Dist: agentconnect-core==0.1.
|
|
6
|
+
Requires-Dist: a2a-sdk[http-server,signing]>=1.2.3
|
|
7
|
+
Requires-Dist: agentconnect-core==0.1.0a2
|
|
8
|
+
Requires-Dist: cryptography>=50.0.2
|
|
8
9
|
Requires-Dist: httpx>=0.28.1
|
|
9
10
|
Requires-Dist: keyring>=25.7.0
|
|
10
11
|
Requires-Dist: mcp>=2.3.0
|
|
@@ -12,7 +13,7 @@ Requires-Dist: pyjwt[crypto]>=2.15.1
|
|
|
12
13
|
Requires-Dist: structlog>=26.1.0
|
|
13
14
|
Requires-Dist: typer>=0.27.3
|
|
14
15
|
Requires-Dist: uvicorn>=0.54.0
|
|
15
|
-
Requires-Dist: websockets>=17.
|
|
16
|
+
Requires-Dist: websockets>=17.3
|
|
16
17
|
Requires-Python: >=3.12
|
|
17
18
|
Description-Content-Type: text/markdown
|
|
18
19
|
|
|
@@ -20,8 +21,8 @@ Description-Content-Type: text/markdown
|
|
|
20
21
|
|
|
21
22
|
The AgentConnect daemon (`agentconnectd`) and CLI (`agentconnect`). The
|
|
22
23
|
distribution is `agentconnect-cli`; the command and the import package are
|
|
23
|
-
`agentconnect`. See the repository [README](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
24
|
-
[SPEC.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
24
|
+
`agentconnect`. See the repository [README](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/README.md) and
|
|
25
|
+
[SPEC.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/SPEC.md).
|
|
25
26
|
|
|
26
27
|
```sh
|
|
27
28
|
agentconnect daemon run [--config PATH] [--tcp-port N] # foreground, JSON logs on stderr
|
|
@@ -31,12 +32,12 @@ agentconnect status [--json] # reads the local API
|
|
|
31
32
|
Everything protocol-related is in the runtime, `agentconnect.runtime`
|
|
32
33
|
(`Runtime`, with its engine, local API, adapters and agent-scope
|
|
33
34
|
`AgentApi`). `agentconnectd` (`agentconnect.daemon`) is one host of it; the
|
|
34
|
-
Python SDK's embedded mode (`AgentConnect.embedded`, [docs/sdk.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
35
|
+
Python SDK's embedded mode (`AgentConnect.embedded`, [docs/sdk.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/sdk.md))
|
|
35
36
|
is the other. Both run the same checks, and both serve the local API below.
|
|
36
37
|
|
|
37
38
|
The invite lifecycle (`group`, `invite`, `accept`, `groups`, `approvals`,
|
|
38
39
|
`verify`, `leave`, `send`, `events`) is documented in
|
|
39
|
-
[docs/cli.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
40
|
+
[docs/cli.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/cli.md), with the `--json` schemas and exit codes.
|
|
40
41
|
`approvals approve`, `approvals reject` and `verify` use the human token:
|
|
41
42
|
**never give an agent the human token or let it run those commands for the
|
|
42
43
|
human.**
|
|
@@ -45,7 +46,7 @@ Don't give agents shell or file access to the data directory either: an
|
|
|
45
46
|
agent that can read it can approve joins and mark codes verified as you,
|
|
46
47
|
because the human token is a file there. Agents that need a shell can run
|
|
47
48
|
as another OS user, with the daemon under a user of its own
|
|
48
|
-
([docs/isolation.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
49
|
+
([docs/isolation.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/isolation.md)).
|
|
49
50
|
|
|
50
51
|
## What the daemon holds
|
|
51
52
|
|
|
@@ -63,13 +64,14 @@ as another OS user, with the daemon under a user of its own
|
|
|
63
64
|
|
|
64
65
|
The data directory (`~/.local/share/agentconnect`) is 0700 and every file in
|
|
65
66
|
it 0600. Configuration is `~/.config/agentconnect/config.toml`; see
|
|
66
|
-
[docs/config.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
67
|
+
[docs/config.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/config.md) for the keys and the `AC_*` overrides.
|
|
67
68
|
|
|
68
69
|
## Local control API
|
|
69
70
|
|
|
70
71
|
JSON over HTTP/1.1 on a Unix socket in the data directory, or on
|
|
71
|
-
`127.0.0.1:<port
|
|
72
|
-
|
|
72
|
+
`https://127.0.0.1:<port>` (TLS with a certificate the clients pin). Every
|
|
73
|
+
request needs `Authorization: Bearer <token>`.
|
|
74
|
+
[docs/api.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/api.md) describes the transport, tokens, errors and
|
|
73
75
|
every route with its request and response shapes.
|
|
74
76
|
|
|
75
77
|
Two tokens live in the data directory: `api-token` (agent scope) and
|
|
@@ -97,7 +99,7 @@ repeat a message or an event. Every delivery has a stable `msg_id` and every
|
|
|
97
99
|
event a stable `event_id`; deduplicate by them. Nothing is lost: an event is
|
|
98
100
|
recorded before the adapter hears of it and sent again on restart until
|
|
99
101
|
`on_event` returns (ADR-004).
|
|
100
|
-
The webhook adapter ([docs/adapters/webhook.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
102
|
+
The webhook adapter ([docs/adapters/webhook.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/adapters/webhook.md)) and the SSE adapter (`GET /v0/events`, [docs/api.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/api.md#event-stream)) serve headless agents.
|
|
101
103
|
`CallbackAdapter` (`agentconnect.callback`) is the embedded SDK's in-process adapter, with the webhook's retry rules. `RecordingAdapter` is the in-memory adapter used by tests. Every message in
|
|
102
104
|
and out goes through the safety policy (`agentconnect.policy`) before any
|
|
103
|
-
adapter sees it ([docs/safety.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
105
|
+
adapter sees it ([docs/safety.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/safety.md)).
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
The AgentConnect daemon (`agentconnectd`) and CLI (`agentconnect`). The
|
|
4
4
|
distribution is `agentconnect-cli`; the command and the import package are
|
|
5
|
-
`agentconnect`. See the repository [README](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
6
|
-
[SPEC.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
5
|
+
`agentconnect`. See the repository [README](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/README.md) and
|
|
6
|
+
[SPEC.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/SPEC.md).
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
9
|
agentconnect daemon run [--config PATH] [--tcp-port N] # foreground, JSON logs on stderr
|
|
@@ -13,12 +13,12 @@ agentconnect status [--json] # reads the local API
|
|
|
13
13
|
Everything protocol-related is in the runtime, `agentconnect.runtime`
|
|
14
14
|
(`Runtime`, with its engine, local API, adapters and agent-scope
|
|
15
15
|
`AgentApi`). `agentconnectd` (`agentconnect.daemon`) is one host of it; the
|
|
16
|
-
Python SDK's embedded mode (`AgentConnect.embedded`, [docs/sdk.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
16
|
+
Python SDK's embedded mode (`AgentConnect.embedded`, [docs/sdk.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/sdk.md))
|
|
17
17
|
is the other. Both run the same checks, and both serve the local API below.
|
|
18
18
|
|
|
19
19
|
The invite lifecycle (`group`, `invite`, `accept`, `groups`, `approvals`,
|
|
20
20
|
`verify`, `leave`, `send`, `events`) is documented in
|
|
21
|
-
[docs/cli.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
21
|
+
[docs/cli.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/cli.md), with the `--json` schemas and exit codes.
|
|
22
22
|
`approvals approve`, `approvals reject` and `verify` use the human token:
|
|
23
23
|
**never give an agent the human token or let it run those commands for the
|
|
24
24
|
human.**
|
|
@@ -27,7 +27,7 @@ Don't give agents shell or file access to the data directory either: an
|
|
|
27
27
|
agent that can read it can approve joins and mark codes verified as you,
|
|
28
28
|
because the human token is a file there. Agents that need a shell can run
|
|
29
29
|
as another OS user, with the daemon under a user of its own
|
|
30
|
-
([docs/isolation.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
30
|
+
([docs/isolation.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/isolation.md)).
|
|
31
31
|
|
|
32
32
|
## What the daemon holds
|
|
33
33
|
|
|
@@ -45,13 +45,14 @@ as another OS user, with the daemon under a user of its own
|
|
|
45
45
|
|
|
46
46
|
The data directory (`~/.local/share/agentconnect`) is 0700 and every file in
|
|
47
47
|
it 0600. Configuration is `~/.config/agentconnect/config.toml`; see
|
|
48
|
-
[docs/config.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
48
|
+
[docs/config.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/config.md) for the keys and the `AC_*` overrides.
|
|
49
49
|
|
|
50
50
|
## Local control API
|
|
51
51
|
|
|
52
52
|
JSON over HTTP/1.1 on a Unix socket in the data directory, or on
|
|
53
|
-
`127.0.0.1:<port
|
|
54
|
-
|
|
53
|
+
`https://127.0.0.1:<port>` (TLS with a certificate the clients pin). Every
|
|
54
|
+
request needs `Authorization: Bearer <token>`.
|
|
55
|
+
[docs/api.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/api.md) describes the transport, tokens, errors and
|
|
55
56
|
every route with its request and response shapes.
|
|
56
57
|
|
|
57
58
|
Two tokens live in the data directory: `api-token` (agent scope) and
|
|
@@ -79,7 +80,7 @@ repeat a message or an event. Every delivery has a stable `msg_id` and every
|
|
|
79
80
|
event a stable `event_id`; deduplicate by them. Nothing is lost: an event is
|
|
80
81
|
recorded before the adapter hears of it and sent again on restart until
|
|
81
82
|
`on_event` returns (ADR-004).
|
|
82
|
-
The webhook adapter ([docs/adapters/webhook.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
83
|
+
The webhook adapter ([docs/adapters/webhook.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/adapters/webhook.md)) and the SSE adapter (`GET /v0/events`, [docs/api.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/api.md#event-stream)) serve headless agents.
|
|
83
84
|
`CallbackAdapter` (`agentconnect.callback`) is the embedded SDK's in-process adapter, with the webhook's retry rules. `RecordingAdapter` is the in-memory adapter used by tests. Every message in
|
|
84
85
|
and out goes through the safety policy (`agentconnect.policy`) before any
|
|
85
|
-
adapter sees it ([docs/safety.md](https://github.com/mbocevski/agent-connect/blob/v0.1.
|
|
86
|
+
adapter sees it ([docs/safety.md](https://github.com/mbocevski/agent-connect/blob/v0.1.0a2/docs/safety.md)).
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "agentconnect-cli"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.0a2"
|
|
4
4
|
description = "AgentConnect daemon (agentconnectd) and CLI"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "Apache-2.0"
|
|
7
7
|
requires-python = ">=3.12"
|
|
8
8
|
dependencies = [
|
|
9
|
-
"a2a-sdk[http-server,signing]>=1.2.
|
|
10
|
-
"agentconnect-core==0.1.
|
|
9
|
+
"a2a-sdk[http-server,signing]>=1.2.3",
|
|
10
|
+
"agentconnect-core==0.1.0a2",
|
|
11
|
+
"cryptography>=50.0.2",
|
|
11
12
|
"httpx>=0.28.1",
|
|
12
13
|
"keyring>=25.7.0",
|
|
13
14
|
"mcp>=2.3.0",
|
|
@@ -15,7 +16,7 @@ dependencies = [
|
|
|
15
16
|
"structlog>=26.1.0",
|
|
16
17
|
"typer>=0.27.3",
|
|
17
18
|
"uvicorn>=0.54.0",
|
|
18
|
-
"websockets>=17.
|
|
19
|
+
"websockets>=17.3",
|
|
19
20
|
]
|
|
20
21
|
|
|
21
22
|
[project.scripts]
|
|
@@ -28,5 +29,5 @@ workspace = true
|
|
|
28
29
|
module-name = "agentconnect"
|
|
29
30
|
|
|
30
31
|
[build-system]
|
|
31
|
-
requires = ["uv_build>=0.13.
|
|
32
|
+
requires = ["uv_build>=0.13.5,<0.14"]
|
|
32
33
|
build-backend = "uv_build"
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "agentconnect-cli"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.0a2"
|
|
4
4
|
description = "AgentConnect daemon (agentconnectd) and CLI"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "Apache-2.0"
|
|
7
7
|
requires-python = ">=3.12"
|
|
8
8
|
dependencies = [
|
|
9
|
-
"a2a-sdk[http-server,signing]>=1.2.
|
|
10
|
-
"agentconnect-core==0.1.
|
|
9
|
+
"a2a-sdk[http-server,signing]>=1.2.3",
|
|
10
|
+
"agentconnect-core==0.1.0a2",
|
|
11
|
+
"cryptography>=50.0.2",
|
|
11
12
|
"httpx>=0.28.1",
|
|
12
13
|
"keyring>=25.7.0",
|
|
13
14
|
"mcp>=2.3.0",
|
|
@@ -15,7 +16,7 @@ dependencies = [
|
|
|
15
16
|
"structlog>=26.1.0",
|
|
16
17
|
"typer>=0.27.3",
|
|
17
18
|
"uvicorn>=0.54.0",
|
|
18
|
-
"websockets>=17.
|
|
19
|
+
"websockets>=17.3",
|
|
19
20
|
]
|
|
20
21
|
|
|
21
22
|
[project.scripts]
|
|
@@ -29,5 +30,5 @@ agentconnect-core = { workspace = true }
|
|
|
29
30
|
module-name = "agentconnect"
|
|
30
31
|
|
|
31
32
|
[build-system]
|
|
32
|
-
requires = ["uv_build>=0.13.
|
|
33
|
+
requires = ["uv_build>=0.13.5,<0.14"]
|
|
33
34
|
build-backend = "uv_build"
|
{agentconnect_cli-0.1.0a1 → agentconnect_cli-0.1.0a2}/src/agentconnect/a2a_adapter/adapter.py
RENAMED
|
@@ -44,6 +44,7 @@ from collections.abc import AsyncIterator, Coroutine, Mapping
|
|
|
44
44
|
from dataclasses import dataclass, field
|
|
45
45
|
from pathlib import Path
|
|
46
46
|
from typing import TYPE_CHECKING, Any
|
|
47
|
+
from urllib.parse import urlsplit
|
|
47
48
|
|
|
48
49
|
import httpx
|
|
49
50
|
from a2a.client import A2ACardResolver, A2AClientError, Client, ClientConfig, ClientFactory
|
|
@@ -73,6 +74,8 @@ from agentconnect.adapter import (
|
|
|
73
74
|
Sender,
|
|
74
75
|
)
|
|
75
76
|
from agentconnect.clock import Clock, unix_seconds
|
|
77
|
+
from agentconnect.isolation import receiver_uids
|
|
78
|
+
from agentconnect.sockets import PeerMismatchError, receiver_async_transport
|
|
76
79
|
from agentconnect.tasks import cancel_and_wait
|
|
77
80
|
|
|
78
81
|
if TYPE_CHECKING:
|
|
@@ -93,6 +96,10 @@ class A2AOptions:
|
|
|
93
96
|
"""The adapter's configuration (``[a2a]`` in config.toml)."""
|
|
94
97
|
|
|
95
98
|
local_agent_url: str | None = None
|
|
99
|
+
# A Unix socket every request to the local agent goes to, served by one
|
|
100
|
+
# of receiver_uids (SR-10); local_agent_url then gives Host and paths.
|
|
101
|
+
local_agent_socket: Path | None = None
|
|
102
|
+
receiver_uids: frozenset[int] = frozenset()
|
|
96
103
|
local_agent_token: str | None = field(default=None, repr=False)
|
|
97
104
|
card_keys: Mapping[str, PyJWK] = field(default_factory=dict, repr=False)
|
|
98
105
|
facade_port: int | None = None # None: no facade; 0: any free port
|
|
@@ -109,6 +116,8 @@ class A2AOptions:
|
|
|
109
116
|
token = text or None
|
|
110
117
|
return A2AOptions(
|
|
111
118
|
local_agent_url=config.a2a_local_agent_url,
|
|
119
|
+
local_agent_socket=config.a2a_local_agent_socket,
|
|
120
|
+
receiver_uids=receiver_uids(config),
|
|
112
121
|
local_agent_token=token,
|
|
113
122
|
card_keys=cards.load_jwks(config.a2a_card_keys_file),
|
|
114
123
|
facade_port=config.a2a_facade_port if config.a2a_facade_port > 0 else None,
|
|
@@ -286,8 +295,20 @@ class A2AAdapter:
|
|
|
286
295
|
headers = {}
|
|
287
296
|
if self.options.local_agent_token is not None:
|
|
288
297
|
headers["Authorization"] = f"Bearer {self.options.local_agent_token}"
|
|
298
|
+
uds = self.options.local_agent_socket
|
|
299
|
+
transport = (
|
|
300
|
+
None if uds is None else receiver_async_transport(uds, self.options.receiver_uids)
|
|
301
|
+
)
|
|
289
302
|
# No read timeout: a stream may stay quiet while the agent works.
|
|
290
|
-
|
|
303
|
+
# No environment proxies and no redirects: the token and the
|
|
304
|
+
# messages go to local_agent_url (or its socket) and nowhere else.
|
|
305
|
+
self._http = httpx.AsyncClient(
|
|
306
|
+
headers=headers,
|
|
307
|
+
timeout=httpx.Timeout(30.0, read=None),
|
|
308
|
+
transport=transport,
|
|
309
|
+
trust_env=False,
|
|
310
|
+
follow_redirects=False,
|
|
311
|
+
)
|
|
291
312
|
await self._load_card()
|
|
292
313
|
if self.options.facade_port is not None and self.facade is None:
|
|
293
314
|
self.facade_token = self._facade_token()
|
|
@@ -451,6 +472,9 @@ class A2AAdapter:
|
|
|
451
472
|
stream = aiter(self._plain.send_message(request))
|
|
452
473
|
first = await anext(stream, None)
|
|
453
474
|
except A2AClientError as e:
|
|
475
|
+
refused = _refusal(e)
|
|
476
|
+
if refused is not None:
|
|
477
|
+
LOG.warning("a2a_receiver_refused", reason=str(refused))
|
|
454
478
|
LOG.warning(
|
|
455
479
|
"a2a_local_unreachable", group=log.hid(group.group_id), error=type(e).__name__
|
|
456
480
|
)
|
|
@@ -543,8 +567,25 @@ class A2AAdapter:
|
|
|
543
567
|
async with asyncio.timeout(CARD_TIMEOUT):
|
|
544
568
|
card = await resolver.get_agent_card()
|
|
545
569
|
except (A2AError, httpx.HTTPError, TimeoutError) as e:
|
|
570
|
+
refused = _refusal(e)
|
|
571
|
+
if refused is not None:
|
|
572
|
+
LOG.warning("a2a_receiver_refused", reason=str(refused))
|
|
546
573
|
LOG.warning("a2a_card_unavailable", error=type(e).__name__)
|
|
547
574
|
return False
|
|
575
|
+
pinned = (
|
|
576
|
+
None if self.options.local_agent_socket is not None else self.options.local_agent_url
|
|
577
|
+
)
|
|
578
|
+
elsewhere = _other_endpoint(card, pinned)
|
|
579
|
+
if elsewhere is not None:
|
|
580
|
+
# On TCP the client would send the token and messages where the
|
|
581
|
+
# card says: only the configured endpoint is trusted (SR-10). On
|
|
582
|
+
# the socket every request goes there, so only an unusable URL is.
|
|
583
|
+
LOG.warning(
|
|
584
|
+
"a2a_card_endpoint_mismatch",
|
|
585
|
+
configured_endpoint=_origin(self.options.local_agent_url),
|
|
586
|
+
card_endpoint=_origin(elsewhere),
|
|
587
|
+
)
|
|
588
|
+
return False
|
|
548
589
|
status = cards.signature_status(card, self.options.card_keys)
|
|
549
590
|
if status == "invalid":
|
|
550
591
|
LOG.warning("a2a_local_card_invalid")
|
|
@@ -563,3 +604,49 @@ class A2AAdapter:
|
|
|
563
604
|
def _forget(self, group_id: str) -> None:
|
|
564
605
|
for table in (self._groups, self._cards, self._peers, self._context):
|
|
565
606
|
table.pop(group_id, None)
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
def _refusal(e: BaseException) -> PeerMismatchError | None:
|
|
610
|
+
"""The receiver check behind a client error, if that was the cause."""
|
|
611
|
+
seen: set[int] = set()
|
|
612
|
+
cur: BaseException | None = e
|
|
613
|
+
while cur is not None and id(cur) not in seen:
|
|
614
|
+
if isinstance(cur, PeerMismatchError):
|
|
615
|
+
return cur
|
|
616
|
+
seen.add(id(cur))
|
|
617
|
+
cur = cur.__cause__ or cur.__context__
|
|
618
|
+
return None
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
def _endpoint(url: str) -> tuple[str, str, int | None] | None:
|
|
622
|
+
"""Scheme, host and port of ``url``; ``None`` when it can't be parsed
|
|
623
|
+
(a port out of range or not a number, an IPv6 host without brackets)."""
|
|
624
|
+
try:
|
|
625
|
+
parts = urlsplit(url)
|
|
626
|
+
port = parts.port
|
|
627
|
+
except ValueError:
|
|
628
|
+
return None
|
|
629
|
+
default = {"http": 80, "https": 443}.get(parts.scheme)
|
|
630
|
+
return parts.scheme, (parts.hostname or "").lower(), port or default
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
def _origin(url: str) -> str:
|
|
634
|
+
"""``scheme://host:port`` of ``url``: what is compared, and safe to log
|
|
635
|
+
(no path, query or credentials)."""
|
|
636
|
+
endpoint = _endpoint(url)
|
|
637
|
+
if endpoint is None:
|
|
638
|
+
return "(unparsable)"
|
|
639
|
+
scheme, host, port = endpoint
|
|
640
|
+
return f"{scheme}://{host}:{port}"
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
def _other_endpoint(card: AgentCard, configured: str | None) -> str | None:
|
|
644
|
+
"""An interface URL of ``card`` that can't be parsed or, with a
|
|
645
|
+
``configured`` endpoint, whose scheme, host or port isn't its; else
|
|
646
|
+
``None``."""
|
|
647
|
+
want = None if configured is None else _endpoint(configured)
|
|
648
|
+
for interface in card.supported_interfaces:
|
|
649
|
+
found = _endpoint(interface.url)
|
|
650
|
+
if found is None or (configured is not None and found != want):
|
|
651
|
+
return interface.url
|
|
652
|
+
return None
|
|
@@ -6,14 +6,17 @@ from agentconnect.adapter import Adapter
|
|
|
6
6
|
from agentconnect.clock import Clock
|
|
7
7
|
from agentconnect.config import Config
|
|
8
8
|
from agentconnect.deliveries import DeliveryQueue
|
|
9
|
+
from agentconnect.isolation import receiver_uids
|
|
10
|
+
from agentconnect.sockets import receiver_async_transport
|
|
9
11
|
from agentconnect.sse import SseAdapter
|
|
10
12
|
from agentconnect.tokens import load_token
|
|
11
|
-
from agentconnect.webhook import SECRET_FILE, WebhookAdapter
|
|
13
|
+
from agentconnect.webhook import SECRET_FILE, SOCKET_URL, WebhookAdapter
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
def build_adapters(config: Config, clock: Clock) -> list[Adapter]:
|
|
15
17
|
"""The adapters named by ``daemon.adapters`` (or ``daemon.default_adapter``),
|
|
16
|
-
in that order: ``webhook`` (needs ``webhook.callback_url``
|
|
18
|
+
in that order: ``webhook`` (needs ``webhook.callback_url`` or
|
|
19
|
+
``webhook.callback_socket``, checked by its server's user), ``sse``,
|
|
17
20
|
``mcp`` (the delivery queue that MCP sessions attached to this daemon pull
|
|
18
21
|
from, docs/adapters/mcp.md) and ``a2a``. The A2A adapter
|
|
19
22
|
(docs/adapters/a2a.md) also runs whenever ``a2a.local_agent_url`` or
|
|
@@ -29,11 +32,22 @@ def build_adapters(config: Config, clock: Clock) -> list[Adapter]:
|
|
|
29
32
|
adapters: list[Adapter] = []
|
|
30
33
|
names = config.adapter_names
|
|
31
34
|
for name in names:
|
|
32
|
-
if name == "webhook" and config.webhook_url is not None:
|
|
35
|
+
if name == "webhook" and (config.webhook_url or config.webhook_socket) is not None:
|
|
33
36
|
config.data_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
34
37
|
secret = load_token(config.data_dir / SECRET_FILE)
|
|
38
|
+
transport = (
|
|
39
|
+
None
|
|
40
|
+
if config.webhook_socket is None
|
|
41
|
+
else receiver_async_transport(config.webhook_socket, receiver_uids(config))
|
|
42
|
+
)
|
|
35
43
|
adapters.append(
|
|
36
|
-
WebhookAdapter(
|
|
44
|
+
WebhookAdapter(
|
|
45
|
+
config.webhook_url or SOCKET_URL,
|
|
46
|
+
secret,
|
|
47
|
+
clock,
|
|
48
|
+
transport=transport,
|
|
49
|
+
timeout=config.webhook_timeout,
|
|
50
|
+
)
|
|
37
51
|
)
|
|
38
52
|
elif name == "sse":
|
|
39
53
|
adapters.append(SseAdapter(clock, config.sse_retention))
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""TLS for the local API on loopback TCP, with a pinned certificate (SR-08).
|
|
2
|
+
|
|
3
|
+
On TCP any local user can bind the port while the daemon is down, so the
|
|
4
|
+
clients authenticate the daemon before sending anything. The daemon keeps a
|
|
5
|
+
self-signed certificate and its key in the data directory, with the
|
|
6
|
+
certificate's SHA-256 next to the API tokens:
|
|
7
|
+
|
|
8
|
+
* ``api-tls.crt``: the certificate (ECDSA P-256, ``127.0.0.1`` and
|
|
9
|
+
``localhost``), PEM. ``curl --cacert`` takes it.
|
|
10
|
+
* ``api-tls.key``: its private key, PKCS#8 PEM.
|
|
11
|
+
* ``api-tls.sha256``: the certificate's SHA-256 (of the DER), in hex.
|
|
12
|
+
|
|
13
|
+
All three are 0600 and owned by the daemon's user; a looser one stops the
|
|
14
|
+
start, as a loose token file does. The certificate is valid for
|
|
15
|
+
:data:`LIFETIME` and replaced :data:`RENEW_BEFORE` its expiry, when the
|
|
16
|
+
daemon starts and while it runs; it is also replaced when a file is missing
|
|
17
|
+
or damaged, or when the key, certificate and fingerprint don't belong
|
|
18
|
+
together. Deleting the three files and restarting rotates it on demand.
|
|
19
|
+
|
|
20
|
+
Clients read the certificate and fingerprint from the directory they read
|
|
21
|
+
the token from, on every connection, so a rotation needs no new client.
|
|
22
|
+
They trust only that certificate (chain, ``127.0.0.1`` and dates checked by
|
|
23
|
+
OpenSSL) and compare the presented certificate's SHA-256 with the
|
|
24
|
+
fingerprint before writing a byte (:mod:`agentconnect.sockets`).
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import hashlib
|
|
30
|
+
import hmac
|
|
31
|
+
import ipaddress
|
|
32
|
+
import os
|
|
33
|
+
import ssl
|
|
34
|
+
import stat
|
|
35
|
+
from dataclasses import dataclass
|
|
36
|
+
from datetime import datetime, timedelta
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
from typing import Any
|
|
39
|
+
|
|
40
|
+
from cryptography import x509
|
|
41
|
+
from cryptography.exceptions import InvalidSignature
|
|
42
|
+
from cryptography.hazmat.primitives import hashes
|
|
43
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
44
|
+
from cryptography.hazmat.primitives.serialization import (
|
|
45
|
+
Encoding,
|
|
46
|
+
NoEncryption,
|
|
47
|
+
PrivateFormat,
|
|
48
|
+
load_pem_private_key,
|
|
49
|
+
)
|
|
50
|
+
from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID
|
|
51
|
+
|
|
52
|
+
from agentconnect import log
|
|
53
|
+
from agentconnect.keystore import write_private
|
|
54
|
+
|
|
55
|
+
LOG = log.get("agentconnect.api_tls")
|
|
56
|
+
|
|
57
|
+
CERT_FILE = "api-tls.crt"
|
|
58
|
+
KEY_FILE = "api-tls.key"
|
|
59
|
+
FINGERPRINT_FILE = "api-tls.sha256"
|
|
60
|
+
LIFETIME = timedelta(days=90)
|
|
61
|
+
RENEW_BEFORE = timedelta(days=30)
|
|
62
|
+
# Valid from a little before it was made, for clocks slightly behind.
|
|
63
|
+
BACKDATE = timedelta(hours=1)
|
|
64
|
+
HOST = "127.0.0.1"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass(frozen=True)
|
|
68
|
+
class ServerCert:
|
|
69
|
+
"""The daemon's certificate on disk."""
|
|
70
|
+
|
|
71
|
+
cert: Path
|
|
72
|
+
key: Path
|
|
73
|
+
fingerprint: str # hex SHA-256 of the DER certificate
|
|
74
|
+
not_after: datetime
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def renew_at(self) -> datetime:
|
|
78
|
+
return self.not_after - RENEW_BEFORE
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _check_private(path: Path) -> None:
|
|
82
|
+
st = path.lstat()
|
|
83
|
+
if stat.S_ISLNK(st.st_mode) or st.st_mode & 0o077 or st.st_uid != os.geteuid():
|
|
84
|
+
msg = f"{path.name} must be a file owned by this user with mode 0600"
|
|
85
|
+
raise PermissionError(msg)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def fingerprint(der: bytes) -> str:
|
|
89
|
+
return hashlib.sha256(der).hexdigest()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _load(data_dir: Path) -> tuple[ServerCert | None, str]:
|
|
93
|
+
"""The certificate on disk if its three files are present and belong
|
|
94
|
+
together; else ``None`` and why not."""
|
|
95
|
+
paths = [data_dir / n for n in (CERT_FILE, KEY_FILE, FINGERPRINT_FILE)]
|
|
96
|
+
present = [p for p in paths if p.exists() or p.is_symlink()]
|
|
97
|
+
for p in present:
|
|
98
|
+
_check_private(p)
|
|
99
|
+
if len(present) < len(paths):
|
|
100
|
+
return None, "missing" if not present else "incomplete"
|
|
101
|
+
cert_file, key_file, fp_file = paths
|
|
102
|
+
try:
|
|
103
|
+
cert = x509.load_pem_x509_certificate(cert_file.read_bytes())
|
|
104
|
+
key = load_pem_private_key(key_file.read_bytes(), password=None)
|
|
105
|
+
pinned = fp_file.read_text(encoding="ascii").strip()
|
|
106
|
+
except (ValueError, TypeError, UnicodeDecodeError):
|
|
107
|
+
return None, "unreadable"
|
|
108
|
+
if not isinstance(key, ec.EllipticCurvePrivateKey) or key.public_key() != cert.public_key():
|
|
109
|
+
return None, "key_mismatch"
|
|
110
|
+
try:
|
|
111
|
+
cert.verify_directly_issued_by(cert)
|
|
112
|
+
except (ValueError, TypeError, InvalidSignature):
|
|
113
|
+
return None, "unreadable"
|
|
114
|
+
found = fingerprint(cert.public_bytes(Encoding.DER))
|
|
115
|
+
if not hmac.compare_digest(found, pinned):
|
|
116
|
+
return None, "fingerprint_mismatch"
|
|
117
|
+
return ServerCert(cert_file, key_file, found, cert.not_valid_after_utc), ""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _create(data_dir: Path, now: datetime) -> ServerCert:
|
|
121
|
+
key = ec.generate_private_key(ec.SECP256R1())
|
|
122
|
+
public = key.public_key()
|
|
123
|
+
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "agentconnectd")])
|
|
124
|
+
not_after = now + LIFETIME
|
|
125
|
+
cert = (
|
|
126
|
+
x509.CertificateBuilder()
|
|
127
|
+
.subject_name(name)
|
|
128
|
+
.issuer_name(name)
|
|
129
|
+
.public_key(public)
|
|
130
|
+
.serial_number(x509.random_serial_number())
|
|
131
|
+
.not_valid_before(now - BACKDATE)
|
|
132
|
+
.not_valid_after(not_after)
|
|
133
|
+
.add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True)
|
|
134
|
+
.add_extension(
|
|
135
|
+
x509.SubjectAlternativeName(
|
|
136
|
+
[x509.IPAddress(ipaddress.ip_address(HOST)), x509.DNSName("localhost")]
|
|
137
|
+
),
|
|
138
|
+
critical=False,
|
|
139
|
+
)
|
|
140
|
+
.add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), critical=False)
|
|
141
|
+
.add_extension(x509.SubjectKeyIdentifier.from_public_key(public), critical=False)
|
|
142
|
+
.add_extension(x509.AuthorityKeyIdentifier.from_issuer_public_key(public), critical=False)
|
|
143
|
+
.sign(key, hashes.SHA256())
|
|
144
|
+
)
|
|
145
|
+
der = cert.public_bytes(Encoding.DER)
|
|
146
|
+
# The key first and the fingerprint last: a client that reads in
|
|
147
|
+
# between sees a certificate that doesn't match the old fingerprint and
|
|
148
|
+
# refuses that one connection; it never trusts a mismatched pair.
|
|
149
|
+
write_private(
|
|
150
|
+
data_dir / KEY_FILE, key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
|
|
151
|
+
)
|
|
152
|
+
write_private(data_dir / CERT_FILE, cert.public_bytes(Encoding.PEM))
|
|
153
|
+
write_private(data_dir / FINGERPRINT_FILE, f"{fingerprint(der)}\n".encode("ascii"))
|
|
154
|
+
return ServerCert(
|
|
155
|
+
data_dir / CERT_FILE, data_dir / KEY_FILE, fingerprint(der), cert.not_valid_after_utc
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def ensure_certificate(data_dir: Path, now: datetime) -> ServerCert:
|
|
160
|
+
"""The daemon's certificate, created or replaced when it is missing,
|
|
161
|
+
damaged, mismatched, or due for renewal at ``now``. Raises
|
|
162
|
+
``PermissionError`` for a file that isn't private."""
|
|
163
|
+
current, why = _load(data_dir)
|
|
164
|
+
if current is not None and now < current.renew_at:
|
|
165
|
+
return current
|
|
166
|
+
if current is not None:
|
|
167
|
+
why = "expired" if now >= current.not_after else "renewal"
|
|
168
|
+
made = _create(data_dir, now)
|
|
169
|
+
LOG.info("api_tls_certificate", reason=why, not_after=made.not_after.isoformat())
|
|
170
|
+
return made
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def server_context(cert: ServerCert) -> ssl.SSLContext:
|
|
174
|
+
"""The TCP listener's TLS context: TLS 1.2 or later, this certificate."""
|
|
175
|
+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
176
|
+
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
177
|
+
ctx.load_cert_chain(cert.cert, cert.key)
|
|
178
|
+
return ctx
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
# -- the client side ------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class PinError(Exception):
|
|
185
|
+
"""The presented certificate isn't the pinned one."""
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@dataclass(frozen=True)
|
|
189
|
+
class Pin:
|
|
190
|
+
"""What a client trusts: the context and the expected SHA-256."""
|
|
191
|
+
|
|
192
|
+
context: ssl.SSLContext
|
|
193
|
+
digest: bytes
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def read_pin(token_dir: Path) -> Pin:
|
|
197
|
+
"""The certificate and fingerprint in ``token_dir``, as a client context
|
|
198
|
+
that trusts only that certificate. Raises ``FileNotFoundError`` when
|
|
199
|
+
there is none, :class:`PinError` when the two don't match."""
|
|
200
|
+
pem = (token_dir / CERT_FILE).read_text(encoding="ascii")
|
|
201
|
+
pinned = (token_dir / FINGERPRINT_FILE).read_text(encoding="ascii").strip()
|
|
202
|
+
try:
|
|
203
|
+
der = ssl.PEM_cert_to_DER_cert(pem)
|
|
204
|
+
except ValueError:
|
|
205
|
+
msg = f"{token_dir / CERT_FILE} is not a certificate"
|
|
206
|
+
raise PinError(msg) from None
|
|
207
|
+
if not hmac.compare_digest(fingerprint(der), pinned):
|
|
208
|
+
msg = f"the certificate and {FINGERPRINT_FILE} in {token_dir} don't match"
|
|
209
|
+
raise PinError(msg)
|
|
210
|
+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) # hostname and chain checked
|
|
211
|
+
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
212
|
+
ctx.load_verify_locations(cadata=der)
|
|
213
|
+
return Pin(ctx, bytes.fromhex(pinned))
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def check_peer(ssl_object: Any, pin: Pin) -> None:
|
|
217
|
+
"""Raise :class:`PinError` unless the connection's certificate is the
|
|
218
|
+
pinned one."""
|
|
219
|
+
der = ssl_object.getpeercert(True)
|
|
220
|
+
if der is None or not hmac.compare_digest(hashlib.sha256(der).digest(), pin.digest):
|
|
221
|
+
msg = "the server's certificate is not the pinned one"
|
|
222
|
+
raise PinError(msg)
|