F: F1 restart on what SIGHUP cannot apply and settle the peers precedence
The config file now always stays a reload trigger, and the restart trigger is the settings reduced to the keys the service cannot apply on SIGHUP, so a peer edit reloads while a port, auth or limit change still restarts. The first-class peers options win over the freeform bags, and the assertions cover enabled peers, the 64-peer cap and LoadCredential-safe token paths.
This commit is contained in:
1 parent
95e3533e83
commit
7128321692
1 file changed
+129
-49
+129
-49
@@ -18,27 +18,6 @@ let
|
|||||||
++ lib.filter (h: !lib.hasInfix "://" h) cfg.allowedHosts
|
++ lib.filter (h: !lib.hasInfix "://" h) cfg.allowedHosts
|
||||||
));
|
));
|
||||||
|
|
||||||
moduleDefaults = {
|
|
||||||
server = {
|
|
||||||
bind = cfg.bind;
|
|
||||||
port = cfg.port;
|
|
||||||
allowed_hosts = hosts;
|
|
||||||
allowed_origins = map (h: "${scheme}://${h}") (lib.filter (h: !lib.hasInfix "://" h) hosts)
|
|
||||||
++ lib.filter (h: lib.hasInfix "://" h) cfg.allowedHosts;
|
|
||||||
} // lib.optionalAttrs cfg.tls.enable {
|
|
||||||
tls = {
|
|
||||||
enabled = true;
|
|
||||||
cert_file = "${credDir}/cert";
|
|
||||||
key_file = "${credDir}/key";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
auth.db_path = "/var/lib/${name}/db.sqlite";
|
|
||||||
} // lib.optionalAttrs cfg.docker.enable {
|
|
||||||
sources.docker_socket = "/var/run/docker.sock";
|
|
||||||
} // lib.optionalAttrs (cfg.peers.enable || cfg.peers.settings != { } || cfg.peers.peers != { }) {
|
|
||||||
peers = peersSection;
|
|
||||||
};
|
|
||||||
|
|
||||||
# Declared peers that stay in the rendered config. A peer with `enable =
|
# Declared peers that stay in the rendered config. A peer with `enable =
|
||||||
# false` is left out entirely: no credential is shipped for it, and the
|
# false` is left out entirely: no credential is shipped for it, and the
|
||||||
# service refuses to start while an enabled peer's token file is missing.
|
# service refuses to start while an enabled peer's token file is missing.
|
||||||
@@ -60,14 +39,73 @@ let
|
|||||||
}
|
}
|
||||||
) enabledPeerNames;
|
) enabledPeerNames;
|
||||||
|
|
||||||
|
# The first-class options are in use once either one is set. From then on
|
||||||
|
# they own `enabled` and `peer`: the freeform bags (`peers.settings` and the
|
||||||
|
# legacy `settings.peers`) may not set either, and the assertions below
|
||||||
|
# reject a config that tries.
|
||||||
|
usingFirstClassPeers = cfg.peers.enable || cfg.peers.peers != { };
|
||||||
|
freeformPeers = lib.recursiveUpdate cfg.peers.settings (cfg.settings.peers or { });
|
||||||
# Only rendered when the feature is in use, so a peer-less deployment keeps
|
# Only rendered when the feature is in use, so a peer-less deployment keeps
|
||||||
# the same generated config as before.
|
# the same generated config as before. Freeform keys first, the first-class
|
||||||
|
# values last, so a stale `enabled = false` in a freeform bag cannot switch
|
||||||
|
# off the feature the module believes is on.
|
||||||
peersSection =
|
peersSection =
|
||||||
{ enabled = cfg.peers.enable; }
|
freeformPeers
|
||||||
// cfg.peers.settings
|
// lib.optionalAttrs (renderedPeers != [ ]) { peer = renderedPeers; }
|
||||||
// lib.optionalAttrs (renderedPeers != [ ]) { peer = renderedPeers; };
|
// lib.optionalAttrs usingFirstClassPeers { enabled = cfg.peers.enable; };
|
||||||
|
peersInUse = usingFirstClassPeers || freeformPeers != { };
|
||||||
|
|
||||||
|
moduleDefaults = {
|
||||||
|
server = {
|
||||||
|
bind = cfg.bind;
|
||||||
|
port = cfg.port;
|
||||||
|
allowed_hosts = hosts;
|
||||||
|
allowed_origins = map (h: "${scheme}://${h}") (lib.filter (h: !lib.hasInfix "://" h) hosts)
|
||||||
|
++ lib.filter (h: lib.hasInfix "://" h) cfg.allowedHosts;
|
||||||
|
} // lib.optionalAttrs cfg.tls.enable {
|
||||||
|
tls = {
|
||||||
|
enabled = true;
|
||||||
|
cert_file = "${credDir}/cert";
|
||||||
|
key_file = "${credDir}/key";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
auth.db_path = "/var/lib/${name}/db.sqlite";
|
||||||
|
} // lib.optionalAttrs cfg.docker.enable {
|
||||||
|
sources.docker_socket = "/var/run/docker.sock";
|
||||||
|
} // lib.optionalAttrs peersInUse {
|
||||||
|
peers = peersSection;
|
||||||
|
};
|
||||||
|
|
||||||
|
settings = lib.recursiveUpdate moduleDefaults cfg.settings
|
||||||
|
# `recursiveUpdate` lets a freeform `settings.peers.enabled` win over the
|
||||||
|
# first-class option, so put the settled section back on top.
|
||||||
|
// lib.optionalAttrs peersInUse { peers = peersSection; };
|
||||||
|
|
||||||
|
# SIGHUP re-reads the TLS certificate and, with the feature on, the `[peers]`
|
||||||
|
# block. It cannot re-bind the listener, reopen the database or replace the
|
||||||
|
# transport that holds the deny list and the concurrency/size limits, so the
|
||||||
|
# parts of `settings` that only take effect at startup are restarted on.
|
||||||
|
# `enabled` counts as one of them because the peer routes are mounted once,
|
||||||
|
# at startup. Without this, a `switch` that changes e.g. the port would
|
||||||
|
# reload into a file the running process never reads and leave the old port
|
||||||
|
# in effect.
|
||||||
|
restartOnlyPeerKeys = [
|
||||||
|
"enabled"
|
||||||
|
"deny_cidrs"
|
||||||
|
"max_response_bytes"
|
||||||
|
"max_concurrent_per_peer"
|
||||||
|
"max_concurrent_total"
|
||||||
|
];
|
||||||
|
restartSettings =
|
||||||
|
lib.filterAttrs (key: _: key != "peers") settings
|
||||||
|
// lib.optionalAttrs (settings ? peers) {
|
||||||
|
peers = builtins.intersectAttrs (lib.genAttrs restartOnlyPeerKeys (_: null)) settings.peers;
|
||||||
|
};
|
||||||
|
# A plain string (the JSON) rather than a path: the unit carries it as the
|
||||||
|
# content of `X-Restart-Triggers`, which `switch-to-configuration` compares,
|
||||||
|
# and evaluating the unit then needs no store path or build.
|
||||||
|
restartTrigger = builtins.toJSON restartSettings;
|
||||||
|
|
||||||
settings = lib.recursiveUpdate moduleDefaults cfg.settings;
|
|
||||||
configFile = toml.generate "${name}.toml" settings;
|
configFile = toml.generate "${name}.toml" settings;
|
||||||
|
|
||||||
port = settings.server.port;
|
port = settings.server.port;
|
||||||
@@ -79,12 +117,21 @@ let
|
|||||||
# enforces, so a bad name fails at build time instead of at startup.
|
# enforces, so a bad name fails at build time instead of at startup.
|
||||||
validPeerName = n: builtins.match "[a-z0-9][a-z0-9_-]{0,31}" n != null;
|
validPeerName = n: builtins.match "[a-z0-9][a-z0-9_-]{0,31}" n != null;
|
||||||
# A path literal is rejected by the string type; an interpolated one
|
# A path literal is rejected by the string type; an interpolated one
|
||||||
# (`"${./secret}"`) becomes a store path. Reject that too: a token in the
|
# (`"${./secret}"`) becomes a store path. That is rejected too, so the file
|
||||||
# store would defeat the whole credential scheme.
|
# is never handed to the service from the store. Interpolation has already
|
||||||
|
# copied it into the local store by then: the assertion stops the deployment,
|
||||||
|
# not the copy, so a secret used this way must be rotated.
|
||||||
outsideStore = p: !lib.hasPrefix "${builtins.storeDir}/" p;
|
outsideStore = p: !lib.hasPrefix "${builtins.storeDir}/" p;
|
||||||
peerTokenOk = peer:
|
# `LoadCredential=<id>:<path>` splits on the first colon; a colon or a space
|
||||||
lib.hasPrefix "/" peer.tokenFile && outsideStore peer.tokenFile;
|
# in the path would hand systemd a different path (or a different ID).
|
||||||
freeformPeers = cfg.settings.peers.peer or [ ];
|
credentialSafe = p: !(lib.hasInfix ":" p || lib.hasInfix " " p || lib.hasInfix "\t" p);
|
||||||
|
peerTokenPathOk = peer: lib.hasPrefix "/" peer.tokenFile && outsideStore peer.tokenFile;
|
||||||
|
# The binary refuses more than 64 rendered peers at startup; legacy freeform
|
||||||
|
# entries count too because they end up in the same list.
|
||||||
|
legacyPeers = cfg.settings.peers.peer or [ ];
|
||||||
|
peerCount = builtins.length renderedPeers + (if usingFirstClassPeers then 0 else builtins.length legacyPeers);
|
||||||
|
firstClassOwnedKeys = [ "enabled" "peer" ];
|
||||||
|
freeformOwnedKeys = lib.filter (key: lib.elem key firstClassOwnedKeys) (lib.attrNames freeformPeers);
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.services.skyg-linux-tracker = {
|
options.services.skyg-linux-tracker = {
|
||||||
@@ -154,7 +201,12 @@ in
|
|||||||
type = toml.type;
|
type = toml.type;
|
||||||
default = { };
|
default = { };
|
||||||
example = { limits.max_lines = 500; };
|
example = { limits.max_lines = 500; };
|
||||||
description = "Freeform config merged over the module defaults and written to /etc/skyg-linux-tracker.toml. Never put secrets here.";
|
description = ''
|
||||||
|
Freeform config merged over the module defaults and written to
|
||||||
|
/etc/skyg-linux-tracker.toml. Never put secrets here. A change that the
|
||||||
|
service cannot apply on SIGHUP (everything except the TLS certificate
|
||||||
|
and most of `[peers]`) restarts it; peer edits and intervals reload it.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
environmentFile = mkOption {
|
environmentFile = mkOption {
|
||||||
@@ -173,8 +225,11 @@ in
|
|||||||
example = { tcp_interval_secs = 10; api_interval_secs = 30; fail_threshold = 2; };
|
example = { tcp_interval_secs = 10; api_interval_secs = 30; fail_threshold = 2; };
|
||||||
description = ''
|
description = ''
|
||||||
Freeform `[peers]` tuning (snake_case keys as in config.example.toml),
|
Freeform `[peers]` tuning (snake_case keys as in config.example.toml),
|
||||||
merged over the defaults. Never put a token here: secrets come from
|
merged over the defaults. `enabled` and `peer` are owned by
|
||||||
`peers.peers.<name>.tokenFile`.
|
`peers.enable` and `peers.peers` and must not be set here. Never put a
|
||||||
|
token here: secrets come from `peers.peers.<name>.tokenFile`. The
|
||||||
|
intervals, timeouts and peer list are applied on reload; `enabled`,
|
||||||
|
`deny_cidrs` and the `max_*` limits need a restart and trigger one.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -192,16 +247,26 @@ in
|
|||||||
description = ''
|
description = ''
|
||||||
Path of a file holding this peer's bearer token, normally
|
Path of a file holding this peer's bearer token, normally
|
||||||
`config.age.secrets.<name>.path`. Must be an absolute path
|
`config.age.secrets.<name>.path`. Must be an absolute path
|
||||||
outside the Nix store: the file is passed to the service with
|
outside the Nix store, without `:` or whitespace: the file is
|
||||||
systemd `LoadCredential`, so the token never enters the store,
|
handed to the service with systemd `LoadCredential`, which
|
||||||
the generated config or the service user's readable files.
|
splits its argument on the first colon, so the token does not
|
||||||
|
appear in the generated config or in files the service user
|
||||||
|
can read. A store path is rejected at evaluation; a secret
|
||||||
|
interpolated that way (`"${./secret}"`) has already been copied
|
||||||
|
into the local store by then, so rotate it.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
caFile = mkOption {
|
caFile = mkOption {
|
||||||
type = types.nullOr types.str;
|
type = types.nullOr types.str;
|
||||||
default = null;
|
default = null;
|
||||||
example = "/etc/ssl/private-ca.pem";
|
example = "/etc/ssl/private-ca.pem";
|
||||||
description = "Optional PEM CA bundle used to verify this peer's TLS certificate.";
|
description = ''
|
||||||
|
Optional PEM CA bundle used to verify this peer's TLS
|
||||||
|
certificate. Unlike `tokenFile` it is not passed through
|
||||||
|
`LoadCredential`: the service reads it directly as the
|
||||||
|
`skyg-linux-tracker` user, so that user needs read access and a
|
||||||
|
root-only file such as `/run/agenix/*` fails at startup.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
enable = mkOption {
|
enable = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
@@ -240,12 +305,16 @@ in
|
|||||||
message = "services.skyg-linux-tracker.peers.peers.<name> requires peers.enable = true.";
|
message = "services.skyg-linux-tracker.peers.peers.<name> requires peers.enable = true.";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
assertion = !cfg.peers.enable || cfg.peers.peers != { } || freeformPeers != [ ];
|
assertion = !cfg.peers.enable || enabledPeerNames != [ ] || (!usingFirstClassPeers && legacyPeers != [ ]);
|
||||||
message = "services.skyg-linux-tracker.peers.enable requires at least one peer under peers.peers.<name>.";
|
message = "services.skyg-linux-tracker.peers.enable requires at least one enabled peer under peers.peers.<name>.";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
assertion = cfg.peers.peers == { } || !((cfg.settings.peers or { }) ? peer);
|
assertion = !usingFirstClassPeers || !(lib.elem "enabled" freeformOwnedKeys);
|
||||||
message = "services.skyg-linux-tracker: declare peers with peers.peers.<name>, not settings.peers.peer.";
|
message = "services.skyg-linux-tracker: peers.settings.enabled and settings.peers.enabled conflict with peers.enable; use the first-class option only.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = !usingFirstClassPeers || !(lib.elem "peer" freeformOwnedKeys);
|
||||||
|
message = "services.skyg-linux-tracker: declare peers with peers.peers.<name>, not settings.peers.peer or peers.settings.peer.";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
# Option attribute names are unique by construction, so only the
|
# Option attribute names are unique by construction, so only the
|
||||||
@@ -254,9 +323,17 @@ in
|
|||||||
message = "services.skyg-linux-tracker.peers.peers.<name>: names must match ^[a-z0-9][a-z0-9_-]{0,31}$.";
|
message = "services.skyg-linux-tracker.peers.peers.<name>: names must match ^[a-z0-9][a-z0-9_-]{0,31}$.";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
assertion = lib.all peerTokenOk (lib.attrValues cfg.peers.peers);
|
assertion = peerCount <= 64;
|
||||||
|
message = "services.skyg-linux-tracker: at most 64 peers may be configured.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.all peerTokenPathOk (lib.attrValues cfg.peers.peers);
|
||||||
message = "services.skyg-linux-tracker.peers.peers.<name>.tokenFile must be an absolute path outside the Nix store (use config.age.secrets.<name>.path).";
|
message = "services.skyg-linux-tracker.peers.peers.<name>.tokenFile must be an absolute path outside the Nix store (use config.age.secrets.<name>.path).";
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.all credentialSafe (map (peer: peer.tokenFile) (lib.attrValues cfg.peers.peers));
|
||||||
|
message = "services.skyg-linux-tracker.peers.peers.<name>.tokenFile must not contain ':', spaces or tabs (systemd splits LoadCredential on the first colon).";
|
||||||
|
}
|
||||||
{
|
{
|
||||||
assertion = lib.all (peer: peer.caFile == null || lib.hasPrefix "/" peer.caFile) (lib.attrValues cfg.peers.peers);
|
assertion = lib.all (peer: peer.caFile == null || lib.hasPrefix "/" peer.caFile) (lib.attrValues cfg.peers.peers);
|
||||||
message = "services.skyg-linux-tracker.peers.peers.<name>.caFile must be an absolute path when set.";
|
message = "services.skyg-linux-tracker.peers.peers.<name>.caFile must be an absolute path when set.";
|
||||||
@@ -290,12 +367,15 @@ in
|
|||||||
description = "skyg-linux-tracker: read-only systemd, journal and Docker monitor (REST, MCP, OTel)";
|
description = "skyg-linux-tracker: read-only systemd, journal and Docker monitor (REST, MCP, OTel)";
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
after = [ "network.target" ] ++ lib.optional cfg.docker.enable "docker.service";
|
after = [ "network.target" ] ++ lib.optional cfg.docker.enable "docker.service";
|
||||||
# A peer (or TLS) change only needs SIGHUP: the server re-reads those
|
# The config file is a reload trigger: SIGHUP re-reads the TLS
|
||||||
# from the config file. SIGHUP cannot re-bind the listener or reopen the
|
# certificate and, with peers on, the `[peers]` block, so a peer edit or
|
||||||
# database, so such changes need `systemctl restart` by hand. Changes to
|
# an interval change is applied without dropping connections. Anything
|
||||||
# the unit itself (package, credential paths) still restart.
|
# SIGHUP cannot apply (listener, auth, sources, otel, transport limits)
|
||||||
restartTriggers = lib.optional (!cfg.peers.enable) configFile;
|
# also changes the restart trigger, and `switch-to-configuration`
|
||||||
reloadTriggers = lib.optional cfg.peers.enable configFile;
|
# restarts when that differs, so a `switch` never leaves the service
|
||||||
|
# running on the old port or auth settings.
|
||||||
|
restartTriggers = [ restartTrigger ];
|
||||||
|
reloadTriggers = [ configFile ];
|
||||||
|
|
||||||
# Mirrors systemd/skyg-linux-tracker.service; keep the two in step.
|
# Mirrors systemd/skyg-linux-tracker.service; keep the two in step.
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
|||||||
Reference in new issue
Block a user