From 22c4d16a20cc8b52f0286b3794b6ba6f65b2bda9 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 22 Nov 2026 23:45:42 +0100 Subject: [PATCH] swarm-otel: stamp sender from the authenticated client id, not a hive --- nix/host-modules/swarm-otel.nix | 424 +++++++++++------------- nix/module-eval/bao-otel-collector.nix | 4 +- nix/module-eval/ci-metrics.nix | 2 +- nix/module-eval/nats-authelia.nix | 6 +- nix/module-eval/swarm-otel-identity.nix | 44 ++- 5 files changed, 245 insertions(+), 235 deletions(-) diff --git a/nix/host-modules/swarm-otel.nix b/nix/host-modules/swarm-otel.nix index ffc2a8168..a357863e0 100644 --- a/nix/host-modules/swarm-otel.nix +++ b/nix/host-modules/swarm-otel.nix @@ -27,10 +27,10 @@ let baoDeploy = deployCfg.bao; # The collector names its components `/`, where `` is a - # hive name for the per-hive pipelines and this literal for the swarm tier's - # own. The two share one namespace and are merged with `//`, so a hive named - # this would silently REPLACE the swarm tier's parts — and lose its own - # pipeline in the process, while its receiver keeps accepting pushes. + # hive name for the per-hive receivers and authenticators and this literal + # for the swarm tier's own. The two share one namespace and are merged with + # `//`, so a hive named this would silently REPLACE the swarm tier's parts + # — and lose its own receiver in the process. # # Bound once and interpolated at every swarm-tier use below, so the assertion # that reserves it is checking the same string the config emits. A literal @@ -282,10 +282,8 @@ let # is only defined when at least one signal goes to it. haveUpstream = upstreamFor "metrics" ++ upstreamFor "logs" != [ ]; - # One list, read by every pipeline: the per-hive pipelines fan out to - # exactly the same destinations as the single pipeline they replace. - # Written once because "which exporters" is a property of this tier, not - # of which hive a sample came from. + # One list, read by every pipeline. Written once because "which exporters" + # is a property of this tier, not of which sender a sample came from. # ⚠️ Unconditional: `deploy.victoriametrics.enable` says "this host RUNS the # store", and a swarm has one either way. Gating on it left a collector # elsewhere with no exporter at all, dropping everything silently. @@ -314,6 +312,13 @@ let # routes and the readiness check all read this attrset, so they cannot name # different sockets. receiverSockets = hiveSockets // lib.genAttrs ([ swarmTierName ] ++ serviceProducerNames) socketOf; + + # The pipelines every authenticated receiver feeds, named for what they have + # in common. Derived from `receiverSockets` so a receiver cannot exist + # without joining them. + authPipeline = "authenticated"; + authReceivers = map (owner: "otlp/${owner}") (lib.attrNames receiverSockets); + upstreamOf = sock: "http://unix:${sock}:/"; # Set by nginx on every response from a route, whatever its status, so the # readiness check can tell a route the running nginx has from the catch-all. @@ -1142,19 +1147,16 @@ in # wiring check: it accepts a receiver naming an absent # extension, and the collector then dies at startup. A green # build does not prove this config starts, never mind that a - # sample arrives — which is why this module's gate pushes a - # real sample through both tiers into the store. + # sample arrives with its `sender`. validateConfigFile = true; settings = { - # One receiver per hive, and that multiplicity is forced - # rather than chosen. The `hive` - # label has to come from something the sender cannot write, - # and the only such thing here is WHICH RECEIVER accepted - # the sample: a processor cannot read the token's claims - # (`from_context` reads request metadata, and asking it for - # an auth claim yields nothing — silently, with a healthy - # startup), and one receiver holding many credentials never - # reveals which one matched. + # One receiver per audience, because an `oidc` authenticator + # checks exactly one. Every authenticated receiver feeds the + # same pipelines, where `resource/sender` stamps `sender` from + # the token's `client_id` claim (`auth.claims.client_id`), a + # value the sender cannot write. A missing claim leaves the + # record without `sender`, silently and with a healthy startup, + # so only a stored sample shows that it works. receivers = lib.mapAttrs' ( h: sock: @@ -1419,116 +1421,87 @@ in # is for looking at this swarm; the upstream is for whoever # aggregates across swarms, and neither replaces the other. # `exporterNames` is shared by every pipeline — where a - # sample goes is a property of this tier, not of the hive - # that sent it. - service.pipelines = - lib.mapAttrs' ( - h: _: - lib.nameValuePair "metrics/${h}" { - receivers = [ "otlp/${h}" ]; - processors = [ "resource/${h}" ]; - exporters = exporterNames; - } - ) hiveSockets - # Its OWN pipeline, and that separation is the ruling, not a - # tidiness choice: every `resource/` above UPSERTS a - # `hive` key, so a scraped swarm sample routed through any of - # them would acquire the one label a swarm-level service must - # not have. Keeping it out of them makes the absence - # structural rather than something to remember to strip. - // { - # `otlp/${swarmTierName}` is unconditional (see the - # receiver above), so this pipeline is too — a swarm-tier - # producer must always have somewhere to land, unlike - # `prometheus`, which only joins the receiver list once - # something has actually declared a scrape target. - # - # ⚠️ That gate is the SAME condition the receiver is - # defined under, and the two must stay spelled the same - # way: a receiver defined here and attached to no - # pipeline is scrape configs that render, deploy and - # deliver nothing — the silent shape this file keeps - # arguing against, one level up. - "metrics/${swarmTierName}" = { - receivers = [ - "otlp/${swarmTierName}" - ] - # The service forwarders' receivers join the swarm - # tier's pipeline rather than bringing their own: the - # stamp they need is `resource/${swarmTierName}`'s exact - # stamp — the swarm's name and deliberately no `hive` — - # and a second pipeline would be that same processor - # under a second name to keep in step. What a producer is - # separated FOR is the credential it proves, and that is - # the receiver's job, not the pipeline's. - ++ map (n: "otlp/${n}") serviceProducerNames - ++ lib.optional (scrapeTargets != { } || cfg.publishedScrapeTargets != { }) "prometheus"; - processors = [ "resource/${swarmTierName}" ]; - exporters = exporterNames; - }; - } - # A hive's logs, and the receiver is the SAME one its metrics - # arrive on: a receiver is not per-signal, so `otlp/${h}` - # feeds this pipeline and `metrics/${h}` above with no second - # port, credential or audience. Which is also why the `hive` - # label stays trustworthy for logs without any new mechanism - # — it still comes from which receiver accepted the sample. - # - # The senders are agent containers, one forwarder each - # (nix/agent-modules/otel.nix), via their own hive's collector. - # Without this pipeline that whole path terminates here: the - # receiver accepts the push, answers 200, and the records go - # nowhere. - // lib.optionalAttrs collectLogs ( - lib.mapAttrs' ( - h: _: - lib.nameValuePair "logs/${h}" { - receivers = [ "otlp/${h}" ]; - processors = [ "resource/${h}" ]; - exporters = logExporterNames; - } - ) hiveSockets - ) - # Logs fan out exactly as metrics do: the swarm's store when it - # runs, the operator's upstream when one is configured, both - # when both. The local store is a destination rather than the - # reason to collect, so turning it off leaves a hive that still - # ships its journal to whoever aggregates across swarms. - # - # Same stamp as the scraped swarm services, for the same reason: - # a host's logs belong to the swarm, and there is no honest - # `hive` value to put on them. - // lib.optionalAttrs collectLogs { - "logs/${swarmTierName}" = { - # This host's own journal, and each service container's, - # which arrives over its forwarder's receiver above - # rather than off a disk — each forwarder reads a journal - # no reader on another host can see. - receivers = [ "journald" ] ++ map (n: "otlp/${n}") serviceProducerNames; - processors = [ - "filter/exclude-user-sessions" - "filter/drop-own-export-failures" - "resource/${swarmTierName}" - ]; - exporters = logExporterNames; - }; - # This container's own journal goes straight to the store, - # not through `otlp/`: a collector forwarding to - # itself would need its own receiver up first. - # - # The store only, never the upstream: an upstream export - # failure is logged here, and shipping that line upstream - # fails again and logs again, forever. - "logs/self" = { - receivers = [ "journald/self" ]; - processors = [ - "filter/drop-store-export-failures" - "resource/self-journal" - "resource/${swarmTierName}" - ]; - exporters = [ "otlphttp/victorialogs" ]; - }; + # sample goes is a property of this tier, not of the sender. + # + # Two kinds of input, two kinds of pipeline. What an + # authenticated receiver proves is WHO sent, and that is the + # same fact for a hive's collector, a service's forwarder and + # the swarm controller, so they share `${authPipeline}` + # and its `resource/sender`. The receivers that authenticate + # nobody (`prometheus`, `journald`) get the swarm stamp and no + # `sender`. + # + # ⛔ No `batch` processor ahead of `resource/sender`: a batch + # exports with a fresh context, so the auth claims are gone and + # the stamp silently stops. + service.pipelines = { + "metrics/${authPipeline}" = { + receivers = authReceivers; + processors = [ "resource/sender" ]; + exporters = exporterNames; }; + } + # The scraped swarm services. Present only when something + # declares a target: a pipeline without a receiver is a + # config the collector refuses to start. + // lib.optionalAttrs (scrapeTargets != { } || cfg.publishedScrapeTargets != { }) { + # ⚠️ The same condition the `prometheus` receiver is + # defined under: a receiver attached to no pipeline is + # scrape configs that render, deploy and deliver nothing. + "metrics/${swarmTierName}" = { + receivers = [ "prometheus" ]; + processors = [ "resource/${swarmTierName}" ]; + exporters = exporterNames; + }; + } + # Logs ride the same receivers as metrics: a receiver is not + # per-signal, so no second port, credential or audience. + # The senders are the agents' forwarders (via their hive's + # collector) and the service containers' forwarders + # (nix/container-modules/swarm-journal-forwarder.nix), which + # drop their own export-failure lines at the source. + // lib.optionalAttrs collectLogs { + "logs/${authPipeline}" = { + receivers = authReceivers; + processors = [ "resource/sender" ]; + exporters = logExporterNames; + }; + } + # Logs fan out exactly as metrics do: the swarm's store when it + # runs, the operator's upstream when one is configured, both + # when both. The local store is a destination rather than the + # reason to collect, so turning it off leaves a hive that still + # ships its journal to whoever aggregates across swarms. + # + # This host's own journal: authenticated by nothing, and a + # host's logs belong to the swarm. + // lib.optionalAttrs collectLogs { + "logs/${swarmTierName}" = { + receivers = [ "journald" ]; + processors = [ + "filter/exclude-user-sessions" + "filter/drop-own-export-failures" + "resource/${swarmTierName}" + ]; + exporters = logExporterNames; + }; + # This container's own journal goes straight to the store, + # not through `otlp/`: a collector forwarding to + # itself would need its own receiver up first. + # + # The store only, never the upstream: an upstream export + # failure is logged here, and shipping that line upstream + # fails again and logs again, forever. + "logs/self" = { + receivers = [ "journald/self" ]; + processors = [ + "filter/drop-store-export-failures" + "resource/self-journal" + "resource/${swarmTierName}" + ]; + exporters = [ "otlphttp/victorialogs" ]; + }; + }; } // { extensions = @@ -1644,108 +1617,105 @@ in ) pushAudiences ); - # `upsert`, not `insert`: a sender that stamps its own - # `hive` must be OVERWRITTEN, not deferred to. This - # processor is the whole attribution boundary — the value - # is a constant per receiver, so it says which hive - # authenticated, not which hive claimed to be sending. - processors = - lib.mapAttrs' ( - h: _: - lib.nameValuePair "resource/${h}" { - attributes = [ - { - key = "hive"; - value = h; - action = "upsert"; - } - # Stamped here rather than on a separate upstream-only - # pipeline, which would double the pipeline count to - # withhold one constant label from the local store. It - # is redundant there — one VictoriaMetrics per swarm, so - # every series in it already belongs to this swarm — but - # a constant label multiplies no series, and it means - # what LEAVES and what STAYS have the same shape. - # - # Upstream is where it stops being redundant: that is the - # one hop where several swarms can land in one store, and - # samples that cannot name their swarm collide there - # exactly as hives collided here before per-hive - # receivers existed. - { - key = "swarm"; - value = swarmDisplayName; - action = "upsert"; - } - ]; + # The attribution boundary. `sender` is the client id the + # token was minted for, read from the authenticated request + # rather than from the record, so it names who authenticated, not + # who claimed to be sending. `delete` runs first because + # `insert` skips an existing key: a missing claim must leave no + # `sender`, never the one the sender wrote. No `hive` key + # survives either — nothing here stamps one, so a sender's own is + # removed. + processors = { + "resource/sender".attributes = [ + { + key = "sender"; + action = "delete"; } - ) hiveSockets - # The swarm tier's own stamp: `swarm` and deliberately NO - # `hive`. A scraped swarm service belongs to the swarm, not to - # any one hive, so there is no honest value to put there — and - # an invented one (a sentinel, the local hive's name) would be - # queried as though it meant something. - # - # ⚠️ Unconditional, not gated on `scrapeTargets != {} || - # collectLogs`: a processor a pipeline names - # but the config does not define is a collector that refuses - # to start, and `metrics/${swarmTierName}` now ALWAYS exists - # (its `otlp/${swarmTierName}` receiver is unconditional too, - # for a swarm-tier producer to push to) — so this processor - # has to exist unconditionally right alongside it. - // { - "resource/${swarmTierName}".attributes = [ - { - # The metric LABEL, a different namespace from the - # component name above — deliberately not interpolated. - key = "swarm"; - value = swarmDisplayName; - action = "upsert"; - } + { + key = "hive"; + action = "delete"; + } + { + key = "sender"; + from_context = "auth.claims.client_id"; + action = "insert"; + } + # Stamped here rather than on a separate upstream-only + # pipeline, which would double the pipeline count to + # withhold one constant label from the local store. It + # is redundant there — one VictoriaMetrics per swarm, so + # every series in it already belongs to this swarm — but + # a constant label multiplies no series, and it means + # what LEAVES and what STAYS have the same shape. + # + # Upstream is where it stops being redundant: that is the + # one hop where several swarms can land in one store, and + # samples that cannot name their swarm collide there. + { + key = "swarm"; + value = swarmDisplayName; + action = "upsert"; + } + ]; + } + # The unauthenticated inputs' stamp: `swarm` and no `sender`, + # because nothing authenticated them. + # + # ⚠️ Unconditional, not gated on `scrapeTargets != {} || + # collectLogs`: a processor a pipeline names but the config + # does not define is a collector that refuses to start, and + # `logs/${swarmTierName}` and `logs/self` name it. + // { + "resource/${swarmTierName}".attributes = [ + { + # The metric LABEL, a different namespace from the + # component name above — deliberately not interpolated. + key = "swarm"; + value = swarmDisplayName; + action = "upsert"; + } + ]; + } + # Drops an operator's desktop session from the host journal: + # journald stamps `_SYSTEMD_SLICE=user-.slice` on every + # record from a logind session scope and from `user@.service`. + # System units, containers and `user.slice` itself do not match. + # + # `ignore`: a record whose body is not a map cannot be indexed, + # and is kept (with a warning) rather than dropped. + // lib.optionalAttrs collectLogs { + # `service.name` on this container's own journal, as the + # other containers' forwarders stamp theirs. + "resource/self-journal".attributes = [ + { + key = "service.name"; + value = cfg.machine; + action = "upsert"; + } + ]; + + "filter/exclude-user-sessions" = { + error_mode = "ignore"; + logs.log_record = [ + ''IsMatch(body["_SYSTEMD_SLICE"], "^user-[0-9]+\\.slice$")'' ]; - } - # Drops an operator's desktop session from the host journal: - # journald stamps `_SYSTEMD_SLICE=user-.slice` on every - # record from a logind session scope and from `user@.service`. - # System units, containers and `user.slice` itself do not match. - # The store's forwarder shares the pipeline, and its container - # runs no login session, so nothing of it is dropped. - # - # `ignore`: a record whose body is not a map cannot be indexed, - # and is kept (with a warning) rather than dropped. - // lib.optionalAttrs collectLogs { - # `service.name` on this container's own journal, as the - # other containers' forwarders stamp theirs. - "resource/self-journal".attributes = [ - { - key = "service.name"; - value = cfg.machine; - action = "upsert"; - } - ]; - - "filter/exclude-user-sessions" = { - error_mode = "ignore"; - logs.log_record = [ - ''IsMatch(body["_SYSTEMD_SLICE"], "^user-[0-9]+\\.slice$")'' - ]; - }; - - # The host journal carries the host collector's own unit; see - # ../collector-own-export-failures.nix. - "filter/drop-own-export-failures" = import ../collector-own-export-failures.nix; - - # The store exporter's own failure lines cannot reach the - # store, and re-feeding them to it repeats the failure. - # Failures of any other exporter pass. `body` is the journal - # entry as a map, the collector's message under `MESSAGE`. - "filter/drop-store-export-failures" = { - error_mode = "ignore"; - logs.log_record = [ - ''IsMatch(body["MESSAGE"], "Exporting failed") and IsMatch(body["MESSAGE"], "otlphttp/victorialogs")'' - ]; - }; }; + + # The host journal carries the host collector's own unit; see + # ../collector-own-export-failures.nix. + "filter/drop-own-export-failures" = import ../collector-own-export-failures.nix; + + # The store exporter's own failure lines cannot reach the + # store, and re-feeding them to it repeats the failure. + # Failures of any other exporter pass. `body` is the journal + # entry as a map, the collector's message under `MESSAGE`. + "filter/drop-store-export-failures" = { + error_mode = "ignore"; + logs.log_record = [ + ''IsMatch(body["MESSAGE"], "Exporting failed") and IsMatch(body["MESSAGE"], "otlphttp/victorialogs")'' + ]; + }; + }; }; }; diff --git a/nix/module-eval/bao-otel-collector.nix b/nix/module-eval/bao-otel-collector.nix index 14ce3ac67..47271eb24 100644 --- a/nix/module-eval/bao-otel-collector.nix +++ b/nix/module-eval/bao-otel-collector.nix @@ -490,8 +490,8 @@ let && s.extensions."oidc/${name}".audience == baoWithCollector.services.hyperhive.swarm.bao.otel.clientId && lib.elem "oidc/${name}" s.service.extensions - && lib.elem "otlp/${name}" s.service.pipelines."metrics/${otel.producerName}".receivers - && lib.elem "otlp/${name}" s.service.pipelines."logs/${otel.producerName}".receivers; + && lib.elem "otlp/${name}" s.service.pipelines."metrics/authenticated".receivers + && lib.elem "otlp/${name}" s.service.pipelines."logs/authenticated".receivers; } { # A client authelia has never heard of mints nothing, so the publisher diff --git a/nix/module-eval/ci-metrics.nix b/nix/module-eval/ci-metrics.nix index a38245793..7f5d8b4a9 100644 --- a/nix/module-eval/ci-metrics.nix +++ b/nix/module-eval/ci-metrics.nix @@ -75,7 +75,7 @@ let && otel.serviceProducers.swarm-ci-metrics.clientId == clientId && s.receivers."otlp/swarm-ci-metrics".protocols.http.auth.authenticator == "oidc/swarm-ci-metrics" && s.extensions."oidc/swarm-ci-metrics".audience == clientId - && lib.elem "otlp/swarm-ci-metrics" s.service.pipelines."metrics/${otel.producerName}".receivers + && lib.elem "otlp/swarm-ci-metrics" s.service.pipelines."metrics/authenticated".receivers && h.services.nginx.virtualHosts.${otel.domain}.locations ? "/swarm-ci-metrics/"; } { diff --git a/nix/module-eval/nats-authelia.nix b/nix/module-eval/nats-authelia.nix index 781b47b92..8555554ba 100644 --- a/nix/module-eval/nats-authelia.nix +++ b/nix/module-eval/nats-authelia.nix @@ -826,7 +826,7 @@ let && s.receivers."otlp/swarm-nats".protocols.http.auth.authenticator == "oidc/swarm-nats" && s.extensions."oidc/swarm-nats".audience == h.services.hyperhive.swarm.nats.clientId && lib.elem "oidc/swarm-nats" s.service.extensions - && lib.elem "otlp/swarm-nats" s.service.pipelines."logs/${otel.producerName}".receivers + && lib.elem "otlp/swarm-nats" s.service.pipelines."logs/authenticated".receivers && h.services.nginx.virtualHosts.${otel.domain}.locations ? "/swarm-nats/" # The store's forwarder keeps its own receiver beside it. && s.extensions."oidc/swarm-bao".audience == h.services.hyperhive.swarm.bao.otel.clientId; @@ -861,7 +861,7 @@ let s.receivers."otlp/${svc.machine}".protocols.http.auth.authenticator == "oidc/${svc.machine}" && s.extensions."oidc/${svc.machine}".audience == clientOf svc && lib.elem "oidc/${svc.machine}" s.service.extensions - && lib.elem "otlp/${svc.machine}" s.service.pipelines."logs/${otel.producerName}".receivers + && lib.elem "otlp/${svc.machine}" s.service.pipelines."logs/authenticated".receivers && h.services.nginx.virtualHosts.${otel.domain}.locations ? "/${svc.machine}/" ) forwarderServices # Their receivers and the queue's on the collector's host, none sharing a socket. @@ -904,7 +904,7 @@ let s.receivers."otlp/${svc.machine}".protocols.http.auth.authenticator == "oidc/${svc.machine}" && s.extensions."oidc/${svc.machine}".audience == ownClientIdOf h svc && lib.elem "oidc/${svc.machine}" s.service.extensions - && lib.elem "otlp/${svc.machine}" s.service.pipelines."logs/${otel.producerName}".receivers + && lib.elem "otlp/${svc.machine}" s.service.pipelines."logs/authenticated".receivers && h.services.nginx.virtualHosts.${otel.domain}.locations ? "/${svc.machine}/" ) ownClientForwarders && lib.length (lib.unique socks) == lib.length socks; diff --git a/nix/module-eval/swarm-otel-identity.nix b/nix/module-eval/swarm-otel-identity.nix index 1db652df3..2614a1354 100644 --- a/nix/module-eval/swarm-otel-identity.nix +++ b/nix/module-eval/swarm-otel-identity.nix @@ -698,14 +698,54 @@ let # config however the mapping is written. The length clause is what # makes the `all` mean anything — over an empty roster it holds # vacuously. - name = "the swarm collector routes every hive's logs, not just one"; + name = "every authenticated receiver, each hive's included, feeds the sender-stamping logs and metrics pipelines"; ok = let p = (otelSettings otelTwoHives).service.pipelines; hives = lib.attrNames otelTwoHives.services.hyperhive.swarm.hives; in lib.length hives == 2 - && lib.all (h: (p ? "logs/${h}") && p."logs/${h}".receivers == [ "otlp/${h}" ]) hives; + && lib.all ( + h: + lib.elem "otlp/${h}" p."logs/authenticated".receivers + && lib.elem "otlp/${h}" p."metrics/authenticated".receivers + ) hives + && !(lib.any (h: (p ? "logs/${h}") || (p ? "metrics/${h}")) hives); + } + { + # 🕳️ The stamp is the attribution boundary, so each half is pinned: the + # claim it reads, the order that makes a missing claim leave NO sender + # (`insert` skips an existing key), and that no `hive` is stamped. The + # unauthenticated pipelines are the control — they carry the swarm + # stamp and must not carry `sender`, since nothing authenticated them. + name = "the sender stamp reads the token's client id, deletes any sender-set value first, and stamps no hive"; + ok = + let + s = otelSettings otelTwoHives; + attrs = s.processors."resource/sender".attributes; + actions = map (a: "${a.action} ${a.key}") attrs; + idx = x: lib.lists.findFirstIndex (a: a == x) null actions; + insert = lib.findFirst (a: a.action == "insert" && a.key == "sender") null attrs; + authed = lib.filterAttrs (_: p: lib.elem "resource/sender" p.processors) s.service.pipelines; + unauthed = lib.filterAttrs (_: p: !(lib.elem "resource/sender" p.processors)) s.service.pipelines; + anyOtlp = ps: lib.any (p: lib.any (r: lib.hasPrefix "otlp/" r) p.receivers) (lib.attrValues ps); + in + insert != null + && insert.from_context == "auth.claims.client_id" + && idx "delete sender" != null + && idx "delete sender" < idx "insert sender" + && idx "delete hive" != null + && lib.all ( + n: !(lib.any (a: a.key == "hive" && a.action != "delete") (s.processors.${n}.attributes or [ ])) + ) (lib.attrNames s.processors) + && + lib.attrNames authed == [ + "logs/authenticated" + "metrics/authenticated" + ] + && lib.all (p: p.processors == [ "resource/sender" ]) (lib.attrValues authed) + && !(anyOtlp unauthed) + && unauthed != { }; } { # The same split as the metrics case above — defining an exporter and