Files
patrickandClaude Sonnet 5.5 4b5dcae2ad
CI / tests (Python 3.10) (pull_request) Successful in 25s
CI / tests (Python 3.11) (pull_request) Successful in 25s
CI / tests (Python 3.12) (pull_request) Successful in 24s
CI / tests (Python 3.13) (pull_request) Successful in 24s
CI / lint (pull_request) Successful in 3s
Add a one-line summary to each docs page and tidy the development page
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 12:10:45 -04:00

191 lines
7.3 KiB
Markdown

# Reverse proxy and access control
[← kernelwatch](../README.md)
How to put TLS, and optionally extra access control, in front of the server.
## TLS reverse proxy
The server speaks plain HTTP on `127.0.0.1:8080`. Put a TLS proxy in front of it.
The agents and your browser both connect through it.
**Caddy** (simplest):
```caddyfile
kernelwatch.example.internal {
reverse_proxy 127.0.0.1:8080
}
```
**nginx**:
```nginx
server {
listen 443 ssl;
server_name kernelwatch.example.internal;
# ssl_certificate / ssl_certificate_key ...
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
With either proxy, set `trust_proxy_headers = true` in `server.toml` so login
rate limiting sees real client IPs.
If you use a private CA (e.g. Caddy's `tls internal`), give the agents its
root cert with `--ca` (see [Agents](agents.md#adding-a-host)).
You can expose only `/api/v1/report` to the fleet network and keep the
dashboard on a VPN or tailnet. That's a proxy rule, not a kernelwatch setting.
## CloudPanel
CloudPanel gives each site its own Linux user and a document root at
`/home/<site-user>/htdocs/<domain>/`. kernelwatch doesn't use that directory.
nginx forwards requests to the app; it doesn't serve kernelwatch's files. Install
with `server/install.sh` as in [Server](server.md), so the app runs as the `kernelwatch` user
with its data in `/var/lib/kernelwatch`, outside every site user's home. Leave
the document root empty, and keep your clone of this repo out of `htdocs`.
1. **Pick a free port.** CloudPanel's Varnish setup can run an nginx backend
on 8080. Check with `ss -ltnp | grep ':8080\b'`. If it's taken, set another port
in `/etc/kernelwatch/server.toml` and restart (`sudo systemctl restart kernelwatch`):
```toml
listen_port = 8095
trust_proxy_headers = true
```
Check it: `curl -s http://127.0.0.1:8095/healthz` should print `ok`.
2. **Create the site as a Reverse Proxy.** In CloudPanel: *Add Site → Create a
Reverse Proxy*, with your domain and Reverse Proxy URL
`http://127.0.0.1:8095`. CloudPanel can't change an existing site's type. If
the domain already exists as a PHP or static site, delete and recreate it.
Deleting a site also removes its site user and home directory, so move
anything you need out first.
3. **TLS.** In the site's *SSL/TLS* tab, issue a Let's Encrypt certificate. Agents
then need no `--ca`.
4. **Check the generated vhost** (the site's *Vhost* tab). The `location`
that proxies to the app should pass the original `Host` header and the
client address:
```nginx
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```
Add them if they're missing. Without `Host`, logins fail the origin check
with "cross-origin request refused". Without `X-Forwarded-For`, every login
appears to come from 127.0.0.1, so one person's failed attempts lock everyone out.
Also check that the login page is styled. If `/static/style.css` returns 404,
a static-file `location` in the template is looking in the empty document root
instead of proxying. Make it fall through to the proxy, or remove it.
5. **Optionally restrict the dashboard.** The site's domain is public, so the
login page is reachable from the internet unless you limit it. Agents only
need `/api/v1/report`. In the Vhost tab, add an `allow <your IP>; deny all;`
to `location /` and a separate unrestricted
`location = /api/v1/report { proxy_pass http://127.0.0.1:8095; }`.
Changes made in the Vhost tab persist. Back them up before
recreating the site.
To require a login through Authelia instead of an IP allowlist, see
[Authelia (forward auth)](#authelia-forward-auth).
## Authelia (forward auth)
If you already run [Authelia](https://www.authelia.com/), you can put it in
front of the dashboard. That adds Authelia's 2FA and single sign-on, and
kernelwatch's own login still applies behind it. You log in to both, but
Authelia's remember-me makes that rare. This uses nginx's `auth_request`, so
you don't need an OIDC client.
**Agents must bypass Authelia.** They authenticate with their bearer token and
can't follow a login redirect. If Authelia covers `/api/v1/report`, every
report gets a redirect instead of a 401 or 204, and the whole fleet goes stale
within 3 hours.
1. **Authelia `access_control`.** Rules are checked in order, so put the bypass
first, and keep a single `rules:` list:
```yaml
access_control:
default_policy: two_factor
rules:
- domain: kernelwatch.example.com
resources:
- '^/api/v1/report$'
- '^/healthz$'
policy: bypass
- domain: "*.example.com"
policy: two_factor
```
For the redirect to the login page to work, your `session.cookies` entry has
to cover the dashboard's domain and set `authelia_url`. Without them, browsers
get a bare 401 page. Authelia reads this file only at startup, so restart it
after editing (in Kubernetes: `kubectl rollout restart deployment/authelia`).
Check the file first with `authelia validate-config`.
2. **nginx.** Add an internal location for Authelia's check, an exact-match
location for the agents with no check, and the check on `location /`.
For CloudPanel, edit the site's *Vhost* tab: leave the template's
`@reverse_proxy` block alone and add or replace these:
```nginx
# Authelia's check (internal subrequest only)
location = /internal/authelia/authz {
internal;
proxy_pass https://auth.example.com/api/authz/auth-request;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_set_header Host auth.example.com;
proxy_set_header X-Original-Method $request_method;
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Content-Length "";
proxy_set_header Connection "";
proxy_pass_request_body off;
}
# Agents: token auth only
location = /api/v1/report {
try_files $uri @reverse_proxy;
}
# Dashboard: Authelia first, then kernelwatch's login
location / {
auth_request /internal/authelia/authz;
auth_request_set $redirection_url $upstream_http_location;
error_page 401 =302 $redirection_url;
try_files $uri @reverse_proxy;
}
```
Without CloudPanel, use `proxy_pass http://127.0.0.1:8095;` plus the `Host`
and `X-Forwarded-For` headers in place of `try_files ... @reverse_proxy`. If
Authelia runs on the same machine, point the check straight at it (e.g.
`http://127.0.0.1:9091/api/authz/auth-request`) and drop the
`proxy_ssl_server_name` and `Host` lines. Keep `location ^~ /.well-known`
unprotected so certificate renewals still work. If something monitors
`/healthz`, give it an exact-match location like the agents' one.
3. **Check it.**
```sh
curl -sI https://kernelwatch.example.com/ | head -3
# 302, Location pointing at Authelia
curl -si -X POST https://kernelwatch.example.com/api/v1/report | head -1
# 401 from kernelwatch (no token), not a 302
```
Then run `sudo systemctl start kernelwatch-agent` on one host and check
that its *Last seen* updates. If the POST gets a 302, the agents' location
isn't matching. Fix that before the fleet goes stale.