Repository navigation
feat(mqtt): optional broker-side TLS termination for mqtts - #14249
ArnabChatterjee20k wants to merge 7 commits into
Conversation
Tls now wraps another transport and adds SSL to its sock type and settings, so one class serves both mqtts (over Tcp) and wss (over WebSocket). Transport gains isWebSocket(), which the wrapper delegates to the inner transport, so a secure WebSocket is still selected as the Swoole master listener. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The broker starts an mqtts TLS listener alongside the plaintext one when _APP_MQTT_TLS_CERT and _APP_MQTT_TLS_KEY are set, so a load balancer can reach it without a TLS-terminating proxy in the path. The listener is off by default, so the existing upstream-termination path is unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR enables TLS termination in the MQTT broker: env vars are added,
Reviewed commit: 93ab8df |
🟡 Tier B · Needs changes before merging
Adds optional broker-side TLS termination for MQTT TCP, configured with certificate, key, and port environment variables, and generalizes the Swoole TLS transport wrapper. The PR also adds TLS fixture coverage for the MQTT package and configures the application broker to use TLS on its WebSocket listener when TLS credentials are present. Latest changes: The newest commits wrap the application WebSocket listener in TLS on the existing WebSocket port whenever MQTT TLS credentials are configured, replacing the plaintext WebSocket listener in that mode.
Fix with agent prompt### Issue 1
app/mqtt.php:247-253
**Keep the plaintext WebSocket listener when enabling mqtts**
When operators set these credentials to enable mqtts, this replaces the existing plaintext WebSocket listener on `_APP_MQTT_WS_PORT` with a TLS WebSocket listener. Existing MQTT-over-WebSocket clients or TLS-terminating proxies still connecting there send plaintext and fail the TLS handshake, contrary to the PR's mqtts-only, additive behavior; keep the plaintext listener and introduce WSS separately.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 11
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 32 existing findings tracked in
|
There was a problem hiding this comment.
Adds broker-side MQTT-over-TLS support (mqtts on port 8883) via a refactored Tls transport wrapper, new _APP_MQTT_TLS_* env vars, and docker-compose wiring.
Key findings
- 🟡 Misconfigured TLS envs fail silently or crash the broker — mqtt.php:226
| if ($tlsCert !== '' && $tlsKey !== '') { | ||
| $transports[] = new Adapter\Swoole\Tls( | ||
| new Adapter\Swoole\Tcp('0.0.0.0', (int) System::getEnv('_APP_MQTT_TLS_PORT', '8883'), $maxPacketSize), | ||
| $tlsCert, | ||
| $tlsKey, | ||
| ); | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Misconfigured TLS envs fail silently or crash the broker
TLS is enabled only when both _APP_MQTT_TLS_CERT and _APP_MQTT_TLS_KEY are non-empty (app/mqtt.php:226); setting just one is accepted silently and 8883 never opens, with no server-side signal. Setting both to missing or unreadable paths gives no pre-check either — the values flow straight into Swoole ssl_cert_file/ssl_key_file and abort server startup opaquely, taking down plain MQTT 1883 and WebSocket 8083 along with mqtts. Operators misconfiguring TLS thus get either a silently insecure broker or an undiagnosed crash.
📋 Prompt for AI Agents
In app/mqtt.php around lines 224-232, before the if ($tlsCert !== '' && $tlsKey !== '') block, add validation: if exactly one of _APP_MQTT_TLS_CERT/_APP_MQTT_TLS_KEY is set (logical XOR), log a Console::error naming the missing variable and stating TLS stays disabled. Inside the block, check is_readable($tlsCert) && is_readable($tlsKey) and fail fast with an error naming the offending path, so operators get an actionable diagnostic instead of a silent plaintext fallback or an opaque Swoole startup abort.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
The broker now reads its TLS cert/key from disk, so give appwrite-mqtt the same read-only /storage/certificates volume Traefik and the app containers already have. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The fixture broker adds an mqtts listener on 18832 from a self-signed cert that e2e.sh generates, and a new E2E test runs a full v5 session over it. This exercises the Tls transport's socket flags and SSL listener settings against real Swoole. Co-Authored-By: Claude Opus 4.8 <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Require both _APP_MQTT_TLS_CERT and _APP_MQTT_TLS_KEY together, and check each path is readable, before building the listener. A half-configured or unreadable cert now exits with a clear message instead of silently skipping mqtts or letting Swoole abort startup and take the plaintext listeners down with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
When a certificate is set the broker serves the WebSocket listener over TLS (wss) too, so a load balancer can reach both transports directly without a proxy. Only one WebSocket listener can be the Swoole master, so wss replaces the plaintext WebSocket rather than running beside it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| $transports[] = new Adapter\Swoole\Tls( | ||
| new Adapter\Swoole\WebSocket('0.0.0.0', $wsPort, $maxPacketSize), | ||
| $tlsCert, | ||
| $tlsKey, | ||
| ); | ||
| } else { | ||
| $transports[] = new Adapter\Swoole\WebSocket('0.0.0.0', $wsPort, $maxPacketSize); |
There was a problem hiding this comment.
Keep the plaintext WebSocket listener when enabling mqtts
When operators set these credentials to enable mqtts, this replaces the existing plaintext WebSocket listener on _APP_MQTT_WS_PORT with a TLS WebSocket listener. Existing MQTT-over-WebSocket clients or TLS-terminating proxies still connecting there send plaintext and fail the TLS handshake, contrary to the PR's mqtts-only, additive behavior; keep the plaintext listener and introduce WSS separately.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/mqtt.php
Line: 247-253
Comment:
**Keep the plaintext WebSocket listener when enabling mqtts**
When operators set these credentials to enable mqtts, this replaces the existing plaintext WebSocket listener on `_APP_MQTT_WS_PORT` with a TLS WebSocket listener. Existing MQTT-over-WebSocket clients or TLS-terminating proxies still connecting there send plaintext and fail the TLS handshake, contrary to the PR's mqtts-only, additive behavior; keep the plaintext listener and introduce WSS separately.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟠 Major · bug · Reply if this doesn't apply.
There was a problem hiding this comment.
@hansi-codes the swoole websocket doesn't allow both wss and ws listener together on the same server
There was a problem hiding this comment.
That Swoole limitation is real: this adapter handles WebSocket messages only on the primary WebSocket listener (Swoole.php). But the PR is scoped to mqtts, so it should not add a WSS listener at all. Keep the existing plaintext WebSocket transport and add only Tls(Tcp(...)) for mqtts; WSS can be handled separately if needed. As written, setting the mqtts cert changes the existing WebSocket port to TLS (app/mqtt.php), breaking existing plaintext WebSocket clients and proxy routes.
There was a problem hiding this comment.
@hansi-codes we need to do it for both mqtts and wss as both are used. so can you approve
There was a problem hiding this comment.
I can’t approve this as-is. The existing WSS route terminates TLS at Traefik and forwards plaintext to the broker on port 8083 (compose config). With these certs set, the code instead makes that same broker listener expect TLS (app/mqtt.php), so the existing proxy route will fail.
The Swoole limitation explains why both WebSocket modes can’t share this server; it doesn’t make changing the existing listener safe. For this mqtts change, keep the plaintext WebSocket listener and add only the TLS-wrapped TCP transport. Broker-side WSS needs a deliberate migration, including updating the proxy’s upstream behavior.
What
Let the MQTT broker terminate TLS for raw MQTT (mqtts) itself, instead of always relying on a TLS-terminating proxy in front of it.
When
_APP_MQTT_TLS_CERTand_APP_MQTT_TLS_KEYare set, the broker starts an mqtts TLS listener (default port8883) alongside the existing plaintext listeners. When they are unset, nothing changes: only the plaintext1883/8083listeners run and TLS terminates upstream as today.Why
A load balancer can then reach the broker directly, without a reverse proxy in the path. That removes the broker from a shared proxy's config-reload churn (which briefly disrupts raw-TCP passthrough routes), while HTTP/WebSocket traffic is unaffected.
How
Tlsbecomes a transport decorator. It wraps any transport and addsSWOOLE_SSLto its sock type plusssl_cert_file/ssl_key_fileto its settings, so one class serves both mqtts (overTcp) and, later, wss (overWebSocket).Transport::isWebSocket()is added and the wrapper delegates it, so a secure WebSocket is still chosen as the Swoole master listener.app/mqtt.php) adds the mqttsTls(Tcp(...))listener only when a cert is configured. It runs next to the plaintext listener, so no cutover._APP_MQTT_TLS_CERT,_APP_MQTT_TLS_KEY,_APP_MQTT_TLS_PORTregistered inapp/config/variables.phpand passed through indocker-compose.yml.Scope
Verification
php -lclean on all changed files🤖 Generated with Claude Code