Every host runs node_exporter, cAdvisor (now against Podman), Alloy and the security-metrics script, on its private address. Each service declares its own expected ports, users and privileged containers next to its definition, so the security baseline follows the config instead of a separate inventory list. On ops: Prometheus, Loki, Grafana, the blackbox prober and the Postgres exporter, with the same scrape jobs, probes, retention, dashboards and 33 alert rules. Two rules matched container-era names and now match the native ones: Forgejo's log is unit forgejo.service, Grafana's route is grafana@file. Grafana keeps its default secret_key on production, because its database was written with it; rotating it is a separate step. Prometheus, Grafana and Alloy come from nixos-unstable: their data is from newer versions than 26.05 ships. LLMNR and mDNS are off: the baseline flagged them as public listeners. Tried on the rehearsal pair: all 28 scrape targets up, logs from both hosts in Loki, Grafana WireGuard-only, no unexpected port, user or container on either host. Assisted-by: Claude:opus-5-5
251 lines
7.7 KiB
Nix
251 lines
7.7 KiB
Nix
# Traefik: the only thing listening on 80 and 443. Certificates from
|
|
# Let's Encrypt over the HTTP challenge, kept in /var/lib/traefik/acme.json.
|
|
#
|
|
# Routes come from two places: for the native services, from here
|
|
# (`asxpio.routes` below and in the services' own files); for the app
|
|
# containers, from their labels, read through Podman's socket exactly as
|
|
# they were read from Docker's.
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
internal = [
|
|
"10.10.0.0/20"
|
|
"10.20.0.0/24"
|
|
];
|
|
in
|
|
{
|
|
options.asxpio.letsencryptCA = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "https://acme-v02.api.letsencrypt.org/directory";
|
|
};
|
|
|
|
# Shorthand for the common case: these hosts, over TLS, to that URL
|
|
options.asxpio.routes = lib.mkOption {
|
|
default = { };
|
|
type = lib.types.attrsOf (
|
|
lib.types.submodule {
|
|
options = {
|
|
hosts = lib.mkOption { type = lib.types.listOf lib.types.str; };
|
|
url = lib.mkOption { type = lib.types.str; };
|
|
middlewares = lib.mkOption {
|
|
type = lib.types.listOf lib.types.str;
|
|
default = [ ];
|
|
};
|
|
};
|
|
}
|
|
);
|
|
};
|
|
|
|
config = {
|
|
sops.secrets."traefik/letsencrypt_email" = { };
|
|
sops.templates."traefik.env".content = ''
|
|
TRAEFIK_CERTIFICATESRESOLVERS_LETSENCRYPT_ACME_EMAIL=${config.sops.placeholder."traefik/letsencrypt_email"}
|
|
'';
|
|
|
|
services.traefik = {
|
|
enable = true;
|
|
package = pkgs.traefik;
|
|
environmentFiles = [ config.sops.templates."traefik.env".path ];
|
|
|
|
staticConfigOptions = {
|
|
api = {
|
|
dashboard = true;
|
|
insecure = true; # on the "traefik" entry point below: WireGuard only
|
|
};
|
|
entryPoints = {
|
|
web = {
|
|
address = ":80";
|
|
http.redirections.entryPoint = {
|
|
to = "websecure";
|
|
scheme = "https";
|
|
};
|
|
};
|
|
websecure.address = ":443";
|
|
traefik.address = "10.20.0.10:8080";
|
|
metrics.address = "127.0.0.1:8082";
|
|
};
|
|
metrics.prometheus = {
|
|
entryPoint = "metrics";
|
|
addRoutersLabels = true;
|
|
addServicesLabels = true;
|
|
};
|
|
providers.docker = {
|
|
endpoint = "unix:///run/podman/podman.sock";
|
|
exposedByDefault = false;
|
|
network = "traefik";
|
|
};
|
|
certificatesResolvers.letsencrypt.acme = {
|
|
caServer = config.asxpio.letsencryptCA;
|
|
storage = "${config.services.traefik.dataDir}/acme.json";
|
|
httpChallenge.entryPoint = "web";
|
|
};
|
|
log = {
|
|
level = "INFO";
|
|
filePath = "/var/log/traefik/traefik.log";
|
|
};
|
|
accessLog = {
|
|
filePath = "/var/log/traefik/access.log";
|
|
bufferingSize = 100;
|
|
fields.headers = {
|
|
defaultMode = "drop";
|
|
names = {
|
|
User-Agent = "keep";
|
|
Referer = "keep";
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
dynamicConfigOptions.http = {
|
|
middlewares = {
|
|
secure-headers.headers = {
|
|
sslRedirect = true;
|
|
stsSeconds = 31536000;
|
|
stsIncludeSubdomains = true;
|
|
stsPreload = true;
|
|
forceSTSHeader = true;
|
|
frameDeny = true;
|
|
contentTypeNosniff = true;
|
|
browserXssFilter = true;
|
|
};
|
|
rate-limit.rateLimit = {
|
|
average = 100;
|
|
burst = 50;
|
|
period = "1s";
|
|
};
|
|
internal-only.ipAllowList.sourceRange = internal;
|
|
wireguard-only.ipAllowList.sourceRange = [ "10.20.0.0/24" ];
|
|
|
|
# Anonymous reads of Garage's public bucket (see garage-public)
|
|
garage-public-strip.stripPrefix.prefixes = [ "/public" ];
|
|
garage-public-host.headers.customRequestHeaders.Host = "public.web.garage";
|
|
};
|
|
|
|
routers = {
|
|
traefik-dashboard = {
|
|
rule = "Host(`traefik.asxp.io`)";
|
|
entryPoints = [ "websecure" ];
|
|
service = "api@internal";
|
|
middlewares = [ "internal-only" ];
|
|
tls.certResolver = "letsencrypt";
|
|
};
|
|
|
|
# Exists only to obtain the XMPP certificate (ejabberd on
|
|
# asxpio-services); xmpp-cert-sync pushes it there on every
|
|
# renewal. No traffic reaches it: these names point at
|
|
# services. asxp.io is pushed from the site's own certificate.
|
|
xmpp-cert = {
|
|
rule = "Host(`xmpp.asxp.io`)";
|
|
entryPoints = [ "websecure" ];
|
|
priority = 1;
|
|
service = "noop@internal";
|
|
tls = {
|
|
certResolver = "letsencrypt";
|
|
domains = [
|
|
{
|
|
main = "xmpp.asxp.io";
|
|
sans = [ "conference.asxp.io" ];
|
|
}
|
|
];
|
|
};
|
|
};
|
|
|
|
# Unsigned GET/HEAD of s3.asxp.io/public/<key> goes to Garage's
|
|
# web endpoint as Host public.web.garage, path /<key>. Signed
|
|
# requests still reach the S3 API (route garage-s3).
|
|
garage-public = {
|
|
rule = "Host(`s3.asxp.io`) && PathPrefix(`/public/`) && (Method(`GET`) || Method(`HEAD`)) && !HeaderRegexp(`Authorization`, `.+`) && !QueryRegexp(`X-Amz-Signature`, `.+`)";
|
|
priority = 1000;
|
|
entryPoints = [ "websecure" ];
|
|
service = "garage-web";
|
|
middlewares = [
|
|
"garage-public-strip"
|
|
"garage-public-host"
|
|
];
|
|
tls.certResolver = "letsencrypt";
|
|
};
|
|
}
|
|
// lib.mapAttrs (name: r: {
|
|
rule = lib.concatMapStringsSep " || " (h: "Host(`${h}`)") r.hosts;
|
|
entryPoints = [ "websecure" ];
|
|
service = name;
|
|
inherit (r) middlewares;
|
|
tls.certResolver = "letsencrypt";
|
|
}) config.asxpio.routes;
|
|
|
|
services = {
|
|
garage-web.loadBalancer.servers = [ { url = "http://127.0.0.1:3902"; } ];
|
|
}
|
|
// lib.mapAttrs (_: r: { loadBalancer.servers = [ { inherit (r) url; } ]; }) config.asxpio.routes;
|
|
};
|
|
};
|
|
|
|
asxpio.routes = {
|
|
forgejo = {
|
|
hosts = [
|
|
"git.asxp.io"
|
|
"git.czsk.it"
|
|
];
|
|
url = "http://127.0.0.1:3000";
|
|
};
|
|
garage-s3 = {
|
|
hosts = [ "s3.asxp.io" ];
|
|
url = "http://127.0.0.1:3900";
|
|
};
|
|
};
|
|
|
|
users.users.traefik.extraGroups = [ "podman" ];
|
|
asxpio.security.podmanUsers = [ "traefik" ];
|
|
asxpio.security.ports = [
|
|
"tcp/80/traefik"
|
|
"tcp/443/traefik"
|
|
"tcp/8080/traefik" # dashboard, WireGuard address
|
|
];
|
|
systemd.services.traefik = {
|
|
after = [
|
|
"podman.socket"
|
|
"wireguard-wg0.service"
|
|
];
|
|
wants = [
|
|
"podman.socket"
|
|
"wireguard-wg0.service"
|
|
];
|
|
serviceConfig.LogsDirectory = "traefik";
|
|
};
|
|
services.logrotate.settings.traefik = {
|
|
files = "/var/log/traefik/*.log";
|
|
frequency = "weekly";
|
|
rotate = 8;
|
|
compress = true;
|
|
delaycompress = true;
|
|
# Traefik reopens its log files on USR1
|
|
postrotate = "${config.systemd.package}/bin/systemctl kill -s USR1 traefik.service";
|
|
};
|
|
|
|
# App containers that Traefik routes to join this network
|
|
systemd.services.podman-network-traefik = {
|
|
description = "Podman network traefik";
|
|
wantedBy = [ "multi-user.target" ];
|
|
before = [ "traefik.service" ];
|
|
path = [ config.virtualisation.podman.package ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
};
|
|
script = "podman network exists traefik || podman network create traefik";
|
|
};
|
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
80
|
|
443
|
|
];
|
|
|
|
# acme.json holds the account key and every certificate
|
|
asxpio.waitForData = [ "traefik" ];
|
|
asxpio.backup.paths = [ "/var/lib/traefik" ];
|
|
};
|
|
}
|