Watch
0
0
Fork
You've already forked hyperhive
0

swarm-otel: stamp sender from the authenticated client id, not a hive

This commit is contained in:
atlas 2026-11-22 23:45:42 +01:00 • committed by mara
commit 22c4d16a20
5 changed files with 243 additions and 233 deletions

View file

@ -27,10 +27,10 @@ let
baoDeploy = deployCfg.bao;
# The collector names its components `<kind>/<owner>`, where `<owner>` 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,92 +1421,63 @@ 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/<hive>` 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.
# sample goes is a property of this tier, not of the sender.
#
# ⚠️ 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.
# 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 = [
"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";
receivers = [ "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}" ];
# 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;
};
}
) 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.
# This host's own journal: authenticated by nothing, and a
# host's logs belong to the swarm.
// 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;
receivers = [ "journald" ];
processors = [
"filter/exclude-user-sessions"
"filter/drop-own-export-failures"
@ -1644,20 +1617,28 @@ 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 = [
# 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";
}
{
key = "hive";
value = h;
action = "upsert";
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
@ -1669,9 +1650,7 @@ in
#
# 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.
# samples that cannot name their swarm collide there.
{
key = "swarm";
value = swarmDisplayName;
@ -1679,20 +1658,13 @@ in
}
];
}
) 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.
# 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 `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.
# 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 = [
{
@ -1708,8 +1680,6 @@ in
# journald stamps `_SYSTEMD_SLICE=user-<uid>.slice` on every
# record from a logind session scope and from `user@<uid>.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.

View file

@ -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

View file

@ -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/";
}
{

View file

@ -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;

View file

@ -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