From 09ae25a57294946609688591e9f91b3031fe48f6 Mon Sep 17 00:00:00 2001 From: Gerrrt Date: Mon, 23 Nov 2026 11:10:39 -0800 Subject: [PATCH 1/2] feat(observability): split logs.alloy out so the range host is metrics-only (#1651) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit config.alloy mixed the metrics and log pipelines; gandalf's journal and auth logs can carry the attacker's segment. The log sources, the level normaliser and loki.write move to a new logs.alloy, deployed with config.alloy everywhere but a metrics-only host. deploy-agent.sh gains --metrics-only (omits logs.alloy, refuses a docker host, verifies metrics alone). The five stacks that mount config.alloy individually now mount logs.alloy too; observability mounts the whole directory. Verified with alloy v1.22.0 fmt and validate on the directory and on config.alloy alone. §7 gives gandalf's deploy command. Refs #1651. Co-Authored-By: Claude Opus 4.8 --- docs/architecture.md | 3 +- docs/changelog.md | 15 ++ docs/observability.md | 2 +- docs/runbooks/build-the-playground.md | 19 +- scripts/deploy-agent.sh | 76 ++++--- stacks/analyst/compose.yaml | 1 + stacks/bloodhound/compose.yaml | 1 + stacks/lab/compose.yaml | 1 + stacks/observability/alloy/config.alloy | 257 ++---------------------- stacks/observability/alloy/logs.alloy | 254 +++++++++++++++++++++++ stacks/sensor/compose.yaml | 1 + stacks/soc/compose.yaml | 1 + 12 files changed, 354 insertions(+), 277 deletions(-) create mode 100644 stacks/observability/alloy/logs.alloy diff --git a/docs/architecture.md b/docs/architecture.md index 5e5c2a2eb..ae7c5083f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -172,7 +172,8 @@ Two collection paths, because the estate has two kinds of device: - **Things that run an agent.** Linux hosts get a Grafana Alloy agent, which gathers node metrics, cAdvisor container metrics, the systemd journal, syslog and `auth.log`, then pushes to Loki and remote-writes to Prometheus. The same - `alloy/` directory runs everywhere — `config.alloy` on every host, + `alloy/` directory runs everywhere — `config.alloy` (metrics) on every host, + `logs.alloy` with it except on a metrics-only host like `gandalf` ([ADR-0126](adr/0126-monitor-gandalf-now-that-it-stays-on.md)), `docker.alloy` where there is a Docker socket, `syslog.alloy` only here — and `scripts/deploy-agent.sh` ships the right subset, in a container or as the native package; only the endpoint variables differ. diff --git a/docs/changelog.md b/docs/changelog.md index 4f7a2eedf..29bf5696e 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -19,6 +19,21 @@ docstring gives: it is a record, not a claim about now. ## 2026-11-23 +- **The agent's log pipeline is its own file, so the range host can ship + metrics and no logs ([#1651](https://github.com/Gerrrt/HomeLab/issues/1651), + [ADR-0126](adr/0126-monitor-gandalf-now-that-it-stays-on.md)).** `config.alloy` + mixed the metrics and log pipelines, and `gandalf`'s journal and auth logs + can carry what happens on the attacker's segment. The log sources, the level + normaliser and `loki.write` move to a new `logs.alloy`, deployed with + `config.alloy` everywhere but a metrics-only host. `deploy-agent.sh` gains + `--metrics-only`: it omits `logs.alloy` (and refuses a docker host, whose + `docker.alloy` needs the log pipeline) and verifies metrics alone. The five + stacks that mount `config.alloy` individually now mount `logs.alloy` beside + it; the observability stack mounts the whole directory, so it picks it up. + `build-the-playground.md` §7 gives `gandalf`'s deploy command. The split was + checked with `alloy validate` on the whole directory and on `config.alloy` + alone (the metrics-only case), both clean, so every cross-file reference + resolves and the metrics-only config has no dangling one. - **`prometheus` will take SSH keys only, and stays without a host firewall ([ADR-0128](adr/0128-give-prometheus-a-key-only-sshd-and-no-host-firewall.md)).** The root-only reads that finished the 2026-11-13 audit were taken today. diff --git a/docs/observability.md b/docs/observability.md index 36ac93c5c..de2864932 100644 --- a/docs/observability.md +++ b/docs/observability.md @@ -72,7 +72,7 @@ equivalent size bound. ## Log level normalisation Logs arrive spelling severity about twenty different ways — `ERROR`, `err`, -`eror`, `crit`, `fatal`, `panic`, `dbug`. `config.alloy` maps all of them onto +`eror`, `crit`, `fatal`, `panic`, `dbug`. `logs.alloy` maps all of them onto five canonical values before they reach Loki: ```text diff --git a/docs/runbooks/build-the-playground.md b/docs/runbooks/build-the-playground.md index d2324b10c..76e135fff 100644 --- a/docs/runbooks/build-the-playground.md +++ b/docs/runbooks/build-the-playground.md @@ -1469,11 +1469,20 @@ ADR-0126 does not allow. `range-state` is `gandalf`'s own control file **Then the agent, metrics only.** `gandalf`'s journal and auth logs can carry what happens on the attacker's segment, so its agent must not ship them to -Loki. The estate's standard `config.alloy` pushes both metrics and logs, so a -metrics-only variant (no `loki.*`, no `3100`) and a deploy check that requires -only metrics are what [`deploy-agent.sh`](../../scripts/deploy-agent.sh) needs -for this host — **still to build (#1651)**; until it exists, do not deploy the -standard agent here, which would ship the range's logs. +Loki. The log pipeline is its own file, `logs.alloy`, deployed with +`config.alloy` everywhere but here; +[`deploy-agent.sh`](../../scripts/deploy-agent.sh) `--metrics-only` leaves it +off and verifies metrics alone. From a Hicks workstation: + +```bash +scripts/deploy-agent.sh --metrics-only --runtime native root@10.0.30.30 +``` + +`--runtime native` because `gandalf` runs the package, not a container; a +docker host cannot be metrics-only, since `docker.alloy` needs the log +pipeline. The agent then pushes node and collector metrics to +`10.0.99.20:9090` and no logs. This is the one crossing the `morpheus` pass +above carries. **What it watches.** Once the collectors report, `gandalf` joins the `PveFirewall*` alerts in `host.rules.yaml`, and diff --git a/scripts/deploy-agent.sh b/scripts/deploy-agent.sh index 054c76dce..49e963a49 100755 --- a/scripts/deploy-agent.sh +++ b/scripts/deploy-agent.sh @@ -3,7 +3,9 @@ # Deploy, or redeploy, the Alloy agent on a monitored host. # # ./scripts/deploy-agent.sh [--runtime docker|native] [--monitoring-host IP] -# [--no-verify] +# [--no-verify] [--metrics-only] +# --metrics-only ships config.alloy without logs.alloy — metrics, no logs, +# for the range host gandalf (#1651); it cannot be a docker host. # # One command, two runtimes, same result: the agent on the target runs the # Alloy version compose.yaml pins, with the hardening compose.yaml applies, on @@ -109,13 +111,14 @@ warn() { printf '\033[0;33m!\033[0m %s\n' "$*" >&2; } die() { printf '\033[0;31m✗\033[0m %s\n' "$*" >&2; exit 1; } usage() { - sed -n '3,6p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2 + sed -n '3,7p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2 exit "${1:-1}" } RUNTIME="" MON="10.0.99.20" VERIFY=1 +METRICS_ONLY=0 TARGET="" while (($#)); do @@ -123,6 +126,7 @@ while (($#)); do --runtime) RUNTIME="${2:-}"; shift 2 ;; --monitoring-host) MON="${2:-}"; shift 2 ;; --no-verify) VERIFY=0; shift ;; + --metrics-only) METRICS_ONLY=1; shift ;; -h|--help) usage 0 ;; -*) die "unknown flag: $1" ;; *) [[ -z "$TARGET" ]] || die "one target only (got '$TARGET' and '$1')" @@ -190,9 +194,18 @@ if [[ -z "$RUNTIME" ]]; then info "runtime not given; ${HOST} has $([[ $RUNTIME == docker ]] && echo "a Docker socket" || echo "no docker binary") → ${RUNTIME}" fi -# Which files this host gets. Never syslog.alloy — see the header. +# Which files this host gets. config.alloy is the metrics pipeline; logs.alloy +# is the log pipeline (#1651), deployed with it everywhere EXCEPT a +# --metrics-only host, which ships metrics and no logs. docker.alloy's +# components forward to logs.alloy's log_processor and loki.write, so it cannot +# ride a metrics-only deploy. Never syslog.alloy — see the header. FILES=(config.alloy) -[[ "$RUNTIME" == docker ]] && FILES+=(docker.alloy) +if ((METRICS_ONLY)); then + [[ "$RUNTIME" != docker ]] || die "--metrics-only cannot ship docker.alloy, whose log components need the logs.alloy pipeline this mode leaves off" +else + FILES+=(logs.alloy) + [[ "$RUNTIME" == docker ]] && FILES+=(docker.alloy) +fi # The estate's CA, at the path the agent will read it from: /etc/alloy is the # config directory on both runtimes, and Alloy loads only *.alloy from it # (measured on the pinned image with a .pem beside the config). @@ -636,17 +649,26 @@ if ((VERIFY)); then # The marker. logger(1) is util-linux, present on every target this ships # to, and needs no root. If it is missing anyway, fall back to asking for # any line newer than the deploy, and say that a quiet host can fail that. + # A --metrics-only host ships no logs (#1651), so there is nothing to look + # for in Loki: in_loki starts satisfied and no marker is sent. loki_query="${marker_logql}"; loki_since=$((ARRIVED_AFTER - 600)); loki_what="the deploy's marker line" loki_until=$((ARRIVED_AFTER + 180 + 600)) # the 3-minute wait, plus the skew allowance - marker_sent=1 - if ! "${SSH[@]}" "logger -t homelab-deploy-check '${MARKER}'" 2>/dev/null; then - warn "could not run logger on ${HOST}; looking for any line newer than the deploy instead, which an idle host can fail" - loki_query="${logql}"; loki_since="${ARRIVED_AFTER}"; loki_what="a line newer than the deploy" - marker_sent=0 + marker_sent=0 + in_loki=0 + if ((METRICS_ONLY)); then + in_loki=1 + info "waiting for metrics newer than the deploy from ${HOST} in Prometheus at ${MON} (up to 3 min); metrics only, no logs" + else + marker_sent=1 + if ! "${SSH[@]}" "logger -t homelab-deploy-check '${MARKER}'" 2>/dev/null; then + warn "could not run logger on ${HOST}; looking for any line newer than the deploy instead, which an idle host can fail" + loki_query="${logql}"; loki_since="${ARRIVED_AFTER}"; loki_what="a line newer than the deploy" + marker_sent=0 + fi + info "waiting for data newer than the deploy from ${HOST} in Prometheus, and ${loki_what} in Loki, at ${MON} (up to 3 min)" fi - info "waiting for data newer than the deploy from ${HOST} in Prometheus, and ${loki_what} in Loki, at ${MON} (up to 3 min)" fresh_promql="count by (job) (timestamp(${promql}) > ${ARRIVED_AFTER})" - deadline=$((SECONDS + 180)); jobs=0; in_loki=0 + deadline=$((SECONDS + 180)); jobs=0 while ((SECONDS < deadline)); do # `|| true`, and it matters: until the first fresh sample lands, grep # matches nothing and exits 1, pipefail makes that the pipeline's @@ -655,7 +677,7 @@ if ((VERIFY)); then jobs="$(mon_curl "${MON_URL}:${PROM_PORT}/api/v1/query" --data-urlencode "query=${fresh_promql}" 2>/dev/null \ | grep -o '"job":"[^"]*"' | sort -u | wc -l | tr -d ' ' || true)" jobs="${jobs:-0}" - if mon_curl -G "${MON_URL}:${LOKI_PORT}/loki/api/v1/query_range" \ + if ((!METRICS_ONLY)) && mon_curl -G "${MON_URL}:${LOKI_PORT}/loki/api/v1/query_range" \ --data-urlencode "query=${loki_query}" --data-urlencode "start=${loki_since}000000000" \ --data-urlencode "end=${loki_until}000000000" \ --data-urlencode "limit=1" --data-urlencode "direction=forward" 2>/dev/null \ @@ -671,22 +693,26 @@ if ((VERIFY)); then warn "Prometheus has samples newer than the deploy from ${jobs} job(s) for instance=\"${HOST}\" (expected ${expected}). A refused token looks exactly like this: check the agent's log for 401, then ${TOKEN_KEY} in ${TOKEN_FILE} and its line in ${PROXY_FILE}. On another VLAN, check the pass to ${MON}:${PROM_PORT}." arrival_failed=1 fi - if ((in_loki)); then - pass "Loki has ${loki_what} for host=\"${HOST}\"" + if ((METRICS_ONLY)); then + pass "metrics only — no logs expected from ${HOST} (#1651)" else - if ((marker_sent)); then - warn "Loki has not received ${loki_what} for host=\"${HOST}\". Same suspects as above, on ${MON}:${LOKI_PORT}. The marker was logged on ${HOST} as: ${MARKER}" + if ((in_loki)); then + pass "Loki has ${loki_what} for host=\"${HOST}\"" else - warn "Loki has not received ${loki_what} for host=\"${HOST}\". Same suspects as above, on ${MON}:${LOKI_PORT}." + if ((marker_sent)); then + warn "Loki has not received ${loki_what} for host=\"${HOST}\". Same suspects as above, on ${MON}:${LOKI_PORT}. The marker was logged on ${HOST} as: ${MARKER}" + else + warn "Loki has not received ${loki_what} for host=\"${HOST}\". Same suspects as above, on ${MON}:${LOKI_PORT}." + fi + arrival_failed=1 + fi + dropped="$(mon_curl "${MON_URL}:${PROM_PORT}/api/v1/query" \ + --data-urlencode "query=sum(increase(loki_write_dropped_entries_total{job=\"${HOST}-alloy\"}[5m]))" 2>/dev/null \ + | grep -o '"value":\[[^]]*\]' | grep -o '"[0-9.e+-]*"\]' | tr -d '"]' || true)" + if [[ -n "${dropped}" ]] && awk -v d="${dropped}" 'BEGIN { exit !(d > 0) }'; then + warn "the agent has dropped ~${dropped} log entries in the last 5 min — see LogEntriesDropped; a 401 from the proxy is counted here" + arrival_failed=1 fi - arrival_failed=1 - fi - dropped="$(mon_curl "${MON_URL}:${PROM_PORT}/api/v1/query" \ - --data-urlencode "query=sum(increase(loki_write_dropped_entries_total{job=\"${HOST}-alloy\"}[5m]))" 2>/dev/null \ - | grep -o '"value":\[[^]]*\]' | grep -o '"[0-9.e+-]*"\]' | tr -d '"]' || true)" - if [[ -n "${dropped}" ]] && awk -v d="${dropped}" 'BEGIN { exit !(d > 0) }'; then - warn "the agent has dropped ~${dropped} log entries in the last 5 min — see LogEntriesDropped; a 401 from the proxy is counted here" - arrival_failed=1 fi fi fi diff --git a/stacks/analyst/compose.yaml b/stacks/analyst/compose.yaml index 8ed65fd5d..402283f96 100644 --- a/stacks/analyst/compose.yaml +++ b/stacks/analyst/compose.yaml @@ -158,6 +158,7 @@ services: - /etc/alloy volumes: - ../observability/alloy/config.alloy:/etc/alloy/config.alloy:ro + - ../observability/alloy/logs.alloy:/etc/alloy/logs.alloy:ro - ../observability/alloy/ingest-ca.pem:/etc/alloy/ingest-ca.pem:ro - ../observability/alloy/docker.alloy:/etc/alloy/docker.alloy:ro - alloy-data:/var/lib/alloy/data diff --git a/stacks/bloodhound/compose.yaml b/stacks/bloodhound/compose.yaml index 38964d063..ffe67525a 100644 --- a/stacks/bloodhound/compose.yaml +++ b/stacks/bloodhound/compose.yaml @@ -347,6 +347,7 @@ services: - /etc/alloy volumes: - ../observability/alloy/config.alloy:/etc/alloy/config.alloy:ro + - ../observability/alloy/logs.alloy:/etc/alloy/logs.alloy:ro - ../observability/alloy/ingest-ca.pem:/etc/alloy/ingest-ca.pem:ro - ../observability/alloy/docker.alloy:/etc/alloy/docker.alloy:ro - ./alloy/bloodhound.alloy:/etc/alloy/bloodhound.alloy:ro diff --git a/stacks/lab/compose.yaml b/stacks/lab/compose.yaml index d7657ef5d..599c0eadd 100644 --- a/stacks/lab/compose.yaml +++ b/stacks/lab/compose.yaml @@ -490,6 +490,7 @@ services: - /etc/alloy volumes: - ../observability/alloy/config.alloy:/etc/alloy/config.alloy:ro + - ../observability/alloy/logs.alloy:/etc/alloy/logs.alloy:ro - ../observability/alloy/docker.alloy:/etc/alloy/docker.alloy:ro - alloy-data:/var/lib/alloy/data - /var/log:/var/log:ro diff --git a/stacks/observability/alloy/config.alloy b/stacks/observability/alloy/config.alloy index d78c24071..4a6afd6dd 100644 --- a/stacks/observability/alloy/config.alloy +++ b/stacks/observability/alloy/config.alloy @@ -2,21 +2,27 @@ // Grafana Alloy — unified metrics and log collection agent. // // Alloy is started on this DIRECTORY, not on this file, and merges every -// *.alloy in it into one configuration. Three files, deployed by where they +// *.alloy in it into one configuration. Four files, deployed by where they // can run rather than by what they collect: // -// config.alloy this file — node metrics, the agent's own metrics, the -// journal and the /var/log files, the level normaliser, and -// the two writers. Every monitored host. +// config.alloy this file — node metrics, the agent's own metrics, and the +// metrics writer. Every monitored host. +// logs.alloy the journal and /var/log file sources, the level +// normaliser, and the log writer (#1651). Every host but a +// metrics-only one: the range host gandalf ships metrics and +// no logs, since its logs can carry the attacker's segment +// (ADR-0126, deploy-agent.sh --metrics-only). // docker.alloy cAdvisor and container stdout. Needs /var/run/docker.sock, -// so only hosts that have one. +// so only hosts that have one. Its log components forward to +// logs.alloy's, so it rides with logs.alloy, never alone. // syslog.alloy the UDP listener morpheus sends to. Monitoring host only. // // A file that is not present is simply not loaded, which is the whole reason // for the split: a component whose socket or port is absent does not go quiet, // it logs an error on every interval, and on a Proxmox hypervisor running the // native package there is no Docker socket to point at and no reason to open a -// syslog port on the host's real interface. scripts/deploy-agent.sh decides +// syslog port on the host's real interface. The same mechanism keeps a +// metrics-only host from reading logs at all. scripts/deploy-agent.sh decides // which files a host gets; compose.yaml bind-mounts the whole directory here. // // The remote endpoints are read from the environment so a host outside the @@ -199,249 +205,10 @@ prometheus.scrape "selfmetrics" { forward_to = [prometheus.remote_write.metrics_service.receiver] } -// ============================================================================ -// JOURNAL LOG COLLECTION -// ============================================================================ - -loki.source.journal "journal" { - max_age = "24h0m0s" - relabel_rules = discovery.relabel.journal.rules - forward_to = [loki.write.grafana_loki.receiver] - labels = { - component = "/var/log/journal", - instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - hostname = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - job = "/var/log/journal", - log_type = "journal", - } - path = "/var/log/journal" -} - -discovery.relabel "journal" { - targets = [] - - rule { - source_labels = ["__journal__systemd_unit"] - target_label = "unit" - } - - rule { - source_labels = ["__journal__boot_id"] - target_label = "boot_id" - } - - rule { - source_labels = ["__journal__transport"] - target_label = "transport" - } - - // The same five-value mapping as loki.relabel.network_syslog in syslog.alloy, - // against journald's priority keyword instead of the syslog PRI. See that block for - // why it is written this way; journald's vocabulary is emerg, alert, crit, - // err, warning, notice, info and debug, all of which the alternations cover. - rule { - source_labels = ["__journal_priority_keyword"] - regex = ".+" - target_label = "level" - replacement = "info" - } - - rule { - source_labels = ["__journal_priority_keyword"] - regex = "emergency|emerg|alert|critical|crit" - target_label = "level" - replacement = "critical" - } - - rule { - source_labels = ["__journal_priority_keyword"] - regex = "error|err" - target_label = "level" - replacement = "error" - } - - rule { - source_labels = ["__journal_priority_keyword"] - regex = "warning|warn" - target_label = "level" - replacement = "warning" - } - - rule { - source_labels = ["__journal_priority_keyword"] - regex = "debug|dbug|dbg" - target_label = "level" - replacement = "debug" - } -} - -// ============================================================================ -// AUTH LOG COLLECTION -// ============================================================================ - -local.file_match "authlogs" { - path_targets = [ - { - __address__ = "localhost", - __path__ = "/var/log/auth.log", - instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - job = "/var/log/auth.log", - }, - ] - sync_period = "60s" -} - -loki.source.file "authlogs" { - targets = local.file_match.authlogs.targets - forward_to = [loki.process.authlogs.receiver] - tail_from_end = true -} - -loki.process "authlogs" { - stage.static_labels { - values = { - log_type = "authlog", - source = "authlog", - } - } - forward_to = [loki.process.log_processor.receiver] -} - -// ============================================================================ -// VAR LOG COLLECTION -// ============================================================================ -// -// The files nothing else ships: dpkg.log, alternatives.log, cloud-init's, -// apport's and the like. What rsyslog ALSO writes to /var/log/syslog is -// excluded, because the `syslog` source below already carries it and the -// journal carries it again (#909). On golem one `logger` line came back as -// three streams: journal, syslog and varlog's user.log. -// -// auth.log the `authlogs` source above has it, as log_type="authlog" -// kern.log Debian and Ubuntu: also in syslog -// user.log Debian: also in syslog -// mail.log Debian and Ubuntu: also in syslog -// cron.log Debian, when enabled: also in syslog -// daemon.log Debian, when enabled: also in syslog -// -// KEPT: `messages`. On a host whose rsyslog writes no `syslog` file it is the -// only general one, and excluding it would take the kernel rules' input with -// it. No host here writes both by default. -// -// A GLOB, NOT A REGEX. __path_exclude__ is matched like __path__, so -// alternatives are `{a,b}`. The pattern that stood here, `(syslog|auth.log)`, -// was a regex and matched nothing: trinity's Alloy was tailing auth.log as -// varlog as well as authlog (checked 2026-10-07), so every auth event was -// stored three times, not the two #909 counted. syslog itself never needed -// excluding; it has no .log suffix, so __path__ never matched it. - -local.file_match "varlogs" { - path_targets = [ - { - __address__ = "localhost", - __path__ = "/var/log/{messages,*.log}", - __path_exclude__ = "/var/log/{auth,kern,user,mail,cron,daemon}.log", - instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - job = "/var/log/*.log", - }, - ] - sync_period = "60s" -} - -loki.source.file "varlogs" { - targets = local.file_match.varlogs.targets - forward_to = [loki.process.varlogs.receiver] - tail_from_end = true -} - -loki.process "varlogs" { - stage.static_labels { - values = { - log_type = "varlog", - source = "varlog", - } - } - forward_to = [loki.process.log_processor.receiver] -} - -// ============================================================================ -// SYSLOG COLLECTION -// ============================================================================ - -local.file_match "syslog" { - path_targets = [ - { - __address__ = "localhost", - __path__ = "/var/log/syslog", - instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - job = "/var/log/syslog", - }, - ] - sync_period = "60s" -} - -loki.source.file "syslog" { - targets = local.file_match.syslog.targets - forward_to = [loki.process.syslog.receiver] - tail_from_end = true -} - -loki.process "syslog" { - stage.static_labels { - values = { - log_type = "syslog", - source = "syslog", - } - } - forward_to = [loki.process.log_processor.receiver] -} - -// ============================================================================ -// LOG PROCESSOR -// ============================================================================ - -loki.process "log_processor" { - forward_to = [loki.write.grafana_loki.receiver] - - stage.regex { - expression = `(?P(?i)\b(emerg|panic|corrupt|fatal|alert|crit|critical|err|eror|error|warn|warning|info|information|informational|notice|opened|closed|dbug|debug|dbg)\b)` - } - - stage.template { - source = "level_lower" - template = "{{ .level_regex | ToLower }}" - } - - stage.template { - source = "level_parsed" - template = `{{ if eq .level_lower "emerg" }}critical{{ else if eq .level_lower "panic" }}critical{{ else if eq .level_lower "corrupt" }}critical{{ else if eq .level_lower "fatal" }}critical{{ else if eq .level_lower "alert" }}critical{{ else if eq .level_lower "crit" }}critical{{ else if eq .level_lower "critical" }}critical{{ else if eq .level_lower "err" }}error{{ else if eq .level_lower "eror" }}error{{ else if eq .level_lower "error" }}error{{ else if eq .level_lower "warn" }}warning{{ else if eq .level_lower "warning" }}warning{{ else if eq .level_lower "info" }}info{{ else if eq .level_lower "information" }}info{{ else if eq .level_lower "informational" }}info{{ else if eq .level_lower "notice" }}info{{ else if eq .level_lower "dbug" }}debug{{ else if eq .level_lower "debug" }}debug{{ else if eq .level_lower "dbg" }}debug{{ else }}info{{ end }}` - } - - stage.labels { - values = { - "level" = "level_parsed", - } - } -} - // ============================================================================ // REMOTE ENDPOINTS // ============================================================================ -loki.write "grafana_loki" { - endpoint { - url = coalesce(sys.env("LOKI_URL"), "http://loki:3100/loki/api/v1/push") - bearer_token = sys.env("INGEST_TOKEN") - - tls_config { - ca_file = sys.env("INGEST_CA_FILE") - } - } - external_labels = { - host = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), - } -} - prometheus.remote_write "metrics_service" { endpoint { url = coalesce(sys.env("PROMETHEUS_REMOTE_WRITE_URL"), "http://prometheus:9090/api/v1/write") diff --git a/stacks/observability/alloy/logs.alloy b/stacks/observability/alloy/logs.alloy new file mode 100644 index 000000000..ddc4658c1 --- /dev/null +++ b/stacks/observability/alloy/logs.alloy @@ -0,0 +1,254 @@ +// Grafana Alloy — the log pipeline, split out of config.alloy (#1651, +// ADR-0126). config.alloy carries the metrics pipeline and is deployed to +// every monitored host; this file carries the journal, auth, var and syslog +// file sources, the level processor and loki.write, and is deployed with it +// EXCEPT on a metrics-only host. The range host gandalf is metrics-only: its +// journal and auth logs can carry what happens on the attacker's segment, so +// it ships metrics and no logs (deploy-agent.sh --metrics-only). Alloy merges +// every *.alloy in the config directory, so the two halves resolve each +// other's references wherever both are deployed; docker.alloy's forward_to +// log_processor and loki.write.grafana_loki are here, so a host with +// docker.alloy always gets this file too. + +// ============================================================================ +// JOURNAL LOG COLLECTION +// ============================================================================ + +loki.source.journal "journal" { + max_age = "24h0m0s" + relabel_rules = discovery.relabel.journal.rules + forward_to = [loki.write.grafana_loki.receiver] + labels = { + component = "/var/log/journal", + instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + hostname = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + job = "/var/log/journal", + log_type = "journal", + } + path = "/var/log/journal" +} + +discovery.relabel "journal" { + targets = [] + + rule { + source_labels = ["__journal__systemd_unit"] + target_label = "unit" + } + + rule { + source_labels = ["__journal__boot_id"] + target_label = "boot_id" + } + + rule { + source_labels = ["__journal__transport"] + target_label = "transport" + } + + // The same five-value mapping as loki.relabel.network_syslog in syslog.alloy, + // against journald's priority keyword instead of the syslog PRI. See that block for + // why it is written this way; journald's vocabulary is emerg, alert, crit, + // err, warning, notice, info and debug, all of which the alternations cover. + rule { + source_labels = ["__journal_priority_keyword"] + regex = ".+" + target_label = "level" + replacement = "info" + } + + rule { + source_labels = ["__journal_priority_keyword"] + regex = "emergency|emerg|alert|critical|crit" + target_label = "level" + replacement = "critical" + } + + rule { + source_labels = ["__journal_priority_keyword"] + regex = "error|err" + target_label = "level" + replacement = "error" + } + + rule { + source_labels = ["__journal_priority_keyword"] + regex = "warning|warn" + target_label = "level" + replacement = "warning" + } + + rule { + source_labels = ["__journal_priority_keyword"] + regex = "debug|dbug|dbg" + target_label = "level" + replacement = "debug" + } +} + +// ============================================================================ +// AUTH LOG COLLECTION +// ============================================================================ + +local.file_match "authlogs" { + path_targets = [ + { + __address__ = "localhost", + __path__ = "/var/log/auth.log", + instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + job = "/var/log/auth.log", + }, + ] + sync_period = "60s" +} + +loki.source.file "authlogs" { + targets = local.file_match.authlogs.targets + forward_to = [loki.process.authlogs.receiver] + tail_from_end = true +} + +loki.process "authlogs" { + stage.static_labels { + values = { + log_type = "authlog", + source = "authlog", + } + } + forward_to = [loki.process.log_processor.receiver] +} + +// ============================================================================ +// VAR LOG COLLECTION +// ============================================================================ +// +// The files nothing else ships: dpkg.log, alternatives.log, cloud-init's, +// apport's and the like. What rsyslog ALSO writes to /var/log/syslog is +// excluded, because the `syslog` source below already carries it and the +// journal carries it again (#909). On golem one `logger` line came back as +// three streams: journal, syslog and varlog's user.log. +// +// auth.log the `authlogs` source above has it, as log_type="authlog" +// kern.log Debian and Ubuntu: also in syslog +// user.log Debian: also in syslog +// mail.log Debian and Ubuntu: also in syslog +// cron.log Debian, when enabled: also in syslog +// daemon.log Debian, when enabled: also in syslog +// +// KEPT: `messages`. On a host whose rsyslog writes no `syslog` file it is the +// only general one, and excluding it would take the kernel rules' input with +// it. No host here writes both by default. +// +// A GLOB, NOT A REGEX. __path_exclude__ is matched like __path__, so +// alternatives are `{a,b}`. The pattern that stood here, `(syslog|auth.log)`, +// was a regex and matched nothing: trinity's Alloy was tailing auth.log as +// varlog as well as authlog (checked 2026-10-07), so every auth event was +// stored three times, not the two #909 counted. syslog itself never needed +// excluding; it has no .log suffix, so __path__ never matched it. + +local.file_match "varlogs" { + path_targets = [ + { + __address__ = "localhost", + __path__ = "/var/log/{messages,*.log}", + __path_exclude__ = "/var/log/{auth,kern,user,mail,cron,daemon}.log", + instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + job = "/var/log/*.log", + }, + ] + sync_period = "60s" +} + +loki.source.file "varlogs" { + targets = local.file_match.varlogs.targets + forward_to = [loki.process.varlogs.receiver] + tail_from_end = true +} + +loki.process "varlogs" { + stage.static_labels { + values = { + log_type = "varlog", + source = "varlog", + } + } + forward_to = [loki.process.log_processor.receiver] +} + +// ============================================================================ +// SYSLOG COLLECTION +// ============================================================================ + +local.file_match "syslog" { + path_targets = [ + { + __address__ = "localhost", + __path__ = "/var/log/syslog", + instance = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + job = "/var/log/syslog", + }, + ] + sync_period = "60s" +} + +loki.source.file "syslog" { + targets = local.file_match.syslog.targets + forward_to = [loki.process.syslog.receiver] + tail_from_end = true +} + +loki.process "syslog" { + stage.static_labels { + values = { + log_type = "syslog", + source = "syslog", + } + } + forward_to = [loki.process.log_processor.receiver] +} + +// ============================================================================ +// LOG PROCESSOR +// ============================================================================ + +loki.process "log_processor" { + forward_to = [loki.write.grafana_loki.receiver] + + stage.regex { + expression = `(?P(?i)\b(emerg|panic|corrupt|fatal|alert|crit|critical|err|eror|error|warn|warning|info|information|informational|notice|opened|closed|dbug|debug|dbg)\b)` + } + + stage.template { + source = "level_lower" + template = "{{ .level_regex | ToLower }}" + } + + stage.template { + source = "level_parsed" + template = `{{ if eq .level_lower "emerg" }}critical{{ else if eq .level_lower "panic" }}critical{{ else if eq .level_lower "corrupt" }}critical{{ else if eq .level_lower "fatal" }}critical{{ else if eq .level_lower "alert" }}critical{{ else if eq .level_lower "crit" }}critical{{ else if eq .level_lower "critical" }}critical{{ else if eq .level_lower "err" }}error{{ else if eq .level_lower "eror" }}error{{ else if eq .level_lower "error" }}error{{ else if eq .level_lower "warn" }}warning{{ else if eq .level_lower "warning" }}warning{{ else if eq .level_lower "info" }}info{{ else if eq .level_lower "information" }}info{{ else if eq .level_lower "informational" }}info{{ else if eq .level_lower "notice" }}info{{ else if eq .level_lower "dbug" }}debug{{ else if eq .level_lower "debug" }}debug{{ else if eq .level_lower "dbg" }}debug{{ else }}info{{ end }}` + } + + stage.labels { + values = { + "level" = "level_parsed", + } + } +} + +// ============================================================================ +// REMOTE ENDPOINTS +// ============================================================================ + +loki.write "grafana_loki" { + endpoint { + url = coalesce(sys.env("LOKI_URL"), "http://loki:3100/loki/api/v1/push") + bearer_token = sys.env("INGEST_TOKEN") + + tls_config { + ca_file = sys.env("INGEST_CA_FILE") + } + } + external_labels = { + host = coalesce(sys.env("ALLOY_HOSTNAME"), constants.hostname), + } +} diff --git a/stacks/sensor/compose.yaml b/stacks/sensor/compose.yaml index 513cac453..fcccc7e3e 100644 --- a/stacks/sensor/compose.yaml +++ b/stacks/sensor/compose.yaml @@ -257,6 +257,7 @@ services: - /etc/alloy volumes: - ../observability/alloy/config.alloy:/etc/alloy/config.alloy:ro + - ../observability/alloy/logs.alloy:/etc/alloy/logs.alloy:ro - ../observability/alloy/ingest-ca.pem:/etc/alloy/ingest-ca.pem:ro - ../observability/alloy/docker.alloy:/etc/alloy/docker.alloy:ro - ./alloy/zeek.alloy:/etc/alloy/zeek.alloy:ro diff --git a/stacks/soc/compose.yaml b/stacks/soc/compose.yaml index 5b74a5421..89452b44a 100644 --- a/stacks/soc/compose.yaml +++ b/stacks/soc/compose.yaml @@ -713,6 +713,7 @@ services: # alexander. volumes: - ../observability/alloy/config.alloy:/etc/alloy/config.alloy:ro + - ../observability/alloy/logs.alloy:/etc/alloy/logs.alloy:ro - ../observability/alloy/ingest-ca.pem:/etc/alloy/ingest-ca.pem:ro - ../observability/alloy/docker.alloy:/etc/alloy/docker.alloy:ro - ./alloy/opensearch.alloy:/etc/alloy/opensearch.alloy:ro From 244999dd6384592b4a3e9102f445d8d965dbc39a Mon Sep 17 00:00:00 2001 From: Gerrrt Date: Mon, 23 Nov 2026 11:15:00 -0800 Subject: [PATCH 2/2] docs(range): register gandalf's ingest token before the metrics-only deploy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review on #1682: deploy-agent needs gandalf's token registered in the ingest proxy first, or pushes get 401. §7 now links add-monitored-device.md's token step and requires recreating the proxy (make up) before the deploy command. Refs #1651. Co-Authored-By: Claude Opus 4.8 --- docs/runbooks/build-the-playground.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/runbooks/build-the-playground.md b/docs/runbooks/build-the-playground.md index 76e135fff..6856e8cb8 100644 --- a/docs/runbooks/build-the-playground.md +++ b/docs/runbooks/build-the-playground.md @@ -1472,7 +1472,15 @@ what happens on the attacker's segment, so its agent must not ship them to Loki. The log pipeline is its own file, `logs.alloy`, deployed with `config.alloy` everywhere but here; [`deploy-agent.sh`](../../scripts/deploy-agent.sh) `--metrics-only` leaves it -off and verifies metrics alone. From a Hicks workstation: +off and verifies metrics alone. + +**Register `gandalf`'s ingest token first**, or the deploy stops for a missing +token and the proxy answers every push with `401`. Do +[`add-monitored-device.md`](add-monitored-device.md)'s *First, the host's +token*: add `INGEST_TOKEN_gandalf` to `secrets/observability.sops.yaml`, add +its line to the ingest proxy's allowlist and compose environment, and `make up` +so the proxy is recreated with it — exporting the token on Hicks alone does not +register it. Then, from a Hicks workstation: ```bash scripts/deploy-agent.sh --metrics-only --runtime native root@10.0.30.30