Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**Issue**: [#1113](https://github.com/dan-petty/devops-cli/issues/1113)
**Scope**: scope/cli, scope/security
**Feasibility**: Checked against valkey-py 6.2.0 (released 2026-10-12, MIT, `requires-python >=3.11`, `py.typed`, no required runtime dependency on Python 3.14), installed into the project environment with `uv add`, which adds only valkey, fakeredis 2.40.0 (dev group) and redis 8.1.0 to `uv.lock`. A client over `valkey.ConnectionPool` was run on CPython 3.14.7 against fakeredis's in-memory Valkey server and against loopback sockets: a wrong password, a refused port, a silent listener, a rejected command and a dropped server each raise a valkey-py exception, and a pipeline raises it from `execute`, outside any single-command wrapper, so the client maps failures around every call it makes. `uv run mypy src` (strict) accepts the client once calls into valkey-py's unannotated functions go through one helper. `python -X importtime` shows no `valkey` module imported by `devops --help`. The checks for Part 2 ran on the release tip 3ef69850. A search of `src` and `tests` found no user outside the deleted code. The search covered the two package names, every exported symbol, the tier constants and the `ai.cache.backend` option. A search of `src` found no reader of the setting. A config file that still holds `ai.cache.backend` loads. The next save drops the key, because `AICacheConfig` ignores unknown keys. Now `devops config get ai.cache.backend` exits 1 with `Unknown config key`.
**Update**: Item #1988 replaced the destination check. The client now resolves a name once, when it dials, vets every answer at the private egress level and dials a vetted address. `ValkeyClient` has no `allow_private_network` argument. This file records the original work.

## Description

Expand Down Expand Up @@ -55,7 +56,7 @@ Behaviour that changes with the library:
- [x] Lower the swallowed-failure ceilings, edit the knowledge-base class name and key examples, and regenerate `docs/ENV_VARS.md` and `docs/CONFIGURATION.md`.
- [x] Add a short note to the task files of #154, #312 and #1741, which describe the deleted code as present.
- Not done: one validated `valkey.url` with `--url` on the `devops valkey` commands, in place of `valkey.host`, `valkey.port` and `valkey.db` (a later Part of #1113).
- Not done: resolve a name once, check every answer and dial the checked address, under #898's resolve-and-check function (a later Part of #1113).
- [x] Resolve a name once, check every answer and dial the checked address, under #898's resolve-and-check function. `docs/agent/tasks/task-1988-valkey-vetted-dial.md` describes this step.
- Not done: build the k8s URLs with `httpx2.URL` (a later Part of #1113).
- [x] Fix the dashboard INFO reading and its plaintext password. `docs/agent/tasks/task-1113-the-valkey-dashboard-panel-reads-the-real-client.md` describes this step.
- Not done: share one fail-soft client between the embeddings, library-symbol and threat-intel caches (a later Part of #1113).
Expand Down
46 changes: 46 additions & 0 deletions docs/agent/tasks/task-1988-valkey-vetted-dial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Task: The Valkey client resolves a name once, vets every answer and dials the vetted address (#1988)

**Issue**: [#1988](https://github.com/dan-petty/devops-cli/issues/1988)
**Scope**: scope/security
**Feasibility**: This check used the source of valkey-py 6.2.0 in the project environment. `Connection._connect` calls `socket.getaddrinfo(self.host, ...)` itself and has no resolver hook. `SSLConnection._connect` gets its socket from `super()._connect()` and then calls `_wrap_socket_with_ssl`, which sends `server_hostname=self.host`. `ssl_check_hostname` is `False` by default. The default retry is `Retry(NoBackoff(), 0)`, and `AbstractConnection.connect` catches only `socket.timeout` and `OSError`. So `SSRFBlockedError` and `httpcore2.ConnectError` pass through valkey-py unchanged. A probe on the tree before the change counted the lookups of one `ping` with a resolver that changes its answer. The client made three lookups and dialled the third answer, 127.0.0.2, which no check saw. `vet_addresses` of #898 at `EgressLevel.PRIVATE` refuses every spelling of a cloud metadata address and admits 169.254.1.2.

## Description

Step C of #1113. Before this change, the Valkey client resolved a name three times: in the constructor, in the check of `_GuardedConnection.connect`, and in the dial of valkey-py. The check saw only the first two answers. A rebinding DNS server could give the dial an address that no check saw. A failed lookup passed the check with no address when private networks were allowed, and the check refused every link-local address, also the 169.254.1.2 that a rootless Podman container reaches its host at.

Now the constructor makes no lookup. `_VettedConnection._connect` resolves the host once for each dial with `vet_addresses` at `EgressLevel.PRIVATE`. It dials the vetted IP literals in answer order and raises the last failure, as `VettingBackend.connect_tcp` does. `_VettedSSLConnection` puts the `SSLConnection` of valkey-py in front of the vetted dial. The TCP connection goes to the vetted literal, and the handshake uses the host name of the endpoint for SNI and for the certificate check, with `ssl_check_hostname=True` and the platform CA store. `_valkey_failures` makes a refusal of the egress check and a failed lookup a `ValkeyConnectionError` that names the host and the port.

Behaviour that changes:

- A cloud metadata address and a scoped IPv6 address are refused when the client dials, not when it is built. The error is `ValkeyConnectionError`.
- A link-local address that is not a cloud metadata address, such as 169.254.1.2, is dialled.
- A failed lookup is a `ValkeyConnectionError` that names the host.
- A `valkeys://` or `rediss://` endpoint in the `host` argument connects over TLS and checks the certificate against the host name. Before, it connected in plaintext. `runs.index_url` gives the client its URL in this argument.
- `validate_destination`, `_resolved_addresses`, `_GuardedConnection` and the `allow_private_network` argument of `ValkeyClient` are gone. No caller in `src` passed the argument, so every connection used the private level before and after.

Open tools used: `vet_addresses` of #898 for the lookup and the check, `SSLConnection` of valkey-py for TLS, and the generators of `devops_cli.crypto.tls_certificates` for the test CA. The build is one `_connect` override of 16 lines and one TLS class with no body.

## Acceptance Criteria

- [x] The constructor makes no DNS lookup (`test_the_constructor_makes_no_lookup`).
- [x] One dial makes one lookup. The name answers loopback first and a public address after, and the client dials loopback (`test_one_dial_makes_one_lookup_and_dials_the_address_it_vetted`).
- [x] The client dials the vetted IP literals in answer order. The first answer refuses the connection and the second accepts it (`test_the_client_dials_the_vetted_addresses_in_answer_order`).
- [x] A name that answers a metadata address, `169.254.169.254`, `2852039166` and `::ffff:169.254.169.254` each give `ValkeyConnectionError`, not `SSRFBlockedError`, and no connect (`test_a_host_that_answers_cloud_metadata_is_a_connection_error_with_no_dial`). The floor fails a test whose code connects to an address that is not loopback.
- [x] A failed lookup gives `ValkeyConnectionError`, and the message names the host (`test_a_failed_lookup_is_a_connection_error_that_names_the_host`).
- [x] The client dials 169.254.1.2 (`test_a_link_local_address_that_is_not_cloud_metadata_is_dialled`).
- [x] The client dials `2130706433` as 127.0.0.1 (`test_a_numeric_spelling_is_dialled_as_the_address_it_spells`). The metadata test refuses the spelling `2852039166`.
- [x] For `valkeys://`, a loopback TLS server with a CA that the generators of `devops_cli.crypto.tls_certificates` make shows that the client accepts a certificate for the URL host and refuses a certificate for another name from the same CA. The server of the refused handshake records no command (`TestValkeyClientTls`).
- [x] `validate_destination`, `_resolved_addresses` and the `allow_private_network` argument are not in `valkey/client.py`.
- [x] The new tests failed on the tree before the change, 11 of them, and pass after it. A change of `ssl_check_hostname` to `False` fails the TLS test. A dial of the host name in place of the vetted literal fails five tests.
- [x] These commands pass: `uv run ruff check`, `uv run ruff format` and `uv run mypy` on the changed files, and the targeted tests of the modules that reach the client.
- Pending a person: against a real Valkey 8 that a DNS name reaches, run `uv run devops valkey ping` and `uv run devops ai runs reindex`. Both connect.
- Pending a person: set `runs.index_url` to the `valkeys://` URL of a TLS Valkey whose certificate names the host, and run `uv run devops ai runs reindex`. It connects. With a certificate for another name, it fails with `certificate verify failed`.

## Deliverables

- [x] Replace the destination check in `src/devops_cli/valkey/client.py` with `_VettedConnection` and `_VettedSSLConnection`. Map the refusals in `_valkey_failures`. Remove the `allow_private_network` argument.
- [x] Add `CONST_VALKEY_TLS_SCHEMES` to `src/devops_cli/config/constants.py`.
- [x] Replace `TestValkeyClientDestination` in `tests/test_valkey.py` and add `TestValkeyClientTls`. The tests use the shared `ScriptedResolver` of `tests/web_fakes.py`.
- [x] Add the TLS handshake to `serving_valkey` in `tests/support/fakes/valkey.py`, and add the `local_ca` fixture in `tests/support/certificates.py`.
- [x] Add an update line to `docs/agent/tasks/task-1113-valkey-py-replaces-the-hand-written-client.md`, and tick its step.
- `valkey.url` gives the client a host and a port, not the scheme, so a `valkeys://` URL there still connects in plaintext. Step B of #1113 gives every endpoint one validated URL: moved to #2026.
3 changes: 3 additions & 0 deletions src/devops_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,9 @@ class WebhookExposure(StrEnum):
{"valkey", "valkeys", "redis", "rediss", "tcp"}
)

# Schemes of a Valkey endpoint that valkey-py dials over TLS
CONST_VALKEY_TLS_SCHEMES: Final[frozenset[str]] = frozenset({"valkeys", "rediss"})

# Characters of a failed embedding reply's body quoted in its EmbeddingsError
CONST_EMBEDDING_REPLY_EXCERPT_CHARS: Final[int] = 500

Expand Down
Loading
Loading