Merge branch 'ws-i/tls' into integration/phase1

This commit is contained in:
ari committed 2026-10-16 07:19:28 -04:00
commit bf5f2d84f5
13 files changed
+1313 -17

No files matched your search

Generated
+238 -2
View File
@@ -82,6 +82,45 @@ version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "asn1-rs"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f43a50ac4fdca5df8e885c21b835997f0a1cdee65494a6847694a98652d9d8"
dependencies = [
"asn1-rs-derive",
"asn1-rs-impl",
"displaydoc",
"nom",
"num-traits",
"rusticata-macros",
"thiserror",
"time",
]
[[package]]
name = "asn1-rs-derive"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
"synstructure 0.13.2",
]
[[package]]
name = "asn1-rs-impl"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "async-broadcast"
version = "0.7.2"
@@ -233,6 +272,15 @@ version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
[[package]]
name = "bit-vec"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -599,6 +647,12 @@ dependencies = [
"syn 3.0.9",
]
[[package]]
name = "data-encoding"
version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
[[package]]
name = "defmt"
version = "1.2.0"
@@ -630,6 +684,26 @@ dependencies = [
"thiserror",
]
[[package]]
name = "der-parser"
version = "10.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07da5016415d5a3c4dd39b11ed26f915f52fc4e0dc197d87908bc916e51bc1a6"
dependencies = [
"asn1-rs",
"displaydoc",
"nom",
"num-bigint",
"num-traits",
"rusticata-macros",
]
[[package]]
name = "deranged"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e"
[[package]]
name = "digest"
version = "0.10.7"
@@ -1666,6 +1740,12 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "mio"
version = "1.2.4"
@@ -1677,6 +1757,16 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
@@ -1686,6 +1776,31 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "num-bigint"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num-integer"
version = "0.1.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -1695,6 +1810,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "oid-registry"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f40cff3dde1b6087cc5d5f5d4d65712f34016a03ed60e9c08dcc392736b5b7"
dependencies = [
"asn1-rs",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -1857,6 +1981,16 @@ dependencies = [
"syn 2.0.119",
]
[[package]]
name = "pem"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d354a98a3d1251555de99e8fdd8afda05573c31b82f59063a7b0a29b5527f120"
dependencies = [
"base64 0.23.1",
"serde_core",
]
[[package]]
name = "percent-encoding"
version = "2.3.2"
@@ -1919,6 +2053,12 @@ dependencies = [
"zerovec",
]
[[package]]
name = "powerfmt"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -2124,6 +2264,20 @@ dependencies = [
"rand_core 0.10.1",
]
[[package]]
name = "rcgen"
version = "0.14.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8774e05a7d0de114588e6a28fe7e71694b82614ed569d86d8b389dfbc98b8ad8"
dependencies = [
"pem",
"ring",
"rustls-pki-types",
"time",
"x509-parser",
"yasna",
]
[[package]]
name = "redox_syscall"
version = "0.5.18"
@@ -2281,6 +2435,15 @@ dependencies = [
"semver",
]
[[package]]
name = "rusticata-macros"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
dependencies = [
"nom",
]
[[package]]
name = "rustix"
version = "1.1.5"
@@ -2655,12 +2818,15 @@ dependencies = [
"clap",
"figment",
"futures",
"hyper-util",
"jiff",
"opentelemetry",
"opentelemetry-otlp",
"opentelemetry_sdk",
"rand 0.10.3",
"rcgen",
"rmcp",
"rustls",
"schemars",
"serde",
"serde_json",
@@ -2669,6 +2835,7 @@ dependencies = [
"systemd",
"thiserror",
"tokio",
"tokio-rustls",
"tokio-util",
"toml 1.1.8+spec-1.1.0",
"tower",
@@ -2956,6 +3123,17 @@ dependencies = [
"futures-core",
]
[[package]]
name = "synstructure"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "synstructure"
version = "0.14.0"
@@ -3024,6 +3202,36 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "time"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134"
dependencies = [
"deranged",
"num-conv",
"powerfmt",
"serde_core",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
[[package]]
name = "time-macros"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tinystr"
version = "0.8.4"
@@ -3852,12 +4060,40 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "x509-parser"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d43b0f71ce057da06bc0851b23ee24f3f86190b07203dd8f567d0b706a185202"
dependencies = [
"asn1-rs",
"data-encoding",
"der-parser",
"lazy_static",
"nom",
"oid-registry",
"ring",
"rusticata-macros",
"thiserror",
"time",
]
[[package]]
name = "yansi"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
[[package]]
name = "yasna"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5f6765e852b9b4dc8e2a76843e4d64d1cea8e79bcde0b6901aea8e7c7f08282"
dependencies = [
"bit-vec",
"time",
]
[[package]]
name = "yoke"
version = "0.8.3"
@@ -3878,7 +4114,7 @@ dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
"synstructure 0.14.0",
]
[[package]]
@@ -3994,7 +4230,7 @@ dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
"synstructure 0.14.0",
]
[[package]]
+6
View File
@@ -28,6 +28,11 @@ axum = "0.8.9"
tower = "0.5.3"
tower-http = { version = "0.7.1", features = ["trace"] }
# Native TLS (DEC-12): rustls with the ring provider passed explicitly, no openssl
rustls = { version = "0.23.45", default-features = false, features = ["ring", "std", "tls12"] }
tokio-rustls = { version = "0.26.5", default-features = false, features = ["ring", "tls12"] }
hyper-util = { version = "0.1.21", features = ["tokio", "server-auto", "service"] }
# Data
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.154"
@@ -66,6 +71,7 @@ base64 = "0.23.1"
[dev-dependencies]
figment = { version = "0.10.19", features = ["test"] }
rcgen = { version = "0.14", default-features = false, features = ["ring", "pem"] }
[profile.release]
lto = true
+27 -1
View File
@@ -109,7 +109,33 @@ curl -si -X POST http://127.0.0.1:9109/mcp \
### Origin and Host checks
`/mcp` validates `Origin` and `Host` to defend against DNS rebinding from browsers. Loopback names, `server.bind` and the configured port are accepted by default. Requests without an `Origin` header (non-browser clients) pass this check, so the bearer token remains the real gate. If you bind to `0.0.0.0` or serve behind a name, add the names clients use to `server.allowed_hosts` (for example `["monitor.lan:9109"]`) and any browser origins to `server.allowed_origins`. With `auth.enabled = false` (loopback only), REST requests are checked against a Host allowlist (loopback names, `server.bind` and `server.allowed_hosts`) and get `403` otherwise, to blunt DNS rebinding. There is no Origin check on REST and no token in that mode, so keep auth enabled on any multi-user host. Terminate TLS in a reverse proxy if the host is reachable beyond a trusted network; the server itself speaks plain HTTP.
`/mcp` validates `Origin` and `Host` to defend against DNS rebinding from browsers. Loopback names, `server.bind` and the configured port are accepted by default. Requests without an `Origin` header (non-browser clients) pass this check, so the bearer token remains the real gate. If you bind to `0.0.0.0` or serve behind a name, add the names clients use to `server.allowed_hosts` (for example `["monitor.lan:9109"]`) and any browser origins to `server.allowed_origins`. With `auth.enabled = false` (loopback only), REST requests are checked against a Host allowlist (loopback names, `server.bind` and `server.allowed_hosts`) and get `403` otherwise, to blunt DNS rebinding. There is no Origin check on REST and no token in that mode, so keep auth enabled on any multi-user host. If the host is reachable beyond a trusted network, enable native TLS (see the TLS section) or terminate TLS in a reverse proxy.
## TLS
TLS is optional and off by default. With `server.tls.enabled = false` (the default) the server speaks plain HTTP exactly as before, the certificate paths are ignored and never read, and no TLS code runs. A non-loopback `bind` without TLS logs a startup warning (it is not refused).
Enable it in the config file or the environment (`SKYG_SERVER__TLS__ENABLED=true`, `SKYG_SERVER__TLS__CERT_FILE`, `SKYG_SERVER__TLS__KEY_FILE`):
```toml
[server.tls]
enabled = true
cert_file = "/etc/skyg/tls/cert.pem" # PEM chain, leaf first
key_file = "/etc/skyg/tls/key.pem" # PEM, PKCS#8, PKCS#1 or SEC1
```
Both files are required and checked at startup; a missing, unparsable or mismatched pair stops the server with an error naming the file (key contents are never logged). REST and `/mcp` are then served over HTTPS only (plain HTTP on that port fails), and the default MCP `Origin` allowlist uses `https://` instead of `http://`. Add your public names to `server.allowed_hosts` and `server.allowed_origins` as before. TLS is rustls with the ring provider; openssl is not used.
Self-signed example:
```sh
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 365 \
-keyout key.pem -out cert.pem -subj '/CN=monitor.lan' \
-addext 'subjectAltName=DNS:monitor.lan,DNS:localhost,IP:127.0.0.1'
curl --cacert cert.pem -H "Authorization: Bearer $TOKEN" https://localhost:9109/api/v1/systemd/units
```
Rotation: replace the files and send `SIGHUP` (`kill -HUP $(pidof slinuxm)`, or `systemctl kill -s HUP <unit>`). New connections get the new certificate; established ones are not dropped. The certificate and key are read as two separate files, so rotation tooling must send SIGHUP only after BOTH are in place; if the pair is unreadable or mismatched (for example the cert was replaced but the key not yet), the old certificate stays, an error is logged, and you send SIGHUP again once the files are consistent. Any intermediate certificates must be in the `cert_file` chain. The TLS listener is HTTP/1.1 only, caps concurrent connections at 1024 and closes connections that send no request header within 30 seconds. SIGHUP is harmless when TLS is disabled (it is logged and ignored). For ACME, point `cert_file` and `key_file` at the client's output (for example certbot's `live/<name>/fullchain.pem` and `privkey.pem`, or the NixOS `security.acme` directory) and send SIGHUP from the renewal hook. For sops-nix or systemd `LoadCredential`, point at the decrypted or credential path (`/run/credentials/<unit>/...`) so the service user needs no access to the original key file.
## Docker access
+8 -8
View File
@@ -106,14 +106,14 @@ Design: [`plan.md`](plan.md) · Review fixes: [`plan/review.md`](plan/review.md)
## Phase 4 — TLS and NixOS packaging
### WS-I TLS · [card](plan/workstreams/WS-I-tls.md)
- [ ] **T1** Config `server.tls.{enabled,cert_file,key_file}` with validation and documented example keys
- [ ] **T2** Serve over rustls with a single pinned crypto provider. No openssl.
- [ ] **T3** Reload cert and key on SIGHUP without dropping connections. A bad cert keeps the old one.
- [ ] **T4** MCP allowed origins use `https://` when TLS is on
- [ ] **T5** Startup warning for non-loopback bind without TLS
- [ ] **T6** Tests with a generated self-signed cert: HTTPS, 401/200 over TLS, plain HTTP rejected, SIGHUP reload, bad files, bounded shutdown
- [ ] **T7** README TLS section
- [ ] **T8** Live check with `curl --cacert` and an `/mcp` initialize over HTTPS
- [x] **T1** Config `server.tls.{enabled,cert_file,key_file}` with validation and documented example keys
- [x] **T2** Serve over rustls with a single pinned crypto provider. No openssl.
- [x] **T3** Reload cert and key on SIGHUP without dropping connections. A bad cert keeps the old one.
- [x] **T4** MCP allowed origins use `https://` when TLS is on
- [x] **T5** Startup warning for non-loopback bind without TLS
- [x] **T6** Tests with a generated self-signed cert: HTTPS, 401/200 over TLS, plain HTTP rejected, SIGHUP reload, bad files, bounded shutdown
- [x] **T7** README TLS section
- [x] **T8** Live check with `curl --cacert` and an `/mcp` initialize over HTTPS
### WS-H NixOS · [card](plan/workstreams/WS-H-nixos.md)
- [ ] **H1** `nix/package.nix`: `buildRustPackage` from `Cargo.lock` with pkg-config, cmake, systemd. Binary `slinuxm`.
+8
View File
@@ -12,6 +12,14 @@ port = 9109 # listen port; SKYG_SERVER__PORT
allowed_hosts = [] # extra Host header values accepted on /mcp, added to loopback names and bind; SKYG_SERVER__ALLOWED_HOSTS
allowed_origins = [] # extra Origin values accepted on /mcp, must include scheme, e.g. "https://app.example.com"; SKYG_SERVER__ALLOWED_ORIGINS
# Native TLS for REST and /mcp. Optional and off by default: when enabled = false the
# server speaks plain HTTP and the file paths are ignored and never read.
# Certificate and key are re-read on SIGHUP (a bad new pair is rejected and the old one kept).
[server.tls]
enabled = false # serve HTTPS; requires cert_file and key_file; SKYG_SERVER__TLS__ENABLED
cert_file = "" # PEM certificate chain, leaf first; SKYG_SERVER__TLS__CERT_FILE
key_file = "" # PEM private key (PKCS#8, PKCS#1 or SEC1); SKYG_SERVER__TLS__KEY_FILE
[auth]
enabled = true # false is rejected at startup unless server.bind is loopback; SKYG_AUTH__ENABLED
db_path = "/var/lib/skyg-linux-tracker/db.sqlite" # SQLite database holding hashed API tokens; SKYG_AUTH__DB_PATH
+1 -1
View File
@@ -58,4 +58,4 @@ Move a card by cutting its row into another column. Keep the card's own **Status
| M2 Surfaces | Verification #3, #4 and #5 pass | 🟨 #3 and #4 verified live on 2026-10-15; #5 (OTel smoke test) not run, no otelcol-contrib binary |
| M3 Release | Verification #1–#6 pass. Unit file and README shipped. | ⬜ |
| M4 NixOS | `nix flake check` passes including the VM test. `:1984` serves and a token is minted with the system-installed CLI. | ⬜ |
| M5 TLS | HTTPS serves REST and `/mcp`, SIGHUP reloads certs, no openssl. | ⬜ |
| M5 TLS | HTTPS serves REST and `/mcp`, SIGHUP reloads certs, no openssl. | 🟨 verified on ws-i/tls, not merged |
+3 -1
View File
@@ -2,7 +2,7 @@
| Field | Value |
|---|---|
| Status | Backlog |
| Status | Done on `ws-i/tls` (not merged) |
| Owner | — |
| Phase | 4 (single track, after P3, before WS-H) |
| Depends on | P3 |
@@ -40,3 +40,5 @@ Tick these in [`TODO.md`](../../TODO.md).
## Log
- 2026-10-16: Card created from design discussion.
- 2026-10-16: Implemented on `ws-i/tls`, T1-T8 done. **TLS is optional, default off** (user requirement): with `server.tls.enabled = false` the plain path is unchanged, the cert/key paths are ignored and never read, no rustls code runs, no provider is built, MCP origins stay `http://`; env (`SKYG_SERVER__TLS__*`) and `--print-config` cover the block. Design: `src/tls.rs` uses `tokio-rustls` + `hyper-util` (auto builder, HTTP/1.1 ALPN) with an accept loop and a `ReloadableCert` (`ResolvesServerCert` behind an RwLock) so SIGHUP swaps the cert for new handshakes without touching live connections; a bad pair logs an error and keeps the old one. The `ring` provider is passed explicitly to `ServerConfig::builder_with_provider`; no process-wide default is installed (aws-lc-rs was already in the tree via reqwest/OTLP; this adds no new provider or openssl). Cargo.toml: rustls, tokio-rustls, hyper-util (separate commit), rcgen dev-dependency. Shutdown: accept stops, each connection gets `graceful_shutdown`, bounded by the existing 10s drain (test: SIGTERM with an idle keep-alive and an open SSE stream over TLS exits 0). T5 warning is logged only when TLS is off and bind is non-loopback. Tests: 3 new unit tests (config validation, env, origins) and 8 in `tests/tls.rs`. Live check (debug binary, openssl-generated certs, curl): healthz, 401/200 REST, MCP initialize over HTTPS, plain HTTP rejected, SIGHUP changed the served fingerprint, a junk cert + SIGHUP kept the rotated one, SIGTERM exit 0.
- 2026-10-16: Review fixes. TLS listener bounded by a semaphore (`tls::Limits::max_conns`, default 1024; at the cap accepting pauses) and a 30s hyper `header_read_timeout` (covers post-handshake silence and idle keep-alive), accept-error warnings rate-limited to one per 10s, HTTP/1.1 only. Note: the auto builder's `serve_connection_with_upgrades` sniffs the protocol before hyper starts, which defeats the header timeout, so the TLS path uses `serve_connection` (no upgrades; none are used). A SIGHUP handler is now installed at the top of `serve` on both paths, so SIGHUP never kills the process; with TLS off it only logs. Cert and key are read separately on reload: rotation tooling must send SIGHUP after BOTH files are in place (a mismatch keeps the old pair; fix the files and send HUP again). Intermediates must be in the `cert_file` chain. Pre-existing and out of scope: the plain-HTTP path has no connection cap or header timeout either (axum::serve defaults), the same exhaustion exposure it had before WS-I. New tests: silent-connection header timeout, connection cap, SIGHUP with TLS off, key/cert mismatch on SIGHUP.
+55
View File
@@ -42,6 +42,20 @@ pub struct ServerConfig {
/// Extra `Origin` values accepted on `/mcp`, added to the defaults
/// (loopback origins plus `bind`). Entries include a scheme.
pub allowed_origins: Vec<String>,
/// Optional native TLS (off by default).
pub tls: TlsConfig,
}
/// Native TLS for REST and `/mcp`. Optional: when `enabled` is false the
/// paths are ignored and never read.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct TlsConfig {
pub enabled: bool,
/// PEM certificate chain (leaf first).
pub cert_file: PathBuf,
/// PEM private key (PKCS#8, PKCS#1 or SEC1).
pub key_file: PathBuf,
}
impl Default for ServerConfig {
@@ -51,6 +65,7 @@ impl Default for ServerConfig {
port: 9109,
allowed_hosts: Vec::new(),
allowed_origins: Vec::new(),
tls: TlsConfig::default(),
}
}
}
@@ -254,6 +269,15 @@ impl Config {
"server.port must be greater than zero".into(),
));
}
let tls = &self.server.tls;
if tls.enabled
&& (tls.cert_file.as_os_str().is_empty() || tls.key_file.as_os_str().is_empty())
{
return Err(ConfigError::Invalid(
"server.tls.enabled = true requires both server.tls.cert_file and server.tls.key_file"
.into(),
));
}
if !self.auth.enabled && !self.server.bind.is_loopback() {
return Err(ConfigError::Invalid(format!(
"auth.enabled = false is only allowed on a loopback bind address (server.bind = {})",
@@ -402,6 +426,37 @@ mod tests {
});
}
#[test]
fn tls_enabled_requires_both_paths() {
let mut c = Config::default();
assert!(!c.server.tls.enabled);
c.validate().unwrap();
c.server.tls.enabled = true;
assert!(c.validate().is_err());
c.server.tls.cert_file = "/c.pem".into();
assert!(c.validate().is_err());
c.server.tls.key_file = "/k.pem".into();
c.validate().unwrap();
}
#[test]
fn tls_disabled_ignores_paths_and_env_overrides() {
let mut c = Config::default();
c.server.tls.cert_file = "/nonexistent/c.pem".into();
c.server.tls.key_file = "/nonexistent/k.pem".into();
c.validate().unwrap();
Jail::expect_with(|jail| {
jail.set_env("SKYG_SERVER__TLS__ENABLED", "true");
jail.set_env("SKYG_SERVER__TLS__CERT_FILE", "/c.pem");
jail.set_env("SKYG_SERVER__TLS__KEY_FILE", "/k.pem");
let c = load(None, &CliOverrides::default()).unwrap();
assert!(c.server.tls.enabled);
assert_eq!(c.server.tls.cert_file, Path::new("/c.pem"));
assert!(c.to_toml_redacted().unwrap().contains("[server.tls]"));
Ok(())
});
}
#[test]
fn example_file_matches_defaults() {
let c: Config = toml::from_str(include_str!("../config.example.toml")).unwrap();
+1
View File
@@ -13,3 +13,4 @@ pub mod server;
pub mod sources;
pub mod state;
pub mod telemetry;
pub mod tls;
+47 -2
View File
@@ -12,7 +12,7 @@ use tokio_util::sync::CancellationToken;
use skyg_linux_tracker::auth::tokens::{self, TokenCmd};
use skyg_linux_tracker::config::{CliOverrides, Config};
use skyg_linux_tracker::state::AppState;
use skyg_linux_tracker::{db, server, telemetry};
use skyg_linux_tracker::{db, server, telemetry, tls};
#[derive(Debug, Parser)]
#[command(name = "slinuxm", version, about = "Read-only Linux system monitor")]
@@ -105,6 +105,10 @@ const DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
const EXPORTER_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
async fn serve(config: Config) -> Result<()> {
// Install SIGHUP handling before anything else: the default action would
// terminate the process, with or without TLS.
let hup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())
.context("installing SIGHUP handler")?;
let guard = telemetry::init(&config).context("initialising telemetry")?;
let db = if config.auth.enabled {
Some(db::open(&config).await?)
@@ -113,6 +117,20 @@ async fn serve(config: Config) -> Result<()> {
None
};
let addr = SocketAddr::new(config.server.bind, config.server.port);
// TLS is optional: nothing from the tls module runs unless enabled.
let tls = if config.server.tls.enabled {
let cert = tls::ReloadableCert::load(&config.server.tls)
.context("loading TLS certificate and key")?;
Some((tls::acceptor(cert.clone())?, cert))
} else {
if !config.server.bind.is_loopback() {
tracing::warn!(
bind = %config.server.bind,
"listening on a non-loopback address without TLS: bearer tokens travel in cleartext (set server.tls.enabled or terminate TLS in front)"
);
}
None
};
let listener = tokio::net::TcpListener::bind(addr)
.await
.with_context(|| format!("binding {addr}"))?;
@@ -122,6 +140,23 @@ async fn serve(config: Config) -> Result<()> {
let exporters = telemetry::spawn_exporters(state.clone());
let pool = state.db.clone();
let app = server::build_router(state);
match &tls {
Some((_, cert)) => tls::spawn_sighup_reload(cert.clone(), hup, shutdown.clone()),
None => {
// Plain path: SIGHUP is ignored (logged only), nothing to reload.
let stop = shutdown.clone();
let mut hup = hup;
tokio::spawn(async move {
loop {
tokio::select! {
_ = stop.cancelled() => break,
r = hup.recv() => if r.is_none() { break },
}
tracing::info!("SIGHUP received; nothing to reload (TLS is disabled)");
}
});
}
}
let signal_token = shutdown.clone();
tokio::spawn(async move {
@@ -133,7 +168,17 @@ async fn serve(config: Config) -> Result<()> {
// `serve_listener` returns once the shutdown token fires and connections
// drain. If the token has not fired it is a server error, so return
// immediately. Bound the drain so a stuck client cannot hang SIGTERM.
let serving = server::serve_listener(listener, app, shutdown.clone());
let serving: std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send>> = match tls
{
Some((acceptor, _)) => Box::pin(tls::serve_tls(
listener,
acceptor,
app,
shutdown.clone(),
tls::Limits::default(),
)),
None => Box::pin(server::serve_listener(listener, app, shutdown.clone())),
};
tokio::pin!(serving);
let result = tokio::select! {
r = &mut serving => r,
+15 -2
View File
@@ -412,7 +412,8 @@ fn allowed_hosts(state: &AppState) -> Vec<String> {
}
/// Allowed `Origin` values: loopback origins and `server.bind` on the
/// configured port, plus configured extras.
/// configured port, plus configured extras. The scheme is `https` when
/// `server.tls.enabled`, otherwise `http`.
fn allowed_origins(state: &AppState) -> Vec<String> {
let s = &state.config.server;
let mut hosts = vec![
@@ -423,9 +424,10 @@ fn allowed_origins(state: &AppState) -> Vec<String> {
if !s.bind.is_unspecified() {
hosts.push(authority(s.bind));
}
let scheme = if s.tls.enabled { "https" } else { "http" };
let mut v: Vec<String> = hosts
.into_iter()
.map(|h| format!("http://{h}:{}", s.port))
.map(|h| format!("{scheme}://{h}:{}", s.port))
.collect();
v.extend(s.allowed_origins.iter().cloned());
v
@@ -783,6 +785,17 @@ mod tests {
let _svc = service(st);
}
#[test]
fn tls_switches_default_origins_to_https() {
let mut cfg = Config::default();
cfg.server.tls.enabled = true;
let st = fake_state(cfg);
let o = allowed_origins(&st);
assert!(o.contains(&"https://localhost:9109".to_string()));
assert!(o.contains(&"https://127.0.0.1:9109".to_string()));
assert!(!o.iter().any(|x| x.starts_with("http://")));
}
#[test]
fn ipv6_non_loopback_bind_allowlists() {
let mut cfg = Config::default();
+239
View File
@@ -0,0 +1,239 @@
//! Native TLS (DEC-12): rustls with the `ring` provider passed explicitly to
//! the config builder (nothing is installed process-wide, so the OTLP
//! clients' provider choice is unaffected). Only used when
//! `server.tls.enabled`; the plain path never touches this module.
//!
//! The certificate lives behind a [`ResolvesServerCert`] that can be swapped
//! at runtime, so SIGHUP reload leaves established connections untouched and
//! new handshakes pick up the new certificate. Key material is never logged.
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use axum::Router;
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use hyper_util::server::conn::auto::Builder;
use hyper_util::service::TowerToHyperService;
use rustls::crypto::ring as provider;
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use rustls::server::{ClientHello, ResolvesServerCert};
use rustls::sign::CertifiedKey;
use rustls::ServerConfig;
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio::time::Instant;
use tokio_rustls::TlsAcceptor;
use tokio_util::sync::CancellationToken;
use crate::config::TlsConfig;
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
/// Hot-swappable server certificate.
#[derive(Debug)]
pub struct ReloadableCert {
current: RwLock<Arc<CertifiedKey>>,
cert_file: PathBuf,
key_file: PathBuf,
}
impl ReloadableCert {
/// Load the initial pair; fails with a clear error naming the file.
pub fn load(cfg: &TlsConfig) -> Result<Arc<Self>> {
let key = load_certified_key(&cfg.cert_file, &cfg.key_file)?;
Ok(Arc::new(Self {
current: RwLock::new(Arc::new(key)),
cert_file: cfg.cert_file.clone(),
key_file: cfg.key_file.clone(),
}))
}
/// Re-read the files. On error the previous certificate stays in use.
pub fn reload(&self) -> Result<()> {
let key = load_certified_key(&self.cert_file, &self.key_file)?;
*self.current.write().unwrap_or_else(|e| e.into_inner()) = Arc::new(key);
Ok(())
}
}
impl ResolvesServerCert for ReloadableCert {
fn resolve(&self, _hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
Some(
self.current
.read()
.unwrap_or_else(|e| e.into_inner())
.clone(),
)
}
}
fn load_certified_key(cert_file: &Path, key_file: &Path) -> Result<CertifiedKey> {
let certs = CertificateDer::pem_file_iter(cert_file)
.map_err(|e| anyhow!("reading server.tls.cert_file {}: {e}", cert_file.display()))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| anyhow!("parsing server.tls.cert_file {}: {e}", cert_file.display()))?;
if certs.is_empty() {
return Err(anyhow!(
"server.tls.cert_file {} contains no PEM certificates",
cert_file.display()
));
}
let key = PrivateKeyDer::from_pem_file(key_file)
.map_err(|e| anyhow!("reading server.tls.key_file {}: {e}", key_file.display()))?;
let signing = provider::sign::any_supported_type(&key).map_err(|_| {
anyhow!(
"server.tls.key_file {}: unsupported or invalid private key",
key_file.display()
)
})?;
let ck = CertifiedKey::new(certs, signing);
ck.keys_match()
.map_err(|_| anyhow!("server.tls.key_file does not match server.tls.cert_file"))?;
Ok(ck)
}
/// Build the acceptor around a reloadable certificate.
pub fn acceptor(cert: Arc<ReloadableCert>) -> Result<TlsAcceptor> {
let mut cfg = ServerConfig::builder_with_provider(Arc::new(provider::default_provider()))
.with_safe_default_protocol_versions()
.context("TLS protocol versions")?
.with_no_client_auth()
.with_cert_resolver(cert);
// HTTP/1.1 only, matching the plain listener.
cfg.alpn_protocols = vec![b"http/1.1".to_vec()];
Ok(TlsAcceptor::from(Arc::new(cfg)))
}
/// Reload on every SIGHUP until `shutdown`. Failures are logged, never fatal.
/// `hup` is installed by the caller before binding so no SIGHUP can kill the
/// process in the meantime.
pub fn spawn_sighup_reload(
cert: Arc<ReloadableCert>,
mut hup: tokio::signal::unix::Signal,
shutdown: CancellationToken,
) {
tokio::spawn(async move {
loop {
tokio::select! {
_ = shutdown.cancelled() => break,
r = hup.recv() => if r.is_none() { break },
}
match cert.reload() {
Ok(()) => tracing::info!("TLS certificate reloaded"),
Err(e) => tracing::error!(
error = %format!("{e:#}"),
"TLS reload failed; keeping previous certificate"
),
}
}
});
}
/// Connection limits for the TLS listener.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
/// Maximum concurrent connections (handshaking or established). At the
/// cap, accepting pauses until a connection closes.
pub max_conns: usize,
/// Time allowed to receive a request header (also covers idle
/// keep-alive and post-handshake silence).
pub header_timeout: Duration,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_conns: 1024,
header_timeout: Duration::from_secs(30),
}
}
}
/// Serve `app` over TLS until `shutdown` is cancelled, then drain: stop
/// accepting, ask each connection to finish gracefully, and wait. The caller
/// bounds the drain by dropping this future (which aborts the connections).
pub async fn serve_tls(
listener: tokio::net::TcpListener,
acceptor: TlsAcceptor,
app: Router,
shutdown: CancellationToken,
limits: Limits,
) -> Result<()> {
let addr = listener.local_addr().context("local_addr")?;
tracing::info!(%addr, tls = true, "listening");
let mut conns: JoinSet<()> = JoinSet::new();
let slots = Arc::new(Semaphore::new(limits.max_conns));
let mut last_accept_warn: Option<Instant> = None;
loop {
// Wait for a free slot first, so at the cap we stop accepting.
let permit = tokio::select! {
_ = shutdown.cancelled() => break,
Some(_) = conns.join_next(), if !conns.is_empty() => continue,
p = slots.clone().acquire_owned() => match p {
Ok(p) => p,
Err(_) => break,
},
};
tokio::select! {
_ = shutdown.cancelled() => break,
Some(_) = conns.join_next(), if !conns.is_empty() => {}
r = listener.accept() => {
let (tcp, peer) = match r {
Ok(x) => x,
Err(e) => {
if last_accept_warn.is_none_or(|t| t.elapsed() > Duration::from_secs(10)) {
tracing::warn!(error = %e, "accept failed (further failures suppressed for 10s)");
last_accept_warn = Some(Instant::now());
}
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
}
};
let acceptor = acceptor.clone();
let svc = TowerToHyperService::new(app.clone());
let stop = shutdown.clone();
conns.spawn(async move {
let _permit = permit;
let tls = match tokio::time::timeout(HANDSHAKE_TIMEOUT, acceptor.accept(tcp)).await {
Ok(Ok(t)) => t,
Ok(Err(e)) => {
tracing::debug!(%peer, error = %e, "TLS handshake failed");
return;
}
Err(_) => {
tracing::debug!(%peer, "TLS handshake timed out");
return;
}
};
let mut builder = Builder::new(TokioExecutor::new()).http1_only();
builder
.http1()
.timer(TokioTimer::new())
.header_read_timeout(limits.header_timeout);
// Plain serve_connection: the auto builder's _with_upgrades
// variant sniffs the protocol first, which would defeat the
// header timeout for silent clients.
let conn = builder.serve_connection(TokioIo::new(tls), svc);
tokio::pin!(conn);
tokio::select! {
r = conn.as_mut() => {
if let Err(e) = r { tracing::debug!(%peer, error = %e, "connection error"); }
}
_ = stop.cancelled() => {
conn.as_mut().graceful_shutdown();
if let Err(e) = conn.as_mut().await {
tracing::debug!(%peer, error = %e, "connection error");
}
}
}
});
}
}
}
drop(listener);
while conns.join_next().await.is_some() {}
Ok(())
}
+665
View File
@@ -0,0 +1,665 @@
//! Native TLS tests (WS-I T6): the real `slinuxm` binary serving HTTPS with
//! generated self-signed certificates.
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, ServerName};
use sha2::{Digest, Sha256};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio_rustls::TlsConnector;
const BIN: &str = env!("CARGO_BIN_EXE_slinuxm");
struct Pair {
cert_pem: String,
key_pem: String,
}
fn gen_pair() -> Pair {
let ck = rcgen::generate_simple_self_signed(vec!["localhost".to_owned(), "127.0.0.1".into()])
.unwrap();
Pair {
cert_pem: ck.cert.pem(),
key_pem: ck.signing_key.serialize_pem(),
}
}
fn write_pair(dir: &Path, p: &Pair) -> (PathBuf, PathBuf) {
let (c, k) = (dir.join("cert.pem"), dir.join("key.pem"));
std::fs::write(&c, &p.cert_pem).unwrap();
std::fs::write(&k, &p.key_pem).unwrap();
(c, k)
}
fn free_port() -> u16 {
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
l.local_addr().unwrap().port()
}
struct Server {
child: Child,
port: u16,
token: String,
dir: tempdir::Dir,
}
mod tempdir {
use std::path::PathBuf;
pub struct Dir(pub PathBuf);
impl Dir {
pub fn new(tag: &str) -> Self {
static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let p = std::env::temp_dir().join(format!(
"slinuxm-tls-{tag}-{}-{}",
std::process::id(),
N.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
));
std::fs::create_dir_all(&p).unwrap();
Self(p)
}
}
impl Drop for Dir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
}
impl Drop for Server {
fn drop(&mut self) {
let _ = self.child.kill();
let _ = self.child.wait();
}
}
fn config_toml(dir: &Path, port: u16, cert: &Path, key: &Path) -> PathBuf {
let f = dir.join("config.toml");
std::fs::write(
&f,
format!(
"[server]\nport = {port}\n[server.tls]\nenabled = true\ncert_file = {:?}\nkey_file = {:?}\n[auth]\ndb_path = {:?}\n",
cert.display().to_string(),
key.display().to_string(),
dir.join("db.sqlite").display().to_string(),
),
)
.unwrap();
f
}
fn mint_token(cfg: &Path) -> String {
let out = Command::new(BIN)
.args(["token", "create", "t", "--config"])
.arg(cfg)
.env_clear()
.output()
.unwrap();
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout).unwrap().trim().to_owned()
}
fn start(pair: &Pair) -> Server {
let dir = tempdir::Dir::new("srv");
let (c, k) = write_pair(&dir.0, pair);
let port = free_port();
let cfg = config_toml(&dir.0, port, &c, &k);
let token = mint_token(&cfg);
let child = Command::new(BIN)
.args(["serve", "--config"])
.arg(&cfg)
.env_clear()
.stderr(Stdio::null())
.spawn()
.unwrap();
let s = Server {
child,
port,
token,
dir,
};
for _ in 0..100 {
if std::net::TcpStream::connect(("127.0.0.1", port)).is_ok() {
return s;
}
std::thread::sleep(Duration::from_millis(100));
}
panic!("server did not start");
}
fn connector(trusted: &Pair) -> TlsConnector {
let mut roots = rustls::RootCertStore::empty();
for c in CertificateDer::pem_slice_iter(trusted.cert_pem.as_bytes()) {
roots.add(c.unwrap()).unwrap();
}
let cfg = rustls::ClientConfig::builder_with_provider(Arc::new(
rustls::crypto::ring::default_provider(),
))
.with_safe_default_protocol_versions()
.unwrap()
.with_root_certificates(roots)
.with_no_client_auth();
TlsConnector::from(Arc::new(cfg))
}
struct Resp {
status: u16,
head: String,
body: String,
peer_fp: String,
}
/// One HTTPS request with `Connection: close`.
async fn https(
port: u16,
trusted: &Pair,
method: &str,
path: &str,
headers: &[(&str, &str)],
body: Option<&str>,
) -> Resp {
let tcp = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
let mut tls = connector(trusted)
.connect(ServerName::try_from("localhost").unwrap(), tcp)
.await
.expect("TLS handshake");
let peer_fp = {
let (_, conn) = tls.get_ref();
let der = &conn.peer_certificates().unwrap()[0];
hex(&Sha256::digest(der.as_ref()))
};
let mut req =
format!("{method} {path} HTTP/1.1\r\nHost: localhost:{port}\r\nConnection: close\r\n");
for (k, v) in headers {
req.push_str(&format!("{k}: {v}\r\n"));
}
if let Some(b) = body {
req.push_str(&format!("Content-Length: {}\r\n", b.len()));
}
req.push_str("\r\n");
if let Some(b) = body {
req.push_str(b);
}
tls.write_all(req.as_bytes()).await.unwrap();
let mut buf = Vec::new();
let mut chunk = [0u8; 4096];
let deadline = Instant::now() + Duration::from_secs(5);
while Instant::now() < deadline {
match tokio::time::timeout(Duration::from_secs(2), tls.read(&mut chunk)).await {
Ok(Ok(0)) | Ok(Err(_)) | Err(_) => break,
Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]),
}
}
let text = String::from_utf8_lossy(&buf).into_owned();
let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, ""));
let status = head
.split_whitespace()
.nth(1)
.and_then(|s| s.parse().ok())
.unwrap_or(0);
Resp {
status,
head: head.to_lowercase(),
body: body.to_owned(),
peer_fp,
}
}
fn hex(b: &[u8]) -> String {
b.iter().map(|x| format!("{x:02x}")).collect()
}
fn signal(child: &Child, sig: &str) {
assert!(Command::new("kill")
.args([sig, &child.id().to_string()])
.status()
.unwrap()
.success());
}
#[tokio::test]
async fn healthz_over_https() {
let pair = gen_pair();
let s = start(&pair);
let r = https(s.port, &pair, "GET", "/healthz", &[], None).await;
assert_eq!(r.status, 200, "{}", r.head);
}
#[tokio::test]
async fn auth_over_tls() {
let pair = gen_pair();
let s = start(&pair);
let r = https(s.port, &pair, "GET", "/api/v1/nope", &[], None).await;
assert_eq!(r.status, 401);
let auth = format!("Bearer {}", s.token);
let r = https(
s.port,
&pair,
"GET",
"/api/v1/nope",
&[("Authorization", &auth)],
None,
)
.await;
assert_eq!(r.status, 404, "token accepted, route unknown");
let r = https(
s.port,
&pair,
"GET",
"/api/v1/systemd/units",
&[("Authorization", "Bearer wrong")],
None,
)
.await;
assert_eq!(r.status, 401);
}
#[tokio::test]
async fn plain_http_to_tls_port_fails() {
let pair = gen_pair();
let s = start(&pair);
let mut c = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.await
.unwrap();
let mut buf = Vec::new();
let _ = tokio::time::timeout(Duration::from_secs(3), c.read_to_end(&mut buf)).await;
assert!(
!String::from_utf8_lossy(&buf).contains("200 OK"),
"plain HTTP must not be served on the TLS port"
);
}
#[tokio::test]
async fn mcp_initialize_over_https() {
let pair = gen_pair();
let s = start(&pair);
let auth = format!("Bearer {}", s.token);
let origin = format!("https://localhost:{}", s.port);
let r = https(
s.port,
&pair,
"POST",
"/mcp",
&[
("Content-Type", "application/json"),
("Accept", "application/json, text/event-stream"),
("Authorization", &auth),
("Origin", &origin),
],
Some(r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"it","version":"0"}}}"#),
)
.await;
assert_eq!(r.status, 200, "{}\n{}", r.head, r.body);
assert!(r.body.contains("serverInfo"), "{}", r.body);
// The http:// origin is no longer a default when TLS is on.
let bad = format!("http://localhost:{}", s.port);
let r = https(
s.port,
&pair,
"POST",
"/mcp",
&[
("Content-Type", "application/json"),
("Accept", "application/json, text/event-stream"),
("Authorization", &auth),
("Origin", &bad),
],
Some("{}"),
)
.await;
assert_eq!(r.status, 403);
}
#[tokio::test]
async fn sighup_serves_new_cert_and_bad_cert_keeps_old() {
let a = gen_pair();
let b = gen_pair();
let s = start(&a);
let fp_a = https(s.port, &a, "GET", "/healthz", &[], None)
.await
.peer_fp;
// Hold a keep-alive connection across the reload.
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
let mut held = connector(&a)
.connect(ServerName::try_from("localhost").unwrap(), tcp)
.await
.unwrap();
held.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
.await
.unwrap();
let mut buf = [0u8; 1024];
assert!(held.read(&mut buf).await.unwrap() > 0);
// Corrupt cert: reload fails, old cert still served.
std::fs::write(s.dir.0.join("cert.pem"), "not a certificate").unwrap();
signal(&s.child, "-HUP");
tokio::time::sleep(Duration::from_millis(500)).await;
let r = https(s.port, &a, "GET", "/healthz", &[], None).await;
assert_eq!(r.status, 200);
assert_eq!(r.peer_fp, fp_a, "bad reload must keep the old certificate");
// Valid new pair: SIGHUP swaps it.
write_pair(&s.dir.0, &b);
signal(&s.child, "-HUP");
let mut fp_b = String::new();
for _ in 0..30 {
tokio::time::sleep(Duration::from_millis(200)).await;
fp_b = https(s.port, &b, "GET", "/healthz", &[], None)
.await
.peer_fp;
if fp_b != fp_a {
break;
}
}
assert_ne!(fp_b, fp_a, "SIGHUP should serve the new certificate");
// The pre-reload connection survived.
held.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
.await
.unwrap();
let n = held.read(&mut buf).await.unwrap();
assert!(String::from_utf8_lossy(&buf[..n]).contains("200 OK"));
}
#[test]
fn bad_files_fail_startup() {
let dir = tempdir::Dir::new("bad");
let pair = gen_pair();
let (c, k) = write_pair(&dir.0, &pair);
let run = |cert: &Path, key: &Path, tag: &str| {
let cfg = config_toml(&dir.0, free_port(), cert, key);
let out = Command::new(BIN)
.args(["serve", "--config"])
.arg(&cfg)
.env_clear()
.output()
.unwrap();
assert!(!out.status.success(), "{tag}: should fail");
String::from_utf8_lossy(&out.stderr).into_owned()
};
let missing = dir.0.join("missing.pem");
assert!(run(&missing, &k, "missing cert").contains("cert_file"));
assert!(run(&c, &missing, "missing key").contains("key_file"));
let junk = dir.0.join("junk.pem");
std::fs::write(&junk, "garbage").unwrap();
assert!(run(&junk, &k, "junk cert").contains("cert_file"));
assert!(run(&c, &junk, "junk key").contains("key_file"));
// Mismatched pair.
let other = gen_pair();
let ok = dir.0.join("other-key.pem");
std::fs::write(&ok, &other.key_pem).unwrap();
assert!(run(&c, &ok, "mismatch").contains("does not match"));
// No key material in the error output.
let err = run(&c, &junk, "junk key");
assert!(!err.contains("PRIVATE KEY") && !err.contains("garbage"));
}
#[test]
fn enabled_without_paths_fails_validation_and_disabled_ignores_paths() {
let dir = tempdir::Dir::new("val");
let f = dir.0.join("c.toml");
std::fs::write(&f, "[server.tls]\nenabled = true\n").unwrap();
let out = Command::new(BIN)
.args(["serve", "--config"])
.arg(&f)
.env_clear()
.output()
.unwrap();
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("server.tls"));
// enabled = false with unreadable paths: startup proceeds on plain HTTP.
let port = free_port();
std::fs::write(
&f,
format!(
"[server]\nport = {port}\n[server.tls]\nenabled = false\ncert_file = \"/nonexistent/c.pem\"\nkey_file = \"/nonexistent/k.pem\"\n[auth]\ndb_path = {:?}\n",
dir.0.join("db.sqlite").display().to_string()
),
)
.unwrap();
let mut child = Command::new(BIN)
.args(["serve", "--config"])
.arg(&f)
.env_clear()
.stderr(Stdio::null())
.spawn()
.unwrap();
let mut up = false;
for _ in 0..100 {
if let Ok(mut c) = std::net::TcpStream::connect(("127.0.0.1", port)) {
use std::io::{Read, Write};
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.unwrap();
let mut s = String::new();
let _ = c.read_to_string(&mut s);
up = s.contains("200 OK");
break;
}
std::thread::sleep(Duration::from_millis(100));
}
let _ = child.kill();
let _ = child.wait();
assert!(up, "plain HTTP should serve with tls disabled");
}
/// SIGTERM with an open (idle keep-alive) TLS connection and an in-flight
/// long-poll still exits 0 within the existing bound.
#[tokio::test]
async fn sigterm_with_open_tls_connections_exits_promptly() {
let pair = gen_pair();
let mut s = start(&pair);
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
let mut idle = connector(&pair)
.connect(ServerName::try_from("localhost").unwrap(), tcp)
.await
.unwrap();
idle.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
.await
.unwrap();
let mut buf = [0u8; 1024];
let _ = idle.read(&mut buf).await.unwrap();
// Open SSE stream (journal follow) over TLS, left running.
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
let mut sse = connector(&pair)
.connect(ServerName::try_from("localhost").unwrap(), tcp)
.await
.unwrap();
let req = format!(
"GET /api/v1/journal/follow HTTP/1.1\r\nHost: localhost\r\nAccept: text/event-stream\r\nAuthorization: Bearer {}\r\n\r\n",
s.token
);
sse.write_all(req.as_bytes()).await.unwrap();
let _ = tokio::time::timeout(Duration::from_millis(500), sse.read(&mut buf)).await;
signal(&s.child, "-TERM");
let start = Instant::now();
loop {
if let Some(st) = s.child.try_wait().unwrap() {
assert!(st.success(), "exit status {st}");
break;
}
assert!(
start.elapsed() < Duration::from_secs(15),
"did not exit within the drain bound"
);
tokio::time::sleep(Duration::from_millis(50)).await;
}
}
/// In-process TLS listener with explicit limits.
async fn inproc(
pair: &Pair,
limits: skyg_linux_tracker::tls::Limits,
) -> (u16, tokio_util::sync::CancellationToken, tempdir::Dir) {
use skyg_linux_tracker::{config::Config, server, sources::fake::fake_state, tls};
let dir = tempdir::Dir::new("inproc");
let (c, k) = write_pair(&dir.0, pair);
let mut cfg = Config::default();
cfg.auth.enabled = false;
cfg.server.tls.enabled = true;
cfg.server.tls.cert_file = c;
cfg.server.tls.key_file = k;
let cert = tls::ReloadableCert::load(&cfg.server.tls).unwrap();
let acceptor = tls::acceptor(cert).unwrap();
let state = fake_state(cfg);
let shutdown = state.shutdown.clone();
let app = server::build_router(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(tls::serve_tls(
listener,
acceptor,
app,
shutdown.clone(),
limits,
));
(port, shutdown, dir)
}
async fn handshake(
port: u16,
pair: &Pair,
) -> std::io::Result<tokio_rustls::client::TlsStream<TcpStream>> {
let tcp = TcpStream::connect(("127.0.0.1", port)).await?;
connector(pair)
.connect(ServerName::try_from("localhost").unwrap(), tcp)
.await
}
#[tokio::test]
async fn silent_connection_closed_after_header_timeout() {
let pair = gen_pair();
let (port, shutdown, _d) = inproc(
&pair,
skyg_linux_tracker::tls::Limits {
max_conns: 16,
header_timeout: Duration::from_millis(500),
},
)
.await;
let mut c = handshake(port, &pair).await.unwrap();
let start = Instant::now();
let mut buf = [0u8; 256];
let r = tokio::time::timeout(Duration::from_secs(5), async {
loop {
match c.read(&mut buf).await {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
})
.await;
assert!(r.is_ok(), "silent post-handshake connection was not closed");
assert!(start.elapsed() < Duration::from_secs(4));
shutdown.cancel();
}
#[tokio::test]
async fn connection_cap_is_enforced() {
let pair = gen_pair();
let (port, shutdown, _d) = inproc(
&pair,
skyg_linux_tracker::tls::Limits {
max_conns: 2,
header_timeout: Duration::from_secs(30),
},
)
.await;
let c1 = handshake(port, &pair).await.unwrap();
let c2 = handshake(port, &pair).await.unwrap();
let third = tokio::time::timeout(Duration::from_millis(800), handshake(port, &pair)).await;
assert!(third.is_err(), "third connection was served beyond the cap");
drop(c1);
let c3 = tokio::time::timeout(Duration::from_secs(5), handshake(port, &pair)).await;
assert!(c3.is_ok(), "slot was not released after a close");
drop(c2);
shutdown.cancel();
}
#[test]
fn sighup_with_tls_disabled_keeps_serving() {
use std::io::{Read, Write};
let dir = tempdir::Dir::new("plainhup");
let port = free_port();
let f = dir.0.join("c.toml");
std::fs::write(
&f,
format!(
"[server]\nport = {port}\n[auth]\ndb_path = {:?}\n",
dir.0.join("db.sqlite").display().to_string()
),
)
.unwrap();
let mut child = Command::new(BIN)
.args(["serve", "--config"])
.arg(&f)
.env_clear()
.stderr(Stdio::null())
.spawn()
.unwrap();
let get = || -> Option<String> {
let mut c = std::net::TcpStream::connect(("127.0.0.1", port)).ok()?;
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.ok()?;
let mut s = String::new();
let _ = c.read_to_string(&mut s);
Some(s)
};
for _ in 0..100 {
if get().is_some_and(|s| s.contains("200 OK")) {
break;
}
std::thread::sleep(Duration::from_millis(100));
}
signal(&child, "-HUP");
std::thread::sleep(Duration::from_millis(500));
assert!(
child.try_wait().unwrap().is_none(),
"SIGHUP killed the process"
);
assert!(get().is_some_and(|s| s.contains("200 OK")));
let _ = child.kill();
let _ = child.wait();
}
#[tokio::test]
async fn mismatched_pair_on_sighup_keeps_old_cert() {
let a = gen_pair();
let b = gen_pair();
let s = start(&a);
let fp_a = https(s.port, &a, "GET", "/healthz", &[], None)
.await
.peer_fp;
// New cert B with old key A: mismatch, old pair kept.
std::fs::write(s.dir.0.join("cert.pem"), &b.cert_pem).unwrap();
signal(&s.child, "-HUP");
tokio::time::sleep(Duration::from_millis(500)).await;
let r = https(s.port, &a, "GET", "/healthz", &[], None).await;
assert_eq!(r.peer_fp, fp_a);
// Completing the rotation (key B) and sending HUP again works.
std::fs::write(s.dir.0.join("key.pem"), &b.key_pem).unwrap();
signal(&s.child, "-HUP");
let mut fp = String::new();
for _ in 0..30 {
tokio::time::sleep(Duration::from_millis(200)).await;
fp = https(s.port, &b, "GET", "/healthz", &[], None)
.await
.peer_fp;
if fp != fp_a {
break;
}
}
assert_ne!(fp, fp_a);
}