Skip to content

fix(axum-rest-api-server): [#2726] check HTTPS REST APIs over TLS - #2730

Merged
josecelano merged 5 commits into
torrust:developfrom
josecelano-bot:2726-rest-api-https-health-check
Nov 5, 2026
Merged

josecelano merged 5 commits into
torrust:developfrom
josecelano-bot:2726-rest-api-https-health-check

Conversation

@josecelano-bot

Copy link
Copy Markdown
Contributor

Summary

Fixes #2726: the REST API health check hard-coded http://, so a REST API serving TLS was probed over plain HTTP and the health check API reported status: Error although the service answered 200 over HTTPS. The URL is now built from the service binding, as the HTTP tracker does since #2089, so the scheme follows the served protocol.

  • check_fn is split into check_fn, check_fn_with_client and health_check_url. Production still uses reqwest::Client::new(), so certificate validation is unchanged; the client-taking variant lets tests trust a known certificate.
  • Two regression tests: a unit test of the URL builder (HTTP and HTTPS bindings) and a test that starts the token-aware server with a TLS launcher and runs the check on the registered binding with a client that trusts the test certificate.
  • The self-signed test certificate and key are copied into packages/axum-rest-api-server/tests/fixtures/ (decision recorded in the spec).
  • The container HEALTHCHECK was never affected (the report is served with HTTP 200); the harm was a false Error body.

Evidence

Red and green test output and the real-binary recheck are in docs/issues/open/2726-rest-api-https-health-check/manual-verification-evidence.md. With a trusted certificate the REST API entry is Ok over https://; with an untrusted self-signed certificate it stays Error, as for the HTTP tracker.

Out of scope and parked: check_fn returns Ok for any HTTP status, including a 500.

Closes #2726

🤖 Generated with Claude Code

josecelano-bot and others added 4 commits November 5, 2026 12:21
…ck URL builder and a client-taking check

Add the REST API test certificate fixtures (a copy of the health check API
package's self-signed test certificate and key) and split check_fn into
check_fn_with_client and health_check_url, as the HTTP tracker does. The
URL is still built from the bind address, so behavior is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Build the health check URL from the service binding so the scheme follows
the served protocol, as the HTTP tracker does. Add regression tests for the
URL builder and for the registered service of a TLS REST API, and record the
red and green runs and the real-binary recheck in the issue folder.
Production certificate validation is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ovider in the TLS health check test

The workspace enables more than one Rustls provider, so the TLS regression
test panicked in the pre-push workspace run. Install ring once, as the health
check API tests do, with rustls as a dev-dependency (already in Cargo.lock).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…cate instead of reading it by path

The Container workflow runs the tests from a different directory than the one
the binaries were built in, so the compile-time fixture path did not exist.
Embed the certificate and key with include_bytes!, as the health check API
test does.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Fixes REST API health checks for TLS-enabled bindings by deriving the health-check URL (including scheme) from the ServiceBinding, and adds regression tests + fixtures to validate HTTPS behavior.

Changes:

  • Build the health-check URL from ServiceBinding::url() and switch the probe to use an injectable reqwest::Client.
  • Add regression tests for URL construction (HTTP/HTTPS) and an end-to-end TLS health check using a trusted test cert.
  • Add TLS test fixtures (cert/key) and supporting documentation/evidence for issue #2726.
File Description
packages/​axum-rest-api-server/​src/​server.rs Derives health-check URL from binding URL and adds tests covering HTTPS probing with a trusted cert.
packages/​axum-rest-api-server/​Cargo.toml Adds rustls dev-dependency to select a crypto provider in tests.
packages/​axum-rest-api-server/​tests/​fixtures/​https-health-check-cert.pem Adds a self-signed certificate fixture for TLS tests.
packages/​axum-rest-api-server/​tests/​fixtures/​https-health-check-key.pem Adds the corresponding private key fixture for TLS tests.
docs/​issues/​open/​2726-rest-api-https-health-check/​manual-verification-evidence.md Records manual verification evidence for the regression and fix.
docs/​issues/​open/​2726-rest-api-https-health-check/​ISSUE.md Updates issue status/progress and aligns spec text with the implemented fix/tests.

Review effort: Lite

@josecelano

Copy link
Copy Markdown
Member

ACK b261933

@josecelano
josecelano merged commit b3f26f6 into torrust:develop Nov 5, 2026
22 checks passed
@josecelano-bot

Copy link
Copy Markdown
Contributor Author

Post-merge audit approval for PR #2730 (recorded by josecelano-bot on behalf of the maintainer, who approved in chat, in reply to the question "Open the archive PR with or without the audit?", answering "OK to both").

PR #2730 merged at 2026-11-05 13:47:50 UTC. Its two Copilot review threads (review 5785639542) were replied to and resolved before the merge, but the audit record docs/pr-reviews/pr-2730-review/PR-REVIEW.md was not committed to the PR branch in time. The maintainer approved adding it to the close-out PR that archives the #2726 spec, as the process-pr-review post-merge workflow requires. Nothing else in the audit changes the merged tree.

josecelano-bot added a commit to josecelano-bot/torrust-tracker that referenced this pull request Nov 5, 2026
josecelano added a commit that referenced this pull request Nov 5, 2026
…ssues/closed

dc1b141 docs(pr-reviews): [#2730] add the review audit record (josecelano-bot)
ca93e39 chore(issues): archive closed issue #2726 spec to docs/issues/closed (josecelano-bot)

Pull request description:

  ## Summary

  Close-out for #2726 (fixed by #2730, spec in #2728).

  - Archives the #2726 specification and its manual-verification evidence from `docs/issues/open/` to `docs/issues/closed/` (`status: done`, new `spec-path`, `related-pr: 2730`, progress-log entry). No other live reference to the old path exists.
  - Adds the review audit record for #2730, `docs/pr-reviews/pr-2730-review/PR-REVIEW.md`. Both Copilot threads were replied to and resolved before the merge, but the audit was not committed in time, so it comes in this close-out PR as the post-merge workflow requires. The maintainer's approval is recorded in #2730 (comment) and in the audit's Ownership section. `validate-audit-record.py` reported `{"status": "ok", "rows": 2, "log_entries": 5, "failures": 0}`.

  Two commits, kept separate: the archive move, then the audit record. Documentation only; no code changes.

  Related to #2726

  🤖 Generated with [Claude Code](https://claude.com/claude-code)

ACKs for top commit:
  josecelano:
    ACK dc1b141

Tree-SHA512: 50f1ec05f5b07e23143da74c5d2157430e7dff39396ca6d8605b5a5df866dec7a1c586ece665bd90830f0ad05a18e53c13b324806aedc85ee5cbc7364b86ae4f

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of the change from #2730 as it stands at a94c3ce on the default branch.

What still runs. check_fn (packages/axum-rest-api-server/src/server.rs:253) passes reqwest::Client::new() to the public check_fn_with_client (server.rs:262), and health_check_url (server.rs:277) builds the probe URL from ServiceBinding::url(), so a TLS REST API is now probed over https://. The only later edit to these lines is c8cffec (#2733), which reports a non-2xx answer as an error, so the "Ok for any status" behaviour the description left out of scope is already fixed. The rustls dev-dependency (packages/axum-rest-api-server/Cargo.toml:74), the one-line Cargo.lock delta, the fixtures and the two regression tests (server.rs:612-691) are still present; the tests now use the package's shared test helpers.

[Minor] The HTTPS probe checks the certificate against the numeric bind address, so the symptom reported in #2726 remains for the default layout. health_check_url (packages/axum-rest-api-server/src/server.rs:277-283) yields https://<bind IP>:<port>/api/health_check, and the default client checks the server certificate against that IP. With bind_address = "0.0.0.0:1212" (as in share/default/config/tracker.development.sqlite3.toml:57) and a [http_api.tls_config] certificate issued for the host's DNS name and trusted by the host, the probe goes to https://0.0.0.0:1212/api/health_check, the name check fails, and the health report lists a healthy REST API as Error, as before the change but for a different reason. Acceptance criterion AC5 of the issue ("healthy when the host trusts the certificate") and the recorded evidence hold only for a certificate with an IP SAN equal to a specific bind address (the evidence binds 127.0.0.1 with an IP:127.0.0.1 certificate). The HTTP tracker records this constraint in docs/issues/closed/2089-fix-https-tracker-health-check-protocol/tls-manual-test.md; nothing in this package or its issue does. Smallest fix: state in the check_fn doc comment (server.rs:245-250) and in the REST API TLS configuration documentation that the check passes only when the certificate carries an IP SAN for the exact bind address, so an unspecified address (0.0.0.0, [::]) always reports Error. A full fix checks the certificate against the configured public URL's host while connecting to the bound socket (for example with ClientBuilder::resolve), which needs that host to reach the check; today the check receives only the ServiceBinding.

[Minor] Client construction moved out of the job and became a panic. Before the change, reqwest::get built its client inside the spawned job and returned a build failure as an Err; now check_fn (packages/axum-rest-api-server/src/server.rs:254) calls reqwest::Client::new() synchronously, which reqwest documents as panicking when the TLS backend cannot be initialised. The health check API runs check_fn through spawn_check() inside its request handler (packages/axum-health-check-api-server/src/handlers.rs:42), before the code at handlers.rs:58-67 that turns a panicked job into a failed entry, and its router has no panic-catching layer. Such a failure therefore drops the whole /health_check response instead of marking one service, the container probe (src/bin/http_health_check.rs) gets no answer, and the container turns unhealthy. The HTTP tracker's check_fn (packages/axum-http-server/src/server.rs:315) has the same shape. Smallest fix: build the client with reqwest::Client::builder().build() and, on Err, return a ServiceHealthCheckJob whose job resolves to Err(error.to_string()), the way src/bin/http_health_check.rs:56-59 already handles the same failure.

[Minor] A failed HTTPS probe reports no cause. check_fn_with_client maps a request error to err.to_string() (packages/axum-rest-api-server/src/server.rs:271), which for reqwest is only "error sending request for url (...)"; the TLS cause (unknown issuer, name mismatch) is in the error's source chain and is dropped. The pull request's own real-binary evidence shows it: an untrusted certificate is reported as {"Err":"error sending request for url (https://127.0.0.1:41212/api/health_check)"}, the same top-level text a refused connection produces. Since certificate verification is now the expected failure for TLS deployments (see the bind-address paragraph above), the report does not tell an operator what to fix. Smallest fix: append the source chain with write!, as unreachable in src/bin/http_health_check.rs:90-100 does.

[Nit] check_fn_with_client is public (packages/axum-rest-api-server/src/server.rs:262) but has no caller outside its crate at a94c3ce; its only callers are the unit tests in the same file (server.rs:683 and server.rs:854), which can reach a private function. It adds to the public API of a publishable crate a probe that accepts any client, including one built to skip certificate checks, while its doc comment says it exists for tests. Smallest fix: make it pub(crate).

[Nit] packages/axum-rest-api-server/tests/fixtures/https-health-check-key.pem is a committed private key without the test-only notice that its byte-identical copies carry in packages/axum-http-server/tests/fixtures/README.md and packages/axum-server/tests/fixtures/README.md, and with publish = true (Cargo.toml:88) and no include or exclude in the package manifest, the packaged crate includes it. Smallest fix: add the same README to this fixture directory and exclude tests/fixtures/*.pem from the package; the files are read only by include_bytes! inside #[cfg(test)] (server.rs:613 and server.rs:626), so a non-test build does not need them.

Checked and found sound: the expect in health_check_url cannot fire, because joining a relative path onto http(s)://ip:port/ cannot fail; the URL unit test covers both schemes; production certificate validation is unchanged (the default client before and after); the fixture certificate (CN and IP SAN 127.0.0.1, CA:FALSE, serverAuth, valid until 2036-08-21) matches its RSA key; the Cargo.lock delta only lists the already-locked rustls under this package, with no new package or version; the dev-dependency's features do not reach non-test builds under the edition 2024 resolver; nothing else in the package installs a Rustls provider, so the one-time ring install cannot collide; the TLS test stops its server through stop() within the test deadline.

This branch was previously deployed

1 inactive deployment
coverage — b261933e Deployed Nov 5, 2026 by josecelano-bot via Generate Coverage Report #2929
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants