mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Admin panel, login throttling, and Cloudflare edge hardening (#150)
* Document the remote shell for production user management * add sitemap to footer links * remove border under theme selection * Add admin theme and user management (#144) Add /admin/themes CRUD and /admin/users list/detail with role and plan editing, guarded by a shared Policy module. Include last-super-admin and self-role protections plus structured id-only admin logs. Also add admin invitations, confirmation resends and password-reset emails, which work in development through the Swoosh mailbox before SES is configured. * Rate-limit password logins and emit auth events (#142) Add a two-bucket login throttle (5 failures per login, 10 per IP in a 15-minute window) and a shared client-address helper for controllers and LiveViews. Blocked attempts skip the database and bcrypt. Log auth.login.failed and auth.login.throttled warnings with boot-scoped HMAC addresses only, and stop repeated confirmation emails on unconfirmed login attempts. * Harden the production login edge for Cloudflare Document the layered login defence: the app throttle is primary, Caddy must pass Cloudflare's CF-Connecting-IP to the app, and Cloudflare edge rate limiting covers request floods. Update the reference Caddyfile to accept only Cloudflare ranges and reject direct-to-origin traffic, and document the exact Cloudflare dashboard rules. * Document 10-second Cloudflare rate-limit window Cloudflare may only offer a 10-second period. Keep the edge rule at 10 login POSTs per 10 seconds per IP and rely on the app's 15-minute login/address throttle for credential guessing. * Document Cloudflare limits for LiveView registration Registration submits over the shared LiveView socket, so a Cloudflare rule on POST /users/register will not count account creations. Note the app-level throttle/Turnstile options instead. * Document current Cloudflare free-plan edge controls Record the production setup: Cloudflare-only Caddy origins, Bot Fight Mode, and the 10-second login flood rule (10 POSTs per 10 seconds, 10-second block). Clarify that the application's 15-minute throttle remains the real credential lockout. * Auto-refresh Cloudflare CIDRs for Caddy Move the origin allowlist into a generated cloudflare-ranges.caddy imported by Caddy. Add an hourly systemd timer and script that fetch Cloudflare's v4/v6 lists, reject malformed responses, validate the candidate Caddyfile, and reload Caddy only when the ranges change. Restore the previous file on validation or reload failure. * Document Cloudflare CIDR refresher setup and operations Record the installed paths, permissions, timer checks, manual run, update procedure, and the direct-on-VPS setup fallback when the deploy directory is absent.
This commit is contained in:
+171
-8
@@ -4,9 +4,10 @@ Phoenix release on the shared Ubuntu VPS, behind Caddy, with Postgres in Docker.
|
||||
|
||||
```text
|
||||
Internet
|
||||
└─ Caddy :443
|
||||
└─ Phoenix release 127.0.0.1:4100
|
||||
└─ Postgres 127.0.0.1:5432
|
||||
└─ Cloudflare (TLS, WAF, edge rate limiting)
|
||||
└─ Caddy :443 (cloudflare ranges only)
|
||||
└─ Phoenix release 127.0.0.1:4100
|
||||
└─ Postgres 127.0.0.1:5432
|
||||
```
|
||||
|
||||
## Pinned versions
|
||||
@@ -442,13 +443,30 @@ is in place.
|
||||
|
||||
## Caddy
|
||||
|
||||
On the current VPS the Caddyfile is a single file with no imports. Add the block
|
||||
from `deploy/Caddyfile` by hand:
|
||||
Add the site block from `deploy/Caddyfile` by hand. It imports
|
||||
`cloudflare-ranges.caddy`, which is generated by
|
||||
`deploy/update-cloudflare-ranges.sh` and refreshed by a systemd timer.
|
||||
|
||||
Caddy must only accept Cloudflare traffic and must replace `X-Forwarded-For`
|
||||
with Cloudflare's `CF-Connecting-IP` value:
|
||||
|
||||
```caddy
|
||||
innersquare.app {
|
||||
encode zstd gzip
|
||||
reverse_proxy 127.0.0.1:4100
|
||||
|
||||
@cloudflare {
|
||||
import cloudflare-ranges.caddy
|
||||
}
|
||||
|
||||
handle @cloudflare {
|
||||
reverse_proxy 127.0.0.1:4100 {
|
||||
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
|
||||
}
|
||||
}
|
||||
|
||||
handle {
|
||||
respond "Access Denied" 403
|
||||
}
|
||||
}
|
||||
|
||||
www.innersquare.app {
|
||||
@@ -456,7 +474,72 @@ www.innersquare.app {
|
||||
}
|
||||
```
|
||||
|
||||
If a new VPS uses imports, put the block in its own site file instead.
|
||||
Install the range file and the hourly refresh timer:
|
||||
|
||||
```sh
|
||||
sudo install -m 0755 /opt/chesstrainer/src/deploy/update-cloudflare-ranges.sh \
|
||||
/usr/local/sbin/chesstrainer-update-cloudflare-ranges
|
||||
sudo install -m 0644 /opt/chesstrainer/src/deploy/cloudflare-ranges.caddy \
|
||||
/etc/caddy/cloudflare-ranges.caddy
|
||||
sudo install -m 0644 /opt/chesstrainer/src/deploy/chesstrainer-cloudflare-ranges.service \
|
||||
/etc/systemd/system/chesstrainer-cloudflare-ranges.service
|
||||
sudo install -m 0644 /opt/chesstrainer/src/deploy/chesstrainer-cloudflare-ranges.timer \
|
||||
/etc/systemd/system/chesstrainer-cloudflare-ranges.timer
|
||||
```
|
||||
|
||||
The installed pieces are:
|
||||
|
||||
| Path | Mode | Purpose |
|
||||
|---|---|---|
|
||||
| `/usr/local/sbin/chesstrainer-update-cloudflare-ranges` | `0755` | fetch/validate/reload script |
|
||||
| `/etc/caddy/cloudflare-ranges.caddy` | `0644` | generated `remote_ip` list |
|
||||
| `/etc/systemd/system/chesstrainer-cloudflare-ranges.service` | `0644` | oneshot service |
|
||||
| `/etc/systemd/system/chesstrainer-cloudflare-ranges.timer` | `0644` | hourly timer |
|
||||
|
||||
If `/opt/chesstrainer/src/deploy` is not present on the VPS, create the same
|
||||
four files directly with `sudo tee` from the canonical contents in `deploy/`.
|
||||
The timer only depends on the installed copies; it does not read the repo.
|
||||
|
||||
Run it once manually and enable the timer:
|
||||
|
||||
```sh
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now chesstrainer-cloudflare-ranges.timer
|
||||
sudo systemctl start chesstrainer-cloudflare-ranges.service
|
||||
sudo journalctl -u chesstrainer-cloudflare-ranges.service -n 50 --no-pager
|
||||
```
|
||||
|
||||
The script fetches <https://www.cloudflare.com/ips-v4> and
|
||||
<https://www.cloudflare.com/ips-v6>, validates the result, and reloads Caddy
|
||||
only when the ranges actually changed. If the download, Caddy validation or
|
||||
reload fails, it keeps or restores the previous file.
|
||||
|
||||
Check the timer and last run:
|
||||
|
||||
```sh
|
||||
systemctl list-timers chesstrainer-cloudflare-ranges.timer
|
||||
systemctl status chesstrainer-cloudflare-ranges.service
|
||||
journalctl -u chesstrainer-cloudflare-ranges.service --since "24 hours ago"
|
||||
```
|
||||
|
||||
Do not edit `/etc/caddy/cloudflare-ranges.caddy` by hand; the next timer run
|
||||
overwrites it.
|
||||
|
||||
To update the refresher itself, replace the installed script or unit files from
|
||||
the deploy directory, then:
|
||||
|
||||
```sh
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl restart chesstrainer-cloudflare-ranges.timer
|
||||
sudo systemctl start chesstrainer-cloudflare-ranges.service
|
||||
```
|
||||
|
||||
Without this, `TRUST_FORWARDED_FOR=true` records a Cloudflare edge address
|
||||
instead of the visitor, and the login throttle and guest puzzle limit bucket
|
||||
whole Cloudflare POPs together.
|
||||
|
||||
If a new VPS uses imports, put the site block in its own file instead; keep the
|
||||
`import cloudflare-ranges.caddy` path next to the Caddyfile.
|
||||
|
||||
Do **not** switch Caddy until a release is running and verified on
|
||||
`127.0.0.1:4100`. Before that, keep the static coming-soon site live.
|
||||
@@ -468,8 +551,16 @@ sudo caddy validate --config /etc/caddy/Caddyfile
|
||||
sudo systemctl reload caddy
|
||||
```
|
||||
|
||||
A direct request to the origin with a forged `CF-Connecting-IP` header should
|
||||
receive `403 Access Denied`.
|
||||
|
||||
Do not let a deploy overwrite the shared Caddyfile.
|
||||
|
||||
See `docs/security.md` for the application throttle, real-IP setup and the
|
||||
Cloudflare edge controls. Production currently has Bot Fight Mode enabled and
|
||||
the free-plan login flood rule: 10 POSTs per 10 seconds per IP, blocked for 10
|
||||
seconds.
|
||||
|
||||
## Building a release
|
||||
|
||||
Build on a machine matching Ubuntu 24.04 / glibc 2.39, or let CI do it.
|
||||
@@ -694,6 +785,75 @@ Set `Storage=persistent` and a `SystemMaxUse=` value in
|
||||
rate-limits bursts; if a crash storm drops lines, raise `RateLimitBurst` or
|
||||
`RateLimitIntervalSec` there.
|
||||
|
||||
## Interactive shell and user management
|
||||
|
||||
Attach an IEx shell to the **running** release. This does not restart the
|
||||
service and does not require direct database access:
|
||||
|
||||
```sh
|
||||
sudo /opt/chesstrainer/current/bin/chesstrainer remote
|
||||
```
|
||||
|
||||
You should get a prompt like `iex(chesstrainer@<host>)1>`. Exit with **Ctrl-C
|
||||
twice**. Never call `System.halt/0`, `:init.stop/0`, or
|
||||
`Application.stop(:chesstrainer)` from this shell — those stop production.
|
||||
|
||||
Use it for account and role management. Go through the application contexts
|
||||
(not SQL) so changesets, bcrypt hashing, timestamps, and token bookkeeping stay
|
||||
consistent:
|
||||
|
||||
```elixir
|
||||
# Look up by email or username
|
||||
u = Chesstrainer.Accounts.get_user_by_login("owen")
|
||||
|
||||
# Promote or demote
|
||||
{:ok, u} = Chesstrainer.Accounts.update_user_role(u, :super_admin)
|
||||
|
||||
# Confirm an account without sending email
|
||||
{:ok, u} = Chesstrainer.Repo.update(Chesstrainer.Accounts.User.confirm_changeset(u))
|
||||
|
||||
# Create an account, generating its password server-side
|
||||
pw = :crypto.strong_rand_bytes(18) |> Base.url_encode64(padding: false)
|
||||
|
||||
{:ok, u} =
|
||||
Chesstrainer.Accounts.register_user(%{
|
||||
email: "you@example.com",
|
||||
username: "owen",
|
||||
password: pw
|
||||
})
|
||||
|
||||
{:ok, u} = Chesstrainer.Repo.update(Chesstrainer.Accounts.User.confirm_changeset(u))
|
||||
pw
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- `register_user/1` hashes the password with bcrypt; `password` is virtual and
|
||||
is never written to the database.
|
||||
- Do not type passwords into the shell if you can avoid it — IEx may keep a
|
||||
history file — and never paste a password from shell history. Generate one as
|
||||
above and change it later at `/users/profile/settings`.
|
||||
- `mix accounts.promote` still works when a compiled source checkout is present
|
||||
on the host, but the release shell is the supported production path.
|
||||
- There is no guard against demoting the last `super_admin` outside the future
|
||||
admin UI, so check the result before demoting.
|
||||
|
||||
If `remote` fails while booting its client VM (for example
|
||||
`Kernel pid terminated (logger)` with `code_server`/`persistent_term` in the
|
||||
trace), the deployed release is stale, mixed, or missing IEx: rebuild and
|
||||
redeploy it. To attach in the meantime, use the release's own ERTS:
|
||||
|
||||
```sh
|
||||
sudo bash -c '
|
||||
COOKIE=$(cat /opt/chesstrainer/current/releases/COOKIE)
|
||||
exec /opt/chesstrainer/current/erts-*/bin/erl \
|
||||
-sname debug -setcookie "$COOKIE" -remsh chesstrainer
|
||||
'
|
||||
```
|
||||
|
||||
That shell speaks Erlang syntax, so quote Elixir module atoms, e.g.
|
||||
`'Elixir.Chesstrainer.Accounts':register_user(...)`.
|
||||
|
||||
## Backups
|
||||
|
||||
`docker exec` connects over the container's Unix socket, which the Postgres
|
||||
@@ -753,6 +913,9 @@ the VPS regularly. A backup that is only on the VPS is not a backup.
|
||||
until a release exists.
|
||||
6. Release built on a matching Ubuntu runner and activated.
|
||||
7. `/health` and `/` verified on `127.0.0.1:4100`.
|
||||
8. Caddy block added, validated, reloaded.
|
||||
8. Caddy block, `cloudflare-ranges.caddy`, and the CIDR refresh timer added;
|
||||
validated and reloaded.
|
||||
9. Public `/`, `/privacy`, `/terms-conditions`, and `/health` verified.
|
||||
10. Read-only role created, tunnel verified, backups scheduled and tested.
|
||||
11. Remote shell attaches and exits cleanly:
|
||||
`sudo /opt/chesstrainer/current/bin/chesstrainer remote` (Ctrl-C twice).
|
||||
|
||||
Reference in New Issue
Block a user