diff --git a/AGENTS/06-DEPLOYMENT.md b/AGENTS/06-DEPLOYMENT.md index 5bd0f27..c179880 100644 --- a/AGENTS/06-DEPLOYMENT.md +++ b/AGENTS/06-DEPLOYMENT.md @@ -148,10 +148,10 @@ rustfrp-agent ```bash # Debian / Ubuntu -sudo apt install ./RustFRP_0.5.4_amd64.deb +sudo apt install ./RustFRP_0.6.0_amd64.deb # 任意发行版(无 root) -chmod +x RustFRP_0.5.4_amd64.AppImage && ./RustFRP_0.5.4_amd64.AppImage +chmod +x RustFRP_0.6.0_amd64.AppImage && ./RustFRP_0.6.0_amd64.AppImage ``` Linux 运行时依赖:`libwebkit2gtk-4.1-0`、`libayatana-appindicator3-1`、`libgtk-3-0`。 @@ -255,18 +255,18 @@ volumes: ### Windows ``` -RustFRP_0.5.4_x64_en-US.msi +RustFRP_0.6.0_x64_en-US.msi ├── 安装位置:%ProgramFiles%\RustFRP\ ├── 开始菜单快捷方式 └── 卸载:控制面板 → 程序和功能(不删除 %USERPROFILE%\.rustfrp 数据目录) -RustFRP_0.5.4_x64-setup.exe # NSIS,可无管理员权限安装到用户目录 +RustFRP_0.6.0_x64-setup.exe # NSIS,可无管理员权限安装到用户目录 ``` ### macOS ``` -RustFRP_0.5.4_aarch64.dmg +RustFRP_0.6.0_aarch64.dmg ├── 拖入 /Applications/ ├── 首次打开需右键 → 打开(未签名构建) └── 已签名/公证构建可直接打开 @@ -275,8 +275,8 @@ RustFRP_0.5.4_aarch64.dmg ### Linux ``` -RustFRP_0.5.4_amd64.deb # 推荐:自动拉取 WebKitGTK 与 AppIndicator 依赖 -RustFRP_0.5.4_amd64.AppImage # 无 root:chmod +x 后运行;宿主需 libfuse2 +RustFRP_0.6.0_amd64.deb # 推荐:自动拉取 WebKitGTK 与 AppIndicator 依赖 +RustFRP_0.6.0_amd64.AppImage # 无 root:chmod +x 后运行;宿主需 libfuse2 deb 内容: ├── /usr/bin/rustfrp-desktop diff --git a/CHANGELOG.md b/CHANGELOG.md index 14cb4a2..727aaf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,173 @@ # Changelog +## [Unreleased] + +## [0.6.0] - 2026-11-21 + +### Added + +- The `tls2raw` client plugin is now a first-class certificate target, next to + `https2http` / `https2https`. It terminates TLS inside frpc and forwards raw + TCP to the backend, so it carries the same `crtPath` / `keyPath` pair — but + binding or applying a certificate to it used to be rejected as "not a + certificate plugin". The proxy editor renders its certificate fields and the + type picker offers it. +- Certificate management for `https` proxies. FRP does not terminate TLS on the + server — `frps` only reads the SNI and routes — so the domain certificate has + to exist on the frpc host at the `crtPath` / `keyPath` its `https2http` plugin + declares. The daemon now stores certificates, writes those files atomically + with `0600` permissions and `rename`, and rolls back to the previous file when + a write fails. Certificates are imported by pasting PEM, reading a local path, + webhook push, or an automatic pull from the Certd open API or a plain URL. + There is deliberately no download, export, or PFX route: a certificate and its + key never leave the host and never appear in an API response. +- Certificate bodies and keys are stored as XChaCha20-Poly1305 blobs keyed by a + per-installation master key in `cert_store.key` (mode `0600`) beside the + database, so a copied database file does not leak a private key. +- The certificate ingest listener is configured from the UI and enabled by + default on loopback. That endpoint never returns certificate material, every + request needs a token, and a token can only cover the domains it was issued + for, so requiring an operator to switch it on first would make Certd + integration look broken. Enabling "reachable from other hosts" rebinds it to + `0.0.0.0` and takes effect immediately, without restarting the daemon. A port + conflict is reported in the panel and never stops the daemon from starting. + It exposes exactly two routes (`POST /ingest/v1/certs` and + `GET /ingest/v1/healthz`), authenticating only through the + `X-Cert-Ingest-Token` header — never a URL path, which callers' logs would + record. A token only accepts certificates whose every SAN it is allowed to + serve; a wildcard covers one label, matching TLS wildcard semantics. + `--cert-ingest-listen` remains as a pin for container deployments that must + fix the address. +- In the Compose stack the ingest port is published to the host loopback only, + not to every interface. A Certd in the same stack reaches the daemon over the + Compose network (`http://daemon:7901/ingest/v1/certs`); an external one needs + `RUSTFRP_CERT_INGEST_BIND=0.0.0.0`. +- The proxy editor now offers a structured certificate picker for + `https2http` / `https2https` plugins, with the raw plugin JSON still available + for fields the form does not model. +- `GET /api/v1/status` reports `config_dir`, so the UI can suggest certificate + paths the daemon is able to create. +- The certificates page groups its push and pull channels behind two summary + cards — the ingest listener's live address and the pull sources' health — + and each card opens one dialog holding the endpoint settings, tokens and + pull sources. They no longer stack as full-width panels under the list. +- FRP version downloads have their own page under 「运维管理 → 版本下载」. It + reads the official release list and shows each build's archive size for this + platform, download count and publish date, with a channel picker (official / + GHFast / GH-Proxy / custom HTTPS) and card and list layouts. Rows are dense: + version and status badges, one line of metadata, the archive URL as a plain + link, then the actions, so the whole list fits without scrolling. +- Several versions download concurrently and each row shows its own progress + bar and byte counter. The daemon now keeps a task per version instead of one + global task, so a slow download no longer blocks the next one. Requesting a + version that is already downloading returns `FRP_INSTALL_BUSY`, and that + version cannot be deleted until its download finishes. + +### Removed + +- Deployment environments. The platform is single-user: one daemon, one SQLite + file, one operator. Environments were a second grouping axis on top of that, + and the shape never worked — the UI could switch an environment but had no way + to create one, so the selector was a one-item dropdown, and switching also + rewrote the profile's `tenant_id`, silently moving a server between tenants. + The tables are gone (schema V24) along with `/api/v1/environments`, + `profile_environment`, the sidebar selector, the per-row environment picker, + and the `environment_id` parameter on `/status` and `/metrics/history`. + `tenant_id` stays on profiles and proxies: it still separates API tokens, which + is the isolation that actually exists. + +### Changed + +- Certificates carry a status, and it is derived rather than stored: `active`, + `expiring_soon`, `expired`, `not_yet_valid` or `pending`. The five states are + distinct answers to "can I serve this?", and collapsing them into a day count + hid the case that actually hurts — a certificate that was configured but never + arrived. `status_reason` says why in one sentence, `usable` is exactly + `status` in `{active, expiring_soon}`, and `channel_error` carries the last + failure from any attached channel so a `pending` certificate can explain + itself instead of showing a blank row. +- Applying a certificate that cannot be served is refused instead of overwriting + a working file with one every client rejects. The check lives in + `apply_certificate`, so the API and the automatic renewal path get the same + answer, and it fires before anything is written. +- Configuring a pull channel creates the certificate it will fill, so the list + shows `*.example.com · pending` immediately instead of staying empty until the + first fetch succeeds. A channel whose first fetch fails used to leave the page + looking untouched, which reads as "my configuration was not saved". +- A certificate is now identified by its domain, not by its fingerprint, and its + material lives in its own table. Renewal was previously modelled as "a new + certificate appears": every reissue has a new fingerprint, so replacing the + certificate for `example.com` appended a row instead of updating one, and the + rows piled up until an operator deleted them by hand. There is now one + `certificate` row per `(tenant, primary_domain)` and one + `certificate_material` row per issued bundle, so a renewal replaces what is + served while keeping the identity — and therefore its bindings — intact. + "Newest" means the most recently stored bundle, not the one expiring last: a + reissue with a shorter validity must still win, or the daemon would keep + serving the older certificate. +- How a certificate gets updated is now a property *of that certificate*, not a + pair of global panels. Push tokens and pull sources became rows of + `certificate_source`; a channel can be attached to a certificate, moved to a + different one, detached, or marked preferred, so switching a certificate + between push and pull no longer means deleting and re-creating credentials. + A certificate may exist before it has any material, which is what lets an + operator configure the source first and let the first push or fetch fill it + in. The two summary cards on the Certificates page are gone; the endpoint + settings moved to System Status → Certificate channels, and each certificate + manages its own channels. +- Pull channels run on their own: the daemon checks once a day, imports what it + fetched, applies it to every bound proxy, and restarts the frpc process that + serves it. Before this, nothing called the pull path except a manual API call, + and an applied certificate needed a manual process restart because `frpc + reload` keeps the TLS certificate it loaded at startup. +- Both directions now validate the domain, and record which one failed. A push + may only submit a certificate whose every SAN the token allows, and a token + attached to a certificate may only replace that certificate's domain + (`CERT_008` otherwise). A pull may only install a certificate that covers the + channel's target domain; containment is required, not equality, because a CA + may reissue with extra SANs. Creating a pull channel rejects a target the + attached certificate does not cover, so the mistake surfaces before the first + fetch. + +### Fixed + +- Every Certd pull failed with `no message (code 20002)` on a host whose clock + was wrong, and the message hid why. Certd reports failures in `message`, a + field the response envelope never declared, so every rejection was reported as + "no message" with only a numeric code. The envelope now reads `message` (with + `msg` as an alias for older builds) and appends the likely cause for the codes + that have one. +- The resource chart's "last hour" could show far less than an hour. The buffer + was bounded by count only (3 600 samples at 1 Hz), so an eight-hour clock + correction left it holding nine hours of samples and the one-hour filter + retained just the two minutes after the jump. Samples are now evicted by age + as well as by count, so the window the UI offers is the window the data + covers. +- A pull now recovers from a skewed clock instead of failing forever. Certd's + rejection is a timestamp check, and its `Date` header says what "now" is, so a + request that comes back `20002` is retried once, signed with the server's + clock. That is the only way a daemon with no privileges can work on a machine + whose time is wrong — observed here as an eight-hour RTC offset, where the pull + now succeeds on the retry and logs both values. +- The certificate push URL shown in the UI was built from the listener's bind + address, so an operator reaching the console over a domain or a LAN address + was told `http://127.0.0.1:7901/ingest/v1/certs` — an address only the host + itself can dial. The push route is now also mounted on the control API, and the + daemon resolves the advertised URL from the request (`Host`, then + `X-Forwarded-Host`/`X-Forwarded-Proto`) so it always names the address the + browser actually used; `RUSTFRP_CERT_INGEST_PUBLIC_URL` pins it for a proxy + that rewrites paths or remaps ports. The response carries `url_source` so the + UI can say whether the value was configured, derived from the request, or + fell back to the bind address, and the token snippet in the same dialog now + uses the same value instead of recomputing it from `window.location`. +- A certificate pull source that stores no credentials (a plain URL, an + anonymous fetch) failed on every run with `DB_003: Invalid column type Null` + instead of fetching. The nullable `enc_secret` column was read as + `Option>`, where rusqlite rejects NULL on the inner column; the row is + now read as `Option>>`, so "no secret" is distinct from "no + such source" — and a source that disappears mid-run now reports + `RecordNotFound` rather than an empty credential. + ## [0.5.4] - 2026-11-15 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index cd05fd6..66d0aa8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,16 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -103,6 +113,45 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" +[[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 2.0.20", + "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", +] + +[[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-trait" version = "0.1.92" @@ -260,6 +309,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + [[package]] name = "chacha20" version = "0.10.1" @@ -268,7 +328,20 @@ checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81" dependencies = [ "cfg-if", "cpufeatures 0.3.0", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20 0.9.1", + "cipher", + "poly1305", + "zeroize", ] [[package]] @@ -285,6 +358,17 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", + "zeroize", +] + [[package]] name = "clap" version = "4.6.6" @@ -535,6 +619,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] @@ -547,6 +632,32 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + +[[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 = "diff" version = "0.1.13" @@ -828,7 +939,7 @@ dependencies = [ "js-sys", "libc", "r-efi", - "rand_core", + "rand_core 0.10.1", "wasm-bindgen", ] @@ -1137,6 +1248,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "insta" version = "1.48.0" @@ -1297,6 +1417,16 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.8.3" @@ -1337,6 +1467,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1382,6 +1518,16 @@ dependencies = [ "libc", ] +[[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 = "ntapi" version = "0.4.3" @@ -1400,6 +1546,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" @@ -1440,6 +1611,15 @@ dependencies = [ "memchr", ] +[[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" @@ -1452,6 +1632,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "option-ext" version = "0.2.0" @@ -1519,6 +1705,17 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash", +] + [[package]] name = "postcard" version = "1.1.3" @@ -1540,6 +1737,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + [[package]] name = "pretty_assertions" version = "1.4.1" @@ -1703,9 +1906,18 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" dependencies = [ - "chacha20", + "chacha20 0.10.1", "getrandom 0.4.3", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", ] [[package]] @@ -1720,7 +1932,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" dependencies = [ - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -1952,7 +2164,7 @@ dependencies = [ [[package]] name = "rustfrp-agent" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "chrono", @@ -1975,7 +2187,7 @@ dependencies = [ [[package]] name = "rustfrp-bin" -version = "0.5.4" +version = "0.6.0" dependencies = [ "dirs", "flate2", @@ -1993,10 +2205,12 @@ dependencies = [ [[package]] name = "rustfrp-client" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "async-trait", + "base64", + "chacha20poly1305", "chrono", "clap", "dirs", @@ -2004,6 +2218,7 @@ dependencies = [ "mimalloc", "nix 0.29.0", "pretty_assertions", + "ring", "rstest", "rusqlite", "rustfrp-bin", @@ -2021,11 +2236,12 @@ dependencies = [ "tracing", "tracing-subscriber", "uuid", + "x509-parser", ] [[package]] name = "rustfrp-common" -version = "0.5.4" +version = "0.6.0" dependencies = [ "chrono", "dirs", @@ -2045,7 +2261,7 @@ dependencies = [ [[package]] name = "rustfrp-control" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "axum", @@ -2068,13 +2284,15 @@ dependencies = [ [[package]] name = "rustfrp-daemon" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "axum", + "base64", "chrono", "clap", "libc", + "md-5", "mime_guess", "reqwest", "rusqlite", @@ -2099,7 +2317,7 @@ dependencies = [ [[package]] name = "rustfrp-sdk" -version = "0.5.4" +version = "0.6.0" dependencies = [ "rustfrp-common", "serde", @@ -2109,6 +2327,15 @@ dependencies = [ "tracing", ] +[[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.4" @@ -2566,6 +2793,36 @@ dependencies = [ "tikv-jemalloc-sys", ] +[[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" @@ -2924,6 +3181,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + [[package]] name = "untrusted" version = "0.9.0" @@ -3747,6 +4014,23 @@ 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", + "rusticata-macros", + "thiserror 2.0.20", + "time", +] + [[package]] name = "xattr" version = "1.6.1" diff --git a/Cargo.toml b/Cargo.toml index 1e73c32..b75b211 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,13 +16,19 @@ members = [ exclude = ["plugins/gui/src-tauri"] [workspace.package] -version = "0.5.4" +version = "0.6.0" edition = "2021" license = "AGPL-3.0-only" repository = "https://github.com/taten/rustfrp" rust-version = "1.95" [workspace.dependencies] +# Internal crates: declared once so a version bump touches one line. +rustfrp-common = { version = "0.6.0", path = "crates/common" } +rustfrp-client = { version = "0.6.0", path = "crates/client" } +rustfrp-bin = { version = "0.6.0", path = "crates/rustfrp-bin" } +rustfrp-daemon = { version = "0.6.0", path = "crates/rustfrp-daemon" } +rustfrp-sdk = { version = "0.6.0", path = "crates/rustfrp-sdk" } # === 异步运行时 === tokio = { version = "1", features = ["full"] } diff --git a/Dockerfile b/Dockerfile index 8936751..c04e967 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,8 @@ ENTRYPOINT ["rustfrp-control"] FROM runtime-base AS daemon COPY --from=builder /src/target/release/rustfrp-daemon /usr/local/bin/rustfrp-daemon -EXPOSE 7900 +# 7900 control API; 7901 certificate ingest endpoint (enable/expose in the UI) +EXPOSE 7900 7901 HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ CMD curl --fail --silent http://127.0.0.1:7900/api/v1/health >/dev/null || exit 1 diff --git a/README.md b/README.md index d9a4476..5e12b32 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,11 @@ RustFRP 不修改 FRP 协议,也不接管流量转发。已有的现代格式
-frpc 安装与多版本管理 +frpc 版本下载与多版本管理 -安装与启用分开,支持官方源、GHFast、GH-Proxy 和自定义 HTTPS 渠道。 +「运维管理 → 版本下载」自动读取官方发布列表(大小、下载数、发布时间),选择渠道后直接下载;多个版本可并发下载,卡片内显示实时进度条。安装与启用分开,支持官方源、GHFast、GH-Proxy 和自定义 HTTPS 渠道。 -![FRP 版本管理](docs/images/webui-versions.png) +![FRP 版本下载](docs/images/webui-versions.png)
@@ -78,10 +78,11 @@ RustFRP 不修改 FRP 协议,也不接管流量转发。已有的现代格式 | 配置管理 | Profile、Proxy、Binding、Visitor 与多环境管理;批量创建 TCP/UDP 映射;端口冲突检查 | | FRP 协议 | TCP、UDP、HTTP、HTTPS、STCP、XTCP、SUDP、TCPMUX;Token、OIDC、`user`、`serverUser` | | 进程守护 | 启动、停止、重启、热重载、优雅退出、崩溃恢复、重复故障归并 | -| 版本管理 | Linux、Windows、macOS 的 x86_64/arm64;后台安装;官方源、GHFast、GH-Proxy 或自定义 HTTPS 镜像;按服务器选择版本;官方校验和验证 | +| 版本管理 | Linux、Windows、macOS 的 x86_64/arm64;发布列表与并发后台下载(含进度条);官方源、GHFast、GH-Proxy 或自定义 HTTPS 镜像;按服务器选择版本;官方校验和验证 | | 迁移备份 | 事务化导入现代 `frpc.toml`;一致性 SQLite 备份导出 | | 服务端管理 | Agent 主动拉取配置,ETag 缓存,`frps verify` 校验,原子切换与失败回滚 | | 可观测性 | 资源趋势与运行进程侧栏;三种口径的流量统计;日志筛选;可选 Prometheus / Grafana | +| 证书管理 | https 代理的域名证书:粘贴 PEM、本机路径、Webhook 推送、Certd/URL 拉取;加密存储、原子落盘、按绑定的插件路径生效;不提供下载或导出 | | API 与扩展 | REST API、Token/scope 鉴权、按 token 的租户隔离、可选 WASM 插件 | ## 系统架构 @@ -96,7 +97,7 @@ RustFRP 的核心链路是:**SQLite 保存期望状态 → 生成并校验 TOM ### Docker 单容器 -使用 [v0.5.4](https://github.com/TATENcn/rustfrp/releases/tag/v0.5.4) 镜像(Linux amd64 / arm64): +使用 [v0.6.0](https://github.com/TATENcn/rustfrp/releases/tag/v0.6.0) 镜像(Linux amd64 / arm64): ```bash export RUSTFRP_API_TOKEN="$(openssl rand -hex 32)" @@ -107,18 +108,28 @@ docker run -d --name rustfrp --restart unless-stopped \ -p 127.0.0.1:7900:7900 \ -v rustfrp-data:/data \ -e RUSTFRP_API_TOKEN \ - ghcr.io/tatencn/rustfrp:0.5.4 + ghcr.io/tatencn/rustfrp:0.6.0 ``` 打开 ,输入上面的 Token。容器默认不下载 frpc,即使尚未安装 frpc,也能打开前端、编辑配置和使用管理 API。数据保存在 `rustfrp-data` 卷中。 -1. 在「系统状态 → FRP 版本」选择版本和下载渠道,安装后设为系统默认。 +1. 在「运维管理 → 版本下载」选择渠道并下载版本,之后可设为系统默认。 2. 在「服务器配置」添加 frps 连接;需要时为该服务器固定 frpc 版本。 3. 在「代理规则」创建本地服务映射,再到「代理分配」将代理分配给服务器。 4. 启动实例,查看日志和流量,验证映射地址。 上面的端口仅绑定本机;远程部署可通过 SSH 转发或 TLS 反向代理访问。容器内的 `127.0.0.1` 指向容器自身,代理宿主机服务时应填写容器可达的宿主机地址。 +`https` 代理的域名证书由 frpc 侧持有:frps 只读 SNI 做路由,不在服务端终结 TLS,所以证书要落在这台机器上、由 `https2http` / `https2https` / `tls2raw` 插件的 `crtPath` / `keyPath` 读取。 + +证书以**域名**为身份,一张证书可以有多份材料(每次续期一份),当前生效的是最后导入的那份;续期替换不会新增证书,也不会动它的绑定。 + +每张证书有一个派生状态:`active`(可用)、`expiring_soon`(30 天内到期)、`expired`、`not_yet_valid`、`pending`(已配置但还没有材料)。只有前两种会写入磁盘——过期或尚未收到材料的证书被 apply 时会直接拒绝,而不是把能用的文件覆盖掉。`pending` 的证书会附带它各通道最近一次失败的原因,所以「配了但没拉成功」在列表里一眼可见。 + +更新方式挂在证书自己身上:推送令牌或自动拉取,可在证书之间移动、更换或设为优先。自动拉取由服务守护进程每天检查一次,导入后自动写入已绑定代理的路径并重启对应 frpc 进程(`frpc reload` 不会重新读取 TLS 证书,必须重启)。拉取到的证书必须覆盖该通道填写的域名,否则拒绝——这是「自动替换」能安全开启的前提。 + +推送端点是平台级设置,在「系统状态 → 证书通道」配置;默认只监听 `127.0.0.1:7901`,但同样的两条路由也挂在控制 API 上,所以经 WebUI 可达的地址就能推送,无需额外开放端口。所有请求都要带 `X-Cert-Ingest-Token`,且证书的每个 SAN 都必须在该令牌允许的域名内。 + ### Docker Compose(含监控组件) 需要 frps、Control、Prometheus、Grafana 和 Alertmanager 的完整部署时,在仓库根目录执行: @@ -178,7 +189,7 @@ GET /api/v1/config/export ## FRP 二进制与版本管理 -在「系统状态 → FRP 版本」按需安装。下载在后台运行,页面显示进度和错误;安装本身不会切换默认版本或重启实例。版本按 `versions///` 隔离存放。 +在「运维管理 → 版本下载」按需下载。发布列表来自官方 GitHub API,显示归档大小、下载数与发布时间;多个版本可同时下载,每张卡片显示自己的进度条与失败原因。安装本身不会切换默认版本或重启实例。版本按 `versions///` 隔离存放。 - **下载渠道**:GitHub 官方、GHFast(`ghfast.top`)、GH-Proxy 或自定义 HTTPS 地址。第三方仅提供发布归档,SHA256 校验信息仍来自官方;获取失败或校验不符时停止安装。 - **服务器版本**:每个服务器配置对应一个 frpc 进程,该服务器下的代理共用版本。在服务器列表点击版本按钮,选择固定版本或跟随系统默认;保存后点击「重启并应用版本」生效。 diff --git a/compose.yml b/compose.yml index 429a3b8..a0ae846 100644 --- a/compose.yml +++ b/compose.yml @@ -6,11 +6,23 @@ services: restart: unless-stopped ports: - "${RUSTFRP_PORT:-7900}:7900" + # Certificate ingest endpoint for Certd. Published to the host loopback + # only: a Certd running in this Compose stack reaches it over the Compose + # network at http://daemon:7901/ingest/v1/certs and needs no host port at + # all. Set RUSTFRP_CERT_INGEST_BIND=0.0.0.0 (or a specific interface) when + # an external Certd has to connect. The container port stays 7901 so the + # mapping and the listener cannot disagree. + - "${RUSTFRP_CERT_INGEST_BIND:-127.0.0.1}:${RUSTFRP_CERT_INGEST_PORT:-7901}:7901" volumes: - rustfrp-data:/data environment: RUSTFRP_API_TOKEN_FILE: /run/secrets/rustfrp_api_token RUST_LOG: ${RUST_LOG:-rustfrp=info} + # Inside a container the ingest endpoint must bind all interfaces, or the + # published port reaches nothing. The container port is fixed at 7901 to + # match the mapping above; whether the listener runs at all still comes + # from the stored setting, which defaults to enabled. + RUSTFRP_CERT_INGEST_LISTEN: 0.0.0.0:7901 secrets: - rustfrp_api_token healthcheck: diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 92a67c8..2f8a14b 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -13,8 +13,8 @@ jemalloc = ["tikv-jemallocator"] mimalloc-dep = ["mimalloc"] [dependencies] -rustfrp-common = { version = "0.5.4", path = "../common" } -rustfrp-bin = { version = "0.5.0", path = "../rustfrp-bin" } +rustfrp-common.workspace = true +rustfrp-bin.workspace = true tokio = { workspace = true, features = ["process", "sync", "time"] } serde.workspace = true serde_json.workspace = true @@ -30,6 +30,10 @@ sha2 = "0.10" anyhow.workspace = true clap = { workspace = true } dirs = "5" +x509-parser = "0.18" +chacha20poly1305 = "0.10" +base64 = "0.22" +ring = "0.17" # Unix signal handling [target.'cfg(unix)'.dependencies] diff --git a/crates/client/src/cert/apply.rs b/crates/client/src/cert/apply.rs new file mode 100644 index 0000000..934569d --- /dev/null +++ b/crates/client/src/cert/apply.rs @@ -0,0 +1,358 @@ +//! Writing certificate material to the paths a `[[proxies]]` entry declares. +//! +//! The paths are read from the proxy's FRP-native `plugin.crtPath` / `keyPath` +//! rather than being duplicated in a second table: one source of truth, and the +//! file the daemon writes is by construction the file frpc reads. +//! +//! Every write is atomic (`tmp` + `rename`) and keeps the previous file as a +//! backup, so a failed apply can be rolled back without re-reading the database. + +use std::path::{Component, Path, PathBuf}; + +use serde_json::Value; + +use crate::cert::CertificateSecrets; +use crate::error::{ClientError, Result}; + +/// Where one binding's material belongs. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ApplyTarget { + pub crt_path: PathBuf, + pub key_path: PathBuf, +} + +/// Result of a successful write, kept so the caller can roll back. +#[derive(Debug)] +pub struct AppliedFiles { + pub target: ApplyTarget, + pub crt_backup: Option, + pub key_backup: Option, +} + +impl AppliedFiles { + /// Restore the files that existed before this apply; remove ones we created. + pub fn rollback(&self) { + for (path, backup) in [ + (&self.target.crt_path, &self.crt_backup), + (&self.target.key_path, &self.key_backup), + ] { + match backup { + Some(backup) => { + if let Err(error) = std::fs::rename(backup, path) { + tracing::warn!(path = %path.display(), %error, "failed to restore certificate backup"); + } + } + None => { + if let Err(error) = std::fs::remove_file(path) { + if error.kind() != std::io::ErrorKind::NotFound { + tracing::warn!(path = %path.display(), %error, "failed to remove written certificate file"); + } + } + } + } + } + } + + /// Drop the backups once the caller decided the new material is live. + pub fn commit(self) { + for backup in [self.crt_backup, self.key_backup].into_iter().flatten() { + if let Err(error) = std::fs::remove_file(&backup) { + if error.kind() != std::io::ErrorKind::NotFound { + tracing::warn!(backup = %backup.display(), %error, "failed to remove certificate backup"); + } + } + } + } +} + +/// Read `crtPath` / `keyPath` out of an FRP client plugin config. +/// +/// Accepts the plugin types that actually hold a certificate; anything else is +/// reported instead of silently writing nowhere. +pub fn target_from_plugin_config(plugin: &Value) -> Result { + let Some(object) = plugin.as_object() else { + return Err(ClientError::CertificateTarget( + "plugin configuration is not a JSON object".into(), + )); + }; + + match object.get("type").and_then(Value::as_str) { + Some("https2http" | "https2https" | "tls2raw") => {} + Some(other) => { + return Err(ClientError::CertificateTarget(format!( + "plugin type '{other}' does not hold a certificate (expected https2http, https2https or tls2raw)" + ))); + } + None => { + return Err(ClientError::CertificateTarget( + "plugin configuration has no 'type' field".into(), + )); + } + } + + let path = |field: &str| -> Result { + match object.get(field).and_then(Value::as_str) { + Some(value) if !value.trim().is_empty() => Ok(PathBuf::from(value.trim())), + _ => Err(ClientError::CertificateTarget(format!( + "plugin configuration has no '{field}'" + ))), + } + }; + + Ok(ApplyTarget { + crt_path: path("crtPath")?, + key_path: path("keyPath")?, + }) +} + +/// Write both files, keeping backups. On failure nothing is left half-written. +pub fn write_material(target: &ApplyTarget, secrets: &CertificateSecrets) -> Result { + if target.crt_path == target.key_path { + return Err(ClientError::CertificateTarget( + "crtPath and keyPath must be different files".into(), + )); + } + for path in [&target.crt_path, &target.key_path] { + reject_unsafe_path(path)?; + } + + let crt_backup = backup_existing(&target.crt_path)?; + let key_backup = match backup_existing(&target.key_path) { + Ok(backup) => backup, + Err(error) => { + // Restore what we just moved before reporting. + if let Some(backup) = &crt_backup { + let _ = std::fs::rename(backup, &target.crt_path); + } + return Err(error); + } + }; + + let files = AppliedFiles { + target: target.clone(), + crt_backup, + key_backup, + }; + + if let Err(error) = write_atomic(&target.crt_path, secrets.fullchain_pem().as_bytes()) { + files.rollback(); + return Err(error); + } + if let Err(error) = write_atomic(&target.key_path, secrets.key_pem().as_bytes()) { + files.rollback(); + return Err(error); + } + + Ok(files) +} + +/// Write `contents` to `path` through a sibling temporary file and a rename. +/// +/// The temporary file is created in the destination directory: a rename across +/// filesystems is not atomic, and the destination may be anywhere on the host. +pub fn write_atomic(path: &Path, contents: &[u8]) -> Result<()> { + use std::io::Write; + + if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { + std::fs::create_dir_all(parent).map_err(|error| { + ClientError::CertificateTarget(format!( + "failed to create {}: {error}", + parent.display() + )) + })?; + } + + let temporary = temporary_path(path); + let mut options = std::fs::OpenOptions::new(); + options.write(true).create(true).truncate(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + // The key must never be world-readable, not even between write and chmod. + options.mode(0o600); + } + + let write = || -> std::io::Result<()> { + let mut file = options.open(&temporary)?; + file.write_all(contents)?; + file.sync_all()?; + Ok(()) + }; + + if let Err(error) = write() { + let _ = std::fs::remove_file(&temporary); + return Err(ClientError::CertificateTarget(format!( + "failed to write {}: {error}", + temporary.display() + ))); + } + + if let Err(error) = std::fs::rename(&temporary, path) { + let _ = std::fs::remove_file(&temporary); + return Err(ClientError::CertificateTarget(format!( + "failed to install {}: {error}", + path.display() + ))); + } + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let permissions = std::fs::Permissions::from_mode(0o600); + if let Err(error) = std::fs::set_permissions(path, permissions) { + tracing::warn!(path = %path.display(), %error, "failed to set 0600 on certificate file"); + } + } + + Ok(()) +} + +/// Move an existing file aside and return the backup path. +fn backup_existing(path: &Path) -> Result> { + if !path.exists() { + return Ok(None); + } + let backup = path.with_extension(format!( + "{}.rustfrp-bak", + path.extension() + .and_then(|ext| ext.to_str()) + .unwrap_or("bak") + )); + std::fs::rename(path, &backup).map_err(|error| { + ClientError::CertificateTarget(format!("failed to back up {}: {error}", path.display())) + })?; + Ok(Some(backup)) +} + +fn temporary_path(path: &Path) -> PathBuf { + let file_name = path + .file_name() + .map(|name| name.to_string_lossy().to_string()) + .unwrap_or_else(|| "cert".to_string()); + path.with_file_name(format!(".{file_name}.rustfrp-tmp")) +} + +/// Refuse paths that would write outside the place an operator can see. +fn reject_unsafe_path(path: &Path) -> Result<()> { + if !path.is_absolute() { + return Err(ClientError::CertificateTarget(format!( + "certificate path {} must be absolute", + path.display() + ))); + } + for component in path.components() { + match component { + Component::ParentDir => { + return Err(ClientError::CertificateTarget(format!( + "certificate path {} must not contain '..'", + path.display() + ))); + } + Component::Normal(name) if name == ".." => { + return Err(ClientError::CertificateTarget(format!( + "certificate path {} must not contain '..'", + path.display() + ))); + } + _ => {} + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn secrets() -> CertificateSecrets { + CertificateSecrets { + cert_der: vec![0x30, 0x03, 0x02, 0x01, 0x01], + chain_der: Vec::new(), + key_label: "PRIVATE KEY".into(), + key_der: vec![0x30, 0x03, 0x02, 0x01, 0x02], + } + } + + #[test] + fn rejects_non_certificate_plugin() { + let plugin = serde_json::json!({"type": "http2https", "localAddr": "127.0.0.1:80"}); + assert!(target_from_plugin_config(&plugin).is_err()); + } + + #[test] + fn accepts_tls2raw_paths() { + let plugin = serde_json::json!({ + "type": "tls2raw", + "localAddr": "192.168.1.206:34618", + "crtPath": "/etc/frp/ssl/powercess/cert.pem", + "keyPath": "/etc/frp/ssl/powercess/cert.key", + }); + let target = target_from_plugin_config(&plugin).unwrap(); + assert_eq!( + target.crt_path, + PathBuf::from("/etc/frp/ssl/powercess/cert.pem") + ); + assert_eq!( + target.key_path, + PathBuf::from("/etc/frp/ssl/powercess/cert.key") + ); + } + + #[test] + fn requires_both_paths() { + let plugin = serde_json::json!({"type": "https2http", "crtPath": "/tmp/a.pem"}); + let error = target_from_plugin_config(&plugin).unwrap_err(); + assert!(error.to_string().contains("keyPath")); + } + + #[test] + fn writes_and_rolls_back() { + let dir = tempfile::tempdir().unwrap(); + let target = ApplyTarget { + crt_path: dir.path().join("fullchain.pem"), + key_path: dir.path().join("privkey.pem"), + }; + std::fs::write(&target.crt_path, "OLD CERT").unwrap(); + + let files = write_material(&target, &secrets()).unwrap(); + assert!(std::fs::read_to_string(&target.crt_path) + .unwrap() + .contains("BEGIN CERTIFICATE")); + + files.rollback(); + assert_eq!( + std::fs::read_to_string(&target.crt_path).unwrap(), + "OLD CERT" + ); + assert!(!target.key_path.exists()); + } + + #[test] + fn commit_keeps_new_material() { + let dir = tempfile::tempdir().unwrap(); + let target = ApplyTarget { + crt_path: dir.path().join("fullchain.pem"), + key_path: dir.path().join("privkey.pem"), + }; + write_material(&target, &secrets()).unwrap().commit(); + assert!(target.crt_path.exists()); + assert!(target.key_path.exists()); + assert!(!dir.path().join("fullchain.pem.rustfrp-bak").exists()); + } + + #[test] + fn refuses_relative_or_traversing_paths() { + let dir = tempfile::tempdir().unwrap(); + let relative = ApplyTarget { + crt_path: PathBuf::from("cert.pem"), + key_path: dir.path().join("key.pem"), + }; + assert!(write_material(&relative, &secrets()).is_err()); + + let traversing = ApplyTarget { + crt_path: dir.path().join("../escape/cert.pem"), + key_path: dir.path().join("key.pem"), + }; + assert!(write_material(&traversing, &secrets()).is_err()); + } +} diff --git a/crates/client/src/cert/crypto.rs b/crates/client/src/cert/crypto.rs new file mode 100644 index 0000000..5d16e10 --- /dev/null +++ b/crates/client/src/cert/crypto.rs @@ -0,0 +1,175 @@ +//! At-rest encryption for certificate material. +//! +//! Certificate bodies and private keys are stored encrypted so that a leaked +//! database file (a backup, a copied SQLite file) does not leak keys. The master +//! key lives beside the database in its own file with `0600` permissions. +//! +//! Scope, stated plainly: this defends the *database file*, not a compromised +//! host. The frpc process must be able to read the plaintext key from `keyPath`, +//! so filesystem permissions and the service account remain the real boundary. + +use std::path::{Path, PathBuf}; + +use chacha20poly1305::aead::{Aead, KeyInit, OsRng}; +use chacha20poly1305::{AeadCore, XChaCha20Poly1305, XNonce}; + +use crate::error::{ClientError, Result}; + +/// File name of the master key, resolved next to the database file. +pub const MASTER_KEY_FILE: &str = "cert_store.key"; + +const KEY_LEN: usize = 32; +const NONCE_LEN: usize = 24; + +/// Encrypts certificate blobs with a per-installation XChaCha20-Poly1305 key. +pub struct CertCipher { + cipher: XChaCha20Poly1305, + path: PathBuf, +} + +impl std::fmt::Debug for CertCipher { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CertCipher") + .field("path", &self.path) + .finish_non_exhaustive() + } +} + +impl CertCipher { + /// Load the master key from `dir`, creating it on first use. + pub fn load_or_create(dir: &Path) -> Result { + let path = dir.join(MASTER_KEY_FILE); + let bytes = match std::fs::read(&path) { + Ok(bytes) => bytes, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + let generated = Self::generate_key(); + write_private_file(&path, &generated)?; + tracing::info!(path = %path.display(), "Generated certificate store master key"); + generated.to_vec() + } + Err(error) => { + return Err(ClientError::CertificateSecrets(format!( + "failed to read {}: {error}", + path.display() + ))); + } + }; + + if bytes.len() != KEY_LEN { + return Err(ClientError::CertificateSecrets(format!( + "{} must contain exactly {KEY_LEN} bytes, found {}", + path.display(), + bytes.len() + ))); + } + + let mut key = [0_u8; KEY_LEN]; + key.copy_from_slice(&bytes); + Ok(Self { + cipher: XChaCha20Poly1305::new((&key).into()), + path, + }) + } + + /// Path of the master key file (for diagnostics; never expose contents). + pub fn key_path(&self) -> &Path { + &self.path + } + + /// Encrypt `plaintext` into `nonce || ciphertext || tag`. + pub fn encrypt(&self, plaintext: &[u8]) -> Result> { + let nonce = XChaCha20Poly1305::generate_nonce(&mut OsRng); + let ciphertext = self.cipher.encrypt(&nonce, plaintext).map_err(|_| { + ClientError::CertificateSecrets("failed to encrypt certificate material".into()) + })?; + let mut out = Vec::with_capacity(NONCE_LEN + ciphertext.len()); + out.extend_from_slice(nonce.as_slice()); + out.extend_from_slice(&ciphertext); + Ok(out) + } + + /// Decrypt a blob produced by [`Self::encrypt`]. + pub fn decrypt(&self, blob: &[u8]) -> Result> { + if blob.len() <= NONCE_LEN { + return Err(ClientError::CertificateSecrets( + "certificate blob is truncated".into(), + )); + } + let (nonce, body) = blob.split_at(NONCE_LEN); + self.cipher + .decrypt(XNonce::from_slice(nonce), body) + .map_err(|_| { + ClientError::CertificateSecrets( + "failed to decrypt certificate material (wrong master key?)".into(), + ) + }) + } + + fn generate_key() -> [u8; KEY_LEN] { + let mut key = [0_u8; KEY_LEN]; + use chacha20poly1305::aead::rand_core::RngCore; + OsRng.fill_bytes(&mut key); + key + } +} + +/// Write a file readable only by the current user. +pub fn write_private_file(path: &Path, contents: &[u8]) -> Result<()> { + use std::io::Write; + + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|error| { + ClientError::CertificateSecrets(format!( + "failed to create {}: {error}", + parent.display() + )) + })?; + } + + let mut options = std::fs::OpenOptions::new(); + options.write(true).create(true).truncate(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + + let mut file = options.open(path).map_err(|error| { + ClientError::CertificateSecrets(format!("failed to open {}: {error}", path.display())) + })?; + file.write_all(contents).map_err(|error| { + ClientError::CertificateSecrets(format!("failed to write {}: {error}", path.display())) + })?; + file.sync_all().ok(); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trip_and_wrong_key_detection() { + let first = tempfile::tempdir().unwrap(); + let second = tempfile::tempdir().unwrap(); + let cipher = CertCipher::load_or_create(first.path()).unwrap(); + let blob = cipher.encrypt(b"PRIVATE KEY MATERIAL").unwrap(); + assert_ne!(&blob[..], b"PRIVATE KEY MATERIAL"); + assert_eq!(cipher.decrypt(&blob).unwrap(), b"PRIVATE KEY MATERIAL"); + + // Reloading from the same directory reuses the key. + let reloaded = CertCipher::load_or_create(first.path()).unwrap(); + assert_eq!(reloaded.decrypt(&blob).unwrap(), b"PRIVATE KEY MATERIAL"); + + // A different installation must fail closed, not return garbage. + let other = CertCipher::load_or_create(second.path()).unwrap(); + assert!(other.decrypt(&blob).is_err()); + } + + #[test] + fn rejects_truncated_blob() { + let dir = tempfile::tempdir().unwrap(); + let cipher = CertCipher::load_or_create(dir.path()).unwrap(); + assert!(cipher.decrypt(&[0_u8; 4]).is_err()); + } +} diff --git a/crates/client/src/cert/domain.rs b/crates/client/src/cert/domain.rs new file mode 100644 index 0000000..da0add5 --- /dev/null +++ b/crates/client/src/cert/domain.rs @@ -0,0 +1,165 @@ +//! Which domains a certificate is allowed to serve, and which certificate a +//! channel is allowed to install. +//! +//! Two opposite directions live here because they are easy to confuse and +//! getting either backwards is a security bug: +//! +//! * **Push**: every SAN of the submitted certificate must be covered by the +//! token's allowlist. Otherwise a leak of one token could overwrite the +//! certificate for an unrelated domain. +//! * **Pull**: the channel's target domain must be covered by the returned +//! certificate. Otherwise a misconfigured or hostile endpoint could install a +//! certificate for a domain the operator never pointed it at. +//! +//! Both use one matching rule: exact, or a single-label wildcard. That mirrors +//! how TLS wildcards actually work, so `*.example.com` covers `a.example.com` +//! and not `a.b.example.com`. + +/// True when `candidate` is served by `pattern`. +/// +/// An exact match always counts. A pattern of the form `*.suffix` covers +/// exactly one extra label. +pub fn covers(pattern: &str, candidate: &str) -> bool { + let pattern = pattern.trim().to_ascii_lowercase(); + let candidate = candidate.trim().to_ascii_lowercase(); + if pattern.is_empty() || candidate.is_empty() { + return false; + } + if candidate == pattern { + return true; + } + if let Some(suffix) = pattern.strip_prefix("*.") { + if let Some((label, rest)) = candidate.split_once('.') { + return !label.is_empty() && rest == suffix; + } + } + false +} + +/// Domains the certificate claims to serve. +/// +/// Falls back to the primary domain when a bundle somehow carries no SANs, so a +/// single-SAN certificate is never treated as "serves nothing". +pub fn served_domains(sans: &[String], primary: &str) -> Vec { + if !sans.is_empty() { + return sans.to_vec(); + } + if primary.trim().is_empty() { + Vec::new() + } else { + vec![primary.trim().to_ascii_lowercase()] + } +} + +/// Push direction: every domain the certificate serves must be allowed. +/// +/// Returns the domains that were not allowed, so the caller can name them in the +/// error instead of saying only "forbidden". +pub fn disallowed(sans: &[String], primary: &str, allowed: &[String]) -> Vec { + served_domains(sans, primary) + .into_iter() + .filter(|domain| !allowed.iter().any(|pattern| covers(pattern, domain))) + .collect() +} + +/// Pull direction: the certificate must actually cover what the channel asked +/// for. +/// +/// A server may return a wider certificate (an extra SAN is normal when a CA +/// reissues), so this checks containment rather than equality. +pub fn covers_target(sans: &[String], primary: &str, target: &str) -> bool { + let served = served_domains(sans, primary); + // The target counts as covered when the certificate serves it directly, and + // also when the certificate serves a wildcard that includes it. + served.iter().any(|domain| covers(domain, target)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sans(values: &[&str]) -> Vec { + values.iter().map(|value| (*value).to_string()).collect() + } + + #[test] + fn exact_and_single_label_wildcards_match() { + assert!(covers("a.example.com", "a.example.com")); + assert!(covers("A.Example.com", "a.example.com")); + assert!(covers("*.example.com", "a.example.com")); + assert!(!covers("*.example.com", "a.b.example.com")); + assert!(!covers("*.example.com", "example.com")); + assert!(!covers("a.example.com", "b.example.com")); + assert!(!covers("", "a.example.com")); + } + + #[test] + fn push_requires_every_san_to_be_allowed() { + let allowed = sans(&["example.com", "*.example.com"]); + assert!(disallowed(&sans(&["a.example.com"]), "a.example.com", &allowed).is_empty()); + + // One uncovered SAN is enough to reject the whole bundle. + let rejected = disallowed( + &sans(&["a.example.com", "other.test"]), + "a.example.com", + &allowed, + ); + assert_eq!(rejected, vec!["other.test".to_string()]); + } + + #[test] + fn push_does_not_require_the_allowlist_to_be_exhausted() { + // Allowing more domains than a certificate covers is normal: the token + // is used for several certificates over its lifetime. + let allowed = sans(&["*.example.com", "*.other.test"]); + assert!(disallowed(&sans(&["a.example.com"]), "a.example.com", &allowed).is_empty()); + } + + #[test] + fn pull_accepts_a_certificate_that_covers_the_target() { + assert!(covers_target( + &sans(&["a.example.com"]), + "a.example.com", + "a.example.com" + )); + assert!(covers_target( + &sans(&["*.example.com"]), + "*.example.com", + "a.example.com" + )); + // A wider certificate is fine: CAs reissue with extra SANs. + assert!(covers_target( + &sans(&["a.example.com", "b.example.com"]), + "a.example.com", + "a.example.com" + )); + } + + #[test] + fn pull_rejects_a_certificate_for_the_wrong_domain() { + assert!(!covers_target( + &sans(&["evil.test"]), + "evil.test", + "a.example.com" + )); + // A wildcard for a parent domain does not cover a subdomain's own name. + assert!(!covers_target( + &sans(&["*.other.test"]), + "*.other.test", + "a.example.com" + )); + // And a deeper wildcard must not be treated as a parent match. + assert!(!covers_target( + &sans(&["*.b.example.com"]), + "*.b.example.com", + "a.example.com" + )); + } + + #[test] + fn pull_falls_back_to_the_primary_domain_without_sans() { + assert!(covers_target(&[], "a.example.com", "a.example.com")); + assert!(!covers_target(&[], "a.example.com", "b.example.com")); + assert!(!covers_target(&[], "", "a.example.com")); + } +} diff --git a/crates/client/src/cert/mod.rs b/crates/client/src/cert/mod.rs new file mode 100644 index 0000000..22e0c00 --- /dev/null +++ b/crates/client/src/cert/mod.rs @@ -0,0 +1,314 @@ +//! Certificate management for the frpc side. +//! +//! FRP's `https` / `tcp` vhost does not terminate TLS on the server: the +//! `https2http` / `https2https` proxies and the `tls2raw` plugin hold the domain +//! certificate as a **local file** (`crtPath` / `keyPath`). This module owns the +//! platform side of that: parse an imported bundle, keep the material encrypted +//! at rest, and write it to the paths a `[[proxies]]` entry already declares. +//! +//! Secrets live in [`CertificateSecrets`], which deliberately implements neither +//! `Serialize` nor `Clone`: only the apply engine can read them back, and no API +//! response can accidentally carry a private key. + +pub mod apply; +pub mod crypto; +pub mod domain; +pub mod parse; + +pub use crypto::CertCipher; +pub use parse::{parse_bundle, ParsedCertificate}; + +use serde::Serialize; + +/// Whether a certificate can be written to frpc and expected to work. +/// +/// This is derived, never stored: a stored flag would drift from the material it +/// describes the moment a clock ticks or a channel fails. The point of naming it +/// is that "no material yet", "expired" and "fine" are different answers to the +/// same question, and treating them as one (`days_remaining >= 0`) hides the +/// case that actually hurts: a configured certificate that never arrived. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum CertificateStatus { + /// Material present and inside its validity window. + Active, + /// Material present, usable, but expiring within [`EXPIRING_SOON_DAYS`]. + ExpiringSoon, + /// Material present but past `not_after`. Not written to disk. + Expired, + /// Material present but `not_before` is still in the future. + NotYetValid, + /// No material at all: the certificate is configured, nothing has arrived. + Pending, +} + +/// How close to expiry counts as "soon". Chosen to match the default renewal +/// window of every common issuer, so the warning appears while a fix still works. +pub const EXPIRING_SOON_DAYS: i64 = 30; + +impl CertificateStatus { + /// True only for states where writing the files is a good idea. + pub fn usable(&self) -> bool { + matches!(self, Self::Active | Self::ExpiringSoon) + } + + pub fn as_str(&self) -> &'static str { + match self { + Self::Active => "active", + Self::ExpiringSoon => "expiring_soon", + Self::Expired => "expired", + Self::NotYetValid => "not_yet_valid", + Self::Pending => "pending", + } + } + + pub fn parse(value: &str) -> Option { + match value { + "active" => Some(Self::Active), + "expiring_soon" => Some(Self::ExpiringSoon), + "expired" => Some(Self::Expired), + "not_yet_valid" => Some(Self::NotYetValid), + "pending" => Some(Self::Pending), + _ => None, + } + } +} + +/// Certificate metadata — the only certificate shape that may leave the daemon. +/// +/// Every field here is non-secret. The private key and the certificate body are +/// absent by construction, so "the API must not expose certificates" is enforced +/// by the type rather than by remembering to skip a field. +/// +/// A certificate exists before it has material: it is configurable (channels, +/// bindings) while its first bundle is still pending. That state is visible as +/// `status: Pending`, `material_id: None` and `days_remaining: None`, not as a +/// fake expiry. +#[derive(Debug, Clone, Serialize)] +pub struct Certificate { + pub id: i64, + pub tenant_id: String, + pub primary_domain: String, + pub sans: Vec, + pub issuer: String, + pub serial: String, + /// Fingerprint of the newest material, empty until the first import. + pub fingerprint_sha256: String, + pub key_algo: String, + pub not_before: String, + pub not_after: String, + /// The material row these fields came from, `None` before the first import. + pub material_id: Option, + /// Whole days until `not_after`; negative once expired. + pub days_remaining: Option, + /// Derived state. `usable` is exactly `status.usable()`. + pub status: CertificateStatus, + /// Why the certificate cannot be used, when it cannot. Empty otherwise. + pub status_reason: Option, + pub usable: bool, + /// The most recent failure recorded on any of its channels, so a `Pending` + /// certificate can explain why nothing arrived. + pub channel_error: Option, + pub source: String, + pub source_detail: Option, + pub created_at: String, + pub updated_at: String, + pub bindings: Vec, + /// Non-fatal findings recorded at import time (incomplete chain, ...). + pub warnings: Vec, +} + +/// One certificate ↔ proxy link. The file paths are *not* stored here: they live +/// in the proxy's FRP-native `plugin_config.crtPath` / `keyPath`, so there is a +/// single source of truth for where material is written. +#[derive(Debug, Clone, Serialize)] +pub struct CertificateBinding { + pub id: i64, + pub tenant_id: String, + pub cert_id: i64, + pub proxy_id: i64, + pub proxy_name: String, + /// Fingerprint written by the last successful apply, if any. + pub applied_fingerprint: Option, + pub applied_at: Option, + pub last_error: Option, + pub created_at: String, + pub updated_at: String, +} + +/// Decrypted certificate material. Never serializable, never `Clone`. +pub struct CertificateSecrets { + pub cert_der: Vec, + pub chain_der: Vec, + pub key_label: String, + pub key_der: Vec, +} + +impl std::fmt::Debug for CertificateSecrets { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CertificateSecrets") + .field("cert_der", &format_args!("<{} bytes>", self.cert_der.len())) + .field( + "chain_der", + &format_args!("<{} bytes>", self.chain_der.len()), + ) + .field("key_label", &self.key_label) + .field("key_der", &format_args!("<{} bytes>", self.key_der.len())) + .finish() + } +} + +impl CertificateSecrets { + /// Full-chain PEM: leaf first, then every intermediate, as frpc expects. + pub fn fullchain_pem(&self) -> String { + let mut out = parse::encode_pem("CERTIFICATE", &self.cert_der); + if !self.chain_der.is_empty() { + for der in parse::split_der_sequence(&self.chain_der) { + out.push_str(&parse::encode_pem("CERTIFICATE", der)); + } + } + out + } + + pub fn key_pem(&self) -> String { + parse::encode_pem(&self.key_label, &self.key_der) + } +} + +/// Derive the state from the material and the clock. +/// +/// `None` for both bounds means "no material"; an empty string is treated the +/// same way so a row written before the split cannot look `Active`. +pub fn classify( + not_before: Option<&str>, + not_after: Option<&str>, +) -> (CertificateStatus, Option) { + let Some(not_after) = not_after.filter(|value| !value.trim().is_empty()) else { + return ( + CertificateStatus::Pending, + Some("no certificate has been received yet".into()), + ); + }; + + let days = days_remaining(not_after); + if days < 0 { + return ( + CertificateStatus::Expired, + Some(format!( + "expired {} days ago ({})", + -days, + ¬_after[..not_after.len().min(10)] + )), + ); + } + + // A bundle whose validity starts in the future is rejected by every client + // that checks dates, so it is reported rather than silently written. + if let Some(not_before) = not_before.filter(|value| !value.trim().is_empty()) { + if let Ok(start) = chrono::DateTime::parse_from_rfc3339(not_before) { + if start > chrono::Utc::now() { + return ( + CertificateStatus::NotYetValid, + Some(format!( + "validity starts at {}", + ¬_before[..not_before.len().min(19)] + )), + ); + } + } + } + + if days <= EXPIRING_SOON_DAYS { + return ( + CertificateStatus::ExpiringSoon, + Some(format!("expires in {days} days")), + ); + } + + (CertificateStatus::Active, None) +} + +/// Whole days from `now` until `not_after` (RFC 3339). Negative when expired. +pub fn days_remaining(not_after: &str) -> i64 { + let Ok(parsed) = chrono::DateTime::parse_from_rfc3339(not_after) else { + return 0; + }; + (parsed.with_timezone(&chrono::Utc) - chrono::Utc::now()).num_days() +} + +#[cfg(test)] +mod status_tests { + use super::*; + + fn at(days: i64) -> String { + (chrono::Utc::now() + chrono::Duration::days(days)).to_rfc3339() + } + + #[test] + fn no_material_is_pending_not_expired() { + let (status, reason) = classify(None, None); + assert_eq!(status, CertificateStatus::Pending); + assert!(!status.usable()); + assert!(reason.unwrap().contains("no certificate")); + + // An empty string is the shape a pre-split row has; it must not read as + // "active until 1970". + assert_eq!(classify(None, Some("")).0, CertificateStatus::Pending); + } + + #[test] + fn expiry_boundaries_are_ordered() { + assert_eq!( + classify(Some(&at(-10)), Some(&at(-1))).0, + CertificateStatus::Expired + ); + assert_eq!( + classify(Some(&at(-10)), Some(&at(1))).0, + CertificateStatus::ExpiringSoon + ); + assert_eq!( + classify(Some(&at(-10)), Some(&at(EXPIRING_SOON_DAYS))).0, + CertificateStatus::ExpiringSoon + ); + // `days_remaining` truncates, so one day past the boundary is still + // reported as the boundary value; step two days to test the gap. + assert_eq!( + classify(Some(&at(-10)), Some(&at(EXPIRING_SOON_DAYS + 2))).0, + CertificateStatus::Active + ); + } + + #[test] + fn a_future_not_before_is_reported() { + let (status, reason) = classify(Some(&at(5)), Some(&at(400))); + assert_eq!(status, CertificateStatus::NotYetValid); + assert!(!status.usable()); + assert!(reason.unwrap().contains("validity starts")); + } + + #[test] + fn only_active_and_expiring_soon_are_usable() { + for (status, usable) in [ + (CertificateStatus::Active, true), + (CertificateStatus::ExpiringSoon, true), + (CertificateStatus::Expired, false), + (CertificateStatus::NotYetValid, false), + (CertificateStatus::Pending, false), + ] { + assert_eq!(status.usable(), usable, "{status:?}"); + } + } + + #[test] + fn status_strings_round_trip() { + for status in [ + CertificateStatus::Active, + CertificateStatus::ExpiringSoon, + CertificateStatus::Expired, + CertificateStatus::NotYetValid, + CertificateStatus::Pending, + ] { + assert_eq!(CertificateStatus::parse(status.as_str()), Some(status)); + } + } +} diff --git a/crates/client/src/cert/parse.rs b/crates/client/src/cert/parse.rs new file mode 100644 index 0000000..25f97e3 --- /dev/null +++ b/crates/client/src/cert/parse.rs @@ -0,0 +1,496 @@ +//! Certificate bundle parsing and validation. +//! +//! Accepts PEM or base64 DER for both halves of a bundle, splits a fullchain +//! into leaf + intermediates, and proves the private key belongs to the leaf. +//! Nothing here trusts the caller's claim about which key goes with which +//! certificate: a mismatch is rejected before anything is written to disk. + +use base64::Engine; +use sha2::{Digest, Sha256}; +use x509_parser::extensions::GeneralName; +use x509_parser::prelude::*; + +use crate::error::{ClientError, Result}; + +/// OIDs we compare against, spelled out as strings so the crate does not depend +/// on generated constant names. +const OID_RSA: &str = "1.2.840.113549.1.1.1"; +const OID_EC: &str = "1.2.840.10045.2.1"; +const OID_ED25519: &str = "1.3.101.112"; + +/// A bundle that passed every structural check. +#[derive(Debug)] +pub struct ParsedCertificate { + pub primary_domain: String, + pub sans: Vec, + pub issuer: String, + pub serial: String, + pub fingerprint_sha256: String, + pub key_algo: String, + /// RFC 3339, UTC. + pub not_before: String, + pub not_after: String, + pub cert_der: Vec, + /// Concatenated DER of every intermediate, leaf excluded. May be empty. + pub chain_der: Vec, + /// PEM label of the key, e.g. `PRIVATE KEY` or `RSA PRIVATE KEY`. + pub key_label: String, + pub key_der: Vec, + /// Non-fatal findings (incomplete chain, short validity, ...). + pub warnings: Vec, +} + +/// Parse and cross-check a certificate + private key bundle. +/// +/// `cert_pem` may be a fullchain; `key_pem` may be PKCS#8 or PKCS#1, PEM or +/// bare base64 DER. +pub fn parse_bundle(cert_pem: &str, key_pem: &str) -> Result { + let cert_blocks = decode_pem_blocks(cert_pem, "CERTIFICATE")?; + if cert_blocks.is_empty() { + return Err(ClientError::CertificateParse( + "no CERTIFICATE block found in the certificate input".into(), + )); + } + + let leaf_der = cert_blocks[0].clone(); + let mut chain_der = Vec::new(); + for block in &cert_blocks[1..] { + chain_der.extend_from_slice(block); + } + + let (_, leaf) = parse_x509_certificate(&leaf_der) + .map_err(|e| ClientError::CertificateParse(format!("leaf certificate is invalid: {e}")))?; + + let mut warnings = Vec::new(); + let mut sans = Vec::new(); + match leaf.subject_alternative_name() { + Ok(Some(extension)) => { + for name in &extension.value.general_names { + match name { + GeneralName::DNSName(dns) => sans.push((*dns).to_ascii_lowercase()), + GeneralName::IPAddress(raw) => sans.push(format_ip(raw)), + _ => {} + } + } + } + Ok(None) => {} + Err(e) => warnings.push(format!("subjectAltName could not be read: {e}")), + } + // The first SAN in the extension is the ordering the issuer intended; sort + // only the display copy, so the "primary" domain is not chosen by alphabet. + let first_san = sans.first().cloned(); + sans.sort(); + sans.dedup(); + + // Prefer the common name: it is what the operator typed into the ACME + // client, whereas the SAN list may begin with a www alias. + let common_name = leaf + .subject() + .iter_common_name() + .next() + .and_then(|cn| cn.as_str().ok()) + .map(|cn| cn.to_ascii_lowercase()) + .filter(|cn| !cn.is_empty()); + + let primary_domain = common_name.or(first_san).ok_or_else(|| { + ClientError::CertificateParse( + "certificate has neither subjectAltName nor a common name".into(), + ) + })?; + + // Collect the subjects present in the chain so we can tell whether the leaf + // was shipped with its issuer. + let mut chain_subjects = Vec::new(); + for der in &cert_blocks[1..] { + if let Ok((_, intermediate)) = parse_x509_certificate(der) { + chain_subjects.push(intermediate.subject().as_raw().to_vec()); + } + } + if chain_subjects.is_empty() { + warnings + .push("no intermediate certificate was supplied; clients may reject the chain".into()); + } else if !chain_subjects + .iter() + .any(|subject| subject.as_slice() == leaf.issuer().as_raw()) + { + warnings.push("the supplied intermediates do not include the certificate's issuer".into()); + } + + let key_algo = describe_key(leaf.public_key()); + let (key_label, key_der) = decode_private_key(key_pem)?; + verify_key_matches(&leaf, &key_der)?; + + let not_before = leaf.validity().not_before.timestamp(); + let not_after = leaf.validity().not_after.timestamp(); + if not_after <= chrono::Utc::now().timestamp() { + warnings.push("the certificate is already expired".into()); + } + + let fingerprint_sha256 = hex(&Sha256::digest(&leaf_der)); + + Ok(ParsedCertificate { + primary_domain, + sans, + issuer: leaf.issuer().to_string(), + serial: hex(leaf.raw_serial()), + fingerprint_sha256, + key_algo, + not_before: rfc3339(not_before), + not_after: rfc3339(not_after), + cert_der: leaf_der, + chain_der, + key_label, + key_der, + warnings, + }) +} + +/// Split a concatenation of DER objects into its members. +/// +/// Chains are stored as concatenated DER rather than PEM so the database holds +/// one canonical form; this recovers the individual certificates when writing +/// the PEM file frpc reads. +pub fn split_der_sequence(der: &[u8]) -> Vec<&[u8]> { + let mut out = Vec::new(); + let mut rest = der; + while !rest.is_empty() { + match der_object_len(rest) { + Some(len) if len <= rest.len() => { + out.push(&rest[..len]); + rest = &rest[len..]; + } + _ => break, + } + } + out +} + +/// Wrap DER bytes in a PEM block. +pub fn encode_pem(label: &str, der: &[u8]) -> String { + let base64 = base64::engine::general_purpose::STANDARD.encode(der); + let mut out = format!("-----BEGIN {label}-----\n"); + for chunk in base64.as_bytes().chunks(64) { + out.push_str(&String::from_utf8_lossy(chunk)); + out.push('\n'); + } + out.push_str(&format!("-----END {label}-----\n")); + out +} + +/// Length of the DER object starting at `bytes`, header included. +fn der_object_len(bytes: &[u8]) -> Option { + let first = *bytes.first()?; + if first != 0x30 { + return None; + } + let length = *bytes.get(1)?; + if length < 0x80 { + return Some(2 + usize::from(length)); + } + let count = usize::from(length & 0x7f); + if count == 0 || count > 4 { + return None; + } + let mut value = 0_usize; + for byte in bytes.get(2..2 + count)? { + value = (value << 8) | usize::from(*byte); + } + Some(2 + count + value) +} + +/// Decode every PEM block with the given label, or treat the input as bare +/// base64 DER when no block is present. +fn decode_pem_blocks(input: &str, label: &str) -> Result>> { + let trimmed = input.trim(); + if trimmed.is_empty() { + return Err(ClientError::CertificateParse(format!( + "no {label} input supplied" + ))); + } + + let mut blocks = Vec::new(); + let mut rest: &str = trimmed; + let begin = format!("-----BEGIN {label}-----"); + let end = format!("-----END {label}-----"); + while let Some(start) = rest.find(&begin) { + let after = &rest[start + begin.len()..]; + let Some(stop) = after.find(&end) else { + return Err(ClientError::CertificateParse(format!( + "{label} block is not terminated" + ))); + }; + let body: String = after[..stop] + .chars() + .filter(|c| !c.is_whitespace()) + .collect(); + let decoded = base64::engine::general_purpose::STANDARD + .decode(body) + .map_err(|e| { + ClientError::CertificateParse(format!("{label} block is not valid base64: {e}")) + })?; + blocks.push(decoded); + rest = &after[stop + end.len()..]; + } + + if !blocks.is_empty() { + return Ok(blocks); + } + + // No PEM header: accept a bare base64 body, which is what clients that + // strip headers before POSTing produce. + let compact: String = trimmed.chars().filter(|c| !c.is_whitespace()).collect(); + if compact.contains("-----") { + return Ok(blocks); + } + match base64::engine::general_purpose::STANDARD.decode(compact) { + Ok(decoded) if !decoded.is_empty() => Ok(vec![decoded]), + _ => Err(ClientError::CertificateParse(format!( + "input is neither a {label} PEM block nor base64 DER" + ))), + } +} + +/// Decode a private key and normalise it to `(PEM label, PKCS#8 DER)`. +/// +/// PKCS#1 (`RSA PRIVATE KEY`) inputs are wrapped so the stored form is always +/// PKCS#8 — a single representation means the PEM we write back is always +/// consistent with the label we write it under. +fn decode_private_key(input: &str) -> Result<(String, Vec)> { + for label in ["PRIVATE KEY", "RSA PRIVATE KEY", "EC PRIVATE KEY"] { + let blocks = decode_pem_blocks(input, label)?; + if let Some(block) = blocks.into_iter().next() { + return normalize_key(block); + } + } + Err(ClientError::CertificateParse( + "no private key found in the key input".into(), + )) +} + +/// Return the key as PKCS#8, wrapping PKCS#1 RSA when needed. +fn normalize_key(der: Vec) -> Result<(String, Vec)> { + if key_parses(&der) { + return Ok(("PRIVATE KEY".to_string(), der)); + } + let wrapped = wrap_pkcs1_rsa(&der)?; + if key_parses(&wrapped) { + return Ok(("PRIVATE KEY".to_string(), wrapped)); + } + Err(ClientError::CertificateParse( + "the private key is neither PKCS#8 nor PKCS#1 RSA".into(), + )) +} + +/// True when a crypto backend can load the key as PKCS#8. +fn key_parses(der: &[u8]) -> bool { + if ring::signature::RsaKeyPair::from_pkcs8(der).is_ok() { + return true; + } + let rng = ring::rand::SystemRandom::new(); + if ring::signature::EcdsaKeyPair::from_pkcs8( + &ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING, + der, + &rng, + ) + .is_ok() + { + return true; + } + if ring::signature::EcdsaKeyPair::from_pkcs8( + &ring::signature::ECDSA_P384_SHA384_ASN1_SIGNING, + der, + &rng, + ) + .is_ok() + { + return true; + } + ring::signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(der).is_ok() +} + +/// Wrap a PKCS#1 RSA key in a PKCS#8 envelope. +fn wrap_pkcs1_rsa(pkcs1: &[u8]) -> Result> { + const RSA_ALGORITHM_ID: &[u8] = &[ + 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, + ]; + let version = [0x02, 0x01, 0x00]; + + let mut inner = Vec::new(); + inner.extend_from_slice(&version); + inner.extend_from_slice(RSA_ALGORITHM_ID); + + let mut octet = Vec::new(); + octet.push(0x04); + push_der_len(&mut octet, pkcs1.len()); + octet.extend_from_slice(pkcs1); + inner.extend_from_slice(&octet); + + let mut out = Vec::new(); + out.push(0x30); + push_der_len(&mut out, inner.len()); + out.extend_from_slice(&inner); + Ok(out) +} + +fn push_der_len(out: &mut Vec, len: usize) { + if len < 0x80 { + out.push(len as u8); + } else if len <= 0xff { + out.push(0x81); + out.push(len as u8); + } else { + out.push(0x82); + out.push((len >> 8) as u8); + out.push((len & 0xff) as u8); + } +} + +/// Reject a bundle whose key does not belong to the leaf certificate. +fn verify_key_matches(leaf: &X509Certificate<'_>, key_der: &[u8]) -> Result<()> { + let mismatched = || { + ClientError::CertificateKeyMismatch( + "the private key does not match the certificate's public key".into(), + ) + }; + + // The BIT STRING payload is exactly the encoding the crypto backend derives + // from a private key: `SEQUENCE{modulus, exponent}` for RSA, and the + // uncompressed point for ECDSA. So one comparison covers both families. + let certificate_public = leaf.public_key().subject_public_key.data.to_vec(); + if certificate_public.is_empty() { + return Err(ClientError::CertificateParse( + "certificate has an empty public key".into(), + )); + } + + if let Ok(pair) = ring::signature::RsaKeyPair::from_pkcs8(key_der) { + return if pair.public().as_ref() == certificate_public.as_slice() { + Ok(()) + } else { + Err(mismatched()) + }; + } + + use ring::signature::KeyPair; + let rng = ring::rand::SystemRandom::new(); + for algorithm in [ + &ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING, + &ring::signature::ECDSA_P384_SHA384_ASN1_SIGNING, + ] { + if let Ok(pair) = ring::signature::EcdsaKeyPair::from_pkcs8(algorithm, key_der, &rng) { + return if pair.public_key().as_ref() == certificate_public.as_slice() { + Ok(()) + } else { + Err(mismatched()) + }; + } + } + + Err(mismatched()) +} + +fn describe_key(spki: &SubjectPublicKeyInfo<'_>) -> String { + let oid = spki.algorithm.algorithm.to_id_string(); + let bits = spki.parsed().map(|key| key.key_size()).unwrap_or_default(); + match oid.as_str() { + OID_RSA => format!("rsa-{bits}"), + // The named curve lives in the algorithm parameters; the key size is + // enough to label it and avoids re-parsing the parameter OID. + OID_EC => match bits { + 256 => "ecdsa-p256".into(), + 384 => "ecdsa-p384".into(), + 521 => "ecdsa-p521".into(), + other => format!("ecdsa-{other}"), + }, + OID_ED25519 => "ed25519".into(), + other => format!("oid-{other}"), + } +} + +fn format_ip(raw: &[u8]) -> String { + match raw.len() { + 4 => std::net::Ipv4Addr::new(raw[0], raw[1], raw[2], raw[3]).to_string(), + 16 => { + let mut octets = [0_u8; 16]; + octets.copy_from_slice(raw); + std::net::Ipv6Addr::from(octets).to_string() + } + _ => raw.iter().fold(String::new(), |mut out, byte| { + if !out.is_empty() { + out.push('.'); + } + out.push_str(&byte.to_string()); + out + }), + } +} + +fn rfc3339(timestamp: i64) -> String { + chrono::DateTime::from_timestamp(timestamp, 0) + .unwrap_or_else(|| chrono::DateTime::from_timestamp(0, 0).expect("epoch is valid")) + .to_rfc3339() +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().fold(String::new(), |mut out, byte| { + use std::fmt::Write; + let _ = write!(out, "{byte:02x}"); + out + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + const CERT: &str = include_str!("../../tests/fixtures/cert_ec.pem"); + const KEY: &str = include_str!("../../tests/fixtures/cert_ec.key"); + const RSA_KEY: &str = include_str!("../../tests/fixtures/cert_rsa.key"); + + #[test] + fn parses_ecdsa_bundle() { + let parsed = parse_bundle(CERT, KEY).unwrap(); + assert_eq!(parsed.primary_domain, "test.example.com"); + assert!(parsed.sans.contains(&"test.example.com".to_string())); + assert_eq!(parsed.key_algo, "ecdsa-p256"); + assert!(parsed.chain_der.is_empty()); + assert_eq!(parsed.fingerprint_sha256.len(), 64); + // No intermediates supplied on purpose: the warning must say so. + assert!(parsed.warnings.iter().any(|w| w.contains("intermediate"))); + } + + #[test] + fn rejects_mismatched_key() { + let error = parse_bundle(CERT, RSA_KEY).unwrap_err(); + assert!(matches!(error, ClientError::CertificateKeyMismatch(_))); + } + + #[test] + fn accepts_bare_base64_certificate() { + let der = decode_pem_blocks(CERT, "CERTIFICATE").unwrap().remove(0); + let bare = base64::engine::general_purpose::STANDARD.encode(der); + let parsed = parse_bundle(&bare, KEY).unwrap(); + assert_eq!(parsed.primary_domain, "test.example.com"); + } + + #[test] + fn fingerprint_is_stable() { + let first = parse_bundle(CERT, KEY).unwrap(); + let second = parse_bundle(CERT, KEY).unwrap(); + assert_eq!(first.fingerprint_sha256, second.fingerprint_sha256); + } + + #[test] + fn splits_concatenated_der() { + let der = decode_pem_blocks(CERT, "CERTIFICATE").unwrap().remove(0); + let joined: Vec = der.iter().chain(der.iter()).copied().collect(); + assert_eq!(split_der_sequence(&joined).len(), 2); + } + + #[test] + fn pem_round_trip() { + let der = decode_pem_blocks(CERT, "CERTIFICATE").unwrap().remove(0); + let pem = encode_pem("CERTIFICATE", &der); + assert_eq!(decode_pem_blocks(&pem, "CERTIFICATE").unwrap(), vec![der]); + } +} diff --git a/crates/client/src/core.rs b/crates/client/src/core.rs index ad5432a..288f4bb 100644 --- a/crates/client/src/core.rs +++ b/crates/client/src/core.rs @@ -405,12 +405,17 @@ mod tests { #[cfg(unix)] { let recorded = std::fs::read_to_string(&marker).unwrap_or_default(); + // Match the generated artifact name, not the bare id: the stub records + // a full path, and a temporary directory whose name happens to contain + // the id's digit would make a substring check pass for the wrong reason. + let kept = format!("{}-kept-running.toml", ids[0].0); + let stopped = format!("{}-user-stopped.toml", ids[1].0); assert!( - recorded.contains(&ids[0].0.to_string()), + recorded.contains(&kept), "the kept profile must be attempted, got: {recorded:?}" ); assert!( - !recorded.contains(&ids[1].0.to_string()), + !recorded.contains(&stopped), "the stopped profile must never be attempted, got: {recorded:?}" ); } diff --git a/crates/client/src/db/cert_ingest.rs b/crates/client/src/db/cert_ingest.rs new file mode 100644 index 0000000..790ac90 --- /dev/null +++ b/crates/client/src/db/cert_ingest.rs @@ -0,0 +1,745 @@ +//! Certificate channels: everything that can bring a bundle in. +//! +//! A channel is either a **push** (a token a certificate manager presents) or a +//! **pull** (an outbound fetch from Certd or a plain HTTPS endpoint). They share +//! one table because they answer the same question — "where does the next bundle +//! for this certificate come from?" — and because an operator should be able to +//! switch a certificate between them without deleting and re-creating rows. +//! +//! A channel may be *unbound* (`certificate_id = NULL`): the legacy shape where +//! a token or source existed on its own. Binding it to a certificate is what +//! makes the pair meaningful, and is what the UI now does by default. +//! +//! Neither kind stores plaintext credentials: tokens keep only a SHA-256 digest, +//! and a pull secret is encrypted with the same master key as certificate +//! material. + +use rusqlite::{params, OptionalExtension}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::db::Database; +use crate::error::{ClientError, Result}; + +/// How a channel brings material in. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum SourceKind { + /// Inbound: a certificate manager POSTs a bundle with a bearer token. + Push, + /// Outbound: Certd's open API, signed with keyId/keySecret. + Certd, + /// Outbound: any HTTPS endpoint returning a PEM bundle or `{crt, key}` JSON. + Url, +} + +impl SourceKind { + pub fn as_str(&self) -> &'static str { + match self { + Self::Push => "push", + Self::Certd => "certd", + Self::Url => "url", + } + } + + pub fn parse(value: &str) -> Result { + match value { + "push" => Ok(Self::Push), + "certd" => Ok(Self::Certd), + "url" => Ok(Self::Url), + other => Err(ClientError::ConfigValidation(format!( + "unknown certificate source kind '{other}'" + ))), + } + } + + /// True when the daemon fetches on its own schedule. + pub fn is_pull(&self) -> bool { + matches!(self, Self::Certd | Self::Url) + } +} + +/// A channel, as the UI sees it. Never carries a secret. +#[derive(Debug, Clone, Serialize)] +pub struct CertificateSource { + pub id: i64, + pub tenant_id: String, + /// `None` for a channel not yet attached to a certificate. + pub certificate_id: Option, + pub kind: SourceKind, + pub name: String, + /// The domain this channel is expected to serve. For a push token this is + /// empty; the allowlist in `domains` governs it instead. + pub domain: String, + pub base_url: String, + /// Allowed domains for a push token. Empty for pull channels. + pub domains: Vec, + /// Whether any credential is stored, without revealing it. + pub has_secret: bool, + /// The one channel a certificate prefers when several exist. + pub is_primary: bool, + pub enabled: bool, + pub expires_at: Option, + pub revoked_at: Option, + pub last_used_at: Option, + pub use_count: i64, + pub last_run_at: Option, + pub last_status: Option, + pub last_error: Option, + pub next_run_at: Option, + pub created_at: String, + pub updated_at: String, +} + +/// Result of creating a channel: the row plus the one-time token for a push. +#[derive(Debug, Serialize)] +pub struct CreatedSource { + pub source: CertificateSource, + /// Shown once, for `push` only. Null on every subsequent read. + pub plaintext: Option, +} + +/// Fields accepted when creating a channel. +#[derive(Debug, Deserialize)] +pub struct SourceInput { + #[serde(default)] + pub certificate_id: Option, + pub kind: SourceKind, + pub name: String, + #[serde(default)] + pub domain: String, + #[serde(default)] + pub base_url: String, + /// Push only: the domains a presented certificate may cover. + #[serde(default)] + pub domains: Vec, + /// Certd only. + #[serde(default)] + pub key_id: Option, + #[serde(default)] + pub key_secret: Option, + #[serde(default = "default_true")] + pub enabled: bool, +} + +fn default_true() -> bool { + true +} + +/// Stored secret payload. No `Serialize`, so it cannot reach a response. +#[derive(Debug, Deserialize, Default)] +pub struct SourceSecret { + #[serde(default)] + pub key_id: Option, + #[serde(default)] + pub key_secret: Option, +} + +/// SHA-256 of a token, lowercase hex. +pub fn token_digest(token: &str) -> String { + Sha256::digest(token.as_bytes()) + .iter() + .fold(String::new(), |mut out, byte| { + use std::fmt::Write; + let _ = write!(out, "{byte:02x}"); + out + }) +} + +/// Columns in [`row_to_source`] order. +const SOURCE_COLUMNS: &str = "id, tenant_id, certificate_id, kind, name, domain, base_url, + domains_json, enc_secret, token_sha256, is_primary, enabled, expires_at, revoked_at, + last_used_at, use_count, last_run_at, last_status, last_error, next_run_at, + created_at, updated_at"; + +/// Digest lookup row: id, tenant, allowed domains, certificate, name, expiry, +/// revocation. +type TokenRow = ( + i64, + String, + String, + Option, + String, + Option, + Option, +); + +/// What a presented token resolved to. +pub struct TokenGrant { + pub tenant: String, + pub allowed_domains: Vec, + /// Set when the token is attached to exactly one certificate, so the push + /// path can refuse a bundle meant for a different certificate. + pub certificate_id: Option, + pub source_id: i64, +} + +impl Database { + /// Create a channel. A push channel returns its plaintext token exactly once. + pub async fn create_source(&self, tenant: &str, input: SourceInput) -> Result { + validate_source_input(&input)?; + + let cipher = self.cert_cipher()?; + let secret = SourceSecret { + key_id: input.key_id.clone(), + key_secret: input.key_secret.clone(), + }; + let enc_secret = if secret.key_id.is_none() && secret.key_secret.is_none() { + None + } else { + // Built as an explicit map so `SourceSecret` never needs `Serialize`: + // the only shape that can reach a response carries `has_secret`. + let mut payload = serde_json::Map::new(); + if let Some(key_id) = &secret.key_id { + payload.insert("key_id".into(), key_id.clone().into()); + } + if let Some(key_secret) = &secret.key_secret { + payload.insert("key_secret".into(), key_secret.clone().into()); + } + let json = serde_json::to_vec(&payload) + .map_err(|e| ClientError::ConfigValidation(e.to_string()))?; + Some(cipher.encrypt(&json)?) + }; + + // A push channel's allowlist is required and normalised; a pull channel + // has no allowlist at all, so it stores an empty list rather than + // tripping the push validation. + let domains_json = if input.kind == SourceKind::Push { + serde_json::to_string(&normalize_domains(input.domains.clone())?) + .map_err(|e| ClientError::ConfigValidation(e.to_string()))? + } else { + "[]".to_string() + }; + + // Only a push channel needs a token; generating one for a pull would + // leave an unused credential in the database. + let plaintext = if input.kind == SourceKind::Push { + Some(generate_token()) + } else { + None + }; + let token_sha256 = plaintext.as_ref().map(|token| token_digest(token)); + + let domain = input.domain.trim().to_ascii_lowercase(); + let now = chrono::Utc::now().to_rfc3339(); + + // A pull channel always belongs to a certificate: it names the domain it + // will keep up to date, and that domain *is* the certificate's identity. + // Creating the certificate here is what makes it visible in the list + // while its first fetch is still pending — otherwise a channel whose + // first pull failed leaves the operator staring at an empty page with no + // hint that anything was configured. + let certificate_id = match (input.certificate_id, input.kind.is_pull()) { + (Some(id), _) => Some(id), + (None, true) if !domain.is_empty() => { + Some(self.ensure_certificate(tenant, &domain).await?.id) + } + (None, _) => None, + }; + + // A pull channel's target domain decides which certificates it accepts + // and, once attached, which certificate it may replace. Checking it here + // turns "the first fetch fails with a target error" into "this channel + // cannot be created", which names the mistake at the point of making it. + if input.kind.is_pull() { + if let Some(certificate_id) = certificate_id { + let certificate = self.certificate_in_tenant(certificate_id, tenant).await?; + if certificate.material_id.is_some() { + let served = crate::cert::domain::served_domains( + &certificate.sans, + &certificate.primary_domain, + ); + if !served + .iter() + .any(|san| crate::cert::domain::covers(san, &domain)) + { + return Err(ClientError::ConfigValidation(format!( + "the certificate serves {}, which does not cover the channel's domain {}", + served.join(", "), + domain + ))); + } + } + } + } + + let id = { + let conn = self.lock().await; + if let Some(certificate_id) = certificate_id { + let exists: i64 = conn.query_row( + "SELECT COUNT(*) FROM certificate WHERE id = ?1 AND tenant_id = ?2", + params![certificate_id, tenant], + |row| row.get(0), + )?; + if exists == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate".into(), + id: certificate_id, + }); + } + } + conn.execute( + "INSERT INTO certificate_source + (tenant_id, certificate_id, kind, name, domain, base_url, enc_secret, + domains_json, token_sha256, is_primary, enabled, created_at, updated_at) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 0, ?10, ?11, ?11)", + params![ + tenant, + certificate_id, + input.kind.as_str(), + input.name.trim(), + domain, + input.base_url.trim(), + enc_secret, + domains_json, + token_sha256, + input.enabled as i32, + now, + ], + )?; + conn.last_insert_rowid() + }; + + // A certificate's first channel becomes its primary: otherwise the UI + // would render a certificate whose only channel is not the one used. + if certificate_id.is_some() { + self.promote_if_primary_missing(id, tenant).await?; + } + + Ok(CreatedSource { + source: self.source(id, tenant).await?, + plaintext, + }) + } + + /// All channels for a tenant, optionally filtered to one certificate. + pub async fn list_sources( + &self, + tenant: &str, + certificate_id: Option, + ) -> Result> { + let conn = self.lock().await; + let mut stmt = conn.prepare(&format!( + "SELECT {SOURCE_COLUMNS} FROM certificate_source + WHERE tenant_id = ?1 AND (?2 IS NULL OR certificate_id = ?2) + ORDER BY is_primary DESC, id" + ))?; + let sources = stmt + .query_map(params![tenant, certificate_id], row_to_source)? + .collect::, _>>()?; + Ok(sources) + } + + pub async fn source(&self, id: i64, tenant: &str) -> Result { + let conn = self.lock().await; + conn.query_row( + &format!( + "SELECT {SOURCE_COLUMNS} FROM certificate_source + WHERE id = ?1 AND tenant_id = ?2" + ), + params![id, tenant], + row_to_source, + ) + .optional()? + .ok_or(ClientError::RecordNotFound { + table: "certificate_source".into(), + id, + }) + } + + /// Every enabled pull channel that is due, oldest due first. + /// + /// Unbound channels are included: a pull source that predates the + /// certificate-first shape still runs, it just has no certificate to attach + /// its result to until one is created for the same domain. + pub async fn due_pull_sources(&self, limit: usize) -> Result> { + let conn = self.lock().await; + let mut stmt = conn.prepare( + "SELECT id, tenant_id FROM certificate_source + WHERE enabled = 1 AND kind IN ('certd', 'url') + AND (next_run_at IS NULL OR next_run_at <= ?1) + ORDER BY COALESCE(next_run_at, ''), id + LIMIT ?2", + )?; + let rows = stmt + .query_map( + params![chrono::Utc::now().to_rfc3339(), limit as i64], + |row| Ok((row.get(0)?, row.get(1)?)), + )? + .collect::, _>>()?; + Ok(rows) + } + + /// Decrypt a channel's credentials for an outbound request. + pub async fn source_secret(&self, id: i64, tenant: &str) -> Result { + let cipher = self.cert_cipher()?; + // The column is nullable: a source without credentials (a plain URL, an + // anonymous fetch) stores NULL, so the row must be read as + // `Option>>` — the outer `None` is "no such source", the + // inner one is "no secret". + let blob: Option>> = { + let conn = self.lock().await; + conn.query_row( + "SELECT enc_secret FROM certificate_source WHERE id = ?1 AND tenant_id = ?2", + params![id, tenant], + |row| row.get(0), + ) + .optional()? + }; + match blob { + Some(Some(blob)) => { + let json = cipher.decrypt(&blob)?; + serde_json::from_slice(&json).map_err(|e| { + ClientError::CertificateSecrets(format!("source secret is unreadable: {e}")) + }) + } + Some(None) => Ok(SourceSecret::default()), + None => Err(ClientError::RecordNotFound { + table: "certificate_source".into(), + id, + }), + } + } + + /// Delete a channel. Pushes using its token start failing immediately. + pub async fn delete_source(&self, id: i64, tenant: &str) -> Result<()> { + let conn = self.lock().await; + let deleted = conn.execute( + "DELETE FROM certificate_source WHERE id = ?1 AND tenant_id = ?2", + params![id, tenant], + )?; + if deleted == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate_source".into(), + id, + }); + } + Ok(()) + } + + /// Attach a channel to a certificate, or detach it with `None`. + /// + /// This is what makes a channel switchable: the same token can move from a + /// certificate to a different one, or become unbound, without being + /// re-issued. + pub async fn set_source_certificate( + &self, + id: i64, + certificate_id: Option, + tenant: &str, + ) -> Result { + { + let conn = self.lock().await; + if let Some(certificate_id) = certificate_id { + let exists: i64 = conn.query_row( + "SELECT COUNT(*) FROM certificate WHERE id = ?1 AND tenant_id = ?2", + params![certificate_id, tenant], + |row| row.get(0), + )?; + if exists == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate".into(), + id: certificate_id, + }); + } + } + let updated = conn.execute( + "UPDATE certificate_source + SET certificate_id = ?1, updated_at = ?2 + WHERE id = ?3 AND tenant_id = ?4", + params![certificate_id, chrono::Utc::now().to_rfc3339(), id, tenant], + )?; + if updated == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate_source".into(), + id, + }); + } + } + if certificate_id.is_some() { + self.promote_if_primary_missing(id, tenant).await?; + } + self.source(id, tenant).await + } + + /// Make `id` the channel a certificate prefers, demoting its siblings. + /// + /// The partial unique index allows one primary per certificate, so the + /// demotion has to happen first or the update would violate it. + pub async fn set_primary_source(&self, id: i64, tenant: &str) -> Result { + let source = self.source(id, tenant).await?; + let Some(certificate_id) = source.certificate_id else { + return Err(ClientError::ConfigValidation( + "a channel must be attached to a certificate before it can be primary".into(), + )); + }; + + let conn = self.lock().await; + let now = chrono::Utc::now().to_rfc3339(); + conn.execute( + "UPDATE certificate_source SET is_primary = 0, updated_at = ?1 + WHERE tenant_id = ?2 AND certificate_id = ?3", + params![now, tenant, certificate_id], + )?; + conn.execute( + "UPDATE certificate_source SET is_primary = 1, updated_at = ?1 + WHERE id = ?2 AND tenant_id = ?3", + params![now, id, tenant], + )?; + drop(conn); + self.source(id, tenant).await + } + + /// Give a channel primary status when its certificate has none. + async fn promote_if_primary_missing(&self, id: i64, tenant: &str) -> Result<()> { + let conn = self.lock().await; + conn.execute( + "UPDATE certificate_source SET is_primary = 1 + WHERE id = ?1 AND tenant_id = ?2 + AND certificate_id IS NOT NULL + AND NOT EXISTS ( + SELECT 1 FROM certificate_source other + WHERE other.certificate_id = certificate_source.certificate_id + AND other.is_primary = 1)", + params![id, tenant], + )?; + Ok(()) + } + + /// Resolve a presented push token. + /// + /// Returns the tenant, the domains the token may serve, and the certificate + /// it is attached to (if any). Revoked and expired tokens resolve to `None`, + /// as does one with an empty allowlist: an unrestricted token would let a + /// leak overwrite any bound domain. + pub async fn authenticate_source_token(&self, presented: &str) -> Result> { + if presented.len() < 24 { + return Ok(None); + } + let digest = token_digest(presented); + let row: Option = { + let conn = self.lock().await; + conn.query_row( + "SELECT id, tenant_id, domains_json, certificate_id, name, expires_at, revoked_at + FROM certificate_source + WHERE token_sha256 = ?1 AND kind = 'push'", + params![digest], + |row| { + Ok(( + row.get(0)?, + row.get(1)?, + row.get(2)?, + row.get(3)?, + row.get(4)?, + row.get(5)?, + row.get(6)?, + )) + }, + ) + .optional()? + }; + + let Some((id, tenant, domains_json, certificate_id, name, expires_at, revoked_at)) = row + else { + return Ok(None); + }; + if revoked_at.is_some() { + return Ok(None); + } + if let Some(expires_at) = expires_at { + let expired = chrono::DateTime::parse_from_rfc3339(&expires_at) + .map(|at| at < chrono::Utc::now()) + .unwrap_or(true); + if expired { + return Ok(None); + } + } + + let allowed_domains: Vec = serde_json::from_str(&domains_json).unwrap_or_default(); + if allowed_domains.is_empty() { + tracing::warn!( + source_id = id, + token_name = %name, + "Push token has no allowed domains; refusing the push" + ); + return Ok(None); + } + + { + let conn = self.lock().await; + conn.execute( + "UPDATE certificate_source + SET last_used_at = ?1, use_count = use_count + 1 + WHERE id = ?2", + params![chrono::Utc::now().to_rfc3339(), id], + )?; + } + tracing::info!(source_id = id, token_name = %name, "Certificate push token used"); + + Ok(Some(TokenGrant { + tenant, + allowed_domains, + certificate_id, + source_id: id, + })) + } + + /// Record the outcome of one run, and schedule the next one. + /// + /// A failure keeps the same interval: backing off would let a transient + /// error hide a certificate that is about to expire. + pub async fn record_source_run( + &self, + id: i64, + tenant: &str, + status: &str, + error: Option<&str>, + next_run_at: Option<&str>, + ) -> Result<()> { + let conn = self.lock().await; + conn.execute( + "UPDATE certificate_source + SET last_run_at = ?1, last_status = ?2, last_error = ?3, next_run_at = ?4 + WHERE id = ?5 AND tenant_id = ?6", + params![ + chrono::Utc::now().to_rfc3339(), + status, + error, + next_run_at, + id, + tenant + ], + )?; + Ok(()) + } +} + +fn row_to_source(row: &rusqlite::Row<'_>) -> rusqlite::Result { + let kind: String = row.get(3)?; + let domains_json: String = row.get(7)?; + let enc_secret: Option> = row.get(8)?; + Ok(CertificateSource { + id: row.get(0)?, + tenant_id: row.get(1)?, + certificate_id: row.get(2)?, + // An unknown kind can only come from a hand-edited row; treating it as a + // push keeps it inert instead of silently turning it into a fetch. + kind: SourceKind::parse(&kind).unwrap_or(SourceKind::Push), + name: row.get(4)?, + domain: row.get(5)?, + base_url: row.get(6)?, + domains: serde_json::from_str(&domains_json).unwrap_or_default(), + has_secret: enc_secret.is_some(), + is_primary: row.get::<_, i64>(10)? != 0, + enabled: row.get::<_, i64>(11)? != 0, + expires_at: row.get(12)?, + revoked_at: row.get(13)?, + last_used_at: row.get(14)?, + use_count: row.get(15)?, + last_run_at: row.get(16)?, + last_status: row.get(17)?, + last_error: row.get(18)?, + next_run_at: row.get(19)?, + created_at: row.get(20)?, + updated_at: row.get(21)?, + }) +} + +fn validate_source_input(input: &SourceInput) -> Result<()> { + if input.name.trim().is_empty() { + return Err(ClientError::ConfigValidation( + "certificate source name cannot be empty".into(), + )); + } + match input.kind { + SourceKind::Push => { + normalize_domains(input.domains.clone())?; + } + SourceKind::Certd | SourceKind::Url => { + if input.domain.trim().is_empty() { + return Err(ClientError::ConfigValidation( + "a pull source needs the domain it serves".into(), + )); + } + let url = input.base_url.trim(); + if !url.starts_with("http://") && !url.starts_with("https://") { + return Err(ClientError::ConfigValidation( + "pull source base URL must start with http:// or https://".into(), + )); + } + if input.kind == SourceKind::Certd + && (input.key_id.as_deref().unwrap_or("").is_empty() + || input.key_secret.as_deref().unwrap_or("").is_empty()) + { + return Err(ClientError::ConfigValidation( + "a Certd source requires key_id and key_secret".into(), + )); + } + } + } + Ok(()) +} + +fn normalize_domains(domains: Vec) -> Result> { + let mut out: Vec = domains + .into_iter() + .map(|domain| domain.trim().to_ascii_lowercase()) + .filter(|domain| !domain.is_empty()) + .collect(); + out.sort(); + out.dedup(); + if out.is_empty() { + return Err(ClientError::ConfigValidation( + "a push token needs at least one allowed domain".into(), + )); + } + Ok(out) +} + +/// 32 bytes of randomness, hex encoded. Long enough that the digest cannot be +/// brute forced, and safe in a header value. +fn generate_token() -> String { + use chacha20poly1305::aead::rand_core::RngCore; + use chacha20poly1305::aead::OsRng; + let mut bytes = [0_u8; 32]; + OsRng.fill_bytes(&mut bytes); + bytes.iter().fold(String::new(), |mut out, byte| { + use std::fmt::Write; + let _ = write!(out, "{byte:02x}"); + out + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn digest_is_stable_and_hex() { + let digest = token_digest("abc"); + assert_eq!(digest.len(), 64); + assert!(digest.chars().all(|c| c.is_ascii_hexdigit())); + assert_eq!(digest, token_digest("abc")); + } + + #[test] + fn generated_tokens_differ() { + assert_ne!(generate_token(), generate_token()); + assert_eq!(generate_token().len(), 64); + } + + #[test] + fn empty_domain_list_is_rejected() { + assert!(normalize_domains(vec![" ".into()]).is_err()); + assert_eq!( + normalize_domains(vec!["A.example.com".into(), "a.example.com".into()]).unwrap(), + vec!["a.example.com".to_string()] + ); + } + + #[test] + fn only_pull_kinds_are_fetched() { + assert!(SourceKind::Certd.is_pull() && SourceKind::Url.is_pull()); + assert!(!SourceKind::Push.is_pull()); + } +} diff --git a/crates/client/src/db/certificate.rs b/crates/client/src/db/certificate.rs new file mode 100644 index 0000000..c50b71a --- /dev/null +++ b/crates/client/src/db/certificate.rs @@ -0,0 +1,843 @@ +//! Certificate storage: identity, material, and the channels that update it. +//! +//! Three shapes are stored apart on purpose: +//! +//! * `certificate` — identity. One row per `(tenant, primary_domain)`, so a +//! renewal updates the certificate instead of appending a near-duplicate. +//! * `certificate_material` — one row per issued bundle: fingerprint, validity, +//! SANs, and the encrypted bodies. +//! * `certificate_source` — how a new bundle arrives: a push token, a Certd +//! pull, a plain URL. A certificate can exist with no material and no +//! channel, which is what "configure it now, fill it later" needs. +//! +//! A certificate's identity is deliberately *not* its fingerprint: every +//! reissue has a new fingerprint, and treating that as a new certificate is +//! exactly the bug this split exists to prevent. +//! +//! [`Certificate`](crate::cert::Certificate) is the only shape that may be +//! serialized; [`CertificateSecrets`](crate::cert::CertificateSecrets) has no +//! `Serialize` implementation, so material cannot leak into a response by +//! accident. + +use rusqlite::{params, OptionalExtension}; + +use crate::cert::{ + apply::{target_from_plugin_config, write_material}, + Certificate, CertificateBinding, CertificateSecrets, ParsedCertificate, +}; +use crate::db::Database; +use crate::error::{ClientError, Result}; + +/// Encrypted blobs as stored: certificate, chain, key. +type SecretsRow = (Vec, Option>, Vec); + +/// Where a certificate came from. Stored verbatim and shown in the UI. +pub const SOURCE_MANUAL: &str = "manual"; +pub const SOURCE_UPLOAD: &str = "upload"; +pub const SOURCE_LOCAL_PATH: &str = "local_path"; +pub const SOURCE_WEBHOOK: &str = "webhook"; +pub const SOURCE_CERTD_PULL: &str = "certd_pull"; +pub const SOURCE_URL_PULL: &str = "url_pull"; + +/// Import request with its provenance attached. +pub struct ImportRequest { + pub parsed: ParsedCertificate, + pub source: String, + pub source_detail: Option, +} + +/// One certificate, identity plus its newest material. +/// +/// A bare identity (no material yet) still produces one of these: the fields +/// that only material can supply are empty and `days_remaining` is `None`, so +/// the UI can show "waiting for the first fetch" instead of a fake expiry. +struct CertificateRow { + id: i64, + tenant_id: String, + primary_domain: String, + created_at: String, + updated_at: String, + material_id: Option, + fingerprint_sha256: Option, + sans_json: Option, + issuer: Option, + serial: Option, + key_algo: Option, + not_before: Option, + not_after: Option, + source: Option, + source_detail: Option, +} + +impl CertificateRow { + /// Column order shared by every query that reads a certificate. + /// + /// `certificate_material` is joined with `LEFT JOIN`, so a certificate with + /// no material yet is still returned rather than silently disappearing. + /// "Newest" is the most recently *stored* material, not the one expiring + /// last: a reissue can legitimately have an earlier `not_after` (a shorter + /// validity), and trusting the date would silently keep serving the old + /// bundle. Explain the choice where it is made, once. + const SELECT: &'static str = "SELECT c.id, c.tenant_id, c.primary_domain, + c.created_at, c.updated_at, + m.id, m.fingerprint_sha256, m.sans_json, m.issuer, m.serial, + m.key_algo, m.not_before, m.not_after, m.source, m.source_detail + FROM certificate c + LEFT JOIN certificate_material m ON m.id = ( + SELECT id FROM certificate_material + WHERE certificate_id = c.id + ORDER BY id DESC LIMIT 1)"; + + fn from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result { + Ok(Self { + id: row.get(0)?, + tenant_id: row.get(1)?, + primary_domain: row.get(2)?, + created_at: row.get(3)?, + updated_at: row.get(4)?, + material_id: row.get(5)?, + fingerprint_sha256: row.get(6)?, + sans_json: row.get(7)?, + issuer: row.get(8)?, + serial: row.get(9)?, + key_algo: row.get(10)?, + not_before: row.get(11)?, + not_after: row.get(12)?, + source: row.get(13)?, + source_detail: row.get(14)?, + }) + } + + fn into_certificate(self) -> Certificate { + let sans: Vec = self + .sans_json + .as_deref() + .map(|json| serde_json::from_str(json).unwrap_or_default()) + .unwrap_or_default(); + let not_after = self.not_after.clone().unwrap_or_default(); + let days_remaining = if not_after.is_empty() { + None + } else { + Some(crate::cert::days_remaining(¬_after)) + }; + let (status, status_reason) = + crate::cert::classify(self.not_before.as_deref(), self.not_after.as_deref()); + // `warnings` predates `status`; keep it as the non-blocking findings list + // so callers that only read warnings keep working unchanged. + let warnings = status_reason + .iter() + .filter(|_| !status.usable()) + .cloned() + .collect(); + Certificate { + id: self.id, + tenant_id: self.tenant_id, + primary_domain: self.primary_domain, + sans, + issuer: self.issuer.unwrap_or_default(), + serial: self.serial.unwrap_or_default(), + fingerprint_sha256: self.fingerprint_sha256.unwrap_or_default(), + key_algo: self.key_algo.unwrap_or_default(), + not_before: self.not_before.unwrap_or_default(), + not_after, + days_remaining, + material_id: self.material_id, + status, + status_reason, + usable: status.usable(), + channel_error: None, + source: self.source.unwrap_or_default(), + source_detail: self.source_detail, + created_at: self.created_at, + updated_at: self.updated_at, + bindings: Vec::new(), + warnings, + } + } +} + +impl Database { + /// Store a parsed bundle, keyed by its primary domain. + /// + /// Re-importing the same bundle is a no-op on the material but still + /// refreshes the recorded source; a renewal replaces the newest material + /// rather than adding a certificate. + pub async fn import_certificate( + &self, + tenant: &str, + request: ImportRequest, + ) -> Result { + let cipher = self.cert_cipher()?; + let ImportRequest { + parsed, + source, + source_detail, + } = request; + + let enc_cert = cipher.encrypt(&parsed.cert_der)?; + let enc_chain = if parsed.chain_der.is_empty() { + None + } else { + Some(cipher.encrypt(&parsed.chain_der)?) + }; + let enc_key = cipher.encrypt(&parsed.key_der)?; + let sans_json = serde_json::to_string(&parsed.sans) + .map_err(|e| ClientError::CertificateParse(e.to_string()))?; + let now = chrono::Utc::now().to_rfc3339(); + + { + let conn = self.lock().await; + // Identity first: a certificate may already exist because its + // channel was configured before any material arrived. + conn.execute( + "INSERT INTO certificate + (tenant_id, primary_domain, created_at, updated_at) + VALUES (?1, ?2, ?3, ?3) + ON CONFLICT(tenant_id, primary_domain) DO UPDATE SET updated_at = excluded.updated_at", + params![tenant, parsed.primary_domain, now], + )?; + let certificate_id: i64 = conn.query_row( + "SELECT id FROM certificate WHERE tenant_id = ?1 AND primary_domain = ?2", + params![tenant, parsed.primary_domain], + |row| row.get(0), + )?; + + // The same bundle arriving twice must not create a second version. + let existing: Option = conn + .query_row( + "SELECT id FROM certificate_material + WHERE certificate_id = ?1 AND fingerprint_sha256 = ?2", + params![certificate_id, parsed.fingerprint_sha256], + |row| row.get(0), + ) + .optional()?; + + match existing { + Some(material_id) => { + conn.execute( + "UPDATE certificate_material + SET sans_json = ?1, issuer = ?2, serial = ?3, key_algo = ?4, + not_before = ?5, not_after = ?6, source = ?7, source_detail = ?8, + enc_cert_der = ?9, enc_chain_der = ?10, enc_key_der = ?11, + updated_at = ?12 + WHERE id = ?13", + params![ + sans_json, + parsed.issuer, + parsed.serial, + parsed.key_algo, + parsed.not_before, + parsed.not_after, + source, + source_detail, + enc_cert, + enc_chain, + enc_key, + now, + material_id, + ], + )?; + } + None => { + conn.execute( + "INSERT INTO certificate_material + (tenant_id, certificate_id, fingerprint_sha256, sans_json, issuer, + serial, key_algo, not_before, not_after, source, source_detail, + enc_cert_der, enc_chain_der, enc_key_der, created_at, updated_at) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?15)", + params![ + tenant, + certificate_id, + parsed.fingerprint_sha256, + sans_json, + parsed.issuer, + parsed.serial, + parsed.key_algo, + parsed.not_before, + parsed.not_after, + source, + source_detail, + enc_cert, + enc_chain, + enc_key, + now, + ], + )?; + } + } + + tracing::info!( + certificate_id, + domain = %parsed.primary_domain, + fingerprint = %parsed.fingerprint_sha256, + source, + "Certificate material imported" + ); + } + + self.certificate_for_domain(tenant, &parsed.primary_domain) + .await + } + + /// All certificates for a tenant, expiring soonest first. + /// + /// Certificates with no material sort last: they have no expiry to sort by + /// and are not yet at risk. + pub async fn list_certificates(&self, tenant: &str) -> Result> { + let rows = { + let conn = self.lock().await; + let mut stmt = conn.prepare(&format!( + "{} WHERE c.tenant_id = ?1 + ORDER BY m.not_after IS NULL, m.not_after, c.primary_domain", + CertificateRow::SELECT + ))?; + let rows = stmt + .query_map(params![tenant], CertificateRow::from_row)? + .collect::, _>>()?; + rows + }; + + let bindings = self.list_certificate_bindings(tenant, None).await?; + let channel_errors = self.latest_source_errors(tenant).await?; + Ok(rows + .into_iter() + .map(|row| { + let mut certificate = row.into_certificate(); + certificate.bindings = bindings + .iter() + .filter(|binding| binding.cert_id == certificate.id) + .cloned() + .collect(); + certificate.channel_error = channel_errors.get(&certificate.id).cloned(); + certificate + }) + .collect()) + } + + /// Last failure per certificate, from any attached channel. + /// + /// A `Pending` certificate is not broken, it is waiting — and the reason it + /// is still waiting is the failure of a channel. Keeping the two together in + /// one response is what lets the UI say why instead of showing an empty row. + async fn latest_source_errors( + &self, + tenant: &str, + ) -> Result> { + let conn = self.lock().await; + let mut stmt = conn.prepare( + "SELECT certificate_id, last_error + FROM certificate_source + WHERE tenant_id = ?1 + AND certificate_id IS NOT NULL + AND last_status = 'failed' + AND last_error IS NOT NULL + ORDER BY COALESCE(last_run_at, ''), id", + )?; + let mut map = std::collections::HashMap::new(); + for row in stmt.query_map(params![tenant], |row| { + Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?)) + })? { + let (certificate_id, error) = row?; + // Last writer wins by ordering, so a newer attempt replaces an older. + map.insert(certificate_id, error); + } + Ok(map) + } + + /// One certificate, scoped to a tenant. + pub async fn certificate_in_tenant(&self, id: i64, tenant: &str) -> Result { + let row = { + let conn = self.lock().await; + conn.query_row( + &format!( + "{} WHERE c.id = ?1 AND c.tenant_id = ?2", + CertificateRow::SELECT + ), + params![id, tenant], + CertificateRow::from_row, + ) + .optional()? + } + .ok_or(ClientError::RecordNotFound { + table: "certificate".into(), + id, + })?; + + let mut certificate = row.into_certificate(); + certificate.bindings = self.list_certificate_bindings(tenant, Some(id)).await?; + certificate.channel_error = self.latest_source_errors(tenant).await?.get(&id).cloned(); + Ok(certificate) + } + + /// One certificate by domain, scoped to a tenant. + /// + /// The pull path uses this to find (or create) the identity a channel + /// belongs to before it has fetched anything. + pub async fn certificate_for_domain(&self, tenant: &str, domain: &str) -> Result { + let row = { + let conn = self.lock().await; + conn.query_row( + &format!( + "{} WHERE c.tenant_id = ?1 AND c.primary_domain = ?2", + CertificateRow::SELECT + ), + params![tenant, domain], + CertificateRow::from_row, + ) + .optional()? + } + .ok_or(ClientError::RecordNotFound { + table: "certificate".into(), + id: 0, + })?; + + let mut certificate = row.into_certificate(); + certificate.bindings = self + .list_certificate_bindings(tenant, Some(certificate.id)) + .await?; + Ok(certificate) + } + + /// Create a certificate identity with no material, returning its row. + /// + /// Idempotent: an existing identity with the same domain is returned as-is. + pub async fn ensure_certificate(&self, tenant: &str, domain: &str) -> Result { + let domain = domain.trim().to_ascii_lowercase(); + if domain.is_empty() { + return Err(ClientError::ConfigValidation( + "certificate domain cannot be empty".into(), + )); + } + let now = chrono::Utc::now().to_rfc3339(); + { + let conn = self.lock().await; + conn.execute( + "INSERT INTO certificate + (tenant_id, primary_domain, created_at, updated_at) + VALUES (?1, ?2, ?3, ?3) + ON CONFLICT(tenant_id, primary_domain) DO NOTHING", + params![tenant, domain, now], + )?; + } + self.certificate_for_domain(tenant, &domain).await + } + + /// Decrypt the newest material for `id`. Never exposed through an API + /// response. + pub async fn certificate_secrets(&self, id: i64, tenant: &str) -> Result { + let cipher = self.cert_cipher()?; + let (cert, chain, key) = { + let conn = self.lock().await; + let row: Option = conn + .query_row( + "SELECT m.enc_cert_der, m.enc_chain_der, m.enc_key_der + FROM certificate_material m + JOIN certificate c ON c.id = m.certificate_id + WHERE m.certificate_id = ?1 AND c.tenant_id = ?2 + ORDER BY m.id DESC + LIMIT 1", + params![id, tenant], + |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)), + ) + .optional()?; + match row { + Some(row) => row, + // Distinguish "no such certificate" from "no material yet": + // only the first is a 404, the second is a configuration state. + None => { + let exists: i64 = conn.query_row( + "SELECT COUNT(*) FROM certificate WHERE id = ?1 AND tenant_id = ?2", + params![id, tenant], + |row| row.get(0), + )?; + if exists == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate".into(), + id, + }); + } + return Err(ClientError::CertificateTarget(format!( + "certificate {id} has no material yet; import one or wait for its channel to fetch it" + ))); + } + } + }; + + Ok(CertificateSecrets { + cert_der: cipher.decrypt(&cert)?, + chain_der: match chain { + Some(blob) => cipher.decrypt(&blob)?, + None => Vec::new(), + }, + key_label: "PRIVATE KEY".into(), + key_der: cipher.decrypt(&key)?, + }) + } + + /// Delete a certificate, its material, and its channels. + /// + /// Refused while bindings still reference it: an apply would otherwise point + /// at a certificate that no longer exists. + pub async fn delete_certificate(&self, id: i64, tenant: &str) -> Result<()> { + let conn = self.lock().await; + let bindings: i64 = conn.query_row( + "SELECT COUNT(*) FROM certificate_binding WHERE cert_id = ?1 AND tenant_id = ?2", + params![id, tenant], + |row| row.get(0), + )?; + if bindings > 0 { + return Err(ClientError::CertificateStillBound { + count: bindings as usize, + }); + } + + // Material and channels cascade; the binding check above is the only + // thing FK RESTRICT would have stopped us on. + let deleted = conn.execute( + "DELETE FROM certificate WHERE id = ?1 AND tenant_id = ?2", + params![id, tenant], + )?; + if deleted == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate".into(), + id, + }); + } + Ok(()) + } + + /// Bind a certificate to a proxy. One proxy holds at most one certificate. + pub async fn bind_certificate( + &self, + cert_id: i64, + proxy_id: i64, + tenant: &str, + ) -> Result { + // Resolve the target before inserting so a mismatched plugin is reported + // as a certificate error rather than as a database constraint failure. + let proxy = self.get_proxy_in_tenant(proxy_id, tenant).await?; + let plugin = proxy.plugin_config.as_ref().ok_or_else(|| { + ClientError::CertificateTarget( + "the proxy has no FRP plugin configuration; set type = https2http and the certificate paths first" + .into(), + ) + })?; + target_from_plugin_config(plugin)?; + + let now = chrono::Utc::now().to_rfc3339(); + let id = { + let conn = self.lock().await; + let exists: i64 = conn.query_row( + "SELECT COUNT(*) FROM certificate WHERE id = ?1 AND tenant_id = ?2", + params![cert_id, tenant], + |row| row.get(0), + )?; + if exists == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate".into(), + id: cert_id, + }); + } + + conn.execute( + "INSERT INTO certificate_binding (tenant_id, cert_id, proxy_id, created_at, updated_at) + VALUES (?1, ?2, ?3, ?4, ?4) + ON CONFLICT(proxy_id) DO UPDATE SET + cert_id = excluded.cert_id, + applied_fingerprint = NULL, + applied_at = NULL, + last_error = NULL, + updated_at = excluded.updated_at", + params![tenant, cert_id, proxy_id, now], + )?; + conn.query_row( + "SELECT id FROM certificate_binding WHERE proxy_id = ?1", + params![proxy_id], + |row| row.get(0), + )? + }; + + self.certificate_binding(id, tenant).await + } + + /// Bindings for one certificate, or for the whole tenant when `cert_id` is None. + pub async fn list_certificate_bindings( + &self, + tenant: &str, + cert_id: Option, + ) -> Result> { + let conn = self.lock().await; + let sql = "SELECT b.id, b.tenant_id, b.cert_id, b.proxy_id, + COALESCE(p.name, ''), b.applied_fingerprint, b.applied_at, + b.last_error, b.created_at, b.updated_at + FROM certificate_binding b + LEFT JOIN local_proxy p ON p.id = b.proxy_id + WHERE b.tenant_id = ?1 AND (?2 IS NULL OR b.cert_id = ?2) + ORDER BY b.id"; + let mut stmt = conn.prepare(sql)?; + let bindings = stmt + .query_map(params![tenant, cert_id], |row| { + Ok(CertificateBinding { + id: row.get(0)?, + tenant_id: row.get(1)?, + cert_id: row.get(2)?, + proxy_id: row.get(3)?, + proxy_name: row.get(4)?, + applied_fingerprint: row.get(5)?, + applied_at: row.get(6)?, + last_error: row.get(7)?, + created_at: row.get(8)?, + updated_at: row.get(9)?, + }) + })? + .collect::, _>>()?; + Ok(bindings) + } + + pub async fn certificate_binding(&self, id: i64, tenant: &str) -> Result { + let conn = self.lock().await; + conn.query_row( + "SELECT b.id, b.tenant_id, b.cert_id, b.proxy_id, + COALESCE(p.name, ''), b.applied_fingerprint, b.applied_at, + b.last_error, b.created_at, b.updated_at + FROM certificate_binding b + LEFT JOIN local_proxy p ON p.id = b.proxy_id + WHERE b.id = ?1 AND b.tenant_id = ?2", + params![id, tenant], + |row| { + Ok(CertificateBinding { + id: row.get(0)?, + tenant_id: row.get(1)?, + cert_id: row.get(2)?, + proxy_id: row.get(3)?, + proxy_name: row.get(4)?, + applied_fingerprint: row.get(5)?, + applied_at: row.get(6)?, + last_error: row.get(7)?, + created_at: row.get(8)?, + updated_at: row.get(9)?, + }) + }, + ) + .optional()? + .ok_or(ClientError::RecordNotFound { + table: "certificate_binding".into(), + id, + }) + } + + /// Remove a binding. `remove_files` deletes what apply wrote, and only when + /// it still carries the fingerprint we wrote (never a user's file). + pub async fn unbind_certificate( + &self, + id: i64, + tenant: &str, + remove_files: bool, + ) -> Result<()> { + let binding = self.certificate_binding(id, tenant).await?; + let proxy = self + .get_proxy_in_tenant(binding.proxy_id, tenant) + .await + .ok(); + let certificate = self + .certificate_in_tenant(binding.cert_id, tenant) + .await + .ok(); + + if remove_files { + if let (Some(proxy), Some(certificate)) = (proxy.as_ref(), certificate.as_ref()) { + if let Some(plugin) = proxy.plugin_config.as_ref() { + if let Ok(target) = target_from_plugin_config(plugin) { + if binding.applied_fingerprint.as_deref() + == Some(certificate.fingerprint_sha256.as_str()) + { + for path in [&target.crt_path, &target.key_path] { + if let Err(error) = std::fs::remove_file(path) { + if error.kind() != std::io::ErrorKind::NotFound { + tracing::warn!(path = %path.display(), %error, "failed to remove certificate file"); + } + } + } + } + } + } + } + } + + let conn = self.lock().await; + let deleted = conn.execute( + "DELETE FROM certificate_binding WHERE id = ?1 AND tenant_id = ?2", + params![id, tenant], + )?; + if deleted == 0 { + return Err(ClientError::RecordNotFound { + table: "certificate_binding".into(), + id, + }); + } + Ok(()) + } + + /// Write this certificate to every bound proxy's declared paths. + /// + /// Each binding succeeds or fails on its own: one unwritable target must not + /// undo a target that is already correct. + pub async fn apply_certificate( + &self, + id: i64, + tenant: &str, + ) -> Result<(Vec, Vec<(i64, String)>)> { + let certificate = self.certificate_in_tenant(id, tenant).await?; + + // Writing an expired (or not-yet-valid) bundle would replace a working + // file with one every client rejects. Refuse here, once, so both the API + // and the automatic renewal path get the same answer. + if !certificate.usable { + let reason = certificate + .status_reason + .clone() + .unwrap_or_else(|| format!("certificate is {}", certificate.status.as_str())); + return Err(ClientError::CertificateTarget(format!( + "refusing to write {} to disk: {reason}", + certificate.primary_domain + ))); + } + + let secrets = self.certificate_secrets(id, tenant).await?; + let bindings = certificate.bindings.clone(); + + let mut applied = Vec::new(); + let mut failed = Vec::new(); + + for binding in bindings { + let result = self + .apply_one_binding(&binding, &secrets, &certificate.fingerprint_sha256) + .await; + match result { + Ok(()) => applied.push(binding.id), + Err(error) => { + tracing::warn!( + binding_id = binding.id, + proxy_id = binding.proxy_id, + %error, + "Certificate apply failed" + ); + self.record_binding_result(binding.id, None, Some(&error.to_string())) + .await?; + failed.push((binding.id, error.to_string())); + } + } + } + + Ok((applied, failed)) + } + + /// Apply every certificate whose material changed after its files were + /// written. + /// + /// Called right after a channel imports new material: the operator already + /// bound the certificate, so a renewal should reach the proxies without a + /// second manual click. Bindings that already carry the current fingerprint + /// are skipped, which makes this idempotent. + pub async fn apply_stale_certificates(&self, tenant: &str) -> Result { + let stale: Vec = { + let conn = self.lock().await; + let mut stmt = conn.prepare( + "SELECT DISTINCT c.id + FROM certificate c + JOIN certificate_material m ON m.certificate_id = c.id + JOIN certificate_binding b ON b.cert_id = c.id + WHERE c.tenant_id = ?1 + AND (b.applied_fingerprint IS NULL OR b.applied_fingerprint != m.fingerprint_sha256) + AND m.id = (SELECT id FROM certificate_material + WHERE certificate_id = c.id + ORDER BY id DESC LIMIT 1)", + )?; + let rows = stmt + .query_map(params![tenant], |row| row.get(0))? + .collect::, _>>()?; + rows + }; + + let mut applied = 0; + for id in stale { + match self.apply_certificate(id, tenant).await { + Ok((ok, _failed)) => applied += ok.len(), + Err(error) => { + tracing::warn!(certificate_id = id, %error, "automatic apply failed") + } + } + } + Ok(applied) + } + + async fn apply_one_binding( + &self, + binding: &CertificateBinding, + secrets: &CertificateSecrets, + fingerprint: &str, + ) -> Result<()> { + let proxy = self + .get_proxy_in_tenant(binding.proxy_id, binding.tenant_id.as_str()) + .await?; + let plugin = proxy.plugin_config.as_ref().ok_or_else(|| { + ClientError::CertificateTarget(format!( + "proxy '{}' has no plugin configuration", + proxy.name + )) + })?; + let target = target_from_plugin_config(plugin)?; + + let files = write_material(&target, secrets)?; + files.commit(); + + self.record_binding_result(binding.id, Some(fingerprint), None) + .await + } + + async fn record_binding_result( + &self, + binding_id: i64, + fingerprint: Option<&str>, + error: Option<&str>, + ) -> Result<()> { + let now = chrono::Utc::now().to_rfc3339(); + let conn = self.lock().await; + conn.execute( + "UPDATE certificate_binding + SET applied_fingerprint = COALESCE(?1, applied_fingerprint), + applied_at = CASE WHEN ?1 IS NULL THEN applied_at ELSE ?2 END, + last_error = ?3, + updated_at = ?2 + WHERE id = ?4", + params![fingerprint, now, error, binding_id], + )?; + Ok(()) + } +} + +impl Database { + /// Profiles whose enabled bindings include a proxy bound to this certificate. + /// + /// The apply path needs it to restart exactly the processes that serve the + /// rewritten files, and the pull path needs the same answer as the API + /// handler — so it lives with the data rather than in either caller. + pub async fn profiles_for_certificate( + &self, + certificate_id: i64, + tenant: &str, + ) -> Result> { + let conn = self.lock().await; + let mut stmt = conn.prepare( + "SELECT DISTINCT r.profile_id + FROM certificate_binding b + JOIN binding_rule r ON r.proxy_id = b.proxy_id + WHERE b.cert_id = ?1 AND b.tenant_id = ?2 AND r.enabled = 1 + ORDER BY r.profile_id", + )?; + let ids = stmt + .query_map(params![certificate_id, tenant], |row| row.get(0))? + .collect::, _>>()?; + Ok(ids) + } +} diff --git a/crates/client/src/db/environment.rs b/crates/client/src/db/environment.rs deleted file mode 100644 index 719e3ac..0000000 --- a/crates/client/src/db/environment.rs +++ /dev/null @@ -1,367 +0,0 @@ -//! Deployment environments and profile membership. - -use super::Database; -use crate::error::{ClientError, Result}; -use rusqlite::{params, OptionalExtension}; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -#[serde(default)] -pub struct Environment { - pub id: Option, - pub name: String, - pub description: Option, - pub color: String, - pub is_default: bool, - #[serde(skip_deserializing)] - pub tenant_id: String, - #[serde(skip_deserializing)] - pub created_at: String, - #[serde(skip_deserializing)] - pub updated_at: String, -} - -impl Default for Environment { - fn default() -> Self { - Self { - id: None, - name: String::new(), - description: None, - color: "#18a058".into(), - is_default: false, - tenant_id: "default".into(), - created_at: String::new(), - updated_at: String::new(), - } - } -} - -impl Database { - pub async fn list_environments(&self) -> Result> { - let conn = self.lock().await; - let mut stmt = conn.prepare( - "SELECT id, name, description, color, is_default, tenant_id, created_at, updated_at - FROM environment ORDER BY is_default DESC, name", - )?; - let environments = stmt - .query_map([], row_to_environment)? - .collect::, _>>()?; - Ok(environments) - } - - pub async fn list_environments_for_tenant(&self, tenant: &str) -> Result> { - let conn = self.lock().await; - let mut stmt = conn.prepare("SELECT id, name, description, color, is_default, tenant_id, created_at, updated_at FROM environment WHERE tenant_id=?1 ORDER BY is_default DESC, name")?; - let environments = stmt - .query_map([tenant], row_to_environment)? - .collect::, _>>()?; - Ok(environments) - } - - pub async fn insert_environment(&self, environment: &Environment) -> Result { - validate(environment)?; - let conn = self.lock().await; - let tx = conn.unchecked_transaction()?; - let tenant_has_default: bool = tx.query_row( - "SELECT EXISTS(SELECT 1 FROM environment WHERE tenant_id=?1 AND is_default=1)", - [&environment.tenant_id], - |row| row.get(0), - )?; - let make_default = environment.is_default || !tenant_has_default; - if make_default { - tx.execute( - "UPDATE environment SET is_default = 0 WHERE tenant_id=?1", - [&environment.tenant_id], - )?; - } - let now = chrono::Utc::now().to_rfc3339(); - tx.execute( - "INSERT INTO environment (name, description, color, is_default, tenant_id, created_at, updated_at) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6)", - params![ - environment.name.trim(), - environment.description, - environment.color, - make_default as i32, - environment.tenant_id, - now, - ], - ) - .map_err(|e| { - if e.to_string().contains("UNIQUE") { - ClientError::RecordAlreadyExists(format!( - "environment '{}' already exists", - environment.name - )) - } else { - ClientError::DatabaseQuery(e) - } - })?; - let id = tx.last_insert_rowid(); - tx.commit()?; - Ok(id) - } - - pub async fn update_environment(&self, environment: &Environment) -> Result<()> { - validate(environment)?; - let id = environment - .id - .ok_or_else(|| ClientError::ConfigValidation("environment id is required".into()))?; - let conn = self.lock().await; - let tx = conn.unchecked_transaction()?; - let current_default: Option = tx - .query_row( - "SELECT id FROM environment WHERE tenant_id=?1 AND is_default=1", - [&environment.tenant_id], - |row| row.get(0), - ) - .optional()?; - if current_default == Some(id) && !environment.is_default { - return Err(ClientError::ConfigValidation( - "select another default environment before demoting this one".into(), - )); - } - if environment.is_default { - tx.execute( - "UPDATE environment SET is_default = 0 WHERE tenant_id=?1 AND id <> ?2", - params![environment.tenant_id, id], - )?; - } - let changed = tx.execute( - "UPDATE environment SET name=?1, description=?2, color=?3, is_default=?4, updated_at=?5 WHERE id=?6", - params![environment.name.trim(), environment.description, environment.color, environment.is_default as i32, chrono::Utc::now().to_rfc3339(), id], - )?; - if changed == 0 { - return Err(ClientError::RecordNotFound { - table: "environment".into(), - id, - }); - } - tx.commit()?; - Ok(()) - } - - pub async fn environment_belongs_to_tenant(&self, id: i64, tenant: &str) -> Result { - let conn = self.lock().await; - conn.query_row( - "SELECT EXISTS(SELECT 1 FROM environment WHERE id=?1 AND tenant_id=?2)", - params![id, tenant], - |row| row.get(0), - ) - .map_err(ClientError::DatabaseQuery) - } - - /// Whether a profile belongs to `tenant`. - /// - /// The tenant lives on the profile row; membership through `environment` was - /// the previous indirect path and required a two-hop join at every call site. - pub async fn profile_belongs_to_tenant(&self, profile_id: i64, tenant: &str) -> Result { - let conn = self.lock().await; - conn.query_row( - "SELECT EXISTS(SELECT 1 FROM frps_profile WHERE id = ?1 AND tenant_id = ?2)", - params![profile_id, tenant], - |row| row.get(0), - ) - .map_err(ClientError::DatabaseQuery) - } - - pub async fn default_environment_for_tenant(&self, tenant: &str) -> Result { - let conn = self.lock().await; - conn.query_row( - "SELECT id FROM environment WHERE tenant_id=?1 AND is_default=1", - [tenant], - |row| row.get(0), - ) - .map_err(ClientError::DatabaseQuery) - } - - pub async fn delete_environment(&self, id: i64) -> Result<()> { - let conn = self.lock().await; - let tx = conn.unchecked_transaction()?; - let environment: Option<(bool, String)> = tx - .query_row( - "SELECT is_default, tenant_id FROM environment WHERE id=?1", - [id], - |row| Ok((row.get(0)?, row.get(1)?)), - ) - .optional()?; - let tenant = match environment { - None => { - return Err(ClientError::RecordNotFound { - table: "environment".into(), - id, - }) - } - Some((true, _)) => { - return Err(ClientError::ConfigValidation( - "the default environment cannot be deleted".into(), - )) - } - Some((false, tenant)) => tenant, - }; - let default_id: i64 = tx.query_row( - "SELECT id FROM environment WHERE tenant_id=?1 AND is_default=1", - [&tenant], - |row| row.get(0), - )?; - tx.execute( - "UPDATE profile_environment SET environment_id=?1 WHERE environment_id=?2", - params![default_id, id], - )?; - tx.execute("DELETE FROM environment WHERE id=?1", [id])?; - tx.commit()?; - Ok(()) - } - - pub async fn set_profile_environment( - &self, - profile_id: i64, - environment_id: i64, - ) -> Result<()> { - let conn = self.lock().await; - conn.execute( - "INSERT INTO profile_environment (profile_id, environment_id) VALUES (?1, ?2) - ON CONFLICT(profile_id) DO UPDATE SET environment_id=excluded.environment_id", - params![profile_id, environment_id], - ) - .map_err(ClientError::DatabaseQuery)?; - // Assigning a profile to a tenant-scoped environment assigns the profile - // to that tenant; ownership otherwise lives on the profile row. - conn.execute( - "UPDATE frps_profile - SET tenant_id = COALESCE((SELECT tenant_id FROM environment WHERE id = ?2), tenant_id), - updated_at = datetime('now') - WHERE id = ?1", - params![profile_id, environment_id], - ) - .map_err(ClientError::DatabaseQuery)?; - Ok(()) - } - - pub async fn profile_environment_id(&self, profile_id: i64) -> Result { - let conn = self.lock().await; - conn.query_row( - "SELECT environment_id FROM profile_environment WHERE profile_id=?1", - [profile_id], - |row| row.get(0), - ) - .map_err(ClientError::DatabaseQuery) - } - - pub async fn environment_profile_ids(&self, environment_id: i64) -> Result> { - let conn = self.lock().await; - let mut stmt = conn.prepare( - "SELECT profile_id FROM profile_environment WHERE environment_id=?1 ORDER BY profile_id", - )?; - let ids = stmt - .query_map([environment_id], |row| row.get(0))? - .collect::, _>>()?; - Ok(ids) - } -} - -fn validate(environment: &Environment) -> Result<()> { - if environment.name.trim().is_empty() || environment.name.len() > 64 { - return Err(ClientError::ConfigValidation( - "environment name must be 1-64 characters".into(), - )); - } - if !environment.color.starts_with('#') - || environment.color.len() != 7 - || !environment.color[1..] - .chars() - .all(|c| c.is_ascii_hexdigit()) - { - return Err(ClientError::ConfigValidation( - "environment color must be #RRGGBB".into(), - )); - } - Ok(()) -} - -fn row_to_environment(row: &rusqlite::Row<'_>) -> rusqlite::Result { - Ok(Environment { - id: row.get(0)?, - name: row.get(1)?, - description: row.get(2)?, - color: row.get(3)?, - is_default: row.get(4)?, - tenant_id: row.get(5)?, - created_at: row.get(6)?, - updated_at: row.get(7)?, - }) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::db::migrate; - - #[tokio::test] - async fn environment_lifecycle_reassigns_profiles_on_delete() { - let db = Database::open(":memory:").await.unwrap(); - migrate::run(&*db.lock().await).unwrap(); - let profile_id = db - .insert_profile(&crate::config::model::FrpsProfile::default()) - .await - .unwrap(); - let environment_id = db - .insert_environment(&Environment { - name: "Production".into(), - ..Environment::default() - }) - .await - .unwrap(); - db.set_profile_environment(profile_id, environment_id) - .await - .unwrap(); - assert_eq!( - db.profile_environment_id(profile_id).await.unwrap(), - environment_id - ); - db.delete_environment(environment_id).await.unwrap(); - assert_eq!(db.profile_environment_id(profile_id).await.unwrap(), 1); - } - - #[tokio::test] - async fn tenants_have_independent_defaults_and_profile_membership() { - let db = Database::open(":memory:").await.unwrap(); - migrate::run(&*db.lock().await).unwrap(); - let acme_environment = db - .insert_environment(&Environment { - name: "Acme production".into(), - tenant_id: "acme".into(), - ..Environment::default() - }) - .await - .unwrap(); - assert_eq!( - db.default_environment_for_tenant("default").await.unwrap(), - 1 - ); - assert_eq!( - db.default_environment_for_tenant("acme").await.unwrap(), - acme_environment - ); - - let profile_id = db - .insert_profile(&crate::config::model::FrpsProfile::default()) - .await - .unwrap(); - db.set_profile_environment(profile_id, acme_environment) - .await - .unwrap(); - assert!(db - .profile_belongs_to_tenant(profile_id, "acme") - .await - .unwrap()); - assert!(!db - .profile_belongs_to_tenant(profile_id, "default") - .await - .unwrap()); - assert_eq!( - db.list_environments_for_tenant("acme").await.unwrap().len(), - 1 - ); - } -} diff --git a/crates/client/src/db/migrate.rs b/crates/client/src/db/migrate.rs index 70e0722..923ac18 100644 --- a/crates/client/src/db/migrate.rs +++ b/crates/client/src/db/migrate.rs @@ -8,7 +8,7 @@ use rusqlite::Connection; use sha2::{Digest, Sha256}; /// 当前最新的 schema 版本 -const LATEST_VERSION: i32 = 20; +const LATEST_VERSION: i32 = 24; /// 运行所有待执行的迁移 /// @@ -103,6 +103,10 @@ fn apply_migration(conn: &Connection, version: i32) -> Result<()> { 18 => migrate_v18(&tx)?, 19 => migrate_v19(&tx)?, 20 => migrate_v20(&tx)?, + 21 => migrate_v21(&tx)?, + 22 => migrate_v22(&tx)?, + 23 => migrate_v23(&tx)?, + 24 => migrate_v24(&tx)?, _ => { return Err(ClientError::DatabaseMigration(format!( "Unknown migration version: {version}" @@ -1035,6 +1039,408 @@ fn migrate_v20(tx: &rusqlite::Transaction) -> Result<()> { Ok(()) } +/// V21: certificate management. +/// +/// These tables belong to the platform, not to FRP: nothing here is serialized +/// into a generated TOML. The only FRP-facing effect is that an apply writes the +/// file named by a proxy's `plugin.crtPath` / `keyPath`, which are FRP-native +/// fields (ARCH-004 / CFG-001). +/// +/// Certificate bodies and keys are stored as XChaCha20-Poly1305 blobs, not as +/// PEM text, so the database never contains a readable private key. +fn migrate_v21(tx: &rusqlite::Transaction) -> Result<()> { + tx.execute( + "CREATE TABLE IF NOT EXISTS certificate ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + primary_domain TEXT NOT NULL, + sans_json TEXT NOT NULL DEFAULT '[]', + issuer TEXT NOT NULL DEFAULT '', + serial TEXT NOT NULL DEFAULT '', + fingerprint_sha256 TEXT NOT NULL, + key_algo TEXT NOT NULL DEFAULT '', + not_before TEXT NOT NULL, + not_after TEXT NOT NULL, + source TEXT NOT NULL, + source_detail TEXT, + enc_cert_der BLOB NOT NULL, + enc_chain_der BLOB, + enc_key_der BLOB NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + )", + [], + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to create certificate: {e}")))?; + + tx.execute( + "CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_fingerprint + ON certificate (tenant_id, fingerprint_sha256)", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to index certificate fingerprint: {e}")) + })?; + + tx.execute( + "CREATE INDEX IF NOT EXISTS idx_certificate_expiry ON certificate (tenant_id, not_after)", + [], + ) + .ok(); + + tx.execute( + "CREATE TABLE IF NOT EXISTS certificate_binding ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + cert_id INTEGER NOT NULL, + proxy_id INTEGER NOT NULL, + applied_fingerprint TEXT, + applied_at TEXT, + last_error TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY (cert_id) REFERENCES certificate(id) ON DELETE RESTRICT, + FOREIGN KEY (proxy_id) REFERENCES local_proxy(id) ON DELETE CASCADE + )", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create certificate_binding: {e}")) + })?; + + // One proxy holds one certificate: a second certificate for the same proxy + // is an import mistake, not a configuration to reconcile. + tx.execute( + "CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_binding_proxy + ON certificate_binding (proxy_id)", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to index certificate bindings: {e}")) + })?; + + tx.execute( + "CREATE TABLE IF NOT EXISTS cert_ingest_token ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + name TEXT NOT NULL, + token_sha256 TEXT NOT NULL, + domains_json TEXT NOT NULL DEFAULT '[]', + expires_at TEXT, + revoked_at TEXT, + last_used_at TEXT, + use_count INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL + )", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create cert_ingest_token: {e}")) + })?; + + tx.execute( + "CREATE UNIQUE INDEX IF NOT EXISTS idx_cert_ingest_token_sha + ON cert_ingest_token (token_sha256)", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to index ingest token digest: {e}")) + })?; + + tx.execute( + "CREATE TABLE IF NOT EXISTS cert_pull_source ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + name TEXT NOT NULL, + kind TEXT NOT NULL, + base_url TEXT NOT NULL, + domain TEXT NOT NULL, + enc_secret BLOB, + enabled INTEGER NOT NULL DEFAULT 1, + last_run_at TEXT, + last_status TEXT, + last_error TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + )", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create cert_pull_source: {e}")) + })?; + + tracing::info!("V21 Migration: Add certificate management tables"); + Ok(()) +} + +/// V22: platform settings that an operator changes at runtime. +/// +/// A key/value table rather than a column per setting: these are switches read by +/// long-lived tasks (the ingest listener), and adding another one must not mean +/// another migration. +fn migrate_v22(tx: &rusqlite::Transaction) -> Result<()> { + tx.execute( + "CREATE TABLE IF NOT EXISTS platform_setting ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL, + updated_at TEXT NOT NULL + )", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create platform_setting: {e}")) + })?; + + tracing::info!("V22 Migration: Add platform settings"); + Ok(()) +} + +/// V23: certificate identity by domain, material split out, channels unified. +/// +/// The v21 shape made the fingerprint the identity of a certificate, which is +/// wrong for renewal: every reissue is a new fingerprint, so "replace the +/// certificate for example.com" created a new row instead of updating one. This +/// migration makes `(tenant_id, primary_domain)` the identity and moves the +/// material — fingerprint, validity, encrypted bodies — into +/// `certificate_material`, one row per issued bundle. +/// +/// Channels (push token, Certd pull, plain URL pull) become rows of +/// `certificate_source` attached to a certificate, so a certificate can exist +/// before it has any material and its channels can change without re-importing. +/// Legacy rows from `cert_ingest_token` / `cert_pull_source` are carried over +/// with `certificate_id = NULL`, which keeps them working as unbound channels. +fn migrate_v23(tx: &rusqlite::Transaction) -> Result<()> { + // The rebuild below deletes rows that `certificate_binding` and + // `certificate_material` point at, so FK checks must be deferred to commit. + tx.execute_batch("PRAGMA defer_foreign_keys = ON") + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to defer foreign keys: {e}")) + })?; + + // --- 1. Material moves to its own table, one row per issued bundle. ------ + tx.execute_batch( + "CREATE TABLE IF NOT EXISTS certificate_material ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + certificate_id INTEGER NOT NULL, + fingerprint_sha256 TEXT NOT NULL, + sans_json TEXT NOT NULL DEFAULT '[]', + issuer TEXT NOT NULL DEFAULT '', + serial TEXT NOT NULL DEFAULT '', + key_algo TEXT NOT NULL DEFAULT '', + not_before TEXT NOT NULL, + not_after TEXT NOT NULL, + source TEXT NOT NULL DEFAULT '', + source_detail TEXT, + enc_cert_der BLOB NOT NULL, + enc_chain_der BLOB, + enc_key_der BLOB NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY (certificate_id) REFERENCES certificate(id) ON DELETE CASCADE + )", + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create certificate_material: {e}")) + })?; + + tx.execute( + "INSERT INTO certificate_material + (tenant_id, certificate_id, fingerprint_sha256, sans_json, issuer, serial, key_algo, + not_before, not_after, source, source_detail, + enc_cert_der, enc_chain_der, enc_key_der, created_at, updated_at) + SELECT tenant_id, id, fingerprint_sha256, sans_json, issuer, serial, key_algo, + not_before, not_after, source, source_detail, + enc_cert_der, enc_chain_der, enc_key_der, created_at, updated_at + FROM certificate", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to copy certificate material: {e}")) + })?; + + // --- 2. Collapse rows that share a domain. ------------------------------ + // A renewal used to append a row; here they become one identity with several + // material versions, and the newest is what the UI reports. + tx.execute_batch( + "CREATE TABLE _cert_dedupe (old_id INTEGER PRIMARY KEY, keep_id INTEGER NOT NULL); + INSERT INTO _cert_dedupe + SELECT id, MIN(id) OVER (PARTITION BY tenant_id, primary_domain) FROM certificate;", + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to map duplicate certificates: {e}")) + })?; + + tx.execute( + "UPDATE certificate_material SET certificate_id = + (SELECT keep_id FROM _cert_dedupe WHERE old_id = certificate_material.certificate_id)", + [], + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to repoint material: {e}")))?; + + tx.execute( + "UPDATE certificate_binding SET cert_id = + (SELECT keep_id FROM _cert_dedupe WHERE old_id = certificate_binding.cert_id)", + [], + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to repoint bindings: {e}")))?; + + tx.execute( + "DELETE FROM certificate WHERE id NOT IN (SELECT keep_id FROM _cert_dedupe)", + [], + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to collapse certificate rows: {e}")) + })?; + + tx.execute_batch("DROP TABLE _cert_dedupe").map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to drop the dedupe map: {e}")) + })?; + + // --- 3. `certificate` keeps identity only. ----------------------------- + // The fingerprint and expiry indexes must go before their columns can be + // dropped; SQLite refuses a DROP COLUMN that an index still references. + tx.execute_batch( + "DROP INDEX IF EXISTS idx_certificate_fingerprint; + DROP INDEX IF EXISTS idx_certificate_expiry;", + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to drop certificate indexes: {e}")) + })?; + + for column in [ + "fingerprint_sha256", + "sans_json", + "issuer", + "serial", + "key_algo", + "not_before", + "not_after", + "source", + "source_detail", + "enc_cert_der", + "enc_chain_der", + "enc_key_der", + ] { + tx.execute(&format!("ALTER TABLE certificate DROP COLUMN {column}"), []) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to drop certificate.{column}: {e}")) + })?; + } + + tx.execute_batch( + "CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_domain + ON certificate (tenant_id, primary_domain); + CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_material_fingerprint + ON certificate_material (certificate_id, fingerprint_sha256); + CREATE INDEX IF NOT EXISTS idx_certificate_material_recent + ON certificate_material (certificate_id, not_after);", + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to index certificates: {e}")))?; + + // --- 4. One table for every way a certificate gets updated. ------------- + tx.execute_batch( + "CREATE TABLE IF NOT EXISTS certificate_source ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tenant_id TEXT NOT NULL DEFAULT 'default', + certificate_id INTEGER, + kind TEXT NOT NULL, + name TEXT NOT NULL, + domain TEXT NOT NULL DEFAULT '', + base_url TEXT NOT NULL DEFAULT '', + enc_secret BLOB, + domains_json TEXT NOT NULL DEFAULT '[]', + token_sha256 TEXT, + expires_at TEXT, + revoked_at TEXT, + last_used_at TEXT, + use_count INTEGER NOT NULL DEFAULT 0, + is_primary INTEGER NOT NULL DEFAULT 0, + enabled INTEGER NOT NULL DEFAULT 1, + last_run_at TEXT, + last_status TEXT, + last_error TEXT, + next_run_at TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY (certificate_id) REFERENCES certificate(id) ON DELETE CASCADE + ); + CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_source_primary + ON certificate_source (certificate_id) WHERE is_primary = 1; + CREATE UNIQUE INDEX IF NOT EXISTS idx_certificate_source_token + ON certificate_source (tenant_id, token_sha256) WHERE token_sha256 IS NOT NULL; + CREATE INDEX IF NOT EXISTS idx_certificate_source_certificate + ON certificate_source (tenant_id, certificate_id);", + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to create certificate_source: {e}")) + })?; + + // Pull sources keep their behaviour; the domain decides whether they are + // already bound to an existing certificate. + tx.execute( + "INSERT INTO certificate_source + (tenant_id, certificate_id, kind, name, domain, base_url, enc_secret, + is_primary, enabled, last_run_at, last_status, last_error, created_at, updated_at) + SELECT s.tenant_id, + (SELECT c.id FROM certificate c + WHERE c.tenant_id = s.tenant_id AND c.primary_domain = s.domain), + s.kind, s.name, s.domain, s.base_url, s.enc_secret, + 0, s.enabled, s.last_run_at, s.last_status, s.last_error, + s.created_at, s.updated_at + FROM cert_pull_source s", + [], + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to move pull sources: {e}")))?; + + // Push tokens keep working as unbound channels: their allowlist is stored in + // `domains_json` until they are attached to a certificate. + tx.execute( + "INSERT INTO certificate_source + (tenant_id, certificate_id, kind, name, domains_json, token_sha256, + expires_at, revoked_at, last_used_at, use_count, is_primary, enabled, + created_at, updated_at) + SELECT tenant_id, NULL, 'push', name, domains_json, token_sha256, + expires_at, revoked_at, last_used_at, use_count, 0, 1, + created_at, created_at + FROM cert_ingest_token", + [], + ) + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to move ingest tokens: {e}")))?; + + tx.execute_batch( + "DROP TABLE IF EXISTS cert_pull_source; + DROP TABLE IF EXISTS cert_ingest_token;", + ) + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to drop legacy channel tables: {e}")) + })?; + + tracing::info!("V23 Migration: certificate identity by domain, channels unified"); + Ok(()) +} + +/// V24: remove deployment environments. +/// +/// The platform is single-user: one daemon, one SQLite file, one operator. An +/// environment was a second grouping axis on top of that, and it never worked as +/// one — the UI could switch it but not create it, and switching also rewrote +/// the profile's `tenant_id`, silently moving a server between tenants. +/// +/// `tenant_id` survives on `frps_profile` and `local_proxy` because it still +/// separates API tokens; nothing here removes that. +fn migrate_v24(tx: &rusqlite::Transaction) -> Result<()> { + // Dependants first: the mapping table references `environment`. + tx.execute_batch("DROP TABLE IF EXISTS profile_environment;") + .map_err(|e| { + ClientError::DatabaseMigration(format!("Failed to drop profile_environment: {e}")) + })?; + tx.execute_batch("DROP TABLE IF EXISTS environment;") + .map_err(|e| ClientError::DatabaseMigration(format!("Failed to drop environment: {e}")))?; + tracing::info!("V24 Migration: remove deployment environments"); + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -1059,6 +1465,184 @@ mod tests { assert_eq!(table_count, 3); } + /// Upgrading a v21/v22 database must collapse same-domain rows into one + /// identity, keep the newest material, and carry old channels over. + #[test] + fn v23_merges_same_domain_certificates_and_keeps_channels() { + let conn = Connection::open_in_memory().unwrap(); + conn.pragma_update(None, "foreign_keys", "ON").unwrap(); + ensure_meta_table(&conn).unwrap(); + for version in 1..=22 { + apply_migration(&conn, version).unwrap(); + } + + // Two certificates for one domain, as a renewal used to produce. + for (id, fingerprint, not_after) in [ + (1, "old-fingerprint", "2026-01-01T00:00:00+00:00"), + (2, "new-fingerprint", "2027-01-01T00:00:00+00:00"), + ] { + conn.execute( + "INSERT INTO certificate + (id, tenant_id, primary_domain, sans_json, issuer, serial, + fingerprint_sha256, key_algo, not_before, not_after, + source, source_detail, enc_cert_der, enc_chain_der, enc_key_der, + created_at, updated_at) + VALUES (?1, 'default', 'a.example.com', '[\"a.example.com\"]', 'CA', 's', + ?2, 'rsa', '2025-01-01T00:00:00+00:00', ?3, 'manual', NULL, + x'01', NULL, x'02', '2025-01-01T00:00:00+00:00', + '2025-01-01T00:00:00+00:00')", + rusqlite::params![id, fingerprint, not_after], + ) + .unwrap(); + } + // A proxy + binding on the older row, which must follow the merge. + conn.execute( + "INSERT INTO local_proxy (id, name, proxy_type, local_ip, local_port, + created_at, updated_at) + VALUES (1, 'web', 'https', '127.0.0.1', 80, 'x', 'x')", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO certificate_binding (tenant_id, cert_id, proxy_id, created_at, updated_at) + VALUES ('default', 1, 1, 'x', 'x')", + [], + ) + .unwrap(); + // A legacy token and a legacy pull source. + conn.execute( + "INSERT INTO cert_ingest_token (tenant_id, name, token_sha256, domains_json, created_at) + VALUES ('default', 'certd', 'abc123', '[\"a.example.com\"]', 'x')", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO cert_pull_source (tenant_id, name, kind, base_url, domain, created_at, updated_at) + VALUES ('default', 'pull', 'certd', 'https://certd', 'a.example.com', 'x', 'x')", + [], + ) + .unwrap(); + + apply_migration(&conn, 23).unwrap(); + + let identities: i64 = conn + .query_row("SELECT COUNT(*) FROM certificate", [], |row| row.get(0)) + .unwrap(); + assert_eq!(identities, 1, "one row per domain after the merge"); + + let kept_id: i64 = conn + .query_row( + "SELECT id FROM certificate WHERE primary_domain = 'a.example.com'", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(kept_id, 1, "the oldest id is the identity that survives"); + + let materials: i64 = conn + .query_row("SELECT COUNT(*) FROM certificate_material", [], |row| { + row.get(0) + }) + .unwrap(); + assert_eq!(materials, 2, "both issued bundles survive as material rows"); + + let binding_cert: i64 = conn + .query_row("SELECT cert_id FROM certificate_binding", [], |row| { + row.get(0) + }) + .unwrap(); + assert_eq!(binding_cert, kept_id, "bindings follow the merged identity"); + + let newest: String = conn + .query_row( + "SELECT fingerprint_sha256 FROM certificate_material + ORDER BY not_after DESC LIMIT 1", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(newest, "new-fingerprint"); + + // Channels survive, and the one whose domain matches is already attached. + let (pull_kind, pull_cert): (String, Option) = conn + .query_row( + "SELECT kind, certificate_id FROM certificate_source WHERE kind != 'push'", + [], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(pull_kind, "certd"); + assert_eq!( + pull_cert, + Some(kept_id), + "a matching pull source binds itself" + ); + + let push_count: i64 = conn + .query_row( + "SELECT COUNT(*) FROM certificate_source WHERE kind = 'push'", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(push_count, 1, "legacy tokens become unbound push channels"); + } + + /// V24 removes the environment tables while leaving tenant ownership on the + /// rows that still use it. + #[test] + fn v24_drops_environments_but_keeps_tenant_ownership() { + let conn = Connection::open_in_memory().unwrap(); + conn.pragma_update(None, "foreign_keys", "ON").unwrap(); + ensure_meta_table(&conn).unwrap(); + for version in 1..=23 { + apply_migration(&conn, version).unwrap(); + } + // A profile with an environment mapping, as a pre-V24 database has. + conn.execute( + "INSERT INTO frps_profile (id, name, server_addr, server_port, token, tenant_id, + created_at, updated_at) + VALUES (1, 'p', '127.0.0.1', 7000, '', 'default', 'x', 'x')", + [], + ) + .unwrap(); + conn.execute( + "INSERT INTO profile_environment (profile_id, environment_id) + SELECT 1, id FROM environment LIMIT 1", + [], + ) + .unwrap(); + + apply_migration(&conn, 24).unwrap(); + + for table in ["environment", "profile_environment"] { + let exists: i64 = conn + .query_row( + "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1", + [table], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(exists, 0, "{table} must be gone"); + } + + // Tenant ownership is what still separates API tokens, so it stays. + let tenant: String = conn + .query_row( + "SELECT tenant_id FROM frps_profile WHERE id = 1", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(tenant, "default"); + conn.query_row("SELECT tenant_id FROM local_proxy LIMIT 0", [], |_| Ok(())) + .or_else(|e| match e { + rusqlite::Error::QueryReturnedNoRows => Ok(()), + other => Err(other), + }) + .expect("local_proxy still carries tenant_id"); + } + #[test] fn test_schema_hash_detects_changes() { let conn = Connection::open_in_memory().unwrap(); diff --git a/crates/client/src/db/mod.rs b/crates/client/src/db/mod.rs index 0545fa9..cc458ab 100644 --- a/crates/client/src/db/mod.rs +++ b/crates/client/src/db/mod.rs @@ -10,15 +10,20 @@ //! - `proxy.rs` — LocalProxy CRUD //! - `binding.rs` — BindingRule CRUD //! - `visitor.rs` — LocalVisitor CRUD +//! - `certificate.rs` — Certificate metadata, encrypted material, bindings +//! - `cert_ingest.rs` — Ingest tokens and pull sources for certificate import pub mod admin_endpoint; pub mod binding; -pub mod environment; +pub mod cert_ingest; +pub mod certificate; pub mod migrate; pub mod profile; pub mod proxy; +pub mod settings; pub mod visitor; +use crate::cert::CertCipher; use crate::error::{ClientError, Result}; use rusqlite::Connection; use std::path::PathBuf; @@ -77,6 +82,21 @@ impl Database { &self.db_path } + /// Load the certificate master key stored next to the database file. + /// + /// Loading is per call rather than cached: the key file is small, reads are + /// infrequent (import / apply), and this keeps `Database` free of another + /// shared handle whose lifetime would have to be reasoned about on rotate. + pub fn cert_cipher(&self) -> Result { + let dir = self + .db_path + .parent() + .filter(|parent| !parent.as_os_str().is_empty()) + .map(std::path::Path::to_path_buf) + .unwrap_or_else(|| PathBuf::from(".")); + CertCipher::load_or_create(&dir) + } + /// 获取内部连接(用于需要在锁内执行多个操作的情况) /// /// 注意:返回的是 MutexGuard,持有期间会阻塞其他操作。 diff --git a/crates/client/src/db/profile.rs b/crates/client/src/db/profile.rs index 0c5fc64..997b62b 100644 --- a/crates/client/src/db/profile.rs +++ b/crates/client/src/db/profile.rs @@ -105,16 +105,24 @@ impl Database { } })?; let id = tx.last_insert_rowid(); - tx.execute( - "INSERT INTO profile_environment (profile_id, environment_id) - SELECT ?1, id FROM environment WHERE tenant_id = 'default' AND is_default = 1", - [id], - ) - .map_err(ClientError::DatabaseQuery)?; tx.commit().map_err(ClientError::DatabaseQuery)?; Ok(id) } + /// Whether a profile belongs to `tenant`. + /// + /// The tenant lives on the profile row itself, so this is one lookup; the + /// previous indirect path went through `environment`, which no longer exists. + pub async fn profile_belongs_to_tenant(&self, profile_id: i64, tenant: &str) -> Result { + let conn = self.lock().await; + conn.query_row( + "SELECT EXISTS(SELECT 1 FROM frps_profile WHERE id = ?1 AND tenant_id = ?2)", + rusqlite::params![profile_id, tenant], + |row| row.get(0), + ) + .map_err(ClientError::DatabaseQuery) + } + /// Move a profile to another tenant. /// /// Used by migrations and by operators reassigning a server configuration; diff --git a/crates/client/src/db/settings.rs b/crates/client/src/db/settings.rs new file mode 100644 index 0000000..70b3035 --- /dev/null +++ b/crates/client/src/db/settings.rs @@ -0,0 +1,175 @@ +//! Platform settings: the few switches an operator flips at runtime. +//! +//! Stored key/value rather than one column per setting so a new switch does not +//! need a schema change, and so a long-lived task can re-read the current value +//! instead of caching a decision made at startup. +//! +//! These settings are *not* FRP configuration: nothing here is serialized into a +//! generated TOML (ARCH-004). + +use rusqlite::{params, OptionalExtension}; + +use crate::db::Database; +use crate::error::Result; + +/// Whether the certificate ingest listener should accept pushes. +pub const CERT_INGEST_ENABLED: &str = "cert_ingest.enabled"; + +/// Loopback-or-any address the ingest listener binds. +pub const CERT_INGEST_LISTEN: &str = "cert_ingest.listen"; + +/// TCP port the ingest listener binds. Kept apart from the address so the UI can +/// offer "just change the port" without making an operator type a socket address. +pub const CERT_INGEST_PORT: &str = "cert_ingest.port"; + +/// Whether the ingest listen address may be reachable from other hosts. +pub const CERT_INGEST_EXPOSE: &str = "cert_ingest.expose"; + +/// Default ingest port. Chosen next to the control API's 7900 so the pair is +/// memorable, and above the ephemeral range so it does not collide with one. +pub const DEFAULT_INGEST_PORT: u16 = 7901; + +impl Database { + /// Read one setting, or `None` when it was never written. + pub async fn platform_setting(&self, key: &str) -> Result> { + let conn = self.lock().await; + Ok(conn + .query_row( + "SELECT value FROM platform_setting WHERE key = ?1", + params![key], + |row| row.get::<_, String>(0), + ) + .optional()?) + } + + /// Read a setting as a boolean. Anything other than `true`/`1` is false; a + /// missing row falls back to `default`. + pub async fn platform_flag(&self, key: &str, default: bool) -> Result { + match self.platform_setting(key).await? { + Some(value) => Ok(matches!(value.as_str(), "true" | "1")), + None => Ok(default), + } + } + + /// Upsert one setting, leaving every other key untouched. + pub async fn set_platform_setting(&self, key: &str, value: &str) -> Result<()> { + let conn = self.lock().await; + conn.execute( + "INSERT INTO platform_setting (key, value, updated_at) VALUES (?1, ?2, ?3) + ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at", + params![key, value, chrono::Utc::now().to_rfc3339()], + )?; + Ok(()) + } +} + +/// Resolved ingest listener configuration. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct IngestConfig { + pub enabled: bool, + /// Address actually bound, derived from `expose`. + pub listen: String, + pub port: u16, + pub expose: bool, +} + +impl IngestConfig { + /// Full socket address for the listener. + pub fn address(&self) -> String { + format!("{}:{}", self.listen, self.port) + } + + /// Read the stored configuration, applying defaults. + /// + /// The default is **enabled on loopback**: a push endpoint that only the + /// local machine can reach is not an exposure, and requiring an operator to + /// switch it on before Certd can be tried would leave the feature looking + /// broken. Widening it to other hosts stays an explicit choice. + pub async fn load(db: &Database) -> Result { + let enabled = db.platform_flag(CERT_INGEST_ENABLED, true).await?; + let expose = db.platform_flag(CERT_INGEST_EXPOSE, false).await?; + let port = match db.platform_setting(CERT_INGEST_PORT).await? { + Some(value) => value.parse::().unwrap_or(DEFAULT_INGEST_PORT), + None => DEFAULT_INGEST_PORT, + }; + let stored_listen = db.platform_setting(CERT_INGEST_LISTEN).await?; + let listen = match stored_listen { + // An explicit address wins, so a hand-configured deployment keeps + // working even if `expose` disagrees with it. + Some(value) if !value.trim().is_empty() => value.trim().to_string(), + _ => { + if expose { + "0.0.0.0".to_string() + } else { + "127.0.0.1".to_string() + } + } + }; + Ok(Self { + enabled, + listen, + port, + expose, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::db::migrate; + + async fn database() -> Database { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + db + } + + #[tokio::test] + async fn ingest_defaults_to_loopback_and_enabled() { + let db = database().await; + let config = IngestConfig::load(&db).await.unwrap(); + assert!(config.enabled, "push must work out of the box on loopback"); + assert!( + !config.expose, + "default must not be reachable from other hosts" + ); + assert_eq!(config.address(), "127.0.0.1:7901"); + } + + #[tokio::test] + async fn exposing_changes_only_the_bind_address() { + let db = database().await; + db.set_platform_setting(CERT_INGEST_EXPOSE, "true") + .await + .unwrap(); + let config = IngestConfig::load(&db).await.unwrap(); + assert_eq!(config.address(), "0.0.0.0:7901"); + assert_eq!(config.port, DEFAULT_INGEST_PORT); + } + + #[tokio::test] + async fn settings_are_independent() { + let db = database().await; + db.set_platform_setting(CERT_INGEST_PORT, "17901") + .await + .unwrap(); + let config = IngestConfig::load(&db).await.unwrap(); + assert_eq!(config.port, 17901); + assert!(config.enabled, "writing one key must not reset another"); + } + + #[tokio::test] + async fn disabling_is_respected() { + let db = database().await; + db.set_platform_setting(CERT_INGEST_ENABLED, "false") + .await + .unwrap(); + assert!(!IngestConfig::load(&db).await.unwrap().enabled); + } +} diff --git a/crates/client/src/error.rs b/crates/client/src/error.rs index 430a7c7..bc94716 100644 --- a/crates/client/src/error.rs +++ b/crates/client/src/error.rs @@ -25,6 +25,22 @@ pub enum ClientError { #[error("Record already exists: {0}")] RecordAlreadyExists(String), + // === Certificate management errors (CERT) === + #[error("Certificate input could not be parsed: {0}")] + CertificateParse(String), + + #[error("Certificate secrets operation failed: {0}")] + CertificateSecrets(String), + + #[error("Certificate key does not match the certificate: {0}")] + CertificateKeyMismatch(String), + + #[error("Certificate is bound to {count} proxy configuration(s); unbind it first")] + CertificateStillBound { count: usize }, + + #[error("Certificate target is invalid: {0}")] + CertificateTarget(String), + // === Config validation errors (CFG) === #[error("Configuration validation failed: {0}")] ConfigValidation(String), @@ -79,6 +95,12 @@ impl ClientError { ClientError::DatabaseQuery(_) => "DB_003", ClientError::RecordNotFound { .. } => "DB_004", ClientError::RecordAlreadyExists(_) => "DB_005", + // CERT + ClientError::CertificateParse(_) => "CERT_001", + ClientError::CertificateSecrets(_) => "CERT_002", + ClientError::CertificateKeyMismatch(_) => "CERT_003", + ClientError::CertificateStillBound { .. } => "CERT_004", + ClientError::CertificateTarget(_) => "CERT_005", // CFG ClientError::ConfigValidation(_) => "CFG_001", ClientError::InvalidIpAddress(_) => "CFG_002", @@ -106,6 +128,11 @@ impl ClientError { ClientError::DatabaseQuery(_) => "error.db.query", ClientError::RecordNotFound { .. } => "error.db.record_not_found", ClientError::RecordAlreadyExists(_) => "error.db.record_exists", + ClientError::CertificateParse(_) => "error.cert.parse", + ClientError::CertificateSecrets(_) => "error.cert.secrets", + ClientError::CertificateKeyMismatch(_) => "error.cert.key_mismatch", + ClientError::CertificateStillBound { .. } => "error.cert.still_bound", + ClientError::CertificateTarget(_) => "error.cert.target", ClientError::ConfigValidation(_) => "error.config.validation", ClientError::InvalidIpAddress(_) => "error.config.invalid_ip", ClientError::InvalidPort(_) => "error.config.invalid_port", diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 2289723..18746d8 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -3,6 +3,7 @@ //! Provides configuration management, TOML generation, and frpc process supervision. //! Uses `rustfrp_common` for plugin infrastructure and signal handling. +pub mod cert; pub mod config; pub mod core; pub mod db; diff --git a/crates/client/tests/certificate_management.rs b/crates/client/tests/certificate_management.rs new file mode 100644 index 0000000..95fdb56 --- /dev/null +++ b/crates/client/tests/certificate_management.rs @@ -0,0 +1,836 @@ +//! Certificate storage and apply behaviour, exercised through the database. +//! +//! These tests target the properties that matter for correctness in production: +//! idempotent import, encrypted-at-rest material, tenant isolation, refusal to +//! delete a bound certificate, and apply keeping the declared files in sync. + +use rustfrp_client::cert::{parse_bundle, CertificateSecrets}; +use rustfrp_client::config::model::LocalProxy; +use rustfrp_client::db::certificate::{ImportRequest, SOURCE_MANUAL}; +use rustfrp_client::db::{migrate, Database}; + +const CERT: &str = include_str!("fixtures/cert_ec.pem"); +const KEY: &str = include_str!("fixtures/cert_ec.key"); + +async fn database() -> (tempfile::TempDir, Database) { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + (dir, db) +} + +fn import_request() -> ImportRequest { + ImportRequest { + parsed: parse_bundle(CERT, KEY).unwrap(), + source: SOURCE_MANUAL.into(), + source_detail: None, + } +} + +#[tokio::test] +async fn import_is_idempotent_and_stores_no_plaintext_key() { + let (_dir, db) = database().await; + + let first = db + .import_certificate("default", import_request()) + .await + .unwrap(); + let second = db + .import_certificate("default", import_request()) + .await + .unwrap(); + assert_eq!(first.id, second.id); + assert_eq!( + db.list_certificates("default").await.unwrap().len(), + 1, + "re-importing the same fingerprint must not add a row" + ); + + // The database file must not contain the private key in readable form. + let bytes = std::fs::read(db.path()).unwrap(); + let haystack = String::from_utf8_lossy(&bytes); + assert!(!haystack.contains("BEGIN PRIVATE KEY")); + assert!(!haystack.contains("BEGIN CERTIFICATE")); + + // Material still round-trips for the apply engine. + let secrets = db.certificate_secrets(first.id, "default").await.unwrap(); + assert!(secrets.fullchain_pem().contains("BEGIN CERTIFICATE")); + assert!(secrets.key_pem().contains("BEGIN PRIVATE KEY")); +} + +#[tokio::test] +async fn certificate_is_scoped_to_its_tenant() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("alpha", import_request()) + .await + .unwrap(); + + assert!(db.list_certificates("beta").await.unwrap().is_empty()); + assert!(db + .certificate_in_tenant(certificate.id, "beta") + .await + .is_err()); + assert!(db + .certificate_secrets(certificate.id, "beta") + .await + .is_err()); +} + +#[tokio::test] +async fn deletion_is_blocked_while_bound() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("default", import_request()) + .await + .unwrap(); + + let proxy = proxy_with_certificate_paths(db.path().parent().unwrap()); + let proxy_id = db.insert_proxy(&proxy).await.unwrap(); + + let binding = db + .bind_certificate(certificate.id, proxy_id, "default") + .await + .unwrap(); + assert_eq!(binding.cert_id, certificate.id); + + let error = db + .delete_certificate(certificate.id, "default") + .await + .unwrap_err(); + assert!(error.to_string().contains("unbind"), "got: {error}"); + + db.unbind_certificate(binding.id, "default", false) + .await + .unwrap(); + db.delete_certificate(certificate.id, "default") + .await + .unwrap(); + assert!(db.list_certificates("default").await.unwrap().is_empty()); +} + +#[tokio::test] +async fn binding_requires_a_plugin_that_holds_a_certificate() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("default", import_request()) + .await + .unwrap(); + + let proxy = LocalProxy { + name: "plain-http".into(), + proxy_type: rustfrp_client::config::model::ProxyType::Http, + custom_domains: Some(vec!["plain.example.com".into()]), + plugin_config: None, + ..LocalProxy::default() + }; + let proxy_id = db.insert_proxy(&proxy).await.unwrap(); + + let error = db + .bind_certificate(certificate.id, proxy_id, "default") + .await + .unwrap_err(); + assert!( + error.to_string().contains("plugin configuration"), + "got: {error}" + ); +} + +#[tokio::test] +async fn apply_writes_the_files_the_proxy_declares() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("default", import_request()) + .await + .unwrap(); + + let dir = tempfile::tempdir().unwrap(); + let proxy = proxy_with_certificate_paths(dir.path()); + let proxy_id = db.insert_proxy(&proxy).await.unwrap(); + db.bind_certificate(certificate.id, proxy_id, "default") + .await + .unwrap(); + + let (applied, failed) = db + .apply_certificate(certificate.id, "default") + .await + .unwrap(); + assert!(failed.is_empty(), "apply failed: {failed:?}"); + assert_eq!(applied.len(), 1); + + let crt = std::fs::read_to_string(dir.path().join("fullchain.pem")).unwrap(); + let key = std::fs::read_to_string(dir.path().join("privkey.pem")).unwrap(); + assert!(crt.contains("BEGIN CERTIFICATE")); + assert!(key.contains("BEGIN PRIVATE KEY")); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(dir.path().join("privkey.pem")) + .unwrap() + .permissions() + .mode() + & 0o777; + assert_eq!(mode, 0o600, "private key must not be readable by others"); + } + + // The binding records what was written, which is what drift detection reads. + let refreshed = db + .certificate_in_tenant(certificate.id, "default") + .await + .unwrap(); + assert_eq!( + refreshed.bindings[0].applied_fingerprint.as_deref(), + Some(certificate.fingerprint_sha256.as_str()) + ); +} + +#[tokio::test] +async fn apply_reports_each_failing_binding_separately() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("default", import_request()) + .await + .unwrap(); + + let good_dir = tempfile::tempdir().unwrap(); + let good_id = db + .insert_proxy(&proxy_with_certificate_paths(good_dir.path())) + .await + .unwrap(); + + // A file where a directory would have to be: creating the path fails. + let blocked = good_dir.path().join("blocked"); + std::fs::write(&blocked, b"not a directory").unwrap(); + let mut bad = proxy_with_certificate_paths(&blocked.join("sub")); + bad.name = "blocked-proxy".into(); + let bad_id = db.insert_proxy(&bad).await.unwrap(); + + db.bind_certificate(certificate.id, good_id, "default") + .await + .unwrap(); + db.bind_certificate(certificate.id, bad_id, "default") + .await + .unwrap(); + + let (applied, failed) = db + .apply_certificate(certificate.id, "default") + .await + .unwrap(); + assert_eq!(applied.len(), 1, "the writable binding must still succeed"); + assert_eq!(failed.len(), 1); + assert!( + good_dir.path().join("fullchain.pem").exists(), + "a failing sibling must not undo a successful write" + ); + + let refreshed = db + .certificate_in_tenant(certificate.id, "default") + .await + .unwrap(); + let failed_binding = refreshed + .bindings + .iter() + .find(|binding| binding.proxy_id == bad_id) + .unwrap(); + assert!(failed_binding.last_error.is_some()); + assert!(failed_binding.applied_fingerprint.is_none()); +} + +#[tokio::test] +async fn ingest_token_is_hashed_and_domain_scoped() { + let (_dir, db) = database().await; + let created = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["Test.Example.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + let plaintext = created.plaintext.clone().unwrap(); + + // Only the digest is stored. + let stored = { + let conn = db.lock().await; + conn.query_row( + "SELECT token_sha256 FROM certificate_source WHERE id = ?1", + [created.source.id], + |row| row.get::<_, String>(0), + ) + .unwrap() + }; + assert_ne!(stored, plaintext); + assert_eq!( + stored, + rustfrp_client::db::cert_ingest::token_digest(&plaintext) + ); + + let grant = db + .authenticate_source_token(&plaintext) + .await + .unwrap() + .unwrap(); + assert_eq!(grant.tenant, "default"); + assert_eq!(grant.allowed_domains, vec!["test.example.com".to_string()]); + assert!( + grant.certificate_id.is_none(), + "an unbound token stays unbound" + ); + + assert!(db + .authenticate_source_token("wrong-token-value-xxxxxxxx") + .await + .unwrap() + .is_none()); + + // Deleting the channel invalidates its token immediately. + db.delete_source(created.source.id, "default") + .await + .unwrap(); + assert!(db + .authenticate_source_token(&plaintext) + .await + .unwrap() + .is_none()); +} + +#[tokio::test] +async fn pull_source_without_credentials_reads_as_empty_secret() { + let (_dir, db) = database().await; + let source = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Url, + name: "plain-url".into(), + domain: "test.example.com".into(), + base_url: "https://example.com/bundle.json".into(), + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + + assert!(!source.source.has_secret); + // A NULL `enc_secret` must read as "no credentials", not as a row that + // does not exist and not as a type error from rusqlite. + let secret = db.source_secret(source.source.id, "default").await.unwrap(); + assert!(secret.key_id.is_none() && secret.key_secret.is_none()); +} + +#[tokio::test] +async fn pull_source_secret_never_leaves_the_database_layer() { + let (_dir, db) = database().await; + let source = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Certd, + name: "certd".into(), + domain: "test.example.com".into(), + base_url: "https://certd.example.com".into(), + domains: vec![], + key_id: Some("kid".into()), + key_secret: Some("super-secret".into()), + enabled: true, + }, + ) + .await + .unwrap(); + + assert!(source.source.has_secret); + let listed = serde_json::to_string(&db.list_sources("default", None).await.unwrap()).unwrap(); + assert!(!listed.contains("super-secret"), "secret leaked: {listed}"); + + let secret = db.source_secret(source.source.id, "default").await.unwrap(); + assert_eq!(secret.key_secret.as_deref(), Some("super-secret")); +} + +fn proxy_with_certificate_paths(dir: &std::path::Path) -> LocalProxy { + LocalProxy { + name: "nas-web".into(), + proxy_type: rustfrp_client::config::model::ProxyType::Https, + local_ip: "127.0.0.1".into(), + local_port: 8080, + custom_domains: Some(vec!["test.example.com".into()]), + plugin_config: Some(serde_json::json!({ + "type": "https2http", + "localAddr": "127.0.0.1:8080", + "crtPath": dir.join("fullchain.pem").to_string_lossy(), + "keyPath": dir.join("privkey.pem").to_string_lossy(), + })), + ..LocalProxy::default() + } +} + +/// Silence the unused import when only the metadata type is needed. +#[allow(dead_code)] +fn _assert_secrets_is_not_serializable(_: CertificateSecrets) {} + +/// A certificate exists before it has material: that is what lets an operator +/// configure a channel first and fill the certificate later. +#[tokio::test] +async fn a_certificate_can_exist_before_any_material_arrives() { + let (_dir, db) = database().await; + + let certificate = db + .ensure_certificate("default", "Test.Example.com") + .await + .unwrap(); + assert_eq!(certificate.primary_domain, "test.example.com"); + assert!(certificate.material_id.is_none()); + assert!( + certificate.days_remaining.is_none(), + "no expiry before material" + ); + assert!(certificate.not_after.is_empty()); + // A certificate with no material is `pending`, and the reason lives in + // `status_reason` rather than the free-form warning list. + assert_eq!( + certificate.status, + rustfrp_client::cert::CertificateStatus::Pending + ); + assert!(!certificate.usable); + assert!(certificate + .status_reason + .as_deref() + .unwrap_or_default() + .contains("no certificate has been received")); + + // Asking for the encryption material is a state error, not a 404. + let error = db + .certificate_secrets(certificate.id, "default") + .await + .unwrap_err(); + assert!(error.to_string().contains("no material yet"), "{error}"); + + // Ensuring twice must not create a second identity. + let again = db + .ensure_certificate("default", "test.example.com") + .await + .unwrap(); + assert_eq!(again.id, certificate.id); + assert_eq!(db.list_certificates("default").await.unwrap().len(), 1); +} + +/// The identity is the domain, so a renewal replaces the newest material +/// instead of appending a second certificate. +#[tokio::test] +async fn reissuing_a_certificate_updates_instead_of_duplicating() { + let (_dir, db) = database().await; + + let first = db + .import_certificate("default", import_request()) + .await + .unwrap(); + assert_eq!(first.fingerprint_sha256.len(), 64); + + // Same bundle again: idempotent. + let same = db + .import_certificate("default", import_request()) + .await + .unwrap(); + assert_eq!(same.id, first.id); + assert_eq!(same.fingerprint_sha256, first.fingerprint_sha256); + assert_eq!(db.list_certificates("default").await.unwrap().len(), 1); + + // A different bundle for the same domain is a renewal: same identity, new + // material. The fixture's RSA pair is a different certificate than the EC + // one, and both cover the same domain set. + let renewed = db + .import_certificate( + "default", + ImportRequest { + parsed: parse_bundle( + include_str!("fixtures/cert_rsa.pem"), + include_str!("fixtures/cert_rsa.key"), + ) + .unwrap(), + source: SOURCE_MANUAL.into(), + source_detail: Some("renewal".into()), + }, + ) + .await + .unwrap(); + + assert_eq!(renewed.id, first.id, "the identity must not change"); + assert_ne!(renewed.fingerprint_sha256, first.fingerprint_sha256); + assert_eq!(db.list_certificates("default").await.unwrap().len(), 1); + assert_eq!(renewed.source_detail.as_deref(), Some("renewal")); +} + +/// Channels are switchable: the same row moves between certificates, and exactly +/// one channel per certificate can be primary. +#[tokio::test] +async fn a_channel_can_move_between_certificates() { + let (_dir, db) = database().await; + let first = db + .ensure_certificate("default", "a.example.com") + .await + .unwrap(); + let second = db + .ensure_certificate("default", "b.example.com") + .await + .unwrap(); + + let push = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: Some(first.id), + kind: rustfrp_client::db::cert_ingest::SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["*.example.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + + // The first channel of a certificate becomes its primary automatically. + assert!(push.source.is_primary); + assert_eq!(push.source.certificate_id, Some(first.id)); + + // Move it. The old certificate must not keep a stale primary. + let moved = db + .set_source_certificate(push.source.id, Some(second.id), "default") + .await + .unwrap(); + assert_eq!(moved.certificate_id, Some(second.id)); + assert!(moved.is_primary, "the new certificate adopts it as primary"); + assert!(db + .list_sources("default", Some(first.id)) + .await + .unwrap() + .is_empty()); + + // Detaching keeps the channel (and its token) alive. + let detached = db + .set_source_certificate(push.source.id, None, "default") + .await + .unwrap(); + assert!(detached.certificate_id.is_none()); + assert!(db + .authenticate_source_token(&push.plaintext.clone().unwrap()) + .await + .unwrap() + .is_some()); +} + +/// A certificate with two channels uses exactly one as primary, and switching it +/// demotes the other rather than violating the partial unique index. +#[tokio::test] +async fn only_one_channel_per_certificate_is_primary() { + let (_dir, db) = database().await; + let certificate = db + .ensure_certificate("default", "a.example.com") + .await + .unwrap(); + + let push = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: Some(certificate.id), + kind: rustfrp_client::db::cert_ingest::SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["a.example.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + let pull = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: Some(certificate.id), + kind: rustfrp_client::db::cert_ingest::SourceKind::Certd, + name: "nightly".into(), + domain: "a.example.com".into(), + base_url: "https://certd.example.com".into(), + domains: vec![], + key_id: Some("kid".into()), + key_secret: Some("secret".into()), + enabled: true, + }, + ) + .await + .unwrap(); + + assert!(push.source.is_primary, "the first channel is primary"); + assert!( + !pull.source.is_primary, + "a second channel does not steal it" + ); + + let promoted = db + .set_primary_source(pull.source.id, "default") + .await + .unwrap(); + assert!(promoted.is_primary); + + let sources = db + .list_sources("default", Some(certificate.id)) + .await + .unwrap(); + assert_eq!(sources.iter().filter(|source| source.is_primary).count(), 1); + assert!(sources + .iter() + .any(|source| source.id == push.source.id && !source.is_primary)); +} + +/// A pull channel's target domain must be one the attached certificate covers. +/// Creating it is where the mistake is cheap to fix; the first fetch is not. +#[tokio::test] +async fn a_pull_channel_target_must_be_covered_by_the_certificate() { + let (_dir, db) = database().await; + let certificate = db + .import_certificate("default", import_request()) + .await + .unwrap(); + assert_eq!(certificate.primary_domain, "test.example.com"); + + let input = |domain: &str| rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: Some(certificate.id), + kind: rustfrp_client::db::cert_ingest::SourceKind::Url, + name: "pull".into(), + domain: domain.into(), + base_url: "https://example.com/bundle.json".into(), + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }; + + // A covered domain is accepted. + db.create_source("default", input("test.example.com")) + .await + .unwrap(); + + // `other.test` is not in the certificate's SANs. + let error = db + .create_source("default", input("other.test")) + .await + .unwrap_err(); + assert!( + error + .to_string() + .contains("does not cover the channel's domain"), + "{error}" + ); + + // An unattached channel has nothing to check against, so it is allowed: + // this is how a certificate is bootstrapped before any material exists. + let mut unattached = input("other.test"); + unattached.certificate_id = None; + unattached.name = "bootstrap".into(); + db.create_source("default", unattached).await.unwrap(); +} + +/// "Newest material" is the most recently stored one, even when it expires +/// earlier. A reissue with a shorter validity must win, or the daemon would keep +/// serving an older bundle that merely has a later `not_after`. +#[tokio::test] +async fn a_reissue_with_a_shorter_validity_still_becomes_current() { + let (_dir, db) = database().await; + + // The rule under test is the ordering, so the fixtures' dates are irrelevant. + let first = db + .import_certificate("default", import_request()) + .await + .unwrap(); + let second = db + .import_certificate( + "default", + ImportRequest { + parsed: parse_bundle( + include_str!("fixtures/cert_rsa.pem"), + include_str!("fixtures/cert_rsa.key"), + ) + .unwrap(), + source: SOURCE_MANUAL.into(), + source_detail: None, + }, + ) + .await + .unwrap(); + + assert_eq!(second.id, first.id); + assert_ne!( + second.fingerprint_sha256, first.fingerprint_sha256, + "a reissue is a different bundle" + ); + + // The reported material must be the one that was imported last, regardless + // of which expiry date happens to be later. + let latest_id = { + let conn = db.lock().await; + conn.query_row( + "SELECT MAX(id) FROM certificate_material WHERE certificate_id = ?1", + [first.id], + |row| row.get::<_, i64>(0), + ) + .unwrap() + }; + assert_eq!( + second.material_id, + Some(latest_id), + "the newest stored material wins, not the one expiring last" + ); + + // And the secrets read back must match that same version. + let secrets = db.certificate_secrets(first.id, "default").await.unwrap(); + let expected = parse_bundle( + include_str!("fixtures/cert_rsa.pem"), + include_str!("fixtures/cert_rsa.key"), + ) + .unwrap(); + assert_eq!(secrets.cert_der, expected.cert_der); +} + +/// Configuring a pull channel must show up immediately. A channel whose first +/// fetch fails used to leave the certificate list empty, which reads as "my +/// configuration was not saved". +#[tokio::test] +async fn a_pull_channel_creates_the_certificate_it_will_fill() { + let (_dir, db) = database().await; + + let created = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Certd, + name: "certd".into(), + domain: "*.powercess.com".into(), + base_url: "https://certd.example.com".into(), + domains: vec![], + key_id: Some("kid".into()), + key_secret: Some("secret".into()), + enabled: true, + }, + ) + .await + .unwrap(); + + // The channel is bound, and the certificate exists with no material yet. + assert!(created.source.certificate_id.is_some()); + assert!( + created.source.is_primary, + "its only channel is the primary one" + ); + + let certificates = db.list_certificates("default").await.unwrap(); + assert_eq!(certificates.len(), 1, "the certificate is visible at once"); + assert_eq!(certificates[0].primary_domain, "*.powercess.com"); + assert!(certificates[0].material_id.is_none()); + assert_eq!(certificates[0].id, created.source.certificate_id.unwrap()); + + // A second channel for the same domain reuses that identity. + let second = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Url, + name: "fallback".into(), + domain: "*.powercess.com".into(), + base_url: "https://example.com/bundle.json".into(), + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + assert_eq!(second.source.certificate_id, created.source.certificate_id); + assert!(!second.source.is_primary, "the first channel stays primary"); + assert_eq!(db.list_certificates("default").await.unwrap().len(), 1); +} + +/// A push channel's allowlist is not a domain it serves, so it must not invent a +/// certificate: the domain only becomes known when a bundle arrives. +#[tokio::test] +async fn a_push_channel_does_not_invent_a_certificate() { + let (_dir, db) = database().await; + let created = db + .create_source( + "default", + rustfrp_client::db::cert_ingest::SourceInput { + certificate_id: None, + kind: rustfrp_client::db::cert_ingest::SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["*.powercess.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + assert!(created.source.certificate_id.is_none()); + assert!(db.list_certificates("default").await.unwrap().is_empty()); +} + +/// An expired or pending certificate must not be written over a working file. +#[tokio::test] +async fn apply_refuses_a_certificate_that_cannot_be_served() { + let (dir, db) = database().await; + let certificate = db + .ensure_certificate("default", "test.example.com") + .await + .unwrap(); + assert_eq!( + certificate.status, + rustfrp_client::cert::CertificateStatus::Pending + ); + + let crt_path = dir.path().join("fullchain.pem"); + let key_path = dir.path().join("privkey.pem"); + let proxy = proxy_with_certificate_paths(dir.path()); + let proxy_id = db.insert_proxy_for_tenant(&proxy, "default").await.unwrap(); + db.bind_certificate(certificate.id, proxy_id, "default") + .await + .unwrap(); + + let error = db + .apply_certificate(certificate.id, "default") + .await + .unwrap_err(); + assert!(error.to_string().contains("refusing to write"), "{error}"); + assert!( + !crt_path.exists() && !key_path.exists(), + "nothing may be written for a certificate that cannot be served" + ); +} diff --git a/crates/client/tests/fixtures/cert_ec.key b/crates/client/tests/fixtures/cert_ec.key new file mode 100644 index 0000000..37b6a0d --- /dev/null +++ b/crates/client/tests/fixtures/cert_ec.key @@ -0,0 +1,5 @@ +-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg6gFhBgFfW1DdL8aV +uo91bmOipgV7eQfj32zPgv44vTChRANCAATImAoimaaIu8YnjwoBhZSwrGbwP5f8 ++Vo5MeGzEEj183mKcN6baKxnjzIdK71uzVjETo+umPLNt/k6T3CrVHw0 +-----END PRIVATE KEY----- diff --git a/crates/client/tests/fixtures/cert_ec.pem b/crates/client/tests/fixtures/cert_ec.pem new file mode 100644 index 0000000..2e48c13 --- /dev/null +++ b/crates/client/tests/fixtures/cert_ec.pem @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE----- +MIIBlzCCAT6gAwIBAgIUN3QfG9mcn6dTkuRYevh1dsHAK3YwCgYIKoZIzj0EAwIw +GzEZMBcGA1UEAwwQdGVzdC5leGFtcGxlLmNvbTAeFw0yNjExMTcwMjM0MjhaFw0y +NzExMTcwMjM0MjhaMBsxGTAXBgNVBAMMEHRlc3QuZXhhbXBsZS5jb20wWTATBgcq +hkjOPQIBBggqhkjOPQMBBwNCAATImAoimaaIu8YnjwoBhZSwrGbwP5f8+Vo5MeGz +EEj183mKcN6baKxnjzIdK71uzVjETo+umPLNt/k6T3CrVHw0o2AwXjAdBgNVHQ4E +FgQUWHRsbpsqNH52mv1VruVtWOmxVQowDwYDVR0TAQH/BAUwAwEB/zAsBgNVHREE +JTAjghB0ZXN0LmV4YW1wbGUuY29tgg9hbHQuZXhhbXBsZS5jb20wCgYIKoZIzj0E +AwIDRwAwRAIgOuEQBZx+iaQErIimLKfu5Zi5zdWhwY4AWynCkHyLSFgCIHdzVJfi +TKGh+IfT9MBD9dcAkuv1ANJaZszM8q++sAhL +-----END CERTIFICATE----- diff --git a/crates/client/tests/fixtures/cert_rsa.key b/crates/client/tests/fixtures/cert_rsa.key new file mode 100644 index 0000000..68c8a94 --- /dev/null +++ b/crates/client/tests/fixtures/cert_rsa.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEuQIBADANBgkqhkiG9w0BAQEFAASCBKMwggSfAgEAAoIBAQCQkNM90aPtXL9c +IDRq4mpqEEE62fnF249YPsEY+BoKP5gksRjmatEEdnwhQAcDd+GM/bvOqXMWskXJ +G0eX46jHndKvZMHqlJ7vtfT7rzTNp5Q+jIT47KX1SrOuxPJWgSUwn3mFwuMGEShN +q44V6OA9u2hnvgrt8r1orDBfIE1YPD2vZqvudbi9vZ6EgtNgUZjf32EG7sN5fJ9b +sN/9u0a6C/jACKxtjNkfIngC33kRVN5ihYFfjHxzYqqRLCKlt2w4XAhggxDxEwN3 +Zx/QVIPZKgJM5Uwx4TYHbQQk4ymLzw4sIAAIquQ+Ks5rldFQNLpmSh5qWGKzyzG1 +sKCpwcTLAgMBAAECgf83X0GKJQld0xpcWcCdocZZoOA+1SJy7bRdmI9dueLqJnGg +DfxtYbV0uQnSlhc2EjTuvc942OBm31OGNBdisqXk7A5/rgnt3jge0b29Gxe7Ay3v +quMK5yf/TDoU4bApx2oTtaxWSJaP5wJ6vYJLN2eQPZuxrwrV/NzlECt27to/cSNz +FYLnhmskc2QnJ9SacQb/h5lYMEeP0MNwWii4Uy+qch9V87iLGpX8wE50P9fpBKnB +S0wTTgMRWNZTONbstIlp99wU9nnOcDVsfk+9BXpHLsabnW7qP253qVb1wWlNgrC5 +9+MHyqVLZpQLUi5S04b7iIi4mdcmFGWI0/5o7G0CgYEAy/n3axDqNNVOFZme189H +1aHWX8AQ9N9H3z1eoU/j4yGOEtlzBTKsttDvimAMP/felbNomA5gGIM3c17m87Md +2yTA4kSHevKOH5PunEJ01/F9++yFhhooM013xMBWWKYqgktM1FzazOlJuMikPt9w +j+RlQQF9drXLtC23E1kJusUCgYEAtW/QPm4OqS6K3DD4CrNIg98HTQcFqPZ+vktF +zjpIxFz4SWPaHEqQBe3hhIZAPlbi39SAIRBdCqKBCe3M8s6oUReCW3/M4GxlO0Ym +90XvD+19eH3QEwgqZj5t2Kz3whh3dMcsXbbOLlBDB7YP4NI3SL1+RsV5lS9gQqLN +GF62uk8CgYEAm7ptBkX0uX98zQrq9dLgAh873wBxMbLiWXKuM7QdxUPSB1wS743/ +xNeBeoDX+WKRgnNl0GlxPmW3yRJUewxuJ+hIhrdRRDG5BizYCF7IfDF4QQUT8A06 +Uei8Hu/FPIsoey9oKd32gmk9iqFMnF/UvVbfYyDzG9Wx/w7D7jaVMg0CgYBodavL +hcnjFezUwUzNvFLvuaSWcIiNsv1dyjCyf1QN2gkJF/gnebZZ8493oWOzgIq2m735 +S1aGHPF2KfdphBncNlDTJU1rmHzgF4+vpg/xd6ZZjPYTPwxKOX881LkAgt2j2lhs +zCcA+yzcb7HMmrtBoffl/yP1BIXDV8AFW+KQeQJ/EMpJemrzg3NY9ohcJAPHwC+E +tkYlEx0lT3J+06pmh+59XuARPt8lRGj8wCT/+PCCyhK41JuSCu6z8CtATaFgqe3+ +q9M+nFj1IhKHlmxYyE1RciVaoH2/S5oKVyiApXZH5szR3o4FB7uXiLru6dFN8d/X +qVd7dZutm8SOuSg+Xg== +-----END PRIVATE KEY----- diff --git a/crates/client/tests/fixtures/cert_rsa.pem b/crates/client/tests/fixtures/cert_rsa.pem new file mode 100644 index 0000000..6419e42 --- /dev/null +++ b/crates/client/tests/fixtures/cert_rsa.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDJDCCAgygAwIBAgIUe9IH36GFi6kn6+WoweDzFy72/lIwDQYJKoZIhvcNAQEL +BQAwGzEZMBcGA1UEAwwQdGVzdC5leGFtcGxlLmNvbTAeFw0yNjExMjAwMDQwMzRa +Fw0zNjExMTcwMDQwMzRaMBsxGTAXBgNVBAMMEHRlc3QuZXhhbXBsZS5jb20wggEi +MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCQkNM90aPtXL9cIDRq4mpqEEE6 +2fnF249YPsEY+BoKP5gksRjmatEEdnwhQAcDd+GM/bvOqXMWskXJG0eX46jHndKv +ZMHqlJ7vtfT7rzTNp5Q+jIT47KX1SrOuxPJWgSUwn3mFwuMGEShNq44V6OA9u2hn +vgrt8r1orDBfIE1YPD2vZqvudbi9vZ6EgtNgUZjf32EG7sN5fJ9bsN/9u0a6C/jA +CKxtjNkfIngC33kRVN5ihYFfjHxzYqqRLCKlt2w4XAhggxDxEwN3Zx/QVIPZKgJM +5Uwx4TYHbQQk4ymLzw4sIAAIquQ+Ks5rldFQNLpmSh5qWGKzyzG1sKCpwcTLAgMB +AAGjYDBeMB0GA1UdDgQWBBQdtAV9E3lFSeIgBP8cq906ssHE6TAPBgNVHRMBAf8E +BTADAQH/MCwGA1UdEQQlMCOCEHRlc3QuZXhhbXBsZS5jb22CD2FsdC5leGFtcGxl +LmNvbTANBgkqhkiG9w0BAQsFAAOCAQEACUHIrtCP6TySp5dQL4tXE12kEvtvJRF0 +MqWx5b02DnMOPZ/V/Ei8/aidpuPa7xRawMGIjYcbFgXMsaYQgfDKNUgIIwv9nsfB +fBvuxjKQKRQS3ufwGNilbEqK/ULnN7pfwdvASma/nieJCwty+V2rMpAs7ESBiDRJ +bcObNHHMX/4o2DOPKtTGKEo4bdG2n/fnEDmP6A2JgyoqO8bNb3htjDyRq41CgKIq +BJQh2l3HbQP0n3YtqmlxpDJj26GyDuZaa67WP04TR9+973CZ5/+8eM6Uh//Fm430 +t32W1g/u7aBh8kyh3El/5Bes0qNLw/HMr68QHx16lsu1kQJ4Xj2YRA== +-----END CERTIFICATE----- diff --git a/crates/rustfrp-bin/src/manager.rs b/crates/rustfrp-bin/src/manager.rs index 6ffe866..83bc09e 100644 --- a/crates/rustfrp-bin/src/manager.rs +++ b/crates/rustfrp-bin/src/manager.rs @@ -22,12 +22,39 @@ pub struct AvailableVersion { pub published_at: Option, pub prerelease: bool, pub draft: bool, + #[serde(default)] + assets: Vec, +} + +/// One downloadable archive of a GitHub release. +/// +/// Only the fields the UI needs to describe a build: which file, how large, +/// and how often it was fetched. The API sends far more, which is ignored. +#[derive(Debug, Clone, serde::Deserialize)] +pub struct ReleaseAsset { + pub name: String, + #[serde(default)] + pub size: u64, + #[serde(default)] + pub download_count: u64, } impl AvailableVersion { pub fn version(&self) -> &str { self.tag.trim_start_matches('v') } + + /// The GitHub tag, always `v`-prefixed. + pub fn tag(&self) -> &str { + &self.tag + } + + /// The archive this platform installs, if the release ships one. + pub fn asset_for(&self, platform: &str) -> Option<&ReleaseAsset> { + let wanted = + crate::download::asset_filename(&crate::FrpVersion::from_tag(&self.tag), platform); + self.assets.iter().find(|asset| asset.name == wanted) + } } #[derive(Debug, Clone)] diff --git a/crates/rustfrp-daemon/Cargo.toml b/crates/rustfrp-daemon/Cargo.toml index 4769550..86d3a30 100644 --- a/crates/rustfrp-daemon/Cargo.toml +++ b/crates/rustfrp-daemon/Cargo.toml @@ -8,9 +8,9 @@ rust-version.workspace = true description = "RustFRP daemon — HTTP API server for frpc configuration management" [dependencies] -rustfrp-client = { version = "0.5.4", path = "../client" } -rustfrp-bin = { version = "0.5.0", path = "../rustfrp-bin" } -rustfrp-common = { version = "0.5.0", path = "../common" } +rustfrp-client.workspace = true +rustfrp-bin.workspace = true +rustfrp-common.workspace = true tokio = { workspace = true, features = ["process", "sync", "time", "signal", "net"] } serde.workspace = true serde_json.workspace = true @@ -32,6 +32,8 @@ tower = { version = "0.4", optional = true, features = ["util"] } tower-http = { version = "0.5", optional = true, features = ["cors", "trace"] } rust-embed = { version = "8", optional = true } mime_guess = { version = "2", optional = true } +md-5 = "0.10" +base64 = "0.22" [features] default = ["http-api"] diff --git a/crates/rustfrp-daemon/src/api/auth.rs b/crates/rustfrp-daemon/src/api/auth.rs index 659765f..b3adeaa 100644 --- a/crates/rustfrp-daemon/src/api/auth.rs +++ b/crates/rustfrp-daemon/src/api/auth.rs @@ -106,7 +106,13 @@ fn required_scope(method: &Method, path: &str) -> &'static str { || path.starts_with("/api/v1/visitors") || path.starts_with("/api/v1/bindings") || path.starts_with("/api/v1/logs") - || path.starts_with("/api/v1/environments"); + // Certificates and their credentials belong to a tenant: one token + // must never see another tenant's domain certificates. + || path.starts_with("/api/v1/certificates") + || path.starts_with("/api/v1/certificate-bindings") + || path.starts_with("/api/v1/cert-ingest") + || path.starts_with("/api/v1/cert-ingest-tokens") + || path.starts_with("/api/v1/cert-pull-sources"); match ( tenant_resource, matches!(*method, Method::GET | Method::HEAD), diff --git a/crates/rustfrp-daemon/src/api/cert_ingest.rs b/crates/rustfrp-daemon/src/api/cert_ingest.rs new file mode 100644 index 0000000..e876a92 --- /dev/null +++ b/crates/rustfrp-daemon/src/api/cert_ingest.rs @@ -0,0 +1,134 @@ +//! Certificate channel API. +//! +//! One resource for both directions: a channel is a push token, a Certd pull, or +//! a plain URL pull, and it is either attached to a certificate or unbound. The +//! legacy `/cert-ingest-tokens` and `/cert-pull-sources` routes are aliases of +//! this one so existing automation keeps working. +//! +//! Creating a push channel returns its plaintext token exactly once; every later +//! read carries only `has_secret`. A pull channel's `key_secret` is write-only in +//! the same way. + +use axum::extract::{Extension, Path, Query, State}; +use axum::http::StatusCode; +use axum::Json; +use serde::Deserialize; + +use rustfrp_client::db::cert_ingest::{CertificateSource, CreatedSource, SourceInput, SourceKind}; + +use super::auth::AuthIdentity; +use super::response::{api_error, ApiResponse}; +use super::state::ApiState; + +type ApiErrorResponse = (StatusCode, Json>); + +#[derive(Debug, Deserialize)] +pub struct ListQuery { + /// Only channels attached to this certificate. + #[serde(default)] + pub certificate_id: Option, +} + +pub async fn list( + State(state): State, + Extension(identity): Extension, + Query(query): Query, +) -> Result>>, ApiErrorResponse> { + let sources = state + .db + .list_sources(&identity.tenant, query.certificate_id) + .await + .map_err(api_error)?; + let count = sources.len(); + Ok(Json(ApiResponse::ok_list(sources, count))) +} + +pub async fn create( + State(state): State, + Extension(identity): Extension, + Json(input): Json, +) -> Result<(StatusCode, Json>), ApiErrorResponse> { + let created = state + .db + .create_source(&identity.tenant, input) + .await + .map_err(api_error)?; + Ok((StatusCode::CREATED, Json(ApiResponse::ok(created)))) +} + +pub async fn delete( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result { + state + .db + .delete_source(id, &identity.tenant) + .await + .map_err(api_error)?; + Ok(StatusCode::NO_CONTENT) +} + +#[derive(Debug, Deserialize)] +pub struct AttachRequest { + /// `null` detaches the channel without deleting it. + pub certificate_id: Option, +} + +/// Attach a channel to a certificate, or detach it. +pub async fn attach( + State(state): State, + Extension(identity): Extension, + Path(id): Path, + Json(request): Json, +) -> Result>, ApiErrorResponse> { + let source = state + .db + .set_source_certificate(id, request.certificate_id, &identity.tenant) + .await + .map_err(api_error)?; + Ok(Json(ApiResponse::ok(source))) +} + +/// Make one channel the certificate's preferred one. +pub async fn promote( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result>, ApiErrorResponse> { + let source = state + .db + .set_primary_source(id, &identity.tenant) + .await + .map_err(api_error)?; + Ok(Json(ApiResponse::ok(source))) +} + +/// Fetch once from a pull channel and import what comes back. +pub async fn run( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result>, ApiErrorResponse> { + let report = crate::cert_pull::pull_once( + &state.db, + Some(&state.process_manager), + id, + &identity.tenant, + ) + .await + .map_err(api_error)?; + Ok(Json(ApiResponse::ok(report))) +} + +/// Supported kinds, for the UI to render the right form. +pub async fn kinds() -> Json>> { + Json(ApiResponse::ok_list( + vec![ + SourceKind::Push.as_str(), + SourceKind::Certd.as_str(), + SourceKind::Url.as_str(), + ], + 3, + )) +} diff --git a/crates/rustfrp-daemon/src/api/cert_ingest_settings.rs b/crates/rustfrp-daemon/src/api/cert_ingest_settings.rs new file mode 100644 index 0000000..48028d5 --- /dev/null +++ b/crates/rustfrp-daemon/src/api/cert_ingest_settings.rs @@ -0,0 +1,395 @@ +//! Certificate ingest settings. +//! +//! The push endpoint is a fixed, two-route surface that never returns certificate +//! material, so it ships enabled on loopback: reaching it requires a token, and +//! the token is limited to the domains it was issued for. Widening it to other +//! hosts (Docker, a desktop on a LAN, a tunnel) is an explicit choice here, and +//! the change takes effect immediately — the listener is rebound in place. +//! +//! Endpoints: +//! - `GET /api/v1/cert-ingest` — current configuration and bound address +//! - `PUT /api/v1/cert-ingest` — enable/disable, set port, allow other hosts + +use axum::extract::{Extension, State}; +use axum::http::HeaderMap; +use axum::Json; +use serde::{Deserialize, Serialize}; + +use rustfrp_client::db::settings::{ + IngestConfig, CERT_INGEST_ENABLED, CERT_INGEST_EXPOSE, CERT_INGEST_PORT, +}; + +use super::auth::AuthIdentity; +use super::response::{api_error, ApiResponse}; +use super::state::ApiState; + +type ApiErrorResponse = (axum::http::StatusCode, Json>); + +/// Path the ingest listener and the control API both answer on. +pub const INGEST_PATH: &str = "/ingest/v1/certs"; + +/// Explicit public address for the push endpoint. +/// +/// Only needed when neither the browser origin nor the bind address describes +/// how a certificate manager reaches this daemon — a reverse proxy that maps a +/// path, or a container whose published port differs from the internal one. +pub const PUBLIC_URL_ENV: &str = "RUSTFRP_CERT_INGEST_PUBLIC_URL"; + +/// What the UI renders. `address` is the address actually bound, or null when +/// the listener is off; `error` explains a configuration that could not be bound. +#[derive(Debug, Serialize)] +pub struct IngestSettings { + pub enabled: bool, + pub expose: bool, + pub port: u16, + pub listen: String, + /// Address the listener is bound to right now, if any. + pub address: Option, + /// Last bind failure, cleared on a successful apply. + pub error: Option, + /// Ready-to-copy endpoint, derived from the address the browser used. + pub url: Option, + /// Where `url` came from: `public_url`, `request`, `bind` or `none`. + /// + /// The UI shows this so an operator can tell "the daemon guessed your + /// origin" from "you configured it", which is the difference between a + /// suggestion and a promise. + pub url_source: &'static str, + /// The push route on the control API itself, always usable because the + /// browser is already talking to it. + pub api_path: String, + /// Set when `RUSTFRP_CERT_INGEST_PUBLIC_URL` is in effect. + pub public_url: Option, +} + +#[derive(Debug, Deserialize)] +pub struct UpdateIngestSettings { + #[serde(default)] + pub enabled: Option, + #[serde(default)] + pub expose: Option, + #[serde(default)] + pub port: Option, +} + +pub async fn get( + State(state): State, + Extension(_identity): Extension, + headers: HeaderMap, +) -> Result>, ApiErrorResponse> { + let config = IngestConfig::load(&state.db).await.map_err(api_error)?; + let address = state.ingest.address().await; + Ok(Json(ApiResponse::ok(settings_for( + &config, address, &headers, + )))) +} + +pub async fn update( + State(state): State, + Extension(_identity): Extension, + headers: HeaderMap, + Json(request): Json, +) -> Result>, ApiErrorResponse> { + // Persist first, then bind: if the bind fails the stored choice still + // reflects what the operator asked for, and the error names the conflict. + if let Some(enabled) = request.enabled { + state + .db + .set_platform_setting(CERT_INGEST_ENABLED, bool_str(enabled)) + .await + .map_err(api_error)?; + } + if let Some(expose) = request.expose { + state + .db + .set_platform_setting(CERT_INGEST_EXPOSE, bool_str(expose)) + .await + .map_err(api_error)?; + } + if let Some(port) = request.port { + state + .db + .set_platform_setting(CERT_INGEST_PORT, &port.to_string()) + .await + .map_err(api_error)?; + } + + let config = IngestConfig::load(&state.db).await.map_err(api_error)?; + let applied = state.ingest.apply(&config).await; + let (address, error) = match applied { + Ok(address) => (address, None), + Err(error) => (None, Some(error)), + }; + + tracing::info!( + enabled = config.enabled, + expose = config.expose, + port = config.port, + bound = address.as_deref().unwrap_or("none"), + "Certificate ingest settings updated" + ); + + let mut settings = settings_for(&config, address, &headers); + settings.error = error; + Ok(Json(ApiResponse::ok(settings))) +} + +fn settings_for( + config: &IngestConfig, + address: Option, + headers: &HeaderMap, +) -> IngestSettings { + let public_url = public_url_override(); + let (url, url_source) = match (&public_url, address.as_deref()) { + (Some(public), _) => (Some(public.clone()), "public_url"), + (None, Some(bound)) => match request_origin(headers) { + Some(origin) => (Some(format!("{origin}{INGEST_PATH}")), "request"), + None => (Some(endpoint_url(bound, config)), "bind"), + }, + (None, None) => (None, "none"), + }; + IngestSettings { + enabled: config.enabled, + expose: config.expose, + port: config.port, + listen: config.listen.clone(), + address, + error: None, + url, + url_source, + api_path: INGEST_PATH.to_string(), + public_url, + } +} + +/// `scheme://authority` the caller actually used, honouring a reverse proxy. +/// +/// The `Host` header is what the browser addressed; `X-Forwarded-*` is what a +/// proxy in front tells us it terminated. Both are attacker-controlled, but this +/// value is only ever echoed back to the same authenticated caller as a +/// convenience — it is never used for routing, authentication, or a redirect. +fn request_origin(headers: &HeaderMap) -> Option { + let header = |name: &str| { + headers + .get(name) + .and_then(|value| value.to_str().ok()) + .map(str::trim) + .filter(|value| !value.is_empty()) + }; + + // `Forwarded: for=..;proto=https;host=example.com` — take the first element. + let forwarded = header("forwarded").and_then(|value| { + value.split(',').next().map(|element| { + element + .split(';') + .filter_map(|pair| pair.split_once('=')) + .map(|(key, value)| { + ( + key.trim().to_ascii_lowercase(), + value.trim().trim_matches('"').to_string(), + ) + }) + .collect::>() + }) + }); + + let scheme = header("x-forwarded-proto") + .and_then(|value| value.split(',').next()) + .map(|value| value.trim().to_ascii_lowercase()) + .or_else(|| { + forwarded + .as_ref() + .and_then(|map| map.get("proto")) + .map(|value| value.to_ascii_lowercase()) + }) + .filter(|scheme| scheme == "http" || scheme == "https"); + + let host = header("x-forwarded-host") + .and_then(|value| value.split(',').next()) + .map(str::trim) + .map(str::to_string) + .or_else(|| forwarded.as_ref().and_then(|map| map.get("host").cloned())) + .or_else(|| header("host").map(str::to_string)); + + let host = host?; + if !is_plausible_host(&host) { + return None; + } + // Without a proxy hint the connection is plain HTTP: the control API binds + // loopback or a LAN address and does not terminate TLS itself. + Some(format!( + "{}://{host}", + scheme.unwrap_or_else(|| "http".into()) + )) +} + +/// Reject anything that would turn the echoed value into an injection vector. +fn is_plausible_host(host: &str) -> bool { + !host.is_empty() + && host.len() <= 255 + && host.bytes().all(|byte| { + byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b':' | b'[' | b']' | b'_') + }) +} + +fn public_url_override() -> Option { + let value = std::env::var(PUBLIC_URL_ENV).ok()?; + let trimmed = value.trim().trim_end_matches('/'); + if trimmed.is_empty() { + return None; + } + // A configured value must already name the ingest route or a bare origin; + // either way it is normalised so the UI never shows a doubled path. + let base = trimmed + .strip_suffix(INGEST_PATH) + .unwrap_or(trimmed) + .trim_end_matches('/'); + let base = base.strip_suffix("/ingest/v1").unwrap_or(base); + Some(format!("{}{INGEST_PATH}", base.trim_end_matches('/'))) +} + +/// Build the URL from the bind address, for the case where there is no request +/// context to learn anything better from. +/// +/// A wildcard bind is reported with a placeholder: the point of the hint is +/// "this is where the endpoint answers", and `0.0.0.0` is not dialable. +fn endpoint_url(address: &str, config: &IngestConfig) -> String { + let host = match address.rsplit_once(':') { + Some(("0.0.0.0", _)) | Some(("[::]", _)) => "", + Some((host, _)) => host, + None => return INGEST_PATH.to_string(), + }; + format!("http://{host}:{}{INGEST_PATH}", config.port) +} + +fn bool_str(value: bool) -> &'static str { + if value { + "true" + } else { + "false" + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wildcard_bind_is_reported_as_a_placeholder_host() { + let config = IngestConfig { + enabled: true, + listen: "0.0.0.0".into(), + port: 7901, + expose: true, + }; + let url = endpoint_url("0.0.0.0:7901", &config); + assert!(url.contains(":7901"), "{url}"); + assert!(!url.contains("0.0.0.0"), "{url}"); + } + + #[test] + fn loopback_bind_reports_its_real_host() { + let config = IngestConfig { + enabled: true, + listen: "127.0.0.1".into(), + port: 7901, + expose: false, + }; + assert_eq!( + endpoint_url("127.0.0.1:7901", &config), + "http://127.0.0.1:7901/ingest/v1/certs" + ); + } + + fn config() -> IngestConfig { + IngestConfig { + enabled: true, + listen: "127.0.0.1".into(), + port: 7901, + expose: false, + } + } + + fn headers(pairs: &[(&str, &str)]) -> HeaderMap { + let mut map = HeaderMap::new(); + for (name, value) in pairs { + map.insert( + axum::http::HeaderName::from_bytes(name.as_bytes()).unwrap(), + value.parse().unwrap(), + ); + } + map + } + + /// The browser's own origin wins over the loopback bind, which is the whole + /// point: an operator on another host must never be told `127.0.0.1`. + #[test] + fn request_origin_beats_the_bind_address() { + let settings = settings_for( + &config(), + Some("127.0.0.1:7901".into()), + &headers(&[("host", "arch-rustfrp.powercess.com")]), + ); + assert_eq!( + settings.url.as_deref(), + Some("http://arch-rustfrp.powercess.com/ingest/v1/certs") + ); + assert_eq!(settings.url_source, "request"); + } + + /// A reverse proxy terminating TLS must produce `https`, not `http`. + #[test] + fn forwarded_proto_selects_the_scheme() { + let settings = settings_for( + &config(), + Some("127.0.0.1:7901".into()), + &headers(&[ + ("host", "127.0.0.1:7900"), + ("x-forwarded-host", "arch-rustfrp.powercess.com"), + ("x-forwarded-proto", "https"), + ]), + ); + assert_eq!( + settings.url.as_deref(), + Some("https://arch-rustfrp.powercess.com/ingest/v1/certs") + ); + } + + #[test] + fn forwarded_header_is_honoured() { + let settings = settings_for( + &config(), + Some("127.0.0.1:7901".into()), + &headers(&[("forwarded", "for=10.0.0.2;proto=https;host=example.com")]), + ); + assert_eq!( + settings.url.as_deref(), + Some("https://example.com/ingest/v1/certs") + ); + } + + /// A host header is echoed to the caller, so it must not carry a payload. + #[test] + fn implausible_host_falls_back_to_the_bind_address() { + let settings = settings_for( + &config(), + Some("127.0.0.1:7901".into()), + &headers(&[("host", "evil.com/")]), + ); + assert_eq!(settings.url_source, "bind"); + assert_eq!( + settings.url.as_deref(), + Some("http://127.0.0.1:7901/ingest/v1/certs") + ); + } + + /// With the listener off there is nothing to advertise, and the UI falls + /// back to the control API path the browser is already using. + #[test] + fn a_stopped_listener_advertises_no_url() { + let settings = settings_for(&config(), None, &headers(&[("host", "example.com")])); + assert!(settings.url.is_none()); + assert_eq!(settings.url_source, "none"); + assert_eq!(settings.api_path, INGEST_PATH); + } +} diff --git a/crates/rustfrp-daemon/src/api/certificates.rs b/crates/rustfrp-daemon/src/api/certificates.rs new file mode 100644 index 0000000..4867b71 --- /dev/null +++ b/crates/rustfrp-daemon/src/api/certificates.rs @@ -0,0 +1,358 @@ +//! Certificate management API. +//! +//! Every response carries [`Certificate`] metadata only. The private key and the +//! certificate body never cross this boundary: `CertificateSecrets` has no +//! `Serialize` implementation, so an accidental `Json(secrets)` does not compile. +//! +//! Endpoints: +//! - `GET /api/v1/certificates` — list with bindings and expiry +//! - `GET /api/v1/certificates/:id` — one certificate +//! - `POST /api/v1/certificates/pem` — import a pasted bundle +//! - `POST /api/v1/certificates/from-path` — import from files on this host +//! - `POST /api/v1/certificates/:id/apply` — write to every bound proxy +//! - `DELETE /api/v1/certificates/:id` — delete (refused while bound) +//! - `POST /api/v1/certificate-bindings` — bind to a proxy +//! - `DELETE /api/v1/certificate-bindings/:id` — unbind + +use axum::extract::{Extension, Path, Query, State}; +use axum::http::StatusCode; +use axum::Json; +use serde::{Deserialize, Serialize}; + +use rustfrp_client::cert::parse_bundle; +use rustfrp_client::db::certificate::{ + ImportRequest, SOURCE_LOCAL_PATH, SOURCE_MANUAL, SOURCE_UPLOAD, +}; + +use super::auth::AuthIdentity; +use super::response::{api_error, ApiResponse}; +use super::state::ApiState; + +/// Axum error tuple used by every handler in this module. +type ApiErrorResponse = (StatusCode, Json>); + +/// Request body for pasted-PEM import. +#[derive(Debug, Deserialize)] +pub struct PemImportRequest { + /// Leaf certificate, or a fullchain, PEM or bare base64 DER. + pub cert: String, + /// Private key, PEM or bare base64 DER. + pub key: String, + #[serde(default)] + pub note: Option, +} + +/// Request body for file import on the daemon host. +#[derive(Debug, Deserialize)] +pub struct PathImportRequest { + pub crt_path: String, + pub key_path: String, + #[serde(default)] + pub note: Option, +} + +/// Request body for upload import (already base64-decoded by the client). +#[derive(Debug, Deserialize)] +pub struct UploadImportRequest { + /// Certificate text, PEM or base64 DER. + pub cert: String, + pub key: String, + #[serde(default)] + pub filename: Option, +} + +#[derive(Debug, Deserialize)] +pub struct ListQuery { + /// Only certificates whose `not_after` is within this many days. + #[serde(default)] + pub expiring_within: Option, + #[serde(default)] + pub domain: Option, +} + +#[derive(Debug, Deserialize)] +pub struct BindRequest { + pub cert_id: i64, + pub proxy_id: i64, +} + +#[derive(Debug, Deserialize)] +pub struct UnbindQuery { + /// Also delete the files the last apply wrote. Off by default: leaving them + /// keeps a live listener serving until the operator changes the proxy. + #[serde(default)] + pub remove_files: bool, +} + +/// Outcome of an apply run. +#[derive(Debug, Serialize)] +pub struct ApplyReport { + pub applied: Vec, + pub failed: Vec, +} + +#[derive(Debug, Serialize)] +pub struct ApplyFailure { + pub binding_id: i64, + pub error: String, +} + +pub async fn list( + State(state): State, + Extension(identity): Extension, + Query(query): Query, +) -> Result>>, ApiErrorResponse> { + let certificates = state + .db + .list_certificates(&identity.tenant) + .await + .map_err(api_error)?; + + let domain = query.domain.map(|domain| domain.to_ascii_lowercase()); + let filtered: Vec<_> = certificates + .into_iter() + .filter(|certificate| { + if let Some(domain) = &domain { + if !certificate.primary_domain.eq_ignore_ascii_case(domain) + && !certificate + .sans + .iter() + .any(|san| san.eq_ignore_ascii_case(domain)) + { + return false; + } + } + match query.expiring_within { + // A certificate with no material yet has no expiry; it cannot + // be "expiring within N days". + Some(days) => certificate.days_remaining.is_some_and(|left| left <= days), + None => true, + } + }) + .collect(); + + let count = filtered.len(); + Ok(Json(ApiResponse::ok_list(filtered, count))) +} + +pub async fn get( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result>, ApiErrorResponse> { + let certificate = state + .db + .certificate_in_tenant(id, &identity.tenant) + .await + .map_err(api_error)?; + Ok(Json(ApiResponse::ok(certificate))) +} + +pub async fn import_pem( + State(state): State, + Extension(identity): Extension, + Json(request): Json, +) -> Result< + ( + StatusCode, + Json>, + ), + ApiErrorResponse, +> { + import_bundle( + &state, + &identity.tenant, + request.cert, + request.key, + SOURCE_MANUAL, + request.note, + ) + .await +} + +pub async fn import_upload( + State(state): State, + Extension(identity): Extension, + Json(request): Json, +) -> Result< + ( + StatusCode, + Json>, + ), + ApiErrorResponse, +> { + import_bundle( + &state, + &identity.tenant, + request.cert, + request.key, + SOURCE_UPLOAD, + request.filename, + ) + .await +} + +async fn import_bundle( + state: &ApiState, + tenant: &str, + cert: String, + key: String, + source: &str, + detail: Option, +) -> Result< + ( + StatusCode, + Json>, + ), + ApiErrorResponse, +> { + let parsed = parse_bundle(&cert, &key).map_err(api_error)?; + let certificate = state + .db + .import_certificate( + tenant, + ImportRequest { + parsed, + source: source.to_string(), + source_detail: detail.filter(|detail| !detail.trim().is_empty()), + }, + ) + .await + .map_err(api_error)?; + Ok((StatusCode::CREATED, Json(ApiResponse::ok(certificate)))) +} + +/// Import a bundle already present on this host, without copying it through +/// HTTP. Useful when an existing acme.sh / certbot deployment writes the files. +pub async fn import_from_path( + State(state): State, + Extension(identity): Extension, + Json(request): Json, +) -> Result< + ( + StatusCode, + Json>, + ), + ApiErrorResponse, +> { + let cert = tokio::fs::read_to_string(&request.crt_path) + .await + .map_err(|error| { + api_error(rustfrp_client::ClientError::CertificateTarget(format!( + "cannot read {}: {error}", + request.crt_path + ))) + })?; + let key = tokio::fs::read_to_string(&request.key_path) + .await + .map_err(|error| { + api_error(rustfrp_client::ClientError::CertificateTarget(format!( + "cannot read {}: {error}", + request.key_path + ))) + })?; + + import_bundle( + &state, + &identity.tenant, + cert, + key, + SOURCE_LOCAL_PATH, + Some(format!("{} + {}", request.crt_path, request.key_path)), + ) + .await +} + +pub async fn apply( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result>, ApiErrorResponse> { + let (applied, failed) = state + .db + .apply_certificate(id, &identity.tenant) + .await + .map_err(api_error)?; + + // The files are on disk; now make frpc read them. This has to be a restart, + // not a reload: `frpc reload` re-reads the proxy list but keeps the TLS + // certificate it loaded at startup, so a reload leaves the old certificate + // serving traffic — the failure looks like "apply did nothing". + // + // A restart drops every connection of that profile for a moment, which is + // the price of a new certificate; it is reported either way and never + // retroactively fails the apply, because the material is already in place. + let mut reload_errors = Vec::new(); + if !applied.is_empty() { + let profile_ids = state + .db + .profiles_for_certificate(id, &identity.tenant) + .await + .map_err(api_error)?; + for profile_id in profile_ids { + let Some(profile) = state.db.get_profile(profile_id).await.ok() else { + continue; + }; + if let Err(error) = state.process_manager.start(profile_id, &profile.name).await { + reload_errors.push(format!("profile {profile_id}: {error}")); + } + } + } + for error in &reload_errors { + tracing::warn!(%error, "frpc restart after certificate apply failed"); + } + + Ok(Json(ApiResponse::ok(ApplyReport { + applied, + failed: failed + .into_iter() + .map(|(binding_id, error)| ApplyFailure { binding_id, error }) + .collect(), + }))) +} + +pub async fn delete( + State(state): State, + Extension(identity): Extension, + Path(id): Path, +) -> Result { + state + .db + .delete_certificate(id, &identity.tenant) + .await + .map_err(api_error)?; + Ok(StatusCode::NO_CONTENT) +} + +pub async fn bind( + State(state): State, + Extension(identity): Extension, + Json(request): Json, +) -> Result< + ( + StatusCode, + Json>, + ), + ApiErrorResponse, +> { + let binding = state + .db + .bind_certificate(request.cert_id, request.proxy_id, &identity.tenant) + .await + .map_err(api_error)?; + Ok((StatusCode::CREATED, Json(ApiResponse::ok(binding)))) +} + +pub async fn unbind( + State(state): State, + Extension(identity): Extension, + Path(id): Path, + Query(query): Query, +) -> Result { + state + .db + .unbind_certificate(id, &identity.tenant, query.remove_files) + .await + .map_err(api_error)?; + Ok(StatusCode::NO_CONTENT) +} diff --git a/crates/rustfrp-daemon/src/api/environments.rs b/crates/rustfrp-daemon/src/api/environments.rs deleted file mode 100644 index 4b97290..0000000 --- a/crates/rustfrp-daemon/src/api/environments.rs +++ /dev/null @@ -1,164 +0,0 @@ -//! Deployment environment CRUD and profile assignment. - -use axum::extract::{Extension, Path, State}; -use axum::http::StatusCode; -use axum::Json; -use rustfrp_client::db::environment::Environment; -use serde::{Deserialize, Serialize}; - -use super::auth::AuthIdentity; -use super::response::{api_error, ApiError, ApiResponse}; -use super::state::ApiState; - -type ApiResult = Result>, (StatusCode, Json>)>; - -#[derive(Debug, Serialize)] -pub struct EnvironmentView { - #[serde(flatten)] - pub environment: Environment, - pub profile_ids: Vec, -} - -#[derive(Debug, Deserialize)] -pub struct Assignment { - pub environment_id: i64, -} - -async fn view( - state: &ApiState, - environment: Environment, -) -> Result { - let profile_ids = state - .db - .environment_profile_ids(environment.id.expect("persisted environment")) - .await?; - Ok(EnvironmentView { - environment, - profile_ids, - }) -} - -pub async fn list( - State(state): State, - Extension(identity): Extension, -) -> ApiResult> { - let environments = state - .db - .list_environments_for_tenant(&identity.tenant) - .await - .map_err(api_error)?; - let mut views = Vec::with_capacity(environments.len()); - for environment in environments { - views.push(view(&state, environment).await.map_err(api_error)?); - } - let count = views.len(); - Ok(Json(ApiResponse::ok_list(views, count))) -} - -pub async fn create( - State(state): State, - Extension(identity): Extension, - Json(mut environment): Json, -) -> Result<(StatusCode, Json>), (StatusCode, Json>)> { - environment.tenant_id = identity.tenant; - let id = state - .db - .insert_environment(&environment) - .await - .map_err(api_error)?; - environment.id = Some(id); - Ok(( - StatusCode::CREATED, - Json(ApiResponse::ok( - view(&state, environment).await.map_err(api_error)?, - )), - )) -} - -pub async fn update( - State(state): State, - Extension(identity): Extension, - Path(id): Path, - Json(mut environment): Json, -) -> ApiResult { - ensure_environment_tenant(&state, id, &identity.tenant).await?; - environment.id = Some(id); - environment.tenant_id = identity.tenant; - state - .db - .update_environment(&environment) - .await - .map_err(api_error)?; - Ok(Json(ApiResponse::ok( - view(&state, environment).await.map_err(api_error)?, - ))) -} - -pub async fn delete( - State(state): State, - Extension(identity): Extension, - Path(id): Path, -) -> ApiResult<()> { - ensure_environment_tenant(&state, id, &identity.tenant).await?; - state.db.delete_environment(id).await.map_err(api_error)?; - Ok(Json(ApiResponse::ok(()))) -} - -pub async fn assign_profile( - State(state): State, - Extension(identity): Extension, - Path(profile_id): Path, - Json(assignment): Json, -) -> ApiResult<()> { - ensure_profile_tenant(&state, profile_id, &identity.tenant).await?; - ensure_environment_tenant(&state, assignment.environment_id, &identity.tenant).await?; - state - .db - .set_profile_environment(profile_id, assignment.environment_id) - .await - .map_err(api_error)?; - Ok(Json(ApiResponse::ok(()))) -} - -fn hidden_resource() -> (StatusCode, Json>) { - ( - StatusCode::NOT_FOUND, - Json(ApiResponse { - success: false, - data: None, - count: None, - error: Some(ApiError::generic( - "TENANT_NOT_FOUND", - "resource was not found in the active tenant".into(), - )), - }), - ) -} - -pub(crate) async fn ensure_environment_tenant( - state: &ApiState, - id: i64, - tenant: &str, -) -> Result<(), (StatusCode, Json>)> { - state - .db - .environment_belongs_to_tenant(id, tenant) - .await - .map_err(api_error)? - .then_some(()) - .ok_or_else(hidden_resource) -} - -async fn ensure_profile_tenant( - state: &ApiState, - id: i64, - tenant: &str, -) -> Result<(), (StatusCode, Json>)> { - state - .db - .profile_belongs_to_tenant(id, tenant) - .await - .map_err(api_error)? - .then_some(()) - .ok_or_else(hidden_resource) -} diff --git a/crates/rustfrp-daemon/src/api/frp_versions.rs b/crates/rustfrp-daemon/src/api/frp_versions.rs index f5d82e2..befbd27 100644 --- a/crates/rustfrp-daemon/src/api/frp_versions.rs +++ b/crates/rustfrp-daemon/src/api/frp_versions.rs @@ -29,7 +29,13 @@ pub struct VersionListResponse { #[derive(Debug, Serialize)] pub struct AvailableVersionResponse { pub version: String, + pub tag: String, pub published_at: Option, + /// Archive size for this daemon's platform, when the release ships one. + pub asset_size: Option, + pub download_count: Option, + /// Archive filename, so the UI can show the exact URL per channel. + pub asset_name: String, } #[derive(Debug, Deserialize, Default)] @@ -94,15 +100,37 @@ pub async fn list(State(state): State) -> ApiResult) -> ApiResult> { + let platform = rustfrp_bin::platform::Platform::detect() + .ok_or_else(|| { + api_error( + StatusCode::INTERNAL_SERVER_ERROR, + "FRP_PLATFORM", + "Unsupported platform", + ) + })? + .slug; let versions = state .frp_versions .list_available() .await .map_err(binary_error)? .into_iter() - .map(|version| AvailableVersionResponse { - version: version.version().to_owned(), - published_at: version.published_at, + .map(|version| { + let asset = version.asset_for(&platform); + let asset_size = asset.map(|asset| asset.size).filter(|size| *size > 0); + let download_count = asset.map(|asset| asset.download_count); + let tag = version.tag().to_owned(); + AvailableVersionResponse { + version: version.version().to_owned(), + asset_name: rustfrp_bin::download::asset_filename( + &rustfrp_bin::FrpVersion::from_tag(&tag), + &platform, + ), + tag, + published_at: version.published_at, + asset_size, + download_count, + } }) .collect::>(); let count = versions.len(); @@ -137,6 +165,20 @@ pub async fn install( request.mirror_base.as_deref(), ) .map_err(binary_error)?; + let requested = request.version.trim().trim_start_matches('v').to_owned(); + if state + .frp_install + .read() + .map_err(|_| registry_error())? + .get(&requested) + .is_some_and(|task| task.status == "running") + { + return Err(api_error( + StatusCode::CONFLICT, + "FRP_INSTALL_BUSY", + "This version is already being installed", + )); + } state .frp_versions .install(&request.version, Some(&base)) @@ -224,6 +266,20 @@ pub async fn activate( pub async fn remove(State(state): State, Path(version): Path) -> ApiResult<()> { let _operation = state.frp_version_operation.lock().await; + let version = version.trim_start_matches('v').to_owned(); + if state + .frp_install + .read() + .map_err(|_| registry_error())? + .get(&version) + .is_some_and(|task| task.status == "running") + { + return Err(api_error( + StatusCode::CONFLICT, + "FRP_INSTALL_BUSY", + "Version is being installed; wait for the download to finish", + )); + } let target = state .frp_versions .version_path(&version) @@ -319,39 +375,62 @@ pub struct InstallTask { pub downloaded_bytes: u64, pub total_bytes: Option, pub error: Option, + pub started_at: chrono::DateTime, + pub finished_at: Option>, +} + +fn registry_error() -> (StatusCode, Json>) { + api_error( + StatusCode::INTERNAL_SERVER_ERROR, + "INSTALL_STATUS", + "Install status unavailable", + ) +} + +/// Mutate the task for `version` if it is still the same task instance. +fn update_task( + state: &ApiState, + version: &str, + task_id: &str, + apply: impl FnOnce(&mut InstallTask), +) { + if let Ok(mut tasks) = state.frp_install.write() { + if let Some(task) = tasks.get_mut(version).filter(|task| task.id == task_id) { + apply(task); + } + } } -pub async fn installation(State(state): State) -> ApiResult> { - let task = state +/// Every install task known to this daemon, newest first. +/// +/// Downloads are independent, so more than one can be running. The UI pairs a +/// task with an installed version by name instead of assuming a single global +/// task. +pub async fn installation(State(state): State) -> ApiResult> { + let mut tasks = state .frp_install .read() - .map_err(|_| { - api_error( - StatusCode::INTERNAL_SERVER_ERROR, - "INSTALL_STATUS", - "Install status unavailable", - ) - })? - .clone(); - Ok(Json(ApiResponse::ok(task))) + .map_err(|_| registry_error())? + .values() + .cloned() + .collect::>(); + tasks.sort_by_key(|task| std::cmp::Reverse(task.started_at)); + let count = tasks.len(); + Ok(Json(ApiResponse::ok_list(tasks, count))) } pub async fn begin_install( State(state): State, Json(request): Json, ) -> ApiResult { - let operation = state - .frp_version_operation - .clone() - .try_lock_owned() - .map_err(|_| { - api_error( - StatusCode::CONFLICT, - "FRP_OPERATION_BUSY", - "Another version operation is running", - ) - })?; let version = request.version.trim().trim_start_matches('v').to_owned(); + if version.is_empty() { + return Err(api_error( + StatusCode::BAD_REQUEST, + "FRP_VERSION_REQUIRED", + "version is required", + )); + } state .frp_versions .version_path(&version) @@ -370,21 +449,38 @@ pub async fn begin_install( downloaded_bytes: 0, total_bytes: None, error: None, + started_at: chrono::Utc::now(), + finished_at: None, }; - *state.frp_install.write().map_err(|_| { - api_error( - StatusCode::INTERNAL_SERVER_ERROR, - "INSTALL_STATUS", - "Install status unavailable", - ) - })? = Some(task.clone()); + { + let mut tasks = state.frp_install.write().map_err(|_| registry_error())?; + // Both the staging directory and the integrity marker belong to one + // version, so a second concurrent install of it would corrupt the + // first. Other versions proceed independently. + if tasks + .get(&version) + .is_some_and(|existing| existing.status == "running") + { + return Err(api_error( + StatusCode::CONFLICT, + "FRP_INSTALL_BUSY", + "This version is already being installed", + )); + } + tasks.insert(version.clone(), task.clone()); + } + let task_id = task.id.clone(); tokio::spawn(async move { - let _operation = operation; let progress_state = state.frp_install.clone(); + let progress_version = version.clone(); + let progress_task = task_id.clone(); let progress: rustfrp_bin::download::ProgressCallback = std::sync::Arc::new(move |update| { - if let Ok(mut status) = progress_state.write() { - if let Some(task) = status.as_mut() { + if let Ok(mut tasks) = progress_state.write() { + if let Some(task) = tasks + .get_mut(&progress_version) + .filter(|task| task.id == progress_task) + { task.stage = update.stage; if update.stage == "downloading" { task.downloaded_bytes = update.downloaded_bytes; @@ -397,20 +493,19 @@ pub async fn begin_install( .frp_versions .install_with_progress(&version, &source, progress) .await; - if let Ok(mut status) = state.frp_install.write() { - if let Some(task) = status.as_mut() { - match result { - Ok(_) => { - task.status = "completed"; - task.stage = "completed"; - } - Err(e) => { - task.status = "failed"; - task.error = Some(e.to_string()); - } + update_task(&state, &version, &task_id, |task| { + task.finished_at = Some(chrono::Utc::now()); + match result { + Ok(_) => { + task.status = "completed"; + task.stage = "completed"; + } + Err(error) => { + task.status = "failed"; + task.error = Some(error.to_string()); } } - }; + }); }); Ok(Json(ApiResponse::ok(task))) } diff --git a/crates/rustfrp-daemon/src/api/metrics.rs b/crates/rustfrp-daemon/src/api/metrics.rs index 5c09f57..1a8de12 100644 --- a/crates/rustfrp-daemon/src/api/metrics.rs +++ b/crates/rustfrp-daemon/src/api/metrics.rs @@ -13,7 +13,6 @@ use crate::metrics::{ResourceSample, TrafficSample, METRICS_HISTORY_CAPACITY}; #[derive(Debug, Default, Deserialize)] pub struct HistoryQuery { - pub environment_id: Option, pub profile_id: Option, pub limit: Option, } @@ -40,7 +39,6 @@ pub async fn history( let mut traffic = state .metrics .traffic( - query.environment_id, query.profile_id, query.limit.unwrap_or(METRICS_HISTORY_CAPACITY * 10), ) @@ -64,9 +62,6 @@ pub async fn history( traffic.retain(|sample| allowed.contains(&sample.profile_id)); traffic_collection.profiles.retain(|item| { allowed.contains(&item.profile_id) - && query - .environment_id - .is_none_or(|id| id == item.environment_id) && query.profile_id.is_none_or(|id| id == item.profile_id) }); for sample in &mut resources { @@ -78,15 +73,11 @@ pub async fn history( .await .into_iter() .filter(|s| { - allowed.contains(&s.profile_id) - && query.environment_id.is_none_or(|id| id == s.environment_id) - && query.profile_id.is_none_or(|id| id == s.profile_id) + allowed.contains(&s.profile_id) && query.profile_id.is_none_or(|id| id == s.profile_id) }) .collect(); traffic_collection.proxies.retain(|s| { - allowed.contains(&s.profile_id) - && query.environment_id.is_none_or(|id| id == s.environment_id) - && query.profile_id.is_none_or(|id| id == s.profile_id) + allowed.contains(&s.profile_id) && query.profile_id.is_none_or(|id| id == s.profile_id) }); let host_traffic = state.metrics.host_traffic().await; Json(ApiResponse::ok(HistoryResponse { @@ -132,35 +123,6 @@ pub async fn ingest_traffic( }), )); } - let expected = state - .db - .profile_environment_id(sample.profile_id) - .await - .map_err(|error| { - ( - super::response::status_code(&error), - Json(ApiResponse { - success: false, - data: None, - count: None, - error: Some(ApiError::from_client_error(&error)), - }), - ) - })?; - if sample.environment_id != expected { - return Err(( - StatusCode::UNPROCESSABLE_ENTITY, - Json(ApiResponse { - success: false, - data: None, - count: None, - error: Some(ApiError::generic( - "METRIC_ENV_MISMATCH", - "profile does not belong to the supplied environment".into(), - )), - }), - )); - } sample.timestamp = chrono::Utc::now(); sample.source = "external".into(); sample.proxy_id = None; @@ -171,7 +133,7 @@ pub async fn ingest_traffic( pub async fn prometheus(State(state): State) -> Response { let resources = state.metrics.resources(1).await; - let traffic = state.metrics.traffic(None, None, 360).await; + let traffic = state.metrics.traffic(None, 360).await; let mut output = String::from( "# HELP rustfrp_daemon_cpu_percent Daemon CPU usage percent.\n\ # TYPE rustfrp_daemon_cpu_percent gauge\n\ @@ -205,16 +167,16 @@ pub async fn prometheus(State(state): State) -> Response { } for sample in latest_traffic_by_profile(traffic) { output.push_str(&format!( - "rustfrp_traffic_received_bytes{{environment_id=\"{}\",profile_id=\"{}\"}} {}\n", - sample.environment_id, sample.profile_id, sample.received_bytes + "rustfrp_traffic_received_bytes{{profile_id=\"{}\"}} {}\n", + sample.profile_id, sample.received_bytes )); output.push_str(&format!( - "rustfrp_traffic_sent_bytes{{environment_id=\"{}\",profile_id=\"{}\"}} {}\n", - sample.environment_id, sample.profile_id, sample.sent_bytes + "rustfrp_traffic_sent_bytes{{profile_id=\"{}\"}} {}\n", + sample.profile_id, sample.sent_bytes )); output.push_str(&format!( - "rustfrp_active_connections{{environment_id=\"{}\",profile_id=\"{}\"}} {}\n", - sample.environment_id, sample.profile_id, sample.active_connections + "rustfrp_active_connections{{profile_id=\"{}\"}} {}\n", + sample.profile_id, sample.active_connections )); } Response::builder() @@ -230,7 +192,7 @@ pub async fn prometheus(State(state): State) -> Response { fn latest_traffic_by_profile(samples: Vec) -> Vec { let mut latest = std::collections::BTreeMap::new(); for sample in samples { - latest.insert((sample.environment_id, sample.profile_id), sample); + latest.insert(sample.profile_id, sample); } latest.into_values().collect() } @@ -239,17 +201,17 @@ fn latest_traffic_by_profile(samples: Vec) -> Vec mod tests { use super::*; use rustfrp_client::{ - config::model::FrpsProfile, - db::{environment::Environment, Database}, - process::manager::ProcessManager, - ClientState, + config::model::FrpsProfile, db::Database, process::manager::ProcessManager, ClientState, }; use rustfrp_common::signal::SignalHandler; use std::sync::Arc; use tokio::sync::RwLock; + /// A token may only see traffic for profiles in its own tenant. This is the + /// property worth testing: `tenant` still separates API callers even though + /// there is no environment axis any more. #[tokio::test] - async fn traffic_history_and_collection_status_are_tenant_and_environment_scoped() { + async fn traffic_history_is_scoped_to_the_calling_tenant() { let root = tempfile::tempdir().unwrap(); let db = Database::open(root.path().join("test.db").to_str().unwrap()) .await @@ -271,20 +233,8 @@ mod tests { }) .await .unwrap(); - let env = db - .insert_environment(&Environment { - name: "other".into(), - tenant_id: "other".into(), - is_default: true, - ..Default::default() - }) - .await - .unwrap(); - db.set_profile_environment(second, env).await.unwrap(); - // Tenancy lives on the profile itself, so move the profile rather than - // relying on its environment. db.set_profile_tenant(second, "other").await.unwrap(); - let own_env = db.profile_environment_id(first).await.unwrap(); + let manager = ProcessManager::new( root.path().into(), "/unused/frpc".into(), @@ -296,14 +246,13 @@ mod tests { root.path().into(), Arc::new(RwLock::new(ClientState::Ready)), ); - for (profile_id, environment_id) in [(first, own_env), (second, env)] { + for (profile_id, received) in [(first, 100), (second, 100)] { state .metrics .push_traffic(TrafficSample { timestamp: chrono::Utc::now(), profile_id, - environment_id, - received_bytes: 100, + received_bytes: received, sent_bytes: 200, active_connections: 1, source: "linux_tcp".into(), @@ -312,14 +261,11 @@ mod tests { interface: None, }) .await; - } - for (profile_id, environment_id) in [(first, own_env), (second, env)] { state .metrics .push_traffic(TrafficSample { timestamp: chrono::Utc::now(), profile_id, - environment_id, received_bytes: 300, sent_bytes: 400, active_connections: 1, @@ -337,26 +283,22 @@ mod tests { profiles: vec![ crate::traffic::ProfileCollection { profile_id: first, - environment_id: own_env, state: "collecting", }, crate::traffic::ProfileCollection { profile_id: second, - environment_id: env, state: "collecting", }, ], proxies: vec![ crate::traffic::ProxyCollection { profile_id: first, - environment_id: own_env, proxy_id: 123, name: "own".into(), state: "collecting", }, crate::traffic::ProxyCollection { profile_id: second, - environment_id: env, proxy_id: 123, name: "other".into(), state: "collecting", @@ -365,37 +307,24 @@ mod tests { ..Default::default() }) .await; + let identity = AuthIdentity { name: "test".into(), tenant: "default".into(), scopes: vec![], }; let Json(response) = history( - State(state.clone()), + State(state), Query(HistoryQuery::default()), - Extension(identity.clone()), + Extension(identity), ) .await; let payload = response.data.unwrap(); + assert_eq!(payload.traffic.len(), 1, "only the caller's tenant"); + assert_eq!(payload.traffic[0].profile_id, first); assert_eq!(payload.proxy_traffic.len(), 1); assert_eq!(payload.proxy_traffic[0].profile_id, first); assert_eq!(payload.traffic_collection.proxies.len(), 1); - assert_eq!(payload.traffic.len(), 1); - assert_eq!(payload.traffic[0].profile_id, first); assert_eq!(payload.traffic_collection.profiles.len(), 1); - let Json(response) = history( - State(state), - Query(HistoryQuery { - environment_id: Some(env), - ..Default::default() - }), - Extension(identity), - ) - .await; - let payload = response.data.unwrap(); - assert!(payload.traffic.is_empty()); - assert!(payload.proxy_traffic.is_empty()); - assert!(payload.traffic_collection.proxies.is_empty()); - assert!(payload.traffic_collection.profiles.is_empty()); } } diff --git a/crates/rustfrp-daemon/src/api/mod.rs b/crates/rustfrp-daemon/src/api/mod.rs index 950bd84..3a72d7a 100644 --- a/crates/rustfrp-daemon/src/api/mod.rs +++ b/crates/rustfrp-daemon/src/api/mod.rs @@ -11,8 +11,10 @@ pub mod state; // Sub-modules for each resource type pub mod auth; pub mod bindings; +pub mod cert_ingest; +pub mod cert_ingest_settings; +pub mod certificates; pub mod config_transfer; -pub mod environments; pub mod frp_versions; pub mod logs; pub mod metrics; @@ -181,6 +183,18 @@ async fn whoami(Extension(identity): Extension) -> Json Router { use crate::web; + // The push endpoint is also served by the control API, so a certificate + // manager can reach it at whatever address the operator already uses for the + // UI (a frpc `https` proxy, a reverse proxy, a LAN address). The dedicated + // listener on 7901 stays for deployments that publish that port instead. + // + // It keeps its own `X-Cert-Ingest-Token` auth and sits outside the API + // prefix, so the bearer middleware ignores it (see `required_scope` callers + // in `api::auth`, which pass through anything not under `/api/`). + let ingest_routes = crate::cert_ingest::create_ingest_router(crate::cert_ingest::IngestState { + db: state.db.clone(), + }); + // Group 1: API routes (matched first) let api_routes = Router::new() // Profile CRUD @@ -194,10 +208,6 @@ pub fn create_router(state: ApiState, auth: impl AuthMiddleware) -> Router { .put(profiles::update) .delete(profiles::delete), ) - .route( - "/api/v1/profiles/:id/environment", - axum::routing::put(environments::assign_profile), - ) .route( "/api/v1/profiles/:id/proxies", axum::routing::put(profiles::replace_proxies), @@ -218,14 +228,6 @@ pub fn create_router(state: ApiState, auth: impl AuthMiddleware) -> Router { "/api/v1/profiles/:id/stop", axum::routing::post(profiles::stop), ) - .route( - "/api/v1/environments", - axum::routing::get(environments::list).post(environments::create), - ) - .route( - "/api/v1/environments/:id", - axum::routing::put(environments::update).delete(environments::delete), - ) // Proxy CRUD .route( "/api/v1/proxies", @@ -290,6 +292,89 @@ pub fn create_router(state: ApiState, auth: impl AuthMiddleware) -> Router { axum::routing::get(metrics::prometheus), ) // Explicit migration import and consistent SQLite backup + // Certificate management: metadata only, no download/export route by design + .route( + "/api/v1/certificates", + axum::routing::get(certificates::list), + ) + .route( + "/api/v1/certificates/:id", + axum::routing::get(certificates::get).delete(certificates::delete), + ) + .route( + "/api/v1/certificates/pem", + axum::routing::post(certificates::import_pem), + ) + .route( + "/api/v1/certificates/upload", + axum::routing::post(certificates::import_upload), + ) + .route( + "/api/v1/certificates/from-path", + axum::routing::post(certificates::import_from_path), + ) + .route( + "/api/v1/certificates/:id/apply", + axum::routing::post(certificates::apply), + ) + .route( + "/api/v1/certificate-bindings", + axum::routing::post(certificates::bind), + ) + .route( + "/api/v1/certificate-bindings/:id", + axum::routing::delete(certificates::unbind), + ) + .route( + "/api/v1/cert-ingest", + axum::routing::get(cert_ingest_settings::get).put(cert_ingest_settings::update), + ) + // Channels: one resource for push tokens and pull sources. + .route( + "/api/v1/certificate-sources", + axum::routing::get(cert_ingest::list).post(cert_ingest::create), + ) + .route( + "/api/v1/certificate-sources/:id", + axum::routing::delete(cert_ingest::delete), + ) + .route( + "/api/v1/certificate-sources/:id/certificate", + axum::routing::put(cert_ingest::attach), + ) + .route( + "/api/v1/certificate-sources/:id/primary", + axum::routing::post(cert_ingest::promote), + ) + .route( + "/api/v1/certificate-sources/:id/run", + axum::routing::post(cert_ingest::run), + ) + .route( + "/api/v1/certificate-sources/kinds", + axum::routing::get(cert_ingest::kinds), + ) + // Aliases kept so existing automation and Certd instructions still work. + .route( + "/api/v1/cert-ingest-tokens", + axum::routing::get(cert_ingest::list).post(cert_ingest::create), + ) + .route( + "/api/v1/cert-ingest-tokens/:id", + axum::routing::delete(cert_ingest::delete), + ) + .route( + "/api/v1/cert-pull-sources", + axum::routing::get(cert_ingest::list).post(cert_ingest::create), + ) + .route( + "/api/v1/cert-pull-sources/:id", + axum::routing::delete(cert_ingest::delete), + ) + .route( + "/api/v1/cert-pull-sources/:id/run", + axum::routing::post(cert_ingest::run), + ) .route( "/api/v1/config/import", axum::routing::post(config_transfer::import), @@ -332,6 +417,7 @@ pub fn create_router(state: ApiState, auth: impl AuthMiddleware) -> Router { let auth = Arc::new(auth); Router::new() .merge(api_routes) + .merge(ingest_routes) .merge(static_routes) .fallback(web::serve_index) .layer( @@ -392,6 +478,21 @@ pub async fn serve_with_listener( state.db.clone(), state.process_manager.clone(), ); + // Certificate channels fetch on their own schedule; nothing about frpc + // supervision depends on it, so it is fire-and-forget. + crate::cert_pull::spawn_scheduler(state.db.clone(), state.process_manager.clone()); + + // Start the push endpoint from the stored settings. A bind failure is + // reported and surfaced through the settings API, never fatal: frpc + // supervision must not depend on a port being free. + match rustfrp_client::db::settings::IngestConfig::load(&state.db).await { + Ok(config) => { + if let Err(error) = state.ingest.apply(&config).await { + tracing::warn!(%error, "certificate ingest listener not started; fix it under Settings"); + } + } + Err(error) => tracing::warn!(%error, "could not read ingest settings"), + } let router = create_router(state, auth); diff --git a/crates/rustfrp-daemon/src/api/profiles.rs b/crates/rustfrp-daemon/src/api/profiles.rs index e1985c3..dc3d57e 100644 --- a/crates/rustfrp-daemon/src/api/profiles.rs +++ b/crates/rustfrp-daemon/src/api/profiles.rs @@ -69,21 +69,11 @@ pub async fn create( profile.created_at = now.clone(); profile.updated_at = now; - let environment_id = state - .db - .default_environment_for_tenant(&identity.tenant) - .await - .map_err(api_error)?; let id = state .db .insert_profile_for_tenant(&profile, &identity.tenant) .await .map_err(api_error)?; - - if let Err(error) = state.db.set_profile_environment(id, environment_id).await { - let _ = state.db.delete_profile(id).await; - return Err(api_error(error)); - } profile.id = Some(id); Ok(( axum::http::StatusCode::CREATED, diff --git a/crates/rustfrp-daemon/src/api/response.rs b/crates/rustfrp-daemon/src/api/response.rs index 523ac6c..ff021a9 100644 --- a/crates/rustfrp-daemon/src/api/response.rs +++ b/crates/rustfrp-daemon/src/api/response.rs @@ -89,6 +89,17 @@ pub fn status_code(err: &ClientError) -> StatusCode { // 409 — Duplicate ClientError::RecordAlreadyExists(_) => StatusCode::CONFLICT, + // 409 — Certificate still referenced by a proxy binding + ClientError::CertificateStillBound { .. } => StatusCode::CONFLICT, + + // 422 — Certificate input rejected: unparseable, mismatched key, bad target + ClientError::CertificateParse(_) + | ClientError::CertificateKeyMismatch(_) + | ClientError::CertificateTarget(_) => StatusCode::UNPROCESSABLE_ENTITY, + + // 500 — Master key missing or corrupt: the material cannot be decrypted + ClientError::CertificateSecrets(_) => StatusCode::INTERNAL_SERVER_ERROR, + // 422 — Validation failures ClientError::ConfigValidation(_) | ClientError::InvalidIpAddress(_) diff --git a/crates/rustfrp-daemon/src/api/state.rs b/crates/rustfrp-daemon/src/api/state.rs index c52614d..b45f9ce 100644 --- a/crates/rustfrp-daemon/src/api/state.rs +++ b/crates/rustfrp-daemon/src/api/state.rs @@ -49,7 +49,16 @@ pub struct ApiState { pub frp_versions: VersionManager, pub frp_version_operation: Arc>, pub metrics: MetricsStore, - pub frp_install: Arc>>, + /// One entry per in-flight or finished install, keyed by version. + /// + /// Downloads run concurrently: each version unpacks into its own directory, + /// so the only shared mutation is this map. Activation and deletion still + /// take `frp_version_operation` and consult this map first. + pub frp_install: + Arc>>, + /// The certificate ingest listener, so the settings API can start, stop, or + /// move it without a restart. + pub ingest: Arc, } impl ApiState { @@ -60,6 +69,7 @@ impl ApiState { config_dir: PathBuf, app_state: Arc>, ) -> Self { + let db_for_ingest = db.clone(); Self { process_manager: process_manager.with_database(db.clone()), db, @@ -70,7 +80,10 @@ impl ApiState { frp_versions: VersionManager::default(), frp_version_operation: Arc::new(tokio::sync::Mutex::new(())), metrics: MetricsStore::new(METRICS_HISTORY_CAPACITY), - frp_install: Arc::new(std::sync::RwLock::new(None)), + frp_install: Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())), + ingest: Arc::new(crate::cert_ingest::IngestServer::new( + crate::cert_ingest::IngestState { db: db_for_ingest }, + )), } } } diff --git a/crates/rustfrp-daemon/src/api/system.rs b/crates/rustfrp-daemon/src/api/system.rs index 61c8c62..4e37ca1 100644 --- a/crates/rustfrp-daemon/src/api/system.rs +++ b/crates/rustfrp-daemon/src/api/system.rs @@ -6,9 +6,7 @@ //! - GET /api/v1/reload/{task_id} — check reload task status //! - GET /api/v1/health — health check -use std::collections::HashSet; - -use axum::extract::{Extension, Path, Query, State}; +use axum::extract::{Extension, Path, State}; use axum::http::StatusCode; use axum::Json; use rustfrp_client::process::diagnostic::ProcessFailure; @@ -20,11 +18,7 @@ use super::state::{ApiState, ReloadPhase, ReloadTaskStatus}; /// Query parameters for the status endpoint. #[derive(Debug, Default, Deserialize)] -pub struct StatusQuery { - /// Restrict profile-derived counts and process info to one environment. - /// Proxies are reusable across profiles, so `total_proxies` stays tenant-wide. - pub environment_id: Option, -} +pub struct StatusQuery {} /// Status response payload #[derive(Debug, Serialize)] @@ -37,6 +31,9 @@ pub struct StatusResponse { pub total_bindings: usize, pub total_visitors: usize, pub processes: Vec, + /// Directory generated frpc TOMLs are written to. The UI uses it to suggest + /// default certificate paths that the daemon can actually create. + pub config_dir: String, } /// Process info in status response (avoids circular dependency by re-defining) @@ -53,15 +50,11 @@ pub struct ProcessInfoResponse { /// GET /api/v1/status — daemon and frpc process status /// -/// Counts and process info are tenant-scoped, and profile-derived values -/// (`total_profiles`, `total_bindings`, `total_visitors`, `active_frpc_instances`, -/// `processes`) are additionally narrowed to `environment_id` when supplied. -/// `total_proxies` stays tenant-wide: a proxy is reusable across profiles and so -/// has no single owning environment. +/// Counts and process info are tenant-scoped. There is no second axis: this is a +/// single-user platform, so every count describes the whole installation. pub async fn status( State(state): State, Extension(identity): Extension, - Query(query): Query, ) -> Result>, (StatusCode, Json>)> { let app_state = state.app_state.read().await; let state_str = app_state.to_string(); @@ -78,34 +71,12 @@ pub async fn status( ) }; - // An unknown or foreign environment is reported like any other foreign id: - // hidden rather than confirmed absent. - let scoped: Option> = match query.environment_id { - Some(id) => { - super::environments::ensure_environment_tenant(&state, id, &identity.tenant).await?; - Some( - state - .db - .environment_profile_ids(id) - .await - .map_err(fail)? - .into_iter() - .collect(), - ) - } - None => None, - }; - let in_scope = |profile_id: i64| scoped.as_ref().is_none_or(|ids| ids.contains(&profile_id)); - let profiles = state .db .list_profiles_for_tenant(&identity.tenant) .await - .map_err(fail)?; - let profiles = profiles - .into_iter() - .filter(|p| p.id.is_none_or(in_scope)) - .count(); + .map_err(fail)? + .len(); let proxies = state .db @@ -118,17 +89,16 @@ pub async fn status( .list_bindings_for_tenant(&identity.tenant) .await .map_err(fail)?; - let bindings = bindings.iter().filter(|b| in_scope(b.profile_id)).count(); + let bindings = bindings.len(); let visitors = state .db .list_visitors_for_tenant(&identity.tenant) .await .map_err(fail)?; - let visitors = visitors.iter().filter(|v| in_scope(v.profile_id)).count(); + let visitors = visitors.len(); let mut process_infos = state.process_manager.list_running().await; - process_infos.retain(|info| in_scope(info.profile_id)); // Fill in profile names for info in &mut process_infos { @@ -163,6 +133,7 @@ pub async fn status( total_bindings: bindings, total_visitors: visitors, processes, + config_dir: state.config_dir.to_string_lossy().to_string(), }))) } @@ -306,17 +277,17 @@ mod tests { use super::*; use crate::api::auth::AuthIdentity; use rustfrp_client::config::model::{BindingRule, FrpsProfile, LocalProxy, LocalVisitor}; - use rustfrp_client::db::{environment::Environment, migrate, Database}; + use rustfrp_client::db::{migrate, Database}; use rustfrp_client::process::manager::ProcessManager; use rustfrp_client::ClientState; use rustfrp_common::signal::SignalHandler; use std::sync::Arc; use tokio::sync::RwLock; - /// Counts and process rows follow `environment_id`, while proxies stay - /// tenant-wide because a proxy can be bound by profiles in any environment. + /// Counts describe the caller's tenant and nothing else. There is no second + /// axis any more: an installation is a single user. #[tokio::test] - async fn status_counts_and_processes_follow_the_selected_environment() { + async fn status_counts_the_calling_tenant() { let root = tempfile::tempdir().unwrap(); let db = Database::open(root.path().join("test.db").to_str().unwrap()) .await @@ -339,35 +310,29 @@ mod tests { }) .await .unwrap(); - let other = db - .insert_environment(&Environment { - name: "other".into(), - ..Default::default() - }) - .await - .unwrap(); - db.set_profile_environment(second, other).await.unwrap(); + db.set_profile_tenant(second, "other").await.unwrap(); let proxy = db .insert_proxy(&LocalProxy { - name: "shared".into(), + name: "web".into(), + local_ip: "127.0.0.1".into(), local_port: 8080, ..Default::default() }) .await .unwrap(); - for profile_id in [first, second] { + for profile in [first, second] { db.insert_binding(&BindingRule { - profile_id, + profile_id: profile, proxy_id: proxy, + enabled: true, ..Default::default() }) .await .unwrap(); db.insert_visitor(&LocalVisitor { - name: format!("visitor-{profile_id}"), - server_name: "server".into(), - profile_id, + name: format!("visitor-{profile}"), + profile_id: profile, ..Default::default() }) .await @@ -390,54 +355,34 @@ mod tests { scopes: vec!["*".into()], }; - let all = status( - State(state.clone()), - Extension(identity.clone()), - Query(StatusQuery::default()), - ) - .await - .unwrap() - .0 - .data - .unwrap(); - assert_eq!(all.total_profiles, 2); - assert_eq!(all.total_proxies, 1); - assert_eq!(all.total_bindings, 2); - assert_eq!(all.total_visitors, 2); - - let scoped = status( - State(state.clone()), - Extension(identity.clone()), - Query(StatusQuery { - environment_id: Some(other), - }), - ) - .await - .unwrap() - .0 - .data - .unwrap(); - assert_eq!(scoped.total_profiles, 1); - assert_eq!(scoped.total_bindings, 1); - assert_eq!(scoped.total_visitors, 1); - assert_eq!( - scoped.total_proxies, 1, - "proxies are reusable across environments" - ); - assert!(scoped.processes.iter().all(|p| p.profile_id == second)); + let own = status(State(state.clone()), Extension(identity.clone())) + .await + .unwrap() + .0 + .data + .unwrap(); + assert_eq!(own.total_profiles, 1, "only this tenant's profile"); + assert_eq!(own.total_bindings, 1); + assert_eq!(own.total_visitors, 1); + assert_eq!(own.total_proxies, 1); + assert!(own.processes.iter().all(|p| p.profile_id == first)); - // A foreign environment id behaves like any other hidden resource. + // Another tenant sees nothing, and nothing it can probe either. let foreign = status( State(state), Extension(AuthIdentity { tenant: "elsewhere".into(), ..identity }), - Query(StatusQuery { - environment_id: Some(other), - }), ) - .await; - assert_eq!(foreign.unwrap_err().0, StatusCode::NOT_FOUND); + .await + .unwrap() + .0 + .data + .unwrap(); + assert_eq!(foreign.total_profiles, 0); + assert_eq!(foreign.total_bindings, 0); + assert_eq!(foreign.total_visitors, 0); + assert!(foreign.processes.is_empty()); } } diff --git a/crates/rustfrp-daemon/src/cert_ingest.rs b/crates/rustfrp-daemon/src/cert_ingest.rs new file mode 100644 index 0000000..78448a3 --- /dev/null +++ b/crates/rustfrp-daemon/src/cert_ingest.rs @@ -0,0 +1,451 @@ +//! Dedicated certificate ingest listener. +//! +//! Kept apart from the control API on purpose. The control API is a +//! configuration plane bound to loopback with scoped bearer tokens; the ingest +//! listener is the one surface that may be reachable from another host (a Certd +//! instance, a renew hook), so it exposes exactly two routes and nothing else: +//! +//! ```text +//! POST /ingest/v1/certs — accept a certificate bundle +//! GET /ingest/v1/healthz — liveness, no information +//! ``` +//! +//! Reaching it grants no read access: there is no route that returns a +//! certificate, a key, a proxy, or a setting. The worst a leaked token can do is +//! overwrite the certificate material of a domain it is explicitly allowed to +//! serve — and only through the paths an operator already configured. + +use std::net::SocketAddr; +use std::sync::Arc; + +use axum::extract::{DefaultBodyLimit, State}; +use axum::http::{HeaderMap, StatusCode}; +use axum::response::{IntoResponse, Response}; +use axum::routing::{get, post}; +use axum::{Json, Router}; +use serde::Deserialize; + +use rustfrp_client::cert::{domain, parse_bundle}; +use rustfrp_client::db::certificate::{ImportRequest, SOURCE_WEBHOOK}; +use rustfrp_client::db::settings::IngestConfig; +use rustfrp_client::db::Database; +use rustfrp_client::ClientError; + +/// Header carrying the ingest token. Never a path segment: URLs end up in the +/// caller's logs, request headers do not. +pub const TOKEN_HEADER: &str = "x-cert-ingest-token"; + +/// A bundle is a few kilobytes; this bounds what one request can make us parse. +const MAX_BODY_BYTES: usize = 64 * 1024; + +/// Shared state for the ingest listener. Deliberately not `ApiState`: this +/// listener must not be able to reach the process manager or the version +/// registry, even by accident. +#[derive(Clone)] +pub struct IngestState { + pub db: Database, +} + +/// Accepted request body. Both fields are PEM or bare base64 DER; `chain` is +/// accepted for callers that split the fullchain themselves. +#[derive(Debug, Deserialize)] +pub struct IngestRequest { + #[serde(alias = "cert", alias = "one")] + pub crt: String, + pub key: String, + #[serde(default)] + pub chain: Option, + #[serde(default)] + pub domains: Option>, +} + +/// Response body. Metadata only — never the material that was submitted. +#[derive(Debug, serde::Serialize)] +struct IngestResponse { + ok: bool, + certificate_id: i64, + primary_domain: String, + not_after: String, + #[serde(skip_serializing_if = "Vec::is_empty")] + warnings: Vec, + /// Hex of the stored fingerprint, so a caller can log what it sent. + fingerprint_sha256: String, +} + +/// Build the ingest router. Two routes, no fallback, no static assets. +pub fn create_ingest_router(state: IngestState) -> Router { + Router::new() + .route("/ingest/v1/certs", post(ingest_certificate)) + .route("/ingest/v1/healthz", get(healthz)) + .layer(DefaultBodyLimit::max(MAX_BODY_BYTES)) + .with_state(state) +} + +/// Bind and serve until shutdown. +pub async fn serve_ingest(state: IngestState, listen: &str) -> anyhow::Result<()> { + let listener = bind_ingest(listen).await?; + axum::serve(listener, create_ingest_router(state)).await?; + Ok(()) +} + +/// One running ingest listener, replaceable at runtime. +/// +/// The listener can be switched on, off, or moved to another port while the +/// daemon keeps running, so "open the endpoint" in the UI takes effect without a +/// restart. Rebinding is serialised: two concurrent changes must not both think +/// they own the port. +pub struct IngestServer { + state: IngestState, + binding: Arc>>, +} + +struct Running { + address: String, + shutdown: tokio::sync::oneshot::Sender<()>, + task: tokio::task::JoinHandle<()>, +} + +impl IngestServer { + pub fn new(state: IngestState) -> Self { + Self { + state, + binding: Arc::new(tokio::sync::Mutex::new(None)), + } + } + + /// Currently bound address, if the listener is running. + pub async fn address(&self) -> Option { + self.binding + .lock() + .await + .as_ref() + .map(|running| running.address.clone()) + } + + /// Start, stop, or move the listener to match `config`. + /// + /// A bind failure is reported, not fatal: the daemon's job is to serve frpc, + /// and a taken port must not stop it from starting. The caller surfaces the + /// error so an operator sees why the endpoint is not reachable. + pub async fn apply(&self, config: &IngestConfig) -> Result, String> { + let mut binding = self.binding.lock().await; + + if !config.enabled { + if let Some(running) = binding.take() { + stop(running).await; + tracing::info!("Certificate ingest listener stopped (disabled)"); + } + return Ok(None); + } + + let wanted = config.address(); + if binding + .as_ref() + .is_some_and(|running| running.address == wanted) + { + return Ok(Some(wanted)); + } + + // Any address change goes through a full stop: rebinding the same port + // in place would race with the old listener still holding it. + if let Some(running) = binding.take() { + stop(running).await; + } + + match bind_ingest(&wanted).await { + Ok(listener) => { + let router = create_ingest_router(self.state.clone()); + let (shutdown, signal) = tokio::sync::oneshot::channel(); + let task = tokio::spawn(async move { + let served = axum::serve(listener, router) + .with_graceful_shutdown(async { + let _ = signal.await; + }) + .await; + if let Err(error) = served { + tracing::error!(%error, "certificate ingest listener stopped"); + } + }); + *binding = Some(Running { + address: wanted.clone(), + shutdown, + task, + }); + Ok(Some(wanted)) + } + Err(error) => { + // Leaving `binding` empty keeps the reported state honest: the + // endpoint really is not listening. + tracing::warn!(address = %wanted, %error, "certificate ingest listener could not bind"); + Err(format!("could not listen on {wanted}: {error}")) + } + } + } +} + +async fn stop(running: Running) { + let _ = running.shutdown.send(()); + // Bounded wait: a slow in-flight push must not block a settings change + // forever, and the listener is idempotent to abandon. + if tokio::time::timeout(std::time::Duration::from_secs(5), running.task) + .await + .is_err() + { + tracing::warn!("certificate ingest listener did not stop cleanly"); + } +} + +async fn bind_ingest(address: &str) -> anyhow::Result { + let addr: SocketAddr = address.parse()?; + let listener = tokio::net::TcpListener::bind(addr).await?; + tracing::info!( + bound = %listener.local_addr()?, + "Certificate ingest listener started (header token only, 2 routes)" + ); + Ok(listener) +} + +async fn healthz() -> impl IntoResponse { + Json(serde_json::json!({ "ok": true })) +} + +async fn ingest_certificate( + State(state): State, + headers: HeaderMap, + Json(request): Json, +) -> Response { + let Some(presented) = headers + .get(TOKEN_HEADER) + .and_then(|value| value.to_str().ok()) + .map(str::trim) + .filter(|value| !value.is_empty()) + else { + // No token shape hint beyond "unauthorized": a prober learns nothing + // about whether the header or the endpoint is missing. + return unauthorized("ingest token required"); + }; + + let resolved = match state.db.authenticate_source_token(presented).await { + Ok(resolved) => resolved, + Err(error) => { + tracing::error!(%error, "ingest token lookup failed"); + return failure( + StatusCode::INTERNAL_SERVER_ERROR, + "CERT_002", + &error.to_string(), + ); + } + }; + let Some(grant) = resolved else { + return unauthorized("ingest token is unknown, revoked, or expired"); + }; + let tenant = grant.tenant.clone(); + + let parsed = match parse_bundle(&request.crt, &request.key) { + Ok(parsed) => parsed, + Err(error) => { + return failure( + StatusCode::UNPROCESSABLE_ENTITY, + error.code(), + &error.to_string(), + ) + } + }; + + // A token may only submit certificates for the domains it was issued for. + // Check every SAN, not just the primary one: a wildcard or multi-SAN + // certificate must not become a way to serve an unrelated host. + let disallowed = + domain::disallowed(&parsed.sans, &parsed.primary_domain, &grant.allowed_domains); + if !disallowed.is_empty() { + tracing::warn!( + domain = %parsed.primary_domain, + rejected = ?disallowed, + "ingest token submitted a certificate for a domain it does not cover" + ); + return failure( + StatusCode::FORBIDDEN, + "CERT_006", + "the certificate covers domains this token is not allowed to serve", + ); + } + + // A token attached to one certificate must not be able to replace a + // different certificate's material just because the domains overlap. + if let Some(attached) = grant.certificate_id { + let expected = match state.db.certificate_in_tenant(attached, &tenant).await { + Ok(certificate) => certificate, + Err(error) => return failure(StatusCode::NOT_FOUND, error.code(), &error.to_string()), + }; + if !domain::covers_target( + &parsed.sans, + &parsed.primary_domain, + &expected.primary_domain, + ) { + tracing::warn!( + certificate_id = attached, + expected = %expected.primary_domain, + presented = %parsed.primary_domain, + "ingest token submitted a certificate for a different domain" + ); + return failure( + StatusCode::FORBIDDEN, + "CERT_008", + "the certificate does not cover the domain this token is bound to", + ); + } + } + + let certificate = match state + .db + .import_certificate( + &tenant, + ImportRequest { + parsed, + source: SOURCE_WEBHOOK.to_string(), + source_detail: request + .domains + .as_ref() + .map(|domains| domains.join(",")) + .filter(|detail| !detail.is_empty()), + }, + ) + .await + { + Ok(certificate) => certificate, + Err(error) => { + let status = match &error { + ClientError::CertificateParse(_) | ClientError::CertificateKeyMismatch(_) => { + StatusCode::UNPROCESSABLE_ENTITY + } + _ => StatusCode::INTERNAL_SERVER_ERROR, + }; + return failure(status, error.code(), &error.to_string()); + } + }; + + // Write to any proxy already bound to this certificate, but never create a + // binding here: which proxy serves which domain is an operator decision. + if !certificate.bindings.is_empty() { + match state.db.apply_certificate(certificate.id, &tenant).await { + Ok((applied, failed)) => tracing::info!( + certificate_id = certificate.id, + applied = applied.len(), + failed = failed.len(), + "Certificate ingested and applied" + ), + Err(error) => tracing::warn!( + certificate_id = certificate.id, + %error, + "Certificate ingested but apply failed" + ), + } + } + + ( + StatusCode::OK, + Json(IngestResponse { + ok: true, + certificate_id: certificate.id, + primary_domain: certificate.primary_domain, + not_after: certificate.not_after, + warnings: certificate.warnings, + fingerprint_sha256: certificate.fingerprint_sha256, + }), + ) + .into_response() +} + +fn unauthorized(message: &str) -> Response { + failure(StatusCode::UNAUTHORIZED, "CERT_007", message) +} + +fn failure(status: StatusCode, code: &str, message: &str) -> Response { + ( + status, + Json(serde_json::json!({ + "ok": false, + "error": { "code": code, "message": message }, + })), + ) + .into_response() +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::body::Body; + use axum::http::Request; + use tower::ServiceExt; + + /// The ingest listener must not grow a control-plane surface: any path other + /// than the two declared routes is a 404, including the API prefix. + #[tokio::test] + async fn exposes_only_the_two_declared_routes() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + rustfrp_client::db::migrate::run(&conn).unwrap(); + } + let router = create_ingest_router(IngestState { db }); + + for (uri, expected) in [ + ("/ingest/v1/healthz", StatusCode::OK), + ("/api/v1/certificates", StatusCode::NOT_FOUND), + ("/api/v1/proxies", StatusCode::NOT_FOUND), + ("/api/v1/config/export", StatusCode::NOT_FOUND), + ("/", StatusCode::NOT_FOUND), + ("/ingest/v1/certs/anything", StatusCode::NOT_FOUND), + ] { + let response = router + .clone() + .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap()) + .await + .unwrap(); + assert_eq!(response.status(), expected, "unexpected status for {uri}"); + } + } + + #[tokio::test] + async fn rejects_missing_and_unknown_tokens() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + rustfrp_client::db::migrate::run(&conn).unwrap(); + } + let router = create_ingest_router(IngestState { db }); + let body = serde_json::json!({ "crt": "x", "key": "y" }).to_string(); + + let missing = router + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/ingest/v1/certs") + .header("content-type", "application/json") + .body(Body::from(body.clone())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(missing.status(), StatusCode::UNAUTHORIZED); + + let unknown = router + .oneshot( + Request::builder() + .method("POST") + .uri("/ingest/v1/certs") + .header("content-type", "application/json") + .header(TOKEN_HEADER, "0".repeat(64)) + .body(Body::from(body)) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(unknown.status(), StatusCode::UNAUTHORIZED); + } +} diff --git a/crates/rustfrp-daemon/src/cert_pull.rs b/crates/rustfrp-daemon/src/cert_pull.rs new file mode 100644 index 0000000..df8b2a4 --- /dev/null +++ b/crates/rustfrp-daemon/src/cert_pull.rs @@ -0,0 +1,755 @@ +//! Outbound certificate pulls. +//! +//! Two source kinds, one flow: resolve credentials, fetch, parse the bundle, and +//! hand it to the same import path a pushed certificate uses. No signing, no +//! issuance — RustFRP only consumes what a certificate manager already produced. +//! +//! ## Certd open API +//! +//! `GET /api/v1/cert/get` on the Certd instance. Authentication is the +//! `x-certd-token` header: +//! +//! ```text +//! content = {"keyId":..,"t":,"encrypt":false,"signType":"md5"} +//! sign = md5(content + keySecret) +//! header = base64(content) + "." + base64(sign) +//! ``` +//! +//! `encrypt:false` is deliberate: encrypted responses would need Certd's own +//! key derivation, which buys nothing over the TLS the request already uses. + +use base64::Engine; +use md5::{Digest, Md5}; +use serde::{Deserialize, Serialize}; + +use rustfrp_client::cert::{domain, parse_bundle}; +use rustfrp_client::db::cert_ingest::SourceKind; +use rustfrp_client::db::certificate::{ImportRequest, SOURCE_CERTD_PULL, SOURCE_URL_PULL}; +use rustfrp_client::db::Database; +use rustfrp_client::process::manager::ProcessManager; +use rustfrp_client::ClientError; + +/// Caps for an outbound fetch: a certificate bundle is small, and a hung peer +/// must not pin a request for a minute. +const FETCH_TIMEOUT_SECS: u64 = 10; +const MAX_BODY_BYTES: usize = 256 * 1024; + +/// What one pull did. +#[derive(Debug, Serialize)] +pub struct PullReport { + pub source_id: i64, + pub status: String, + pub certificate_id: Option, + pub primary_domain: Option, + /// Bindings rewritten because their files were behind the new material. + pub applied: usize, + pub message: String, +} + +/// How long to wait before the next automatic run. +/// +/// A certificate lives 90 days and is reissued with days to spare, so a daily +/// check is plenty and keeps the outbound request rate trivially low. +pub const PULL_INTERVAL_SECS: i64 = 24 * 60 * 60; + +/// Fetch and import once. Records the outcome on the channel either way. +/// +/// `processes` is optional so the database-level tests can run without frpc; when +/// present, a profile whose certificate files were rewritten is restarted, since +/// frpc only reads `crtPath` at process start. +pub async fn pull_once( + db: &Database, + processes: Option<&ProcessManager>, + id: i64, + tenant: &str, +) -> Result { + match pull_inner(db, processes, id, tenant).await { + Ok(report) => { + db.record_source_run(id, tenant, "success", None, Some(&next_run())) + .await?; + Ok(report) + } + Err(error) => { + // Keep the same interval on failure: backing off would let an + // intermittent error hide a certificate that is about to expire. + db.record_source_run( + id, + tenant, + "failed", + Some(&error.to_string()), + Some(&next_run()), + ) + .await?; + Err(error) + } + } +} + +fn next_run() -> String { + (chrono::Utc::now() + chrono::Duration::seconds(PULL_INTERVAL_SECS)).to_rfc3339() +} + +async fn pull_inner( + db: &Database, + processes: Option<&ProcessManager>, + id: i64, + tenant: &str, +) -> Result { + let source = db.source(id, tenant).await?; + if !source.enabled { + return Err(ClientError::ConfigValidation( + "this channel is disabled".into(), + )); + } + if !source.kind.is_pull() { + return Err(ClientError::ConfigValidation( + "only a pull channel can be fetched; a push channel waits for the sender".into(), + )); + } + let secret = db.source_secret(id, tenant).await?; + + let client = reqwest::Client::builder() + .timeout(std::time::Duration::from_secs(FETCH_TIMEOUT_SECS)) + .build() + .map_err(|e| ClientError::CertificateTarget(format!("failed to build HTTP client: {e}")))?; + + let (cert, key, source_label) = match source.kind { + SourceKind::Certd => { + let response = fetch_certd(&client, &source.base_url, &source.domain, &secret).await?; + let fullchain = response.fullchain()?; + let key = response.key.ok_or_else(|| { + ClientError::CertificateParse( + "Certd returned a certificate without a private key".into(), + ) + })?; + (fullchain, key, SOURCE_CERTD_PULL) + } + SourceKind::Url => { + let body = fetch_url(&client, &source.base_url, &secret).await?; + let (cert, key) = split_url_body(&body)?; + (cert, key, SOURCE_URL_PULL) + } + SourceKind::Push => { + return Err(ClientError::ConfigValidation( + "a push channel is driven by the sender, not fetched".into(), + )) + } + }; + + let parsed = parse_bundle(&cert, &key)?; + + // The certificate must actually serve the domain this channel was created + // for. A misconfigured or hostile endpoint must not be able to install a + // certificate for an unrelated host, and containment (not equality) is the + // right test: a CA may reissue with extra SANs. + if !domain::covers_target(&parsed.sans, &parsed.primary_domain, &source.domain) { + return Err(ClientError::CertificateTarget(format!( + "the fetched certificate serves {}, which does not cover the channel's domain {}", + domain::served_domains(&parsed.sans, &parsed.primary_domain).join(", "), + source.domain + ))); + } + + let certificate = db + .import_certificate( + tenant, + ImportRequest { + parsed, + source: source_label.to_string(), + source_detail: Some(format!("{} ({})", source.name, source.base_url)), + }, + ) + .await?; + + // A channel attached to a certificate replaces that certificate's material; + // an unbound one resolves (or creates) the identity by domain, so a pull + // that was configured before the certificate still lands somewhere. + let certificate = match source.certificate_id { + Some(attached) if attached != certificate.id => { + return Err(ClientError::CertificateTarget(format!( + "the fetched certificate was stored as {}, but this channel is attached to {}", + certificate.primary_domain, attached + ))) + } + Some(_) => certificate, + None => match db + .set_source_certificate(id, Some(certificate.id), tenant) + .await + { + Ok(_) => certificate, + Err(error) => { + tracing::warn!(source_id = id, %error, "pulled certificate could not be attached"); + certificate + } + }, + }; + + // A renewal should reach the proxies the operator already bound, without a + // second manual click — including the process that serves it, because + // `frpc reload` keeps the TLS certificate it loaded at startup. + let applied = match db.apply_stale_certificates(tenant).await { + Ok(applied) => applied, + Err(error) => { + tracing::warn!(source_id = id, %error, "automatic apply after pull failed"); + 0 + } + }; + if applied > 0 { + if let Some(processes) = processes { + match db.profiles_for_certificate(certificate.id, tenant).await { + Ok(profile_ids) => { + for profile_id in profile_ids { + let Ok(profile) = db.get_profile(profile_id).await else { + continue; + }; + if let Err(error) = processes.start(profile_id, &profile.name).await { + tracing::warn!( + profile_id, + %error, + "frpc restart after automatic certificate apply failed" + ); + } + } + } + Err(error) => { + tracing::warn!(%error, "could not resolve profiles for a renewed certificate") + } + } + } + } + + Ok(PullReport { + source_id: id, + status: "success".into(), + certificate_id: Some(certificate.id), + primary_domain: Some(certificate.primary_domain.clone()), + applied, + message: format!( + "imported {}, expires {}", + certificate.primary_domain, certificate.not_after + ), + }) +} + +/// Certd's `CertInfo` response. +/// +/// `crt` is normally the fullchain. Some pipeline steps return the leaf in `oc` +/// and the intermediates in `ic` instead, so both shapes are assembled here. +#[derive(Debug, Deserialize)] +struct CertdCertInfo { + #[serde(default)] + crt: String, + #[serde(default)] + key: Option, + /// Certificate only, without intermediates. + #[serde(default)] + oc: Option, + /// Intermediates, concatenated. + #[serde(default)] + ic: Option, +} + +impl CertdCertInfo { + fn fullchain(&self) -> Result { + if !self.crt.trim().is_empty() { + return Ok(self.crt.clone()); + } + match (&self.oc, &self.ic) { + (Some(leaf), Some(intermediates)) => Ok(format!("{leaf}\n{intermediates}")), + (Some(leaf), None) => Ok(leaf.clone()), + _ => Err(ClientError::CertificateParse( + "Certd response carried no certificate body".into(), + )), + } + } +} + +/// Certd wraps every payload in `{code, message, data}`. +/// +/// `message` is the field real deployments use; `msg` is accepted as well +/// because older builds emitted it. Both were previously ignored on failure, +/// which turned every rejection into "no message (code 20002)" — the code alone +/// does not tell an operator whether the key, the clock, or the domain is wrong. +#[derive(Debug, Deserialize)] +struct CertdEnvelope { + code: Option, + #[serde(default, alias = "msg")] + message: Option, + data: Option, +} + +async fn fetch_certd( + client: &reqwest::Client, + base_url: &str, + domain: &str, + secret: &rustfrp_client::db::cert_ingest::SourceSecret, +) -> Result { + let key_id = secret + .key_id + .as_deref() + .ok_or_else(|| ClientError::ConfigValidation("Certd source has no key id".into()))?; + let key_secret = secret + .key_secret + .as_deref() + .ok_or_else(|| ClientError::ConfigValidation("Certd source has no key secret".into()))?; + + let url = format!( + "{}/api/v1/cert/get?domains={}", + base_url.trim_end_matches('/'), + urlencode(domain) + ); + // A skewed clock is the one failure this daemon can work around: Certd's + // own `Date` header says what "now" is, and the signature is nothing but + // (key, timestamp). Trying again with that value turns a permanent + // "timestamp error" into a successful pull on a machine whose clock is wrong + // — which is exactly the machine that cannot fix it without root. + let first = certd_request(client, &url, key_id, key_secret, None).await; + match first { + Ok(info) => Ok(info), + Err(CertdFailure::Clock(server_time)) => { + tracing::warn!( + local = chrono::Utc::now().timestamp(), + server = server_time, + "Certd rejected the request timestamp; retrying with the server's clock" + ); + certd_request(client, &url, key_id, key_secret, Some(server_time)) + .await + .map_err(|failure| failure.into_error()) + } + Err(failure) => Err(failure.into_error()), + } +} + +/// Why a Certd call failed, so the caller can decide whether to retry. +enum CertdFailure { + /// A rejection that a different timestamp would fix, carrying the server's + /// notion of "now" when it advertised one. + Clock(i64), + /// Anything else: wrong key, missing certificate, transport error. + Other(ClientError), +} + +impl CertdFailure { + fn into_error(self) -> ClientError { + match self { + Self::Clock(_) => ClientError::CertificateTarget( + "Certd rejected the request timestamp even after retrying with its own clock" + .into(), + ), + Self::Other(error) => error, + } + } +} + +async fn certd_request( + client: &reqwest::Client, + url: &str, + key_id: &str, + key_secret: &str, + at: Option, +) -> std::result::Result { + let token = match at { + Some(timestamp) => certd_token_at(key_id, key_secret, timestamp), + None => certd_token(key_id, key_secret), + }; + + let response = client + .get(url) + .header("x-certd-token", token) + .send() + .await + .map_err(|e| { + CertdFailure::Other(ClientError::CertificateTarget(format!( + "Certd request failed: {e}" + ))) + })?; + + // The header is captured before the body is consumed; it is the only place + // a skewed host can learn the correct time. + let server_time = response + .headers() + .get(reqwest::header::DATE) + .and_then(|value| value.to_str().ok()) + .and_then(|value| chrono::DateTime::parse_from_rfc2822(value).ok()) + .map(|date| date.timestamp()); + + let status = response.status(); + let body = read_bounded(response).await.map_err(CertdFailure::Other)?; + if !status.is_success() { + return Err(CertdFailure::Other(ClientError::CertificateTarget( + format!( + "Certd returned HTTP {}: {}", + status.as_u16(), + truncate(&body, 200) + ), + ))); + } + + // Certd answers both plain `{crt,...}` and `{code,message,data:{crt,...}}`. + if let Ok(envelope) = serde_json::from_str::>(&body) { + if let Some(data) = envelope.data { + return Ok(data); + } + if let Some(code) = envelope.code { + if code != 0 && code != 200 { + let message = envelope.message.unwrap_or_else(|| "no message".into()); + // Only the timestamp rejection is retryable, and only when the + // server told us its time. + if code == CERTD_TIMESTAMP_REJECTED { + if let Some(server_time) = server_time { + return Err(CertdFailure::Clock(server_time)); + } + } + return Err(CertdFailure::Other(ClientError::CertificateTarget( + format!( + "Certd rejected the request: {message} (code {code}){}", + certd_hint(code), + ), + ))); + } + } + } + serde_json::from_str::(&body).map_err(|e| { + CertdFailure::Other(ClientError::CertificateParse(format!( + "Certd response is not a certificate payload: {e} — {}", + truncate(&body, 200) + ))) + }) +} + +async fn fetch_url( + client: &reqwest::Client, + base_url: &str, + secret: &rustfrp_client::db::cert_ingest::SourceSecret, +) -> Result { + let mut request = client.get(base_url); + if let Some(token) = secret + .key_secret + .as_deref() + .filter(|value| !value.is_empty()) + { + request = request.header("Authorization", format!("Bearer {token}")); + } + let response = request + .send() + .await + .map_err(|e| ClientError::CertificateTarget(format!("URL fetch failed: {e}")))?; + let status = response.status(); + let body = read_bounded(response).await?; + if !status.is_success() { + return Err(ClientError::CertificateTarget(format!( + "source returned HTTP {}: {}", + status.as_u16(), + truncate(&body, 200) + ))); + } + Ok(body) +} + +/// A URL source may answer with a PEM bundle containing both halves, or with +/// `{ "crt": "...", "key": "..." }`. +fn split_url_body(body: &str) -> Result<(String, String), ClientError> { + if let Ok(json) = serde_json::from_str::(body) { + let field = |name: &str| { + json.get(name) + .and_then(serde_json::Value::as_str) + .map(str::to_string) + }; + if let (Some(cert), Some(key)) = (field("crt").or(field("cert")), field("key")) { + return Ok((cert, key)); + } + } + + // Split on PEM headers so a concatenated bundle resolves without guessing. + let blocks: Vec<&str> = body.split("-----BEGIN ").skip(1).collect(); + if blocks.len() >= 2 { + let mut cert_parts = Vec::new(); + let mut key_part = None; + for block in blocks { + let full = format!("-----BEGIN {block}"); + if block.starts_with("PRIVATE KEY") + || block.starts_with("RSA PRIVATE KEY") + || block.starts_with("EC PRIVATE KEY") + { + key_part = Some(full); + } else if block.starts_with("CERTIFICATE") { + cert_parts.push(full); + } + } + if let (false, Some(key)) = (cert_parts.is_empty(), key_part) { + return Ok((cert_parts.join("\n"), key)); + } + } + + Err(ClientError::CertificateParse( + "source body contains neither {crt,key} JSON nor a PEM certificate + key pair".into(), + )) +} + +/// What a Certd rejection code usually means, appended to the message. +/// +/// Naming the likely cause is the difference between "it failed" and "your key +/// is wrong"; the codes are Certd's, not ours, so they are explained rather than +/// translated into our own taxonomy. +/// Certd's "ApiToken时间戳错误". Verified against a live instance: a request +/// signed eight hours in the future is accepted, which is why this is the only +/// rejection a clock correction can fix. +const CERTD_TIMESTAMP_REJECTED: i64 = 20002; + +fn certd_hint(code: i64) -> &'static str { + match code { + 20001 => " — the key id or secret does not match; regenerate the open API key in Certd", + // The signature covers a second-resolution timestamp, so this is the + // one failure a correct key still hits when the two clocks disagree. + // Either side can be wrong; the message must not assume it is ours. + 20002 => " — this host and Certd disagree about the current time; the retry below uses Certd's own clock, so a second failure means the key is wrong too", + _ => "", + } +} + +/// Build the `x-certd-token` header value. +fn certd_token(key_id: &str, key_secret: &str) -> String { + certd_token_at(key_id, key_secret, chrono::Utc::now().timestamp()) +} + +/// Same token, signed for an explicit timestamp. +/// +/// Split out because the timestamp is the one input a host with a wrong clock +/// can substitute: everything else in the signature is the caller's key. +fn certd_token_at(key_id: &str, key_secret: &str, timestamp: i64) -> String { + let content = format!( + r#"{{"keyId":"{}","t":{},"encrypt":false,"signType":"md5"}}"#, + key_id, timestamp + ); + let mut hasher = Md5::new(); + hasher.update(content.as_bytes()); + hasher.update(key_secret.as_bytes()); + let sign = hex(&hasher.finalize()); + + let engine = base64::engine::general_purpose::STANDARD; + format!( + "{}.{}", + engine.encode(content.as_bytes()), + engine.encode(sign.as_bytes()) + ) +} + +async fn read_bounded(response: reqwest::Response) -> Result { + let bytes = response + .bytes() + .await + .map_err(|e| ClientError::CertificateTarget(format!("failed to read response: {e}")))?; + if bytes.len() > MAX_BODY_BYTES { + return Err(ClientError::CertificateTarget(format!( + "response is {} bytes, above the {MAX_BODY_BYTES} byte limit", + bytes.len() + ))); + } + Ok(String::from_utf8_lossy(&bytes).to_string()) +} + +fn urlencode(value: &str) -> String { + let mut out = String::with_capacity(value.len()); + for byte in value.bytes() { + match byte { + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b',' => { + out.push(byte as char) + } + other => out.push_str(&format!("%{other:02X}")), + } + } + out +} + +fn truncate(value: &str, limit: usize) -> String { + if value.len() <= limit { + return value.to_string(); + } + let mut end = limit; + while end > 0 && !value.is_char_boundary(end) { + end -= 1; + } + format!("{}…", &value[..end]) +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().fold(String::new(), |mut out, byte| { + use std::fmt::Write; + let _ = write!(out, "{byte:02x}"); + out + }) +} + +/// Run due pull channels forever. +/// +/// A single task for the whole process, not one per channel: the work is a +/// handful of outbound requests per day, and one loop makes "what is due right +/// now" a single query instead of a scheduling structure to keep in sync. +pub fn spawn_scheduler(db: Database, processes: ProcessManager) { + tokio::spawn(async move { + let mut ticker = tokio::time::interval(std::time::Duration::from_secs(60)); + ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + ticker.tick().await; + let due = match db.due_pull_sources(16).await { + Ok(due) => due, + Err(error) => { + tracing::warn!(%error, "could not list due certificate channels"); + continue; + } + }; + for (id, tenant) in due { + match pull_once(&db, Some(&processes), id, &tenant).await { + Ok(report) => tracing::info!( + source_id = id, + domain = report.primary_domain.as_deref().unwrap_or("?"), + applied = report.applied, + "automatic certificate pull succeeded" + ), + Err(error) => tracing::warn!( + source_id = id, + %error, + "automatic certificate pull failed" + ), + } + } + } + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn certd_token_has_the_documented_shape() { + let token = certd_token("kid", "secret"); + let (content_b64, sign_b64) = token.split_once('.').unwrap(); + let engine = base64::engine::general_purpose::STANDARD; + let content = String::from_utf8(engine.decode(content_b64).unwrap()).unwrap(); + let sign = String::from_utf8(engine.decode(sign_b64).unwrap()).unwrap(); + + assert!(content.contains(r#""keyId":"kid""#)); + assert!(content.contains(r#""signType":"md5""#)); + assert!(content.contains(r#""encrypt":false"#)); + assert_eq!(sign.len(), 32); + + // The signature must be md5(content + secret). + let expected = { + let mut hasher = Md5::new(); + hasher.update(content.as_bytes()); + hasher.update(b"secret"); + hex(&hasher.finalize()) + }; + assert_eq!(sign, expected); + } + + #[test] + fn splits_json_and_pem_bodies() { + let json = r#"{"crt":"CERT","key":"KEY"}"#; + assert_eq!( + split_url_body(json).unwrap(), + ("CERT".to_string(), "KEY".to_string()) + ); + + let pem = "-----BEGIN CERTIFICATE-----\nAAA\n-----END CERTIFICATE-----\n-----BEGIN PRIVATE KEY-----\nBBB\n-----END PRIVATE KEY-----\n"; + let (cert, key) = split_url_body(pem).unwrap(); + assert!(cert.contains("BEGIN CERTIFICATE")); + assert!(key.contains("BEGIN PRIVATE KEY")); + + assert!(split_url_body("nonsense").is_err()); + } + + #[test] + fn encodes_domain_query_safely() { + assert_eq!(urlencode("a.example.com"), "a.example.com"); + assert_eq!(urlencode("*.example.com"), "%2A.example.com"); + assert_eq!(urlencode("a b"), "a%20b"); + } + + #[test] + fn truncates_on_char_boundaries() { + let value = "中文中文中文"; + let truncated = truncate(value, 7); + assert!(truncated.ends_with('…')); + assert!(std::str::from_utf8(truncated.as_bytes()).is_ok()); + } +} + +#[cfg(test)] +mod envelope_tests { + use super::*; + + #[test] + fn certd_message_field_is_surfaced() { + // The real Certd payload uses `message`; ignoring it produced + // "no message (code 20002)" for every rejection. + let body = r#"{"code":20002,"message":"ApiToken时间戳错误"}"#; + let envelope: CertdEnvelope = serde_json::from_str(body).unwrap(); + assert_eq!(envelope.code, Some(20002)); + assert_eq!(envelope.message.as_deref(), Some("ApiToken时间戳错误")); + assert!(envelope.data.is_none()); + + // Older builds used `msg`; both must land in the same field. + let legacy: CertdEnvelope = + serde_json::from_str(r#"{"code":1,"msg":"openKey不存在"}"#).unwrap(); + assert_eq!(legacy.message.as_deref(), Some("openKey不存在")); + } + + #[test] + fn a_success_payload_still_parses() { + let body = r#"{"code":0,"message":"ok","data":{"crt":"CERT","key":"KEY"}}"#; + let envelope: CertdEnvelope = serde_json::from_str(body).unwrap(); + let data = envelope.data.unwrap(); + assert_eq!(data.crt, "CERT"); + assert_eq!(data.key.as_deref(), Some("KEY")); + } + + #[test] + fn known_codes_explain_the_likely_cause() { + assert!(certd_hint(20001).contains("key id or secret")); + assert!(certd_hint(20002).contains("disagree about the current time")); + assert_eq!(CERTD_TIMESTAMP_REJECTED, 20002); + assert!(certd_hint(12345).is_empty()); + } +} + +#[cfg(test)] +mod clock_retry_tests { + use super::*; + + /// The token must be signed with the timestamp it was given, or the retry + /// would resend the same rejected value. + #[test] + fn token_honours_an_explicit_timestamp() { + let engine = base64::engine::general_purpose::STANDARD; + let decode = |key: &str, secret: &str, at: i64| { + let token = certd_token_at(key, secret, at); + let (content, sign) = token.split_once('.').unwrap(); + ( + String::from_utf8(engine.decode(content).unwrap()).unwrap(), + String::from_utf8(engine.decode(sign).unwrap()).unwrap(), + ) + }; + + let (content, sign) = decode("kid", "s", 1_700_000_000); + assert!(content.contains(r#""t":1700000000"#), "{content}"); + + let mut hasher = Md5::new(); + hasher.update(content.as_bytes()); + hasher.update(b"s"); + assert_eq!(sign, hex(&hasher.finalize())); + + // A different timestamp is a different signature; that is what makes the + // retry meaningful rather than a repeat. + assert_ne!(decode("kid", "s", 1).1, decode("kid", "s", 2).1); + } + + /// A skew-retry must not swallow a genuine key failure. + #[test] + fn only_the_timestamp_code_is_retryable() { + assert_eq!(CERTD_TIMESTAMP_REJECTED, 20002); + // 20001 means the key is wrong; retrying with a better clock cannot help. + assert_ne!(CERTD_TIMESTAMP_REJECTED, 20001); + } +} diff --git a/crates/rustfrp-daemon/src/desktop.rs b/crates/rustfrp-daemon/src/desktop.rs index d2658cc..da078ae 100644 --- a/crates/rustfrp-daemon/src/desktop.rs +++ b/crates/rustfrp-daemon/src/desktop.rs @@ -50,6 +50,22 @@ pub struct DesktopOptions { pub frpc_path: Option, } +impl DesktopOptions { + /// Options for `~/.rustfrp` with an OS-assigned loopback API port. + /// + /// The desktop shell passes its own paths; this exists so tests and + /// embedders have one definition instead of repeating the defaults. + pub fn with_data_dir(data_dir: impl Into, listen: impl Into) -> Self { + let data_dir = data_dir.into(); + Self { + config_dir: data_dir.join("runtime"), + data_dir, + listen: listen.into(), + frpc_path: None, + } + } +} + /// Errors that stop a desktop session from starting. #[derive(Debug, thiserror::Error)] pub enum DesktopError { @@ -70,6 +86,8 @@ pub struct DesktopSession { data_dir: PathBuf, token: String, url: String, + /// Address the certificate push endpoint is listening on, if it started. + ingest: Option, shutdown: rustfrp_common::signal::SignalHandler, server: JoinHandle>, // Flipped to `true` when the server task ends, however it ends. The shell @@ -108,6 +126,28 @@ impl DesktopSession { listener.local_addr().map_err(anyhow::Error::from)? ); + // The push endpoint is started here too: a desktop install is the case + // where Certd has no other way to reach the machine, and the endpoint is + // the same three-route surface with the same token check. + let ingest_state = crate::cert_ingest::IngestState { + db: core.db().clone(), + }; + let ingest_server = crate::cert_ingest::IngestServer::new(ingest_state.clone()); + let ingest = match rustfrp_client::db::settings::IngestConfig::load(&ingest_state.db).await + { + Ok(config) => match ingest_server.apply(&config).await { + Ok(address) => address, + Err(error) => { + tracing::warn!(%error, "certificate ingest endpoint is not listening"); + None + } + }, + Err(error) => { + tracing::warn!(%error, "could not read ingest settings"); + None + } + }; + let auth = BearerToken::new(token.clone()); let (stop_tx, stopped) = watch::channel(false); let server = tokio::spawn(async move { @@ -118,12 +158,18 @@ impl DesktopSession { result }); - tracing::info!(%url, data_dir = %options.data_dir.display(), "desktop session started"); + tracing::info!( + %url, + data_dir = %options.data_dir.display(), + ingest = ingest.as_deref().unwrap_or("none"), + "desktop session started" + ); Ok(Self { data_dir: options.data_dir, token, url, + ingest, shutdown, server, stopped, @@ -141,6 +187,14 @@ impl DesktopSession { &self.token } + /// Address the certificate push endpoint listens on, once it is up. + /// + /// A desktop user needs this to paste into Certd; it is loopback unless the + /// stored settings were changed to expose the endpoint. + pub fn ingest_address(&self) -> Option<&str> { + self.ingest.as_deref() + } + /// Data directory (`config.db`, `logs/`, `frp/`). pub fn data_dir(&self) -> &Path { &self.data_dir diff --git a/crates/rustfrp-daemon/src/lib.rs b/crates/rustfrp-daemon/src/lib.rs index 75ae341..d842f8b 100644 --- a/crates/rustfrp-daemon/src/lib.rs +++ b/crates/rustfrp-daemon/src/lib.rs @@ -7,6 +7,9 @@ #[cfg(feature = "http-api")] pub mod api; #[cfg(feature = "http-api")] +pub mod cert_ingest; +pub mod cert_pull; +#[cfg(feature = "http-api")] pub mod desktop; pub mod instance; pub mod metrics; diff --git a/crates/rustfrp-daemon/src/main.rs b/crates/rustfrp-daemon/src/main.rs index 71f6387..853106f 100644 --- a/crates/rustfrp-daemon/src/main.rs +++ b/crates/rustfrp-daemon/src/main.rs @@ -35,6 +35,16 @@ struct Cli { #[arg(long, env = "RUSTFRP_AUTH_POLICY_FILE")] auth_policy_file: Option, + /// Force the certificate ingest listener address, e.g. `0.0.0.0:7901`. + /// + /// Normally unset: the endpoint is configured in the UI (enabled by default + /// on loopback) and stored in the database. This flag exists for container + /// and orchestration deployments that must pin the address, and it wins over + /// the stored value for the address while still respecting the enable flag. + #[cfg(feature = "http-api")] + #[arg(long, env = "RUSTFRP_CERT_INGEST_LISTEN")] + cert_ingest_listen: Option, + /// Use this frpc binary instead of a managed version. #[arg(long, env = "RUSTFRP_FRPC_PATH")] frpc_path: Option, @@ -91,6 +101,28 @@ async fn main() -> anyhow::Result<()> { #[cfg(feature = "http-api")] { + // A pinned address from the environment/CLI is persisted before the API + // server starts, so the listener the settings surface reports is the one + // that is actually bound. Everything else is configured in the UI. + if let Some(listen) = cli.cert_ingest_listen.as_deref() { + if let Some((host, port)) = listen.rsplit_once(':') { + if let Ok(port) = port.parse::() { + core.db() + .set_platform_setting( + rustfrp_client::db::settings::CERT_INGEST_LISTEN, + host, + ) + .await?; + core.db() + .set_platform_setting( + rustfrp_client::db::settings::CERT_INGEST_PORT, + &port.to_string(), + ) + .await?; + } + } + } + let api_token = cli .api_token .or_else(|| { diff --git a/crates/rustfrp-daemon/src/metrics.rs b/crates/rustfrp-daemon/src/metrics.rs index 8fce63c..a0dd4cc 100644 --- a/crates/rustfrp-daemon/src/metrics.rs +++ b/crates/rustfrp-daemon/src/metrics.rs @@ -8,8 +8,17 @@ use sysinfo::{Pid, ProcessesToUpdate, System}; use tokio::sync::RwLock; pub const RESOURCE_SAMPLE_INTERVAL_SECS: u64 = 1; +/// Nominal one-hour window at one sample per second. pub const METRICS_HISTORY_CAPACITY: usize = 3_600; +/// Wall-clock window the resource buffer claims to cover. +/// +/// The count and the window disagree whenever the clock is not monotonic: an +/// eight-hour RTC correction left 3_600 samples spanning nine hours, so "last +/// hour" contained only the two minutes after the jump. Evicting by age as well +/// as by count keeps the label honest. +pub const METRICS_HISTORY_WINDOW: chrono::Duration = chrono::Duration::hours(1); + #[derive(Debug, Clone, Serialize)] pub struct ProcessResourceSample { pub profile_id: i64, @@ -42,7 +51,6 @@ pub struct EbpfStatus { pub struct TrafficSample { #[serde(default = "chrono::Utc::now")] pub timestamp: chrono::DateTime, - pub environment_id: i64, pub profile_id: i64, pub received_bytes: u64, pub sent_bytes: u64, @@ -142,6 +150,12 @@ impl MetricsStore { pub async fn push_resource(&self, sample: ResourceSample) { let mut resources = self.resources.write().await; + // Drop anything older than the advertised window before appending, so a + // clock jump cannot stretch the buffer past the range the UI offers. + let cutoff = sample.timestamp - METRICS_HISTORY_WINDOW; + while resources.front().is_some_and(|s| s.timestamp < cutoff) { + resources.pop_front(); + } push_bounded(&mut resources, sample, self.capacity); } @@ -182,21 +196,13 @@ impl MetricsStore { .collect() } - pub async fn traffic( - &self, - environment_id: Option, - profile_id: Option, - limit: usize, - ) -> Vec { + pub async fn traffic(&self, profile_id: Option, limit: usize) -> Vec { self.traffic .read() .await .iter() .rev() - .filter(|sample| { - environment_id.is_none_or(|id| sample.environment_id == id) - && profile_id.is_none_or(|id| sample.profile_id == id) - }) + .filter(|sample| profile_id.is_none_or(|id| sample.profile_id == id)) .take(limit.min(self.capacity.saturating_mul(10))) .cloned() .collect::>() @@ -250,7 +256,6 @@ mod tests { store .push_traffic(TrafficSample { timestamp: chrono::Utc::now(), - environment_id: 7, profile_id, received_bytes: 1, sent_bytes: 2, @@ -262,9 +267,9 @@ mod tests { }) .await; } - assert_eq!(store.traffic(None, None, 100).await.len(), 20); - assert_eq!(store.traffic(Some(7), Some(1), 100).await.len(), 10); - assert!(store.traffic(Some(8), None, 100).await.is_empty()); + assert_eq!(store.traffic(None, 100).await.len(), 20); + assert_eq!(store.traffic(Some(1), 100).await.len(), 10); + assert!(store.traffic(Some(99), 100).await.is_empty()); } #[test] @@ -283,3 +288,81 @@ mod tests { assert_eq!(status.unprivileged_disabled, Some(2)); } } + +#[cfg(test)] +mod window_tests { + use super::*; + + fn sample(at: chrono::DateTime) -> ResourceSample { + ResourceSample { + timestamp: at, + daemon_cpu_percent: 0.0, + daemon_memory_bytes: 0, + system_cpu_percent: 0.0, + system_memory_used_bytes: 0, + system_memory_total_bytes: 0, + ebpf: EbpfStatus { + available: false, + pinned_objects: 0, + unprivileged_disabled: None, + }, + processes: Vec::new(), + } + } + + /// A clock jump must not stretch the buffer past the window the UI offers: + /// the count bound alone allowed nine hours of samples to masquerade as one. + #[tokio::test] + async fn samples_older_than_the_window_are_evicted() { + let store = MetricsStore::new(METRICS_HISTORY_CAPACITY); + let start = chrono::Utc::now(); + + // Fill half the buffer an hour ago, as a skewed clock would have. + for i in 0..100 { + store + .push_resource(sample( + start - chrono::Duration::hours(9) + chrono::Duration::seconds(i), + )) + .await; + } + assert_eq!(store.resources(usize::MAX).await.len(), 100); + + // One sample "now" must discard all of them. + store.push_resource(sample(start)).await; + let kept = store.resources(usize::MAX).await; + assert_eq!( + kept.len(), + 1, + "only the current sample is inside the window" + ); + assert_eq!(kept[0].timestamp, start); + } + + #[tokio::test] + async fn samples_inside_the_window_are_kept() { + let store = MetricsStore::new(METRICS_HISTORY_CAPACITY); + let start = chrono::Utc::now(); + for i in 0..60 { + store + .push_resource(sample( + start - chrono::Duration::minutes(59) + chrono::Duration::seconds(i), + )) + .await; + } + store.push_resource(sample(start)).await; + assert_eq!(store.resources(usize::MAX).await.len(), 61); + } + + /// The count bound still applies: capacity is the hard ceiling. + #[tokio::test] + async fn capacity_still_bounds_the_buffer() { + let store = MetricsStore::new(10); + let start = chrono::Utc::now(); + for i in 0..50 { + store + .push_resource(sample(start + chrono::Duration::seconds(i))) + .await; + } + assert_eq!(store.resources(usize::MAX).await.len(), 10); + } +} diff --git a/crates/rustfrp-daemon/src/traffic/mod.rs b/crates/rustfrp-daemon/src/traffic/mod.rs index 12b0274..bf95b0c 100644 --- a/crates/rustfrp-daemon/src/traffic/mod.rs +++ b/crates/rustfrp-daemon/src/traffic/mod.rs @@ -11,14 +11,12 @@ mod linux; #[derive(Debug, Clone, Serialize)] pub struct ProfileCollection { pub profile_id: i64, - pub environment_id: i64, pub state: &'static str, } #[derive(Debug, Clone, Serialize)] pub struct ProxyCollection { pub profile_id: i64, - pub environment_id: i64, pub proxy_id: i64, pub name: String, pub state: &'static str, @@ -81,7 +79,7 @@ use std::{ #[cfg(target_os = "linux")] #[derive(Default)] struct CounterState { - identity: (u32, String, i64), + identity: (u32, String), previous: HashMap, received: u64, sent: u64, @@ -89,11 +87,7 @@ struct CounterState { } #[cfg(target_os = "linux")] impl CounterState { - fn update( - &mut self, - identity: (u32, String, i64), - sockets: &[linux::SocketCounter], - ) -> (u64, u64) { + fn update(&mut self, identity: (u32, String), sockets: &[linux::SocketCounter]) -> (u64, u64) { if self.identity != identity { *self = Self { identity, @@ -176,15 +170,13 @@ impl Collector { .timestamp_nanos_opt() .unwrap_or_default() .to_string(), - 0, ); } - let identity = (index, counter.identity.1.clone(), 0); + let identity = (index, counter.identity.1.clone()); let (received_bytes, sent_bytes) = counter.update(identity, &[socket]); store .push_traffic(crate::metrics::TrafficSample { timestamp, - environment_id: 0, profile_id: 0, proxy_id: None, interface: Some(name), @@ -282,16 +274,8 @@ impl Collector { let timestamp = Utc::now(); for profile in profiles { let Some(id) = profile.id else { continue }; - let environment_id = match db.profile_environment_id(id).await { - Ok(id) => id, - Err(_) => { - status.state = "database_error"; - continue; - } - }; let mut item = ProfileCollection { profile_id: id, - environment_id, state: "stopped", }; let process = processes.iter().find(|p| p.profile_id == id && p.running); @@ -395,7 +379,6 @@ impl Collector { "{identity}:{}:{}", proxy.local_ip, proxy.local_port ), - environment_id, ), &local, ); @@ -403,7 +386,6 @@ impl Collector { store .push_traffic(crate::metrics::TrafficSample { timestamp, - environment_id, profile_id: id, proxy_id: Some(proxy_id), interface: None, @@ -423,7 +405,6 @@ impl Collector { } status.proxies.push(ProxyCollection { profile_id: id, - environment_id, proxy_id, name: proxy.name.clone(), state: local_state, @@ -448,7 +429,7 @@ impl Collector { .counters .entry(id) .or_default() - .update((pid, identity, environment_id), &sockets); + .update((pid, identity), &sockets); item.state = if sockets.is_empty() { "no_connection" } else { @@ -459,7 +440,6 @@ impl Collector { store .push_traffic(crate::metrics::TrafficSample { timestamp, - environment_id, profile_id: id, received_bytes: received, sent_bytes: sent, @@ -492,7 +472,6 @@ impl Collector { { status.proxies.push(ProxyCollection { profile_id: id, - environment_id, proxy_id: binding.proxy_id, name: proxies .iter() @@ -523,9 +502,9 @@ mod tests { } } #[test] - fn counter_deltas_do_not_double_count_and_reset_on_process_or_environment_change() { + fn counter_deltas_do_not_double_count_and_reset_on_identity_change() { let mut counters = CounterState::default(); - let identity = (42, "100".into(), 1); + let identity = (42, "100".into()); assert_eq!( counters.update(identity.clone(), &[socket(1, 100, 200)]), (0, 0) @@ -543,12 +522,14 @@ mod tests { (70, 90) ); assert_eq!(counters.update(identity.clone(), &[]), (70, 90)); + // A different identity (a restarted process) is a fresh baseline, not a + // negative delta. assert_eq!( - counters.update((42, "101".into(), 1), &[socket(3, 50, 50)]), + counters.update((42, "101".into()), &[socket(3, 50, 50)]), (0, 0) ); assert_eq!( - counters.update((42, "101".into(), 2), &[socket(3, 80, 80)]), + counters.update((43, "101".into()), &[socket(3, 80, 80)]), (0, 0) ); } diff --git a/crates/rustfrp-daemon/tests/cert_ingest_listener.rs b/crates/rustfrp-daemon/tests/cert_ingest_listener.rs new file mode 100644 index 0000000..941b298 --- /dev/null +++ b/crates/rustfrp-daemon/tests/cert_ingest_listener.rs @@ -0,0 +1,175 @@ +//! The ingest endpoint must be configurable at runtime, and its failure mode must +//! never be "the daemon does not start". +//! +//! These tests drive the real listener: what matters is whether a socket is +//! actually accepting connections after a settings change, not what the settings +//! table says. + +use axum::body::Body; +use axum::http::Request; +use rustfrp_client::db::settings::{IngestConfig, CERT_INGEST_ENABLED, CERT_INGEST_PORT}; +use rustfrp_client::db::{migrate, Database}; +use rustfrp_daemon::cert_ingest::{IngestServer, IngestState}; +use std::net::TcpListener; +use tower::ServiceExt; + +async fn database() -> (tempfile::TempDir, Database) { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + (dir, db) +} + +/// A port that was free a moment ago, so the test does not depend on 7901. +fn free_port() -> u16 { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.local_addr().unwrap().port() +} + +async fn reachable(address: &str) -> bool { + reqwest::Client::new() + .get(format!("http://{address}/ingest/v1/healthz")) + .timeout(std::time::Duration::from_secs(2)) + .send() + .await + .map(|response| response.status().is_success()) + .unwrap_or(false) +} + +#[tokio::test] +async fn listener_starts_stops_and_moves_without_a_restart() { + let (_dir, db) = database().await; + let server = IngestServer::new(IngestState { db: db.clone() }); + + let first = free_port(); + let config = IngestConfig { + enabled: true, + listen: "127.0.0.1".into(), + port: first, + expose: false, + }; + let bound = server.apply(&config).await.unwrap(); + assert_eq!( + bound.as_deref(), + Some(format!("127.0.0.1:{first}").as_str()) + ); + assert!( + reachable(&format!("127.0.0.1:{first}")).await, + "the endpoint must accept connections after being enabled" + ); + + // Moving to another port must free the old one. + let second = free_port(); + let moved = IngestConfig { + port: second, + ..config.clone() + }; + server.apply(&moved).await.unwrap(); + assert!( + reachable(&format!("127.0.0.1:{second}")).await, + "the listener must follow the new port" + ); + + // Disabling must actually close the socket. + let off = IngestConfig { + enabled: false, + ..moved.clone() + }; + assert!(server.apply(&off).await.unwrap().is_none()); + assert!( + !reachable(&format!("127.0.0.1:{second}")).await, + "a disabled listener must not still answer" + ); + assert!(server.address().await.is_none()); +} + +#[tokio::test] +async fn a_taken_port_is_reported_rather_than_fatal() { + let (_dir, db) = database().await; + let server = IngestServer::new(IngestState { db }); + + // Hold the port so the bind must fail. + let blocker = TcpListener::bind("127.0.0.1:0").unwrap(); + let taken = blocker.local_addr().unwrap().port(); + + let config = IngestConfig { + enabled: true, + listen: "127.0.0.1".into(), + port: taken, + expose: false, + }; + let error = server.apply(&config).await.unwrap_err(); + assert!(error.contains(&taken.to_string()), "got: {error}"); + // Reporting no address keeps the state honest. + assert!(server.address().await.is_none()); +} + +#[tokio::test] +async fn settings_round_trip_through_the_database_and_bind() { + let (_dir, db) = database().await; + let port = free_port(); + db.set_platform_setting(CERT_INGEST_PORT, &port.to_string()) + .await + .unwrap(); + db.set_platform_setting(CERT_INGEST_ENABLED, "true") + .await + .unwrap(); + + let config = IngestConfig::load(&db).await.unwrap(); + assert_eq!(config.port, port); + assert_eq!(config.address(), format!("127.0.0.1:{port}")); + + let server = IngestServer::new(IngestState { db }); + server.apply(&config).await.unwrap(); + assert!(reachable(&config.address()).await); +} + +#[tokio::test] +async fn ingest_still_rejects_requests_without_a_token() { + let (_dir, db) = database().await; + let server = IngestServer::new(IngestState { db: db.clone() }); + let port = free_port(); + server + .apply(&IngestConfig { + enabled: true, + listen: "127.0.0.1".into(), + port, + expose: false, + }) + .await + .unwrap(); + + let response = reqwest::Client::new() + .post(format!("http://127.0.0.1:{port}/ingest/v1/certs")) + .header("content-type", "application/json") + .body(r#"{"crt":"x","key":"y"}"#) + .send() + .await + .unwrap(); + assert_eq!(response.status(), 401); +} + +/// The router itself must stay two routes even when reached through the network. +#[tokio::test] +async fn bound_listener_exposes_no_control_routes() { + let (_dir, db) = database().await; + + // Sanity check that the same service, mounted in-process, 404s these paths. + let app = rustfrp_daemon::cert_ingest::create_ingest_router(IngestState { db }); + for uri in ["/api/v1/certificates", "/api/v1/config/export", "/"] { + let response = app + .clone() + .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap()) + .await + .unwrap(); + assert_eq!( + response.status(), + 404, + "{uri} must not be served by the ingest listener" + ); + } +} diff --git a/crates/rustfrp-daemon/tests/cert_pull_flow.rs b/crates/rustfrp-daemon/tests/cert_pull_flow.rs new file mode 100644 index 0000000..569ca1d --- /dev/null +++ b/crates/rustfrp-daemon/tests/cert_pull_flow.rs @@ -0,0 +1,470 @@ +//! Automatic pull: the daemon fetches a bundle, refuses one that does not cover +//! the channel's domain, and rewrites the files an already-bound proxy reads. +//! +//! Driven against a real local HTTP server rather than a mocked client, because +//! the property under test is end to end: what ends up on disk. + +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use rustfrp_client::config::model::LocalProxy; +use rustfrp_client::db::cert_ingest::{SourceInput, SourceKind}; +use rustfrp_client::db::{certificate::SOURCE_MANUAL, migrate, Database}; +use rustfrp_client::process::manager::ProcessManager; +use rustfrp_client::ClientState; +use rustfrp_common::signal::SignalHandler; +use rustfrp_daemon::api::{create_router, state::ApiState, NoAuth}; +use serde_json::json; +use std::sync::Arc; +use tokio::sync::RwLock; +use tower::ServiceExt; + +const CERT: &str = include_str!("../../client/tests/fixtures/cert_ec.pem"); +const KEY: &str = include_str!("../../client/tests/fixtures/cert_ec.key"); +const OTHER_CERT: &str = include_str!("../../client/tests/fixtures/cert_rsa.pem"); +const OTHER_KEY: &str = include_str!("../../client/tests/fixtures/cert_rsa.key"); + +struct Harness { + db: Database, + router: axum::Router, + config_dir: std::path::PathBuf, + _dir: tempfile::TempDir, +} + +async fn harness() -> Harness { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + let config_dir = dir.path().join("runtime"); + std::fs::create_dir_all(&config_dir).unwrap(); + let manager = ProcessManager::new( + config_dir.clone(), + "/nonexistent/frpc".into(), + SignalHandler::new(), + ); + let state = ApiState::new( + db.clone(), + manager, + config_dir.clone(), + Arc::new(RwLock::new(ClientState::Ready)), + ); + Harness { + db, + router: create_router(state, NoAuth), + config_dir, + _dir: dir, + } +} + +/// Serve `crt`/`key` as the JSON a URL channel accepts, on a random port. +async fn serve_bundle(crt: &str, key: &str) -> String { + let body = json!({ "crt": crt, "key": key }).to_string(); + let app = axum::Router::new().route( + "/bundle.json", + axum::routing::get(move || { + let body = body.clone(); + async move { body } + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + format!("http://{addr}/bundle.json") +} + +async fn send(router: &axum::Router, request: Request) -> (StatusCode, String) { + let response = router.clone().oneshot(request).await.unwrap(); + let status = response.status(); + let bytes = axum::body::to_bytes(response.into_body(), 1 << 20) + .await + .unwrap(); + (status, String::from_utf8_lossy(&bytes).to_string()) +} + +async fn post_json( + router: &axum::Router, + uri: &str, + body: serde_json::Value, +) -> (StatusCode, String) { + send( + router, + Request::builder() + .method("POST") + .uri(uri) + .header("content-type", "application/json") + .body(Body::from(body.to_string())) + .unwrap(), + ) + .await +} + +/// The happy path: a certificate configured ahead of its material, a pull +/// channel for its domain, and a bound proxy whose files must be rewritten. +#[tokio::test] +async fn a_pull_imports_the_certificate_and_rewrites_bound_files() { + let harness = harness().await; + let certificate = harness + .db + .ensure_certificate("default", "test.example.com") + .await + .unwrap(); + assert!(certificate.material_id.is_none()); + + // A proxy that already declares where its certificate belongs. + let crt_path = harness.config_dir.join("certs/fullchain.pem"); + let key_path = harness.config_dir.join("certs/privkey.pem"); + std::fs::create_dir_all(crt_path.parent().unwrap()).unwrap(); + let proxy = LocalProxy { + name: "web".into(), + proxy_type: rustfrp_client::config::model::ProxyType::Https, + local_ip: "127.0.0.1".into(), + local_port: 8080, + custom_domains: Some(vec!["test.example.com".into()]), + plugin_config: Some(json!({ + "type": "tls2raw", + "localAddr": "127.0.0.1:8080", + "crtPath": crt_path.to_string_lossy(), + "keyPath": key_path.to_string_lossy(), + })), + ..LocalProxy::default() + }; + let proxy_id = harness + .db + .insert_proxy_for_tenant(&proxy, "default") + .await + .unwrap(); + + let url = serve_bundle(CERT, KEY).await; + let (status, body) = post_json( + &harness.router, + "/api/v1/certificate-sources", + json!({ + "kind": "url", + "name": "nightly", + "certificate_id": certificate.id, + "domain": "test.example.com", + "base_url": url, + }), + ) + .await; + assert_eq!(status, StatusCode::CREATED, "channel: {body}"); + let source_id = serde_json::from_str::(&body).unwrap()["data"]["source"] + ["id"] + .as_i64() + .unwrap(); + + let (status, body) = post_json( + &harness.router, + &format!("/api/v1/certificate-sources/{source_id}/run"), + json!({}), + ) + .await; + assert_eq!(status, StatusCode::OK, "pull: {body}"); + let report: serde_json::Value = serde_json::from_str(&body).unwrap(); + assert_eq!(report["data"]["certificate_id"], certificate.id); + + // Material arrived on the same identity, so nothing was duplicated. + let stored = harness + .db + .certificate_in_tenant(certificate.id, "default") + .await + .unwrap(); + assert!(stored.material_id.is_some()); + assert_eq!(stored.primary_domain, "test.example.com"); + assert_eq!( + harness.db.list_certificates("default").await.unwrap().len(), + 1 + ); + + // Bind, then pull again: the files must already be correct without a manual + // apply, because a renewal is expected to reach the proxy on its own. + harness + .db + .bind_certificate(certificate.id, proxy_id, "default") + .await + .unwrap(); + let applied = harness + .db + .apply_stale_certificates("default") + .await + .unwrap(); + assert_eq!(applied, 1, "the stale binding is rewritten"); + assert!(std::fs::read_to_string(&crt_path) + .unwrap() + .contains("BEGIN CERTIFICATE")); + assert!(std::fs::read_to_string(&key_path) + .unwrap() + .contains("BEGIN PRIVATE KEY")); + + // A second pull with identical material changes nothing on disk. + let before = std::fs::read_to_string(&crt_path).unwrap(); + let _ = post_json( + &harness.router, + &format!("/api/v1/certificate-sources/{source_id}/run"), + json!({}), + ) + .await; + assert_eq!(std::fs::read_to_string(&crt_path).unwrap(), before); +} + +/// The guard that makes automatic replacement safe: a channel may only install a +/// certificate that covers the domain it was created for. +#[tokio::test] +async fn a_pull_refuses_a_certificate_for_a_different_domain() { + let harness = harness().await; + let certificate = harness + .db + .ensure_certificate("default", "test.example.com") + .await + .unwrap(); + + // This endpoint answers with a certificate for `not-test.example.com`. + let url = serve_bundle(OTHER_CERT, OTHER_KEY).await; + let created = harness + .db + .create_source( + "default", + SourceInput { + certificate_id: Some(certificate.id), + kind: SourceKind::Url, + name: "wrong".into(), + domain: "other.example.com".into(), + base_url: url, + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + + let (status, body) = post_json( + &harness.router, + &format!("/api/v1/certificate-sources/{}/run", created.source.id), + json!({}), + ) + .await; + assert_ne!(status, StatusCode::OK, "this must be refused: {body}"); + assert!( + body.contains("does not cover the channel's domain"), + "the error must name the mismatch: {body}" + ); + + // Nothing was imported, and the failure is visible on the channel. + let stored = harness + .db + .certificate_in_tenant(certificate.id, "default") + .await + .unwrap(); + assert!(stored.material_id.is_none(), "no material must be stored"); + + let source = harness + .db + .source(created.source.id, "default") + .await + .unwrap(); + assert_eq!(source.last_status.as_deref(), Some("failed")); + assert!(source + .last_error + .as_deref() + .unwrap_or_default() + .contains("does not cover")); + assert!(source.next_run_at.is_some(), "a retry is scheduled"); +} + +/// A push token bound to one certificate must not be able to replace another. +#[tokio::test] +async fn a_bound_push_token_rejects_a_certificate_for_another_certificate() { + let harness = harness().await; + let certificate = harness + .db + .ensure_certificate("default", "other.example.com") + .await + .unwrap(); + let created = harness + .db + .create_source( + "default", + SourceInput { + certificate_id: Some(certificate.id), + kind: SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["*.example.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + + // Allowed by the token's wildcard, but it does not cover the bound domain. + let (status, body) = send( + &harness.router, + Request::builder() + .method("POST") + .uri("/ingest/v1/certs") + .header("content-type", "application/json") + .header("x-cert-ingest-token", created.plaintext.as_deref().unwrap()) + .body(Body::from(json!({ "crt": CERT, "key": KEY }).to_string())) + .unwrap(), + ) + .await; + assert_eq!(status, StatusCode::FORBIDDEN, "body: {body}"); + assert!(body.contains("CERT_008"), "body: {body}"); +} + +/// An unbound pull channel still works: it resolves its certificate by domain, +/// which is what makes a pre-existing source usable without re-creation. +#[tokio::test] +async fn an_unbound_pull_channel_resolves_its_certificate_by_domain() { + let harness = harness().await; + let url = serve_bundle(CERT, KEY).await; + let created = harness + .db + .create_source( + "default", + SourceInput { + certificate_id: None, + kind: SourceKind::Url, + name: "legacy".into(), + domain: "test.example.com".into(), + base_url: url, + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + + let report = + rustfrp_daemon::cert_pull::pull_once(&harness.db, None, created.source.id, "default") + .await + .unwrap(); + assert_eq!(report.primary_domain.as_deref(), Some("test.example.com")); + + let attached = harness + .db + .source(created.source.id, "default") + .await + .unwrap(); + assert!( + attached.certificate_id.is_some(), + "the channel attaches itself to the certificate it produced" + ); +} + +/// A channel that predates any certificate creates the identity it needs. +#[tokio::test] +async fn a_pull_for_an_unknown_domain_creates_the_certificate() { + let harness = harness().await; + let url = serve_bundle(CERT, KEY).await; + let created = harness + .db + .create_source( + "default", + SourceInput { + certificate_id: None, + kind: SourceKind::Url, + name: "fresh".into(), + domain: "test.example.com".into(), + base_url: url, + domains: vec![], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + // The identity exists as soon as the channel does, so the operator sees + // something in the list while the first fetch is still pending. + let before = harness.db.list_certificates("default").await.unwrap(); + assert_eq!(before.len(), 1); + assert!(before[0].material_id.is_none()); + + rustfrp_daemon::cert_pull::pull_once(&harness.db, None, created.source.id, "default") + .await + .unwrap(); + + let certificates = harness.db.list_certificates("default").await.unwrap(); + assert_eq!( + certificates.len(), + 1, + "the fetch must not create a second row" + ); + assert_eq!(certificates[0].primary_domain, "test.example.com"); + assert_eq!(certificates[0].id, before[0].id); + assert!(certificates[0].material_id.is_some()); + assert_eq!(certificates[0].source, "url_pull"); +} + +/// The scheduled path must not touch a disabled channel. +#[tokio::test] +async fn a_disabled_channel_is_never_due() { + let harness = harness().await; + let url = serve_bundle(CERT, KEY).await; + harness + .db + .create_source( + "default", + SourceInput { + certificate_id: None, + kind: SourceKind::Url, + name: "off".into(), + domain: "test.example.com".into(), + base_url: url, + domains: vec![], + key_id: None, + key_secret: None, + enabled: false, + }, + ) + .await + .unwrap(); + assert!(harness.db.due_pull_sources(16).await.unwrap().is_empty()); +} + +/// A push channel is not something the daemon fetches, so the run endpoint must +/// refuse it rather than silently succeed. +#[tokio::test] +async fn running_a_push_channel_is_refused() { + let harness = harness().await; + let created = harness + .db + .create_source( + "default", + SourceInput { + certificate_id: None, + kind: SourceKind::Push, + name: "certd".into(), + domain: String::new(), + base_url: String::new(), + domains: vec!["test.example.com".into()], + key_id: None, + key_secret: None, + enabled: true, + }, + ) + .await + .unwrap(); + let error = + rustfrp_daemon::cert_pull::pull_once(&harness.db, None, created.source.id, "default") + .await + .unwrap_err(); + assert!(error.to_string().contains("push channel"), "{error}"); +} + +/// Unused import guard: `SOURCE_MANUAL` documents where a manual import lands. +#[allow(dead_code)] +fn _keeps_source_constant_used() -> &'static str { + SOURCE_MANUAL +} diff --git a/crates/rustfrp-daemon/tests/certificate_api.rs b/crates/rustfrp-daemon/tests/certificate_api.rs new file mode 100644 index 0000000..ca38ade --- /dev/null +++ b/crates/rustfrp-daemon/tests/certificate_api.rs @@ -0,0 +1,326 @@ +//! Certificate API behaviour, including the guarantee that no response carries +//! certificate material. +//! +//! These tests drive the real router: the point is what a caller over HTTP can +//! observe, not what the internal types intend. + +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use rustfrp_client::db::{migrate, Database}; +use rustfrp_client::process::manager::ProcessManager; +use rustfrp_client::ClientState; +use rustfrp_common::signal::SignalHandler; +use rustfrp_daemon::api::{create_router, state::ApiState, NoAuth}; +use serde_json::Value; +use std::sync::Arc; +use tokio::sync::RwLock; +use tower::ServiceExt; + +const CERT: &str = include_str!("../../client/tests/fixtures/cert_ec.pem"); +const KEY: &str = include_str!("../../client/tests/fixtures/cert_ec.key"); + +struct Harness { + router: axum::Router, + _dir: tempfile::TempDir, +} + +async fn harness() -> Harness { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + let runtime = dir.path().join("runtime"); + let manager = ProcessManager::new(runtime, "/nonexistent/frpc".into(), SignalHandler::new()); + let state = ApiState::new( + db, + manager, + dir.path().to_path_buf(), + Arc::new(RwLock::new(ClientState::Ready)), + ); + Harness { + router: create_router(state, NoAuth), + _dir: dir, + } +} + +async fn send(router: &axum::Router, request: Request) -> (StatusCode, String) { + let response = router.clone().oneshot(request).await.unwrap(); + let status = response.status(); + let bytes = axum::body::to_bytes(response.into_body(), 1 << 20) + .await + .unwrap(); + (status, String::from_utf8_lossy(&bytes).to_string()) +} + +async fn post_json(router: &axum::Router, uri: &str, body: Value) -> (StatusCode, String) { + send( + router, + Request::builder() + .method("POST") + .uri(uri) + .header("content-type", "application/json") + .body(Body::from(body.to_string())) + .unwrap(), + ) + .await +} + +#[tokio::test] +async fn import_then_read_never_returns_certificate_material() { + let harness = harness().await; + let router = &harness.router; + + let (status, body) = post_json( + router, + "/api/v1/certificates/pem", + serde_json::json!({ "cert": CERT, "key": KEY }), + ) + .await; + assert_eq!(status, StatusCode::CREATED, "import failed: {body}"); + + let imported: Value = serde_json::from_str(&body).unwrap(); + let id = imported["data"]["id"].as_i64().unwrap(); + assert_eq!(imported["data"]["primary_domain"], "test.example.com"); + + // The write response, the list, and the detail view must all be material-free. + for (label, payload) in [ + ("import", body), + ( + "list", + send( + router, + Request::builder() + .uri("/api/v1/certificates") + .body(Body::empty()) + .unwrap(), + ) + .await + .1, + ), + ( + "detail", + send( + router, + Request::builder() + .uri(format!("/api/v1/certificates/{id}")) + .body(Body::empty()) + .unwrap(), + ) + .await + .1, + ), + ] { + for needle in [ + "BEGIN CERTIFICATE", + "BEGIN PRIVATE KEY", + "-----BEGIN", + "cert_pem", + "key_pem", + "chain_pem", + "enc_key", + "enc_cert", + ] { + assert!( + !payload.contains(needle), + "{label} response leaked {needle}: {payload}" + ); + } + } +} + +#[tokio::test] +async fn errors_are_reported_without_echoing_the_submitted_key() { + let harness = harness().await; + // A key that does not match the certificate must be rejected with a specific + // code, and the message must not quote the key back. + let (status, body) = post_json( + &harness.router, + "/api/v1/certificates/pem", + serde_json::json!({ + "cert": CERT, + "key": include_str!("../../client/tests/fixtures/cert_rsa.key"), + }), + ) + .await; + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}"); + assert!(body.contains("CERT_003"), "{body}"); + assert!(!body.contains("PRIVATE KEY"), "{body}"); +} + +#[tokio::test] +async fn duplicate_import_is_idempotent_over_http() { + let harness = harness().await; + let body = serde_json::json!({ "cert": CERT, "key": KEY }); + + let (_, first) = post_json(&harness.router, "/api/v1/certificates/pem", body.clone()).await; + let (_, second) = post_json(&harness.router, "/api/v1/certificates/pem", body).await; + + let first: Value = serde_json::from_str(&first).unwrap(); + let second: Value = serde_json::from_str(&second).unwrap(); + assert_eq!(first["data"]["id"], second["data"]["id"]); + + let (_, list) = send( + &harness.router, + Request::builder() + .uri("/api/v1/certificates") + .body(Body::empty()) + .unwrap(), + ) + .await; + let list: Value = serde_json::from_str(&list).unwrap(); + assert_eq!(list["count"], 1); +} + +#[tokio::test] +async fn certificate_routes_require_write_or_read_scope() { + use rustfrp_daemon::api::auth::{AuthPolicy, TokenPolicy}; + use rustfrp_daemon::api::ScopedBearerToken; + use sha2::{Digest, Sha256}; + + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.db"); + let db = Database::open(path.to_str().unwrap()).await.unwrap(); + { + let conn = db.lock().await; + migrate::run(&conn).unwrap(); + } + let runtime = dir.path().join("runtime"); + let manager = ProcessManager::new(runtime, "/nonexistent/frpc".into(), SignalHandler::new()); + let state = ApiState::new( + db, + manager, + dir.path().to_path_buf(), + Arc::new(RwLock::new(ClientState::Ready)), + ); + + let reader = "reader-token-value-0123456789abcdef"; + let digest = Sha256::digest(reader.as_bytes()) + .iter() + .fold(String::new(), |mut out, byte| { + use std::fmt::Write; + let _ = write!(out, "{byte:02x}"); + out + }); + let policy = AuthPolicy { + tokens: vec![TokenPolicy { + name: "reader".into(), + tenant: "default".into(), + token_sha256: digest, + scopes: vec!["read".into()], + }], + }; + let router = create_router(state, ScopedBearerToken::new(policy)); + + let list = send( + &router, + Request::builder() + .uri("/api/v1/certificates") + .header("authorization", format!("Bearer {reader}")) + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(list.0, StatusCode::OK, "read scope must allow listing"); + + let write = send( + &router, + Request::builder() + .method("POST") + .uri("/api/v1/certificates/pem") + .header("authorization", format!("Bearer {reader}")) + .header("content-type", "application/json") + .body(Body::from( + serde_json::json!({ "cert": CERT, "key": KEY }).to_string(), + )) + .unwrap(), + ) + .await; + assert_eq!( + write.0, + StatusCode::UNAUTHORIZED, + "a read-only token must not import: {}", + write.1 + ); +} + +/// The push route is also mounted on the control API, so a certificate manager +/// can reach it at whatever address the operator already uses for the UI — +/// including a frpc `https` proxy on 443. It must stay token-gated there: the +/// bearer middleware deliberately ignores anything outside `/api/`, so the +/// ingest token is the only thing standing in front of imported material. +#[tokio::test] +async fn the_control_api_serves_the_push_route_and_keeps_its_own_token() { + let harness = harness().await; + let router = &harness.router; + + let created = post_json( + router, + "/api/v1/cert-ingest-tokens", + serde_json::json!({ + "kind": "push", + "name": "certd", + "domains": ["test.example.com", "alt.example.com"] + }), + ) + .await; + assert_eq!(created.0, StatusCode::CREATED, "token: {}", created.1); + let token: Value = serde_json::from_str(&created.1).unwrap(); + let plaintext = token["data"]["plaintext"].as_str().unwrap().to_string(); + + let without = post_json( + router, + "/ingest/v1/certs", + serde_json::json!({ "crt": CERT, "key": KEY }), + ) + .await; + assert_eq!( + without.0, + StatusCode::UNAUTHORIZED, + "the API path must not bypass the ingest token" + ); + + let wrong = send( + router, + Request::builder() + .method("POST") + .uri("/ingest/v1/certs") + .header("content-type", "application/json") + .header("x-cert-ingest-token", "not-a-real-token") + .body(Body::from( + serde_json::json!({ "crt": CERT, "key": KEY }).to_string(), + )) + .unwrap(), + ) + .await; + assert_eq!(wrong.0, StatusCode::UNAUTHORIZED); + + let accepted = send( + router, + Request::builder() + .method("POST") + .uri("/ingest/v1/certs") + .header("content-type", "application/json") + .header("x-cert-ingest-token", &plaintext) + .body(Body::from( + serde_json::json!({ "crt": CERT, "key": KEY }).to_string(), + )) + .unwrap(), + ) + .await; + assert_eq!(accepted.0, StatusCode::OK, "push: {}", accepted.1); + let pushed: Value = serde_json::from_str(&accepted.1).unwrap(); + assert_eq!(pushed["primary_domain"], "test.example.com"); + + let health = send( + router, + Request::builder() + .uri("/ingest/v1/healthz") + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(health.0, StatusCode::OK); +} diff --git a/crates/rustfrp-sdk/Cargo.toml b/crates/rustfrp-sdk/Cargo.toml index 318cd46..b804d88 100644 --- a/crates/rustfrp-sdk/Cargo.toml +++ b/crates/rustfrp-sdk/Cargo.toml @@ -14,7 +14,7 @@ serde_json.workspace = true tracing.workspace = true thiserror.workspace = true -rustfrp-common = { version = "0.5.4", path = "../common" } +rustfrp-common.workspace = true # WASM 运行时(未来 Wasmtime 集成) # wasmtime = "24" diff --git a/crates/server/agent/Cargo.toml b/crates/server/agent/Cargo.toml index ac5f1eb..84efbfd 100644 --- a/crates/server/agent/Cargo.toml +++ b/crates/server/agent/Cargo.toml @@ -8,8 +8,8 @@ rust-version.workspace = true description = "RustFRP agent — optional frps wrapper for server-side config management and plugin runtime" [dependencies] -rustfrp-common = { version = "0.5.4", path = "../../common" } -rustfrp-bin = { version = "0.5.0", path = "../../rustfrp-bin" } +rustfrp-common.workspace = true +rustfrp-bin.workspace = true tokio = { workspace = true, features = ["process", "sync", "time"] } serde.workspace = true serde_json.workspace = true diff --git a/crates/server/control/Cargo.toml b/crates/server/control/Cargo.toml index 04284f5..9e3738e 100644 --- a/crates/server/control/Cargo.toml +++ b/crates/server/control/Cargo.toml @@ -8,7 +8,7 @@ rust-version.workspace = true description = "Control server — frps management, metrics collection, config template API" [dependencies] -rustfrp-common = { version = "0.5.4", path = "../../common" } +rustfrp-common.workspace = true tokio.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/deny.toml b/deny.toml index 2e620e2..ad10fa3 100644 --- a/deny.toml +++ b/deny.toml @@ -4,6 +4,22 @@ all-features = true [advisories] yanked = "deny" +# Reviewed 2026-11-21 for v0.6.0. Each entry names why it does not block: +# an upgrade is either impossible (chrono has no fixed release) or out of scope +# for a patch release (wasmtime is pinned by the plugin ABI). +ignore = [ + # `unmaintained`, not a vulnerability; the whole 0.4 line is affected, so + # there is no upgrade that clears it. Replacing chrono touches every date + # path in the client and the API; that is its own change, not a release fix. + { id = "RUSTSEC-2026-0543", reason = "chrono is unmaintained (all versions); replacing it is a separate change" }, + # wasmtime only runs WASM plugins. The advisories need a malicious plugin to + # be loaded, which is the same trust boundary as running the plugin's code; + # an upgrade is still worth doing, but it is pinned to the plugin ABI. + { id = "RUSTSEC-2026-0566", reason = "wasmtime: requires a hostile WASM plugin; plugin loading is already trusted-code" }, + { id = "RUSTSEC-2026-0569", reason = "wasmtime: requires a hostile WASM plugin; plugin loading is already trusted-code" }, + { id = "RUSTSEC-2026-0571", reason = "wasmtime-wasi-http is not enabled by this build (no wasi-http feature)" }, +] + [licenses] confidence-threshold = 0.8 allow = [ diff --git a/deploy/compose/README.md b/deploy/compose/README.md index a1b9924..0afcc91 100644 --- a/deploy/compose/README.md +++ b/deploy/compose/README.md @@ -14,9 +14,42 @@ export GRAFANA_ADMIN_PASSWORD='replace-with-a-long-password' docker compose up -d --build ``` -Endpoints default to daemon `7900`, FRPS `7000`, control `3000`, Prometheus -`9090`, Alertmanager `9093`, and Grafana `3001`. The FRPS HTTP/HTTPS vhost ports -default to `8080` and `8443`. +Endpoints default to daemon `7900`, certificate ingest `7901`, FRPS `7000`, +control `3000`, Prometheus `9090`, Alertmanager `9093`, and Grafana `3001`. The +FRPS HTTP/HTTPS vhost ports default to `8080` and `8443`. + +## Certificate ingest endpoint + +Certificates for `https` proxies are pushed to the daemon, because frps does not +terminate TLS — it only reads the SNI and routes. The push route +(`/ingest/v1/certs`, plus `/ingest/v1/healthz`) is served by **both** the control +API on `7900` and a dedicated listener on `7901`. It authenticates with the +`X-Cert-Ingest-Token` header and returns no certificate material. + +Prefer the control API route: it answers at whatever address the UI is reached +on — a frpc `https` proxy, a reverse proxy, a LAN address — so nothing extra +needs publishing. The dedicated `7901` listener exists for deployments that map +that port directly; it is loopback-only by default and enabled by default *inside +the container*, so the port mapping is what makes it reachable. + +The URL shown in System Status → Certificate channels is resolved per request: +the `Host` (or `X-Forwarded-Host`/`X-Forwarded-Proto`) the browser used wins, then +the bind address. Set `RUSTFRP_CERT_INGEST_PUBLIC_URL` to pin it when neither +describes how Certd reaches the daemon (a path-rewriting proxy, a remapped port). + +In a container the `7901` listener has to bind all interfaces or the published +port reaches nothing, so `RUSTFRP_CERT_INGEST_LISTEN=0.0.0.0:7901` is set for you. +Whether the listener runs at all still comes from the stored setting exposed in +the UI (System Status → Certificate channels), which defaults to enabled. The host side of that mapping defaults to `127.0.0.1`, so nothing on the network +can reach it. A Certd running in this same Compose stack does not need the host +port at all — point its webhook at `http://daemon:7900/ingest/v1/certs`. For an +external Certd that must use the dedicated port, set `RUSTFRP_CERT_INGEST_BIND=0.0.0.0` +(or a specific interface) +and publish `RUSTFRP_CERT_INGEST_PORT`; the container port stays `7901` so the +mapping and the listener cannot disagree. + +Removing the `7901` mapping keeps the endpoint unreachable from outside the +container while leaving it usable inside the Compose network. The provisioned `RustFRP Overview` dashboard is read-only and refreshes every 15 seconds. `RustFRPNodeOffline` fires after two minutes without a successful diff --git a/docs/frpc-versions.md b/docs/frpc-versions.md index 7b7236e..547f600 100644 --- a/docs/frpc-versions.md +++ b/docs/frpc-versions.md @@ -2,7 +2,7 @@ ## 入口与行为 -- **系统状态 → FRP 版本**:安装版本、查看后台进度与来源、设置系统默认或删除未使用版本。 +- **运维管理 → 版本下载**:自动读取官方发布列表(版本、归档大小、下载数、发布时间),按渠道下载,卡片或表格内显示每个版本自己的进度、安装来源,并可设为系统默认或删除未使用版本。 - **服务器配置 → 每行的 frpc 版本按钮**:选择“跟随系统默认”或固定已安装且校验通过的版本。新建配置默认跟随系统,保存配置后可单独设置。 - **代理分配**:显示服务器当前运行版本;版本变更未应用时显示“待重启”,点击跳转服务器配置。 @@ -28,7 +28,8 @@ ## API - `GET/PUT /api/v1/profiles/:id/frpc-version`:读取版本与待重启状态;提交 `{"version": null}` 跟随默认,或指定版本字符串。按租户检查配置权限。 -- `GET/POST /api/v1/frp/installations`:查询任务或创建任务;提交 `version`、`channel`(`official`、`ghfast`、`gh-proxy`、`custom`)及可选 `mirror_base`。 +- `GET/POST /api/v1/frp/installations`:查询全部任务(数组,最新在前)或创建任务;提交 `version`、`channel`(`official`、`ghfast`、`gh-proxy`、`custom`)及可选 `mirror_base`。不同版本可并发下载;同一版本重复提交返回 `FRP_INSTALL_BUSY`。 +- `GET /api/v1/frp/releases`:官方发布列表,含 `tag`、`published_at`、`asset_size`、`download_count` 与 `asset_name`(按守护进程自身平台挑选归档)。 - 保留旧同步安装 `POST /api/v1/frp/versions` 以兼容现有调用方。 - 版本注册表和安装任务使用平台级读写权限;服务器版本选择使用对应租户读写权限。 diff --git a/docs/images/webui-certificates.png b/docs/images/webui-certificates.png new file mode 100644 index 0000000..92f7831 Binary files /dev/null and b/docs/images/webui-certificates.png differ diff --git a/docs/images/webui-versions.png b/docs/images/webui-versions.png index 92c1898..9a070bb 100644 Binary files a/docs/images/webui-versions.png and b/docs/images/webui-versions.png differ diff --git a/plugins/gui/package.json b/plugins/gui/package.json index aab3399..3b20a56 100644 --- a/plugins/gui/package.json +++ b/plugins/gui/package.json @@ -1,7 +1,7 @@ { "name": "rustfrp-desktop-shell", "private": true, - "version": "0.5.4", + "version": "0.6.0", "scripts": { "dev": "tauri dev", "build": "tauri build", diff --git a/plugins/gui/src-tauri/Cargo.lock b/plugins/gui/src-tauri/Cargo.lock index cce9416..9d98616 100644 --- a/plugins/gui/src-tauri/Cargo.lock +++ b/plugins/gui/src-tauri/Cargo.lock @@ -8,6 +8,16 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -97,6 +107,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 2.0.21", + "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" @@ -607,6 +656,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + [[package]] name = "chacha20" version = "0.10.2" @@ -615,7 +675,20 @@ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", "cpufeatures 0.3.1", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20 0.9.1", + "cipher", + "poly1305", + "zeroize", ] [[package]] @@ -632,6 +705,17 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", + "zeroize", +] + [[package]] name = "clap" version = "4.6.7" @@ -811,6 +895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] @@ -902,6 +987,12 @@ dependencies = [ "syn 3.0.10", ] +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + [[package]] name = "dbus" version = "0.9.12" @@ -913,6 +1004,20 @@ dependencies = [ "windows-sys 0.61.2", ] +[[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" @@ -1552,7 +1657,7 @@ dependencies = [ "js-sys", "libc", "r-efi 6.0.0", - "rand_core", + "rand_core 0.10.1", "wasm-bindgen", ] @@ -2063,6 +2168,15 @@ dependencies = [ "cfb", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -2394,6 +2508,16 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.8.3" @@ -2425,6 +2549,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -2537,6 +2667,16 @@ dependencies = [ "libc", ] +[[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 = "ntapi" version = "0.4.3" @@ -2555,12 +2695,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" @@ -2799,6 +2958,15 @@ dependencies = [ "objc2-foundation", ] +[[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" @@ -2811,6 +2979,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "option-ext" version = "0.2.0" @@ -3076,6 +3250,17 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash", +] + [[package]] name = "portable-atomic" version = "1.15.0" @@ -3266,9 +3451,18 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ - "chacha20", + "chacha20 0.10.2", "getrandom 0.4.3", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", ] [[package]] @@ -3283,7 +3477,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" dependencies = [ - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -3562,7 +3756,7 @@ dependencies = [ [[package]] name = "rustfrp-bin" -version = "0.5.4" +version = "0.6.0" dependencies = [ "dirs 5.0.1", "flate2", @@ -3579,14 +3773,17 @@ dependencies = [ [[package]] name = "rustfrp-client" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "async-trait", + "base64 0.22.1", + "chacha20poly1305", "chrono", "clap", "dirs 5.0.1", "nix 0.29.0", + "ring", "rusqlite", "rustfrp-bin", "rustfrp-common", @@ -3599,11 +3796,12 @@ dependencies = [ "tracing", "tracing-subscriber", "uuid", + "x509-parser", ] [[package]] name = "rustfrp-common" -version = "0.5.4" +version = "0.6.0" dependencies = [ "chrono", "dirs 5.0.1", @@ -3617,13 +3815,15 @@ dependencies = [ [[package]] name = "rustfrp-daemon" -version = "0.5.4" +version = "0.6.0" dependencies = [ "anyhow", "axum", + "base64 0.22.1", "chrono", "clap", "libc", + "md-5", "mime_guess", "reqwest 0.12.28", "rust-embed", @@ -3645,7 +3845,7 @@ dependencies = [ [[package]] name = "rustfrp-desktop" -version = "0.5.4" +version = "0.6.0" dependencies = [ "rustfrp-common", "rustfrp-daemon", @@ -3659,6 +3859,15 @@ dependencies = [ "tracing", ] +[[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.6" @@ -4285,6 +4494,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" @@ -5186,6 +5406,16 @@ version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + [[package]] name = "untrusted" version = "0.9.0" @@ -6024,6 +6254,23 @@ dependencies = [ "pkg-config", ] +[[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", + "rusticata-macros", + "thiserror 2.0.21", + "time", +] + [[package]] name = "xattr" version = "1.7.0" @@ -6054,7 +6301,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.10", - "synstructure", + "synstructure 0.14.0", ] [[package]] @@ -6145,7 +6392,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.10", - "synstructure", + "synstructure 0.14.0", ] [[package]] diff --git a/plugins/gui/src-tauri/Cargo.toml b/plugins/gui/src-tauri/Cargo.toml index 96978e0..1b542bc 100644 --- a/plugins/gui/src-tauri/Cargo.toml +++ b/plugins/gui/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustfrp-desktop" -version = "0.5.4" +version = "0.6.0" edition = "2021" license = "AGPL-3.0-only" repository = "https://github.com/taten/rustfrp" @@ -20,8 +20,8 @@ path = "src/main.rs" tauri-build = { version = "2", features = [] } [dependencies] -rustfrp-common = { version = "0.5.4", path = "../../../crates/common" } -rustfrp-daemon = { version = "0.5.0", path = "../../../crates/rustfrp-daemon" } +rustfrp-common = { version = "0.6.0", path = "../../../crates/common" } +rustfrp-daemon = { version = "0.6.0", path = "../../../crates/rustfrp-daemon" } tauri = { version = "2", features = ["tray-icon"] } tauri-plugin-dialog = "2" tauri-plugin-single-instance = "2" diff --git a/plugins/webui/e2e/app.e2e.ts b/plugins/webui/e2e/app.e2e.ts index a93e883..bbd7a83 100644 --- a/plugins/webui/e2e/app.e2e.ts +++ b/plugins/webui/e2e/app.e2e.ts @@ -77,7 +77,7 @@ async function fixture(page: Page, authenticated = true) { route.fulfill({ json: { success: true, data } }); const [kind, rawId, action] = path.split("/"); const id = Number(rawId); - if (path === "frp/installations" && method === "GET") return ok(null); + if (path === "frp/installations" && method === "GET") return ok([]); if (path === "status") return ok({ state: "running", @@ -199,7 +199,16 @@ async function fixture(page: Page, authenticated = true) { stderr_path: "/tmp/stderr", }); if (path === "frp/releases") - return ok([{ version: "0.70.1", published_at: null }]); + return ok( + ["0.70.2", "0.70.1"].map((version) => ({ + version, + tag: `v${version}`, + published_at: "2026-10-14T00:00:00Z", + asset_size: 13959168, + download_count: 166576, + asset_name: `frp_${version}_linux_amd64.tar.gz`, + })), + ); if (path === "frp/versions" && method === "GET") return ok({ active: "0.70.1", @@ -387,12 +396,12 @@ test("logs support stream filters, ANSI colors and escaped HTML", async ({ await expect(page.locator(".log-console")).toContainText("INFO ready"); expect(writes).toHaveLength(0); }); -test("system actions preserve import/export, mirror validation and version confirmation", async ({ +test("downloads page lists releases and deletes an installed version", async ({ page, }) => { const { writes } = await fixture(page); - await page.goto("/#/status"); - await page.getByRole("tab", { name: "FRP versions", exact: true }).click(); + await page.goto("/#/downloads"); + await page.getByRole("button", { name: "List", exact: true }).click(); await page .getByRole("row") .filter({ hasText: "0.69.0" }) @@ -410,7 +419,7 @@ test("system actions preserve import/export, mirror validation and version confi ), ) .toBe(true); - await page.getByRole("tab", { name: "Import & export", exact: true }).click(); + await page.goto("/#/status?tab=transfer"); await page.locator("input[type=file]").setInputFiles({ name: "sample.toml", mimeType: "text/plain", @@ -515,9 +524,11 @@ test("batch ports create typed proxy records and reject name conflicts", async ( .getByRole("button", { name: "Save", exact: true }) .click(); await expect(page.getByRole("dialog")).toBeVisible(); - await expect(page.locator("[data-sonner-toast]").last()).toContainText( - "9000", - ); + // Scope by content, not position: the successful first batch leaves its own + // toast in the stack, and toast order depends on which one expires first. + await expect( + page.locator("[data-sonner-toast]").filter({ hasText: "9000" }), + ).toBeVisible(); expect( writes.filter((w) => w.path === "proxies" && w.method === "POST"), ).toHaveLength(3); @@ -809,28 +820,29 @@ test("installer submits accelerator channel and shows background task progress", downloaded_bytes: 524288, total_bytes: 1048576, error: null, + started_at: "2026-11-16T00:00:00Z", + finished_at: null, }; } - await route.fulfill({ json: { success: true, data: task } }); + const list = task ? [task] : []; + await route.fulfill({ + json: { success: true, data: route.request().method() === "GET" ? list : task }, + }); }); - await page.goto("/#/status?tab=versions"); - await page.getByLabel("Version", { exact: true }).fill("0.70.1"); + await page.goto("/#/downloads"); await page .getByRole("combobox", { name: "Download channel", exact: true }) .click(); await page .getByRole("option", { name: "GHFast · ghfast.top", exact: true }) .click(); - await page - .getByRole("button", { name: "Install version", exact: true }) - .click(); + const card = page.locator("article").filter({ hasText: "v0.70.2" }); + await card.getByRole("button", { name: "Download", exact: true }).click(); await expect.poll(() => submitted?.channel).toBe("ghfast"); + await expect(card.locator("progress")).toHaveAttribute("value", "50"); await expect( - page.getByRole("status").filter({ hasText: "Downloading archive" }), - ).toContainText("50%"); - await expect( - page.getByRole("button", { name: "Install version", exact: true }), - ).toBeDisabled(); + card.getByRole("button", { name: "Download", exact: true }), + ).toHaveCount(0); await page.screenshot({ path: "test-results/frpc-installation.png", animations: "disabled", @@ -840,7 +852,7 @@ test("installer submits accelerator channel and shows background task progress", page.getByText("Checksum mismatch", { exact: true }), ).toBeVisible(); await expect( - page.getByRole("button", { name: "Install version", exact: true }), + page.getByRole("button", { name: "Retry", exact: true }), ).toBeEnabled(); }); diff --git a/plugins/webui/package.json b/plugins/webui/package.json index b07e639..fe3ad2f 100644 --- a/plugins/webui/package.json +++ b/plugins/webui/package.json @@ -1,6 +1,6 @@ { "name": "rustfrp-webui", - "version": "0.5.4", + "version": "0.6.0", "private": true, "type": "module", "packageManager": "bun@>=1.2", diff --git a/plugins/webui/screenshots/readme.screenshots.ts b/plugins/webui/screenshots/readme.screenshots.ts index 237b7a2..362fc22 100644 --- a/plugins/webui/screenshots/readme.screenshots.ts +++ b/plugins/webui/screenshots/readme.screenshots.ts @@ -28,6 +28,15 @@ test("capture populated documentation pages without contacting a daemon", async effective_version: id === 2 ? "0.69.1" : "0.70.1", running_version: id === 2 ? "0.69.1" : "0.70.1", running: true, restart_required: false, }); + if (path.endsWith("/proxy-status")) return ok({ + profile_id: id, available: true, + proxies: bindings.filter(b => b.profile_id === id).map(b => ({ + proxy_type: proxies.find(p => p.id === b.proxy_id)?.type ?? "tcp", + name: proxies.find(p => p.id === b.proxy_id)?.name ?? "", + status: "running", local_addr: "127.0.0.1:8080", + remote_addr: `example.com:${19000 + b.proxy_id!}`, + })), + }); if (path.endsWith("/runtime")) return ok({ profile_id: id, running: true, desired_running: true, config_pending: false, enabled_proxy_count: bindings.filter(b => b.profile_id === id).length, process_status: "running" }); if (path.startsWith("logs/")) return ok({ profile_id: id, profile_name: profiles.find(p => p.id === id)?.name, @@ -35,8 +44,22 @@ test("capture populated documentation pages without contacting a daemon", async const responses: Record = { status, profiles, proxies, bindings, visitors: [], "metrics/history": metrics, environments: [{ id: 1, name: "演示环境", description: "README 示例数据", profile_ids: [1,2,3], is_default: true, color: "#2563eb" }], - "frp/versions": versions, "frp/installations": null, - "frp/releases": ["0.70.1", "0.70.0", "0.69.1", "0.69.0", "0.68.1"].map(version => ({ version, published_at: null })), + "frp/versions": versions, "frp/installations": [], + certificates: [{ + id: 1, primary_domain: "example.com", sans: ["example.com", "*.example.com"], + not_after: "2027-01-14T00:00:00Z", days_remaining: 56, source: "certd_pull", + source_detail: "certd.example.com", key_algo: "ECDSA P-256", + fingerprint_sha256: "aa11bb33cc55dd77ee99ff11223344556677889900aabbccddeeff1122334455", + bindings: [], + }], + "cert-ingest": { enabled: true, expose: false, port: 7901, listen: "127.0.0.1:7901", address: "127.0.0.1:7901", error: null, url: "http://127.0.0.1:7901/ingest/v1/certs" }, + "cert-ingest-tokens": [], + "cert-pull-sources": [{ id: 1, name: "演示 Certd", kind: "certd", base_url: "https://certd.example.com", domain: "example.com", last_run_at: "2026-11-18T09:30:00Z", last_error: null }], + "frp/releases": ["0.71.1", "0.70.1", "0.70.0", "0.69.1", "0.69.0", "0.68.1"].map((version, i) => ({ + version, tag: `v${version}`, published_at: ["2026-10-14", "2026-08-14", "2026-07-23", "2026-06-01", "2026-04-18", "2026-02-09"][i], + asset_size: 13500000 + i * 420_000, download_count: 166_576 - i * 24_000, + asset_name: `frp_${version}_linux_amd64.tar.gz`, + })), health: { status: "ok", version: "0.4.0" }, }; if (path in responses) return ok(responses[path]); @@ -70,16 +93,19 @@ test("capture populated documentation pages without contacting a daemon", async await page.goto("/#/bindings"); await expect(page.getByText("香港 · 主节点", { exact: true })).toBeVisible(); - await expect(page.getByText("0.69.1", { exact: false }).first()).toBeVisible(); await capture("webui-proxy-assignments.png"); - await page.goto("/#/status?tab=versions"); - await expect(page.getByRole("cell", { name: "0.69.1", exact: false })).toBeVisible(); + await page.goto("/#/downloads"); + await expect(page.getByRole("heading", { name: "v0.71.1", exact: true })).toBeVisible(); await page.getByRole("combobox", { name: "下载渠道" }).click(); await page.getByRole("option", { name: "GHFast · ghfast.top" }).click(); - await page.getByRole("heading", { name: "系统状态", exact: true }).click(); + await page.keyboard.press("Escape"); await capture("webui-versions.png"); + await page.goto("/#/certificates"); + await expect(page.getByRole("cell", { name: "example.com *.example.com" })).toBeVisible(); + await capture("webui-certificates.png"); + await page.goto("/#/logs"); await expect(page.getByText(/login to server success/)).toBeVisible(); await capture("webui-logs.png"); diff --git a/plugins/webui/src/api/certificates.ts b/plugins/webui/src/api/certificates.ts new file mode 100644 index 0000000..83bd883 --- /dev/null +++ b/plugins/webui/src/api/certificates.ts @@ -0,0 +1,205 @@ +import { apiDelete, apiGet, apiPost, apiPut } from '@/api/client' +import type { ApiEnvelope } from '@/api/types' + +/** + * Certificate metadata as returned by the daemon. + * + * This type intentionally has no certificate or key fields: the daemon never + * sends them, and there is no endpoint to fetch them. + * + * A certificate exists before it has material, which is why the fields that + * only a bundle can supply are nullable: `material_id` is `null` until the first + * import, and `days_remaining` follows it. + */ +export interface Certificate { + id: number + tenant_id: string + primary_domain: string + sans: string[] + issuer: string + serial: string + fingerprint_sha256: string + key_algo: string + not_before: string + not_after: string + /** Material row these fields came from; null before the first import. */ + material_id: number | null + days_remaining: number | null + /** Derived state. `usable` is exactly `status !== expired|not_yet_valid|pending`. */ + status: 'active' | 'expiring_soon' | 'expired' | 'not_yet_valid' | 'pending' + /** Why it cannot be used, when it cannot. */ + status_reason: string | null + usable: boolean + /** Last failure on any attached channel, so a pending certificate can explain itself. */ + channel_error: string | null + source: string + source_detail: string | null + created_at: string + updated_at: string + bindings: CertificateBinding[] + warnings: string[] +} + +export interface CertificateBinding { + id: number + tenant_id: string + cert_id: number + proxy_id: number + proxy_name: string + applied_fingerprint: string | null + applied_at: string | null + last_error: string | null + created_at: string + updated_at: string +} + +/** How a certificate gets updated. */ +export type SourceKind = 'push' | 'certd' | 'url' + +/** + * One channel for one certificate. + * + * A channel can be unbound (`certificate_id: null`) until it is attached; that is + * what the legacy global tokens became after the upgrade. + */ +export interface CertificateSource { + id: number + tenant_id: string + certificate_id: number | null + kind: SourceKind + name: string + domain: string + base_url: string + /** Allowed domains, for a push token only. */ + domains: string[] + /** Whether a credential is stored; the credential itself is never sent. */ + has_secret: boolean + is_primary: boolean + enabled: boolean + expires_at: string | null + revoked_at: string | null + last_used_at: string | null + use_count: number + last_run_at: string | null + last_status: string | null + last_error: string | null + next_run_at: string | null + created_at: string + updated_at: string +} + +export interface SourceInput { + certificate_id?: number | null + kind: SourceKind + name: string + domain?: string + base_url?: string + domains?: string[] + key_id?: string + key_secret?: string + enabled?: boolean +} + +export interface CreatedSource { + source: CertificateSource + /** Shown once, for a push channel only. */ + plaintext: string | null +} + +export interface ApplyReport { + applied: number[] + failed: { binding_id: number; error: string }[] +} + +export interface PullReport { + source_id: number + status: string + certificate_id: number | null + primary_domain: string | null + applied: number + message: string +} + +export function listCertificates(params?: { + expiring_within?: number + domain?: string +}): Promise> { + const query = new URLSearchParams() + if (params?.expiring_within !== undefined) + query.set('expiring_within', String(params.expiring_within)) + if (params?.domain) query.set('domain', params.domain) + const qs = query.toString() + return apiGet(`/certificates${qs ? `?${qs}` : ''}`) +} + +export function importPem(body: { + cert: string + key: string + note?: string +}): Promise> { + return apiPost('/certificates/pem', body) +} + +export function importFromPath(body: { + crt_path: string + key_path: string + note?: string +}): Promise> { + return apiPost('/certificates/from-path', body) +} + +export function applyCertificate(id: number): Promise> { + return apiPost(`/certificates/${id}/apply`) +} + +export function deleteCertificate(id: number): Promise> { + return apiDelete(`/certificates/${id}`) +} + +export function bindCertificate(body: { + cert_id: number + proxy_id: number +}): Promise> { + return apiPost('/certificate-bindings', body) +} + +export function unbindCertificate( + id: number, + removeFiles = false, +): Promise> { + return apiDelete(`/certificate-bindings/${id}?remove_files=${removeFiles}`) +} + +// === Channels === + +export function listSources(certificateId?: number): Promise> { + const qs = + certificateId === undefined ? '' : `?certificate_id=${certificateId}` + return apiGet(`/certificate-sources${qs}`) +} + +export function createSource(body: SourceInput): Promise> { + return apiPost('/certificate-sources', body) +} + +export function deleteSource(id: number): Promise> { + return apiDelete(`/certificate-sources/${id}`) +} + +/** Attach to a certificate, or detach with `null`. */ +export function attachSource( + id: number, + certificateId: number | null, +): Promise> { + return apiPut(`/certificate-sources/${id}/certificate`, { + certificate_id: certificateId, + }) +} + +export function promoteSource(id: number): Promise> { + return apiPost(`/certificate-sources/${id}/primary`) +} + +export function runSource(id: number): Promise> { + return apiPost(`/certificate-sources/${id}/run`) +} diff --git a/plugins/webui/src/api/environments.ts b/plugins/webui/src/api/environments.ts deleted file mode 100644 index bcc9394..0000000 --- a/plugins/webui/src/api/environments.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { apiGet, apiPut } from '@/api/client' -import type { ApiEnvelope, Environment } from '@/api/types' - -// The WebUI ships a switcher and per-profile reassignment only; creating, -// renaming, and deleting environments stays an API/operator action, so no -// client bindings exist for those routes. -export const listEnvironments = (): Promise> => - apiGet('/environments') - -export const assignProfile = (profileId: number, environmentId: number): Promise> => - apiPut(`/profiles/${profileId}/environment`, { environment_id: environmentId }) diff --git a/plugins/webui/src/api/frp.ts b/plugins/webui/src/api/frp.ts index cd09b1c..2a31071 100644 --- a/plugins/webui/src/api/frp.ts +++ b/plugins/webui/src/api/frp.ts @@ -3,7 +3,6 @@ import type { ApiEnvelope, AvailableFrpVersion, FrpVersionList, - InstalledFrpVersion, } from "@/api/types"; export function listFrpVersions(): Promise> { @@ -16,16 +15,6 @@ export function listAvailableFrpVersions(): Promise< return apiGet("/frp/releases"); } -export function installFrpVersion( - version: string, - mirrorBase?: string, -): Promise> { - return apiPost("/frp/versions", { - version, - mirror_base: mirrorBase || null, - }); -} - export function activateFrpVersion( version: string, ): Promise> { @@ -47,9 +36,14 @@ export interface InstallTask { downloaded_bytes: number; total_bytes: number | null; error: string | null; + started_at: string; + finished_at: string | null; } -export const getInstallation = () => - apiGet("/frp/installations"); + +/** Every install task this daemon knows about; several may run at once. */ +export const getInstallations = () => + apiGet("/frp/installations"); + export const beginInstallation = ( version: string, channel: string, diff --git a/plugins/webui/src/api/metrics.ts b/plugins/webui/src/api/metrics.ts index 5f5ae4c..333a0c3 100644 --- a/plugins/webui/src/api/metrics.ts +++ b/plugins/webui/src/api/metrics.ts @@ -1,10 +1,8 @@ import { apiGet, apiPost } from '@/api/client' import type { ApiEnvelope, MetricsHistory, TrafficSample } from '@/api/types' -export function getMetricsHistory(environmentId?: number, limit = 36_000): Promise> { - const query = new URLSearchParams({ limit: String(limit) }) - if (environmentId) query.set('environment_id', String(environmentId)) - return apiGet(`/metrics/history?${query}`) +export function getMetricsHistory(limit = 36_000): Promise> { + return apiGet(`/metrics/history?limit=${limit}`) } export function ingestTraffic(sample: TrafficSample): Promise> { diff --git a/plugins/webui/src/api/system.ts b/plugins/webui/src/api/system.ts index b349b27..4d8b8c5 100644 --- a/plugins/webui/src/api/system.ts +++ b/plugins/webui/src/api/system.ts @@ -1,9 +1,8 @@ import { apiGet, apiPost } from '@/api/client' import type { ApiEnvelope, StatusResponse, ReloadResponse, ReloadTaskStatus, HealthResponse } from '@/api/types' -export function getStatus(environmentId?: number | null): Promise> { - const query = environmentId ? `?environment_id=${environmentId}` : '' - return apiGet(`/status${query}`) +export function getStatus(): Promise> { + return apiGet('/status') } export function triggerReload(): Promise> { diff --git a/plugins/webui/src/api/types.ts b/plugins/webui/src/api/types.ts index 5741fea..566d012 100644 --- a/plugins/webui/src/api/types.ts +++ b/plugins/webui/src/api/types.ts @@ -232,11 +232,16 @@ export interface FrpVersionList { export interface AvailableFrpVersion { version: string + tag: string published_at: string | null + asset_size: number | null + download_count: number | null + asset_name: string } export interface StatusResponse { state: string + config_dir: string uptime_secs: number active_frpc_instances: number total_profiles: number @@ -246,17 +251,6 @@ export interface StatusResponse { processes: ProcessInfo[] } -export interface Environment { - id?: number - name: string - description?: string | null - color: string - is_default: boolean - created_at: string - updated_at: string - profile_ids: number[] -} - export interface ResourceSample { timestamp: string daemon_cpu_percent: number @@ -273,7 +267,6 @@ export interface TrafficSample { source?: string counter_epoch?: string timestamp: string - environment_id: number profile_id: number received_bytes: number sent_bytes: number @@ -323,6 +316,6 @@ export interface TrafficCollection { state: string updated_at: string | null host_state?: string - proxies?: { profile_id: number; environment_id: number; proxy_id: number; name: string; state: string }[] - profiles: { profile_id: number; environment_id: number; state: string }[] + proxies?: { profile_id: number; proxy_id: number; name: string; state: string }[] + profiles: { profile_id: number; state: string }[] } diff --git a/plugins/webui/src/app/router.tsx b/plugins/webui/src/app/router.tsx index 214bed5..d90686a 100644 --- a/plugins/webui/src/app/router.tsx +++ b/plugins/webui/src/app/router.tsx @@ -8,6 +8,8 @@ import Shell from "./shell"; const Overview = lazy(() => import("@/features/overview/page")); const Resources = lazy(() => import("@/features/resources/page")); const Bindings = lazy(() => import("@/features/bindings/page")); +const Certificates = lazy(() => import("@/features/certificates/page")); +const Downloads = lazy(() => import("@/features/downloads/page")); const Logs = lazy(() => import("@/features/logs/page")); const Settings = lazy(() => import("@/features/settings/page")); const Login = lazy(() => import("@/features/auth/login")); @@ -34,6 +36,8 @@ export default function Router() { /> ))} } /> + } /> + } /> } /> } /> } /> diff --git a/plugins/webui/src/app/shell.tsx b/plugins/webui/src/app/shell.tsx index 4f7332f..2ff00ff 100644 --- a/plugins/webui/src/app/shell.tsx +++ b/plugins/webui/src/app/shell.tsx @@ -9,7 +9,7 @@ import { import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useI18n } from "@/i18n"; import { healthCheck } from "@/api/system"; -import { data, useActiveEnvironment, useStatus } from "@/hooks/api"; +import { data, useStatus } from "@/hooks/api"; import { usePreferences, type ThemeMode } from "@/hooks/preferences"; import { isDesktop } from "@/lib/desktop"; import { AppIcon, type IconName } from "@/components/icon/AppIcon"; @@ -47,14 +47,16 @@ import { SidebarTrigger, useSidebar, } from "@/components/ui/sidebar"; -import { Choice, PageLoading, RuntimeBadge } from "@/components/common/page"; +import { PageLoading, RuntimeBadge } from "@/components/common/page"; type NavKey = | "dashboard" | "profiles" | "proxies" | "bindings" | "visitors" + | "certificates" | "logs" + | "downloads" | "status"; const sections: { key: string; items: [NavKey, string][] }[] = [ { key: "overview", items: [["dashboard", "/"]] }, @@ -65,12 +67,14 @@ const sections: { key: string; items: [NavKey, string][] }[] = [ ["proxies", "/proxies"], ["bindings", "/bindings"], ["visitors", "/visitors"], + ["certificates", "/certificates"], ], }, { key: "management", items: [ ["logs", "/logs"], + ["downloads", "/downloads"], ["status", "/status"], ], }, @@ -79,8 +83,6 @@ function Navigation() { const { t, text } = useI18n(); const location = useLocation(); const { setOpenMobile } = useSidebar(); - const prefs = usePreferences(), - { environments, environment: active } = useActiveEnvironment(); // Read the running daemon's version rather than hardcoding it: the build in // the window is whatever that daemon serves, so a literal here drifts on every // release (v0.5.0 shipped a sidebar still claiming v0.4.0). @@ -111,17 +113,6 @@ function Navigation() { -
- ({ - label: e.name, - value: e.id!, - }))} - onChange={(v) => prefs.selectEnvironment(Number(v))} - /> -
{sections.map((section) => ( diff --git a/plugins/webui/src/components/common/data-table.tsx b/plugins/webui/src/components/common/data-table.tsx index 66dfc23..7b04bcc 100644 --- a/plugins/webui/src/components/common/data-table.tsx +++ b/plugins/webui/src/components/common/data-table.tsx @@ -23,11 +23,13 @@ export function DataTable({ columns, rowKey, paginate = true, + dense = false, }: { rows: T[]; columns: Column[]; rowKey: (row: T) => string | number; paginate?: boolean; + dense?: boolean; }) { const { text } = useI18n(); const [sort, setSort] = useState({ key: "", direction: 1 }); @@ -95,7 +97,7 @@ export function DataTable({ {columns.map((c) => ( {c.cell(row)} diff --git a/plugins/webui/src/components/icon/AppIcon.tsx b/plugins/webui/src/components/icon/AppIcon.tsx index 841829f..b2982f0 100644 --- a/plugins/webui/src/components/icon/AppIcon.tsx +++ b/plugins/webui/src/components/icon/AppIcon.tsx @@ -21,6 +21,7 @@ import { LoaderCircle, LogOut, Menu, + EllipsisVertical, Network, Palette, Play, @@ -30,6 +31,7 @@ import { Server, Settings, Shield, + ShieldCheck, Square, Trash2, Upload, @@ -37,6 +39,7 @@ import { type LucideProps, } from "lucide-react"; const icons = { + more: EllipsisVertical, activity: Activity, alert: CircleAlert, info: Info, @@ -52,6 +55,7 @@ const icons = { copy: Copy, cpu: Cpu, download: Download, + downloads: Download, logs: FileText, visitors: Globe, memory: HardDrive, @@ -68,11 +72,13 @@ const icons = { profiles: Server, status: Settings, shield: Shield, + "shield-check": ShieldCheck, stop: Square, trash: Trash2, upload: Upload, close: X, bindings: Network, + certificates: ShieldCheck, }; export type IconName = keyof typeof icons; export function AppIcon({ name, ...props }: LucideProps & { name: IconName }) { diff --git a/plugins/webui/src/features/bindings/page.tsx b/plugins/webui/src/features/bindings/page.tsx index 7775724..aff084a 100644 --- a/plugins/webui/src/features/bindings/page.tsx +++ b/plugins/webui/src/features/bindings/page.tsx @@ -15,7 +15,6 @@ import { useBindings, useProfiles, useProxies, - useActiveEnvironment, } from "@/hooks/api"; import { useI18n } from "@/i18n"; import { AppIcon } from "@/components/icon/AppIcon"; @@ -45,8 +44,7 @@ export default function Bindings() { const { t, text } = useI18n(), profiles = useProfiles(), proxies = useProxies(), - bindings = useBindings(), - { environments, environment } = useActiveEnvironment(); + bindings = useBindings(); const [active, setActive] = useState(null), [selected, setSelected] = useState([]), [search, setSearch] = useState(""); @@ -59,9 +57,7 @@ export default function Bindings() { (bindings.data ?? []) .filter((b) => b.profile_id === id && b.enabled) .map((b) => b.proxy_id); - const visible = (profiles.data ?? []).filter( - (p) => !environment || environment.profile_ids.includes(p.id!), - ); + const visible = profiles.data ?? []; const filtered = (proxies.data ?? []).filter((p) => `${p.name} ${p.proxy_type} ${p.local_ip} ${p.local_port}` .toLowerCase() @@ -87,14 +83,12 @@ export default function Bindings() { error={ profiles.error || proxies.error || - bindings.error || - environments.error + bindings.error } retry={() => { void profiles.refetch(); void proxies.refetch(); void bindings.refetch(); - void environments.refetch(); }} /> {profiles.isPending || proxies.isPending || bindings.isPending ? ( diff --git a/plugins/webui/src/features/certificates/ingest-settings.tsx b/plugins/webui/src/features/certificates/ingest-settings.tsx new file mode 100644 index 0000000..e102664 --- /dev/null +++ b/plugins/webui/src/features/certificates/ingest-settings.tsx @@ -0,0 +1,208 @@ +/** + * Certificate ingest endpoint settings. + * + * The endpoint ships enabled on loopback: it only accepts a token, returns no + * certificate material, and can only write a certificate for domains that token + * was issued for. So the default that "just works" for a local Certd is on, and + * the switch that widens it beyond this machine is explicit. + */ +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { toast } from "sonner"; +import { apiGet, apiPut } from "@/api/client"; +import type { ApiEnvelope } from "@/api/types"; +import { extractApiError } from "@/api/errors"; +import { data } from "@/hooks/api"; +import { useI18n } from "@/i18n"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Badge } from "@/components/ui/badge"; +import { Switch } from "@/components/ui/switch"; + +export interface IngestSettings { + enabled: boolean; + expose: boolean; + port: number; + listen: string; + /** Address actually bound right now, or null when not listening. */ + address: string | null; + /** Last bind failure, e.g. the port is taken. */ + error: string | null; + /** + * Ready-to-copy endpoint. The daemon derives it from the address this browser + * used, so it never shows loopback to someone on another host. + */ + url: string | null; + /** Where `url` came from: `public_url`, `request`, `bind` or `none`. */ + url_source: "public_url" | "request" | "bind" | "none"; + /** The push route on the control API, reachable wherever the UI is. */ + api_path: string; + /** Set when the environment pins the public URL. */ + public_url: string | null; +} + +export function getIngestSettings(): Promise> { + return apiGet("/cert-ingest"); +} + +export function updateIngestSettings(body: { + enabled?: boolean; + expose?: boolean; + port?: number; +}): Promise> { + return apiPut("/cert-ingest", body); +} + +export function IngestSettingsCard() { + const { text } = useI18n(); + const client = useQueryClient(); + + const settings = useQuery({ + queryKey: ["cert-ingest"], + queryFn: () => data(getIngestSettings()), + }); + + // Applies immediately: the daemon rebinds the listener in place, so there is + // no "restart the daemon to apply" step to get wrong. + const update = useMutation({ + mutationFn: (body: { enabled?: boolean; expose?: boolean; port?: number }) => + data(updateIngestSettings(body)), + onSuccess: (result) => { + if (result.error) toast.error(result.error); + else toast.success(text("接收端点已更新", "Ingest endpoint updated")); + void client.invalidateQueries({ queryKey: ["cert-ingest"] }); + }, + onError: (error) => toast.error(extractApiError(error).message), + }); + + const current = settings.data; + if (!current) return null; + + const bound = current.address; + // The daemon already resolved this against the request origin, so the UI must + // not second-guess it with `window.location`: doing so produced a loopback URL + // for an operator who was not on the host. + const url = current.url; + + return ( +
+
+
+

+ {text("接收端点(推送)", "Ingest endpoint (push)")} + + {bound ? text("监听中", "Listening") : text("已停止", "Stopped")} + +

+

+ {text( + "只有两条路由:推送证书与健康检查。没有 token 一律拒绝,且证书只能覆盖该令牌允许的域名。", + "Two routes only: push a certificate and a health check. Every request needs a token, and a certificate may only cover the domains that token allows.", + )} +

+
+
+ + {current.error && ( +

+ {current.error} +

+ )} + +
+
+
+ +

+ {text("默认开启,仅本机可访问。", "On by default; loopback only.")} +

+
+ update.mutate({ enabled })} + /> +
+
+
+ +

+ {text( + "把 7901 监听放到 0.0.0.0;经控制 API 推送不需要这个开关。", + "Binds 7901 on 0.0.0.0; pushing through the control API does not need it.", + )} +

+
+ update.mutate({ expose })} + /> +
+
+ +
+
+ + { + const port = Number(event.target.value); + if (port && port !== current.port) update.mutate({ port }); + }} + /> +
+ {!current.expose && ( +

+ {text( + "7901 端口只在本机监听;推送地址走的是控制 API,无需打开上面的开关。只有需要直接连 7901(Docker 内 Certd、独立端口映射)时才打开。", + "Port 7901 is loopback only; the push URL above goes through the control API, so the switch is not needed. Enable it only to reach 7901 directly (a Certd inside Docker, a published port).", + )} +

+ )} +
+ + {url && ( +
+ + {url} +

+ {current.url_source === "public_url" + ? text( + "来自环境变量 RUSTFRP_CERT_INGEST_PUBLIC_URL", + "From the RUSTFRP_CERT_INGEST_PUBLIC_URL environment variable", + ) + : current.url_source === "request" + ? text( + "按当前浏览器访问的地址推导;换一个地址打开本页会显示对应的地址。", + "Derived from the address this browser used; open the page from another host and it shows that host's address.", + ) + : text( + "按监听地址推导,未使用浏览器地址。", + "Derived from the bind address, not from the browser.", + )} +

+
+ )} + + +
+ ); +} diff --git a/plugins/webui/src/features/certificates/page.tsx b/plugins/webui/src/features/certificates/page.tsx new file mode 100644 index 0000000..4f54004 --- /dev/null +++ b/plugins/webui/src/features/certificates/page.tsx @@ -0,0 +1,998 @@ +import { useState } from "react"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { toast } from "sonner"; +import { + applyCertificate, + attachSource, + bindCertificate, + createSource, + deleteCertificate, + deleteSource, + importFromPath, + importPem, + listCertificates, + listSources, + promoteSource, + runSource, + unbindCertificate, + type Certificate, + type CertificateSource, + type CreatedSource, + type SourceKind, +} from "@/api/certificates"; +import { command, data, useAction, useProxies } from "@/hooks/api"; +import { useI18n } from "@/i18n"; +import { extractApiError } from "@/api/errors"; +import { AppIcon } from "@/components/icon/AppIcon"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { DataTable, type Column } from "@/components/common/data-table"; +import { + Choice, + EmptyState, + ErrorState, + PageHeading, + PageLoading, +} from "@/components/common/page"; +import { DeleteDialog, EditorDialog } from "@/components/common/dialogs"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu"; + +/** Sort order for the status column: problems first, healthy last. */ +const STATUS_ORDER: Record = { + expired: 0, + not_yet_valid: 1, + pending: 2, + expiring_soon: 3, + active: 4, +}; + +export default function Certificates() { + const { t, text } = useI18n(); + const [importing, setImporting] = useState(false); + const [deleting, setDeleting] = useState(null); + const [channelsFor, setChannelsFor] = useState(null); + + const certificates = useQuery({ + queryKey: ["certificates"], + queryFn: () => data(listCertificates()), + }); + // One request for every channel: the table needs to say how each certificate + // is kept up to date, and asking per row would be a request per certificate. + const sources = useQuery({ + queryKey: ["certificate-sources", "all"], + queryFn: () => data(listSources()), + }); + + const remove = useAction( + (id: number) => command(deleteCertificate(id)), + text("证书已删除", "Certificate deleted"), + ); + const apply = useAction( + (id: number) => data(applyCertificate(id)), + text("证书已写入配置文件", "Certificate written to the configured files"), + ); + // Bind and unbind share one dialog: the row menu offers whichever applies. + const [binding, setBinding] = useState(null); + const bindTarget = (row: Certificate) => setBinding(row); + + if (certificates.isLoading) return ; + if (certificates.error) + return ( + void certificates.refetch()} + /> + ); + + const rows = certificates.data ?? []; + // Unattached channels (an older shape, or one whose certificate was deleted) + // are surfaced in the empty state instead of being silently invisible. + const orphans = (sources.data ?? []).filter( + (source) => source.certificate_id === null, + ); + + const columns: Column[] = [ + { + key: "domain", + title: text("域名", "Domain"), + sort: (row) => row.primary_domain, + className: "max-w-40", + cell: (row) => ( +
+
{row.primary_domain}
+ {row.sans.length > 1 && ( +
+ {row.sans.filter((san) => san !== row.primary_domain).join(", ")} +
+ )} +
+ ), + }, + { + key: "status", + title: text("状态", "Status"), + sort: (row) => STATUS_ORDER[row.status], + cell: (row) => ( +
+ + {{ + active: text("可用", "Active"), + expiring_soon: text("即将到期", "Expiring soon"), + expired: text("已过期", "Expired"), + not_yet_valid: text("尚未生效", "Not yet valid"), + pending: text("待拉取", "Pending"), + }[row.status]} + + {row.status === "pending" ? ( + /* A pending certificate is not broken, it is waiting — and the + reason it is still waiting is a channel failure, so show that. */ +
+ {row.channel_error ?? text("等待首次推送或拉取", "Waiting for the first push or fetch")} +
+ ) : ( +
+ {row.status_reason ?? row.not_after.slice(0, 10)} +
+ )} +
+ ), + }, + { + key: "source", + title: text("来源", "Source"), + sort: (row) => row.source, + className: "max-w-44", + cell: (row) => + row.material_id === null ? ( + + {text("尚未导入", "No material yet")} + + ) : ( +
+ {{ + manual: text("手动粘贴", "Pasted"), + upload: text("上传文件", "Uploaded"), + local_path: text("本机路径", "Local path"), + webhook: text("推送", "Push"), + certd_pull: text("Certd 拉取", "Certd pull"), + url_pull: text("URL 拉取", "URL pull"), + }[row.source] ?? row.source} + {row.source_detail && ( +
+ {row.source_detail} +
+ )} +
+ ), + }, + { + key: "key", + title: text("密钥", "Key"), + sort: (row) => row.key_algo, + cell: (row) => + row.material_id === null ? ( + — + ) : ( +
+
{row.key_algo}
+
+ {row.fingerprint_sha256.slice(0, 16)} +
+
+ ), + }, + { + key: "channels", + title: text("更新方式", "Updates"), + className: "max-w-40", + cell: (row) => { + const mine = (sources.data ?? []).filter( + (source) => source.certificate_id === row.id, + ); + if (mine.length === 0) + return ( + + {text("仅手动", "Manual only")} + + ); + return ( +
+ {mine.map((source) => ( +
+ + {{ + push: text("推送", "Push"), + certd: text("Certd 拉取", "Certd pull"), + url: text("URL 拉取", "URL pull"), + }[source.kind]} + + {source.is_primary && ( + {text("优先", "Preferred")} + )} + {source.last_status === "failed" ? ( + + {text("失败", "Failed")} + + ) : source.enabled === false ? ( + {text("已停用", "Disabled")} + ) : null} +
+ ))} + {mine.some((source) => source.next_run_at) && ( +
+ {text("下次检查", "Next check")}{" "} + {mine + .map((source) => source.next_run_at) + .filter(Boolean) + .sort()[0] + ?.slice(0, 16)} +
+ )} +
+ ); + }, + }, + { + key: "bindings", + title: text("绑定代理", "Bound proxies"), + cell: (row) => + row.bindings.length === 0 ? ( + + {text("未绑定", "Not bound")} + + ) : ( +
+ {row.bindings.map((binding) => ( +
+ {binding.proxy_name || `#${binding.proxy_id}`} + {binding.last_error ? ( + {text("失败", "Failed")} + ) : row.fingerprint_sha256 && + binding.applied_fingerprint === row.fingerprint_sha256 ? ( + {text("已生效", "Applied")} + ) : ( + {text("待应用", "Pending")} + )} +
+ ))} +
+ ), + }, + { + key: "actions", + title: "", + // One button keeps the table from scrolling horizontally; the four + // actions do not fit as a row at this width. + className: "w-12", + cell: (row) => ( + + + + + + setChannelsFor(row)}> + {text("更新方式", "Channels")} + + apply.mutate(row.id)} + > + {text("应用到文件", "Apply to files")} + + bindTarget(row)}> + {row.bindings.length ? text("解绑", "Unbind") : text("绑定代理", "Bind proxy")} + + + setDeleting(row)}> + {t("common.delete")} + + + + ), + }, + ]; + + return ( +
+ + + + + {rows.length === 0 ? ( +
+ + {/* A channel whose first fetch failed used to leave this page looking + untouched, which reads as "my configuration was not saved". */} + {orphans.length > 0 && ( +
+
+

+ {text( + "已配置更新方式,但还没有拉到证书", + "A channel is configured but has not delivered a certificate", + )} +

+

+ {text( + "证书会在首次成功推送或拉取后出现在上面。失败原因如下。", + "The certificate appears above once a push or fetch succeeds. Here is why it has not yet.", + )} +

+
+ {orphans.map((source) => ( +
+
+ {source.name} + + {{ push: text("推送", "Push"), certd: "Certd", url: "URL" }[source.kind]} + + {source.domain} + {source.last_status === "failed" && ( + {text("失败", "Failed")} + )} +
+ {source.last_error && ( +

{source.last_error}

+ )} + {source.next_run_at && ( +

+ {text("下次检查", "Next check")} {source.next_run_at.slice(0, 16)} +

+ )} +
+ ))} +
+ )} +
+ ) : ( + row.id} /> + )} + + {binding && ( + setBinding(null)} + onDone={() => { + setBinding(null); + void certificates.refetch(); + }} + /> + )} + + { + setImporting(false); + void sources.refetch(); + }} + /> + + {channelsFor && ( + { + setChannelsFor(null); + void sources.refetch(); + }} + /> + )} + + setDeleting(null)} + onConfirm={() => { + if (deleting) remove.mutate(deleting.id, { onSuccess: () => setDeleting(null) }); + }} + /> +
+ ); +} + + + +/** Bind a certificate to a proxy, or unbind the one it already has. */ +function BindDialog({ + certificate, + onClose, + onDone, +}: { + certificate: Certificate; + onClose: () => void; + onDone: () => void; +}) { + const { text } = useI18n(); + const proxies = useProxies(); + const [proxyId, setProxyId] = useState(""); + const bound = certificate.bindings[0]; + + // Only proxies that can actually hold a certificate are offered. + const candidates = (proxies.data ?? []).filter( + (proxy) => + typeof proxy.plugin_config === "object" && + proxy.plugin_config !== null && + ["https2http", "https2https", "tls2raw"].includes( + String((proxy.plugin_config as Record).type ?? ""), + ), + ); + const bind = useAction( + (body: { cert_id: number; proxy_id: number }) => data(bindCertificate(body)), + text("已绑定", "Bound"), + onDone, + ); + const unbind = useAction( + (id: number) => command(unbindCertificate(id, false)), + text("已解绑,目标文件已保留", "Unbound; files kept on disk"), + onDone, + ); + + if (bound) { + return ( + +
+

+ {text("当前绑定到", "Currently bound to")}{" "} + {bound.proxy_name || `#${bound.proxy_id}`} +

+ +
+
+ ); + } + + return ( + +
+ {candidates.length === 0 ? ( +

+ {text( + "没有可用的代理。请先在代理规则中添加 https2http / https2https / tls2raw 插件与证书路径。", + "No eligible proxy. Add an https2http / https2https / tls2raw plugin with certificate paths first.", + )} +

+ ) : ( + ({ + value: String(proxy.id), + label: proxy.name, + }))} + onChange={setProxyId} + /> + )} + +
+
+ ); +} + +function ImportDialog({ open, onClose }: { open: boolean; onClose: () => void }) { + const { text } = useI18n(); + const [tab, setTab] = useState("pem"); + const [cert, setCert] = useState(""); + const [key, setKey] = useState(""); + const [crtPath, setCrtPath] = useState(""); + const [keyPath, setKeyPath] = useState(""); + const [note, setNote] = useState(""); + + // Channel-first import: the certificate is created before any material + // exists, and the channel that will supply it is configured in the same step. + const [channelKind, setChannelKind] = useState("certd"); + const [domain, setDomain] = useState(""); + const [baseUrl, setBaseUrl] = useState(""); + const [keyId, setKeyId] = useState(""); + const [keySecret, setKeySecret] = useState(""); + const [allowDomains, setAllowDomains] = useState(""); + const client = useQueryClient(); + + const reset = () => { + setCert(""); + setKey(""); + setCrtPath(""); + setKeyPath(""); + setNote(""); + setDomain(""); + setBaseUrl(""); + setKeyId(""); + setKeySecret(""); + setAllowDomains(""); + }; + const done = () => { + reset(); + onClose(); + }; + + const importPasted = useAction( + (body: { cert: string; key: string; note?: string }) => data(importPem(body)), + text("证书已导入", "Certificate imported"), + done, + ); + const importPath = useAction( + (body: { crt_path: string; key_path: string; note?: string }) => + data(importFromPath(body)), + text("证书已导入", "Certificate imported"), + done, + ); + + // A channel-only import needs no material, so it uses the channel endpoint + // directly and lets the first fetch fill the certificate in. + const createChannel = useMutation({ + mutationFn: (body: Parameters[0]) => + data(createSource(body)), + onSuccess: async (result) => { + if (result.source.kind === "push" && result.plaintext) { + setCreatedToken(result); + } else { + toast.success(text("更新方式已配置", "Channel configured")); + done(); + } + await client.invalidateQueries(); + }, + onError: (error) => toast.error(extractApiError(error).message), + }); + const [createdToken, setCreatedToken] = useState(null); + + // Push is configured against a domain allowlist; certd/url against one domain. + const channelReady = + channelKind === "push" + ? allowDomains.trim().length > 0 + : domain.trim().length > 0 && baseUrl.trim().length > 0; + + return ( + + + + {text("粘贴 PEM", "Paste PEM")} + {text("本机路径", "Local path")} + {text("推送 / 自动拉取", "Push / pull")} + + +
+ +