gunk-dev/the-valley

telemetry.nix

sourced472c3909b6fmain's published tip history
13.6 KiB
1# Telemetry on a valley host (services.valley.telemetry): the collector a
2# session reads the host's diagnostics through, and the diagnostic
3# directories the host's producers export to it.
4#
5# valley-host.nix imports this with what it derives from the declaration,
6# so the catalog below names exactly the units the host runs.
7#
8# A session reaches the collector only through the git user's shell, over
9# ssh, with the telemetry command (../telemetry/protocol.go). The shell
10# derives the principal from the authenticated key, holds it to the
11# observe or report grant this module writes, and passes the collector one
12# request line on a socket only the git user can open. systemd starts one
13# collector per connection (Accept=yes), as its own unprivileged user,
14# under the limits below, and kills one that outlives them.
15#
16# The collector reads what the manifest names and nothing else: the
17# enumerated units' properties (an explicit list, never the environment)
18# and journal, the streams the worker and the integrator export, and the
19# reports it keeps itself. It is in the systemd-journal group, which lets
20# its account read the whole system journal; its code asks only for the
21# manifest's units. It holds no grant on the repositories, no key, and no
22# way into the worker's private directory.
23#
24# Each producer exports into a directory of its own here, owned by it and
25# group-readable by the collector alone, apart from its private state: the
26# worker's key stays in its 0700 state directory.
27derived:
28{
29 config,
30 lib,
31 pkgs,
32 ...
33}:
34let
35 cfg = config.services.valley;
36 tcfg = cfg.telemetry;
37 package = cfg.packages.telemetry;
38
39 root = "/var/lib/valley-telemetry";
40 collectorState = "${root}/collector";
41 workerDir = "${root}/worker";
42 integratorDir = "${root}/integrator";
43 socket = "/run/valley-telemetry.sock";
44
45 grantsFile = pkgs.writeText "valley-telemetry-grants" (
46 ''
47 # Declared in services.valley.telemetry — do not edit.
48 ''
49 + lib.concatMapStrings (p: "observe ${p}\n") tcfg.observers
50 + lib.concatMapStrings (p: "report ${p}\n") tcfg.reporters
51 );
52
53 webPublisher = config.services.valley.webPublisher.enable or false;
54 webReceiver = config.services.valley.webReceiver.enable or false;
55 integrated = lib.attrNames derived.integratedProjects;
56 published = lib.attrNames derived.publishedProjects;
57
58 source =
59 name: description: enabled: units: extra:
60 {
61 inherit
62 name
63 description
64 enabled
65 units
66 ;
67 location = "local";
68 }
69 // extra;
70
71 manifest = pkgs.writeText "valley-telemetry-manifest.json" (
72 builtins.toJSON {
73 v = 1;
74 host = config.networking.hostName;
75 reporters = tcfg.reporters;
76 sources = [
77 (source "worker" "the check worker, and the attest runs its passes start" cfg.worker.enable
78 [
79 "valley-worker.service"
80 "valley-worker.timer"
81 "valley-worker.path"
82 ]
83 {
84 stream = "${workerDir}/stream";
85 state = "${workerDir}/state.json";
86 configured = {
87 worker = "${derived.workerPackage}";
88 attest = "${derived.attestPackage}";
89 };
90 instruments = [
91 "passes, the change, version, head and tree each reads, batches, publication and backoff with its reason"
92 "each check's evaluation, slot wait, build and result as it happens, and its build output, bounded"
93 "a pass killed before its end, inferred at the next pass"
94 ];
95 }
96 )
97 (source "integrator" "the per-project integrators" cfg.integrator.enable
98 (map (p: "valley-integrator@${p}.service") integrated)
99 {
100 stream = "${integratorDir}/stream";
101 configured.integrator = "${derived.integratorPackage}";
102 instruments = [ "each decision's outcome and its stages, from when it began judging the request" ];
103 not_instrumented = [
104 "when a request was first seen, before its decision began: a follow-up to the integrator wakeup work"
105 ];
106 }
107 )
108 (source "restacker" "the per-project restackers" cfg.restacker.enable
109 (map (p: "valley-restacker@${p}.service") cfg.restacker.projects)
110 {
111 configured.restacker = "${derived.restackerPackage}";
112 not_instrumented = [ "no producer records: lifecycle and output only" ];
113 }
114 )
115 (source "identity" "the registry compiler" cfg.identity.enable [
116 "valley-identity.service"
117 "valley-identity.timer"
118 ] { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
119 (source "signers" "the known-signers publisher" derived.publishesSigners [
120 "valley-signers.service"
121 "valley-signers.path"
122 ] { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
123 (source "mirrors" "the publish queues and their drains" (published != [ ]) (
124 map (p: "valley-publish@${p}.service") published ++ map (p: "valley-publish@${p}.path") published
125 ) { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
126 (source "bus" "the NATS bus" cfg.bus.enable [
127 "valley-bus.service"
128 "valley-bus-init.service"
129 ] { })
130 (source "web-publisher" "the web publisher" webPublisher [
131 "valley-web-publish.service"
132 "valley-web-publish.timer"
133 "valley-web-publish-policy.service"
134 ] { })
135 (source "web-receiver" "the web receiver, on this host" webReceiver [
136 "valley-web-receive.service"
137 "valley-web.service"
138 ] { })
139 (source "backup" "the offsite backup" derived.backupEnabled [
140 "restic-backups-valley.service"
141 "restic-backups-valley.timer"
142 ] { })
143 (source "convergence" "valley-init, which converges the repositories and hooks" true [
144 "valley-init.service"
145 ] { })
146 (source "build-slice" "the build slice and the nix daemon whose builds run in it" cfg.buildSlice.enable [
147 "valley-builds.slice"
148 "nix-daemon.service"
149 ] { })
150 (source "telemetry" "the collector's socket" true [ "valley-telemetry.socket" ] {
151 configured.collector = "${package}";
152 })
153 ]
154 ++ map (r: {
155 inherit (r) name description;
156 enabled = true;
157 location = "remote";
158 reporter = r.principal;
159 stale_after_s = r.staleAfter;
160 }) tcfg.remoteSources;
161 }
162 );
163in
164{
165 options.services.valley.telemetry = {
166 enable = lib.mkEnableOption ''
167 the telemetry collector: the host's diagnostics, read from a session
168 over ssh with the telemetry command (valley telemetry). Only the
169 principals in {option}`services.valley.telemetry.observers` can read,
170 and only those in {option}`services.valley.telemetry.reporters` can
171 send records'';
172
173 observers = lib.mkOption {
174 type = lib.types.listOf (lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*");
175 default = [ ];
176 example = [ "operator" ];
177 description = ''
178 Principals that hold the observe grant: they read every source on
179 the host, the shared units' output included, which spans every
180 project. It is a host-wide grant, apart from any project's read
181 consent. A principal is named by its key's tag, as a push is.
182 '';
183 };
184
185 reporters = lib.mkOption {
186 type = lib.types.listOf (lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*");
187 default = [ ];
188 example = [ "alice" ];
189 description = ''
190 Principals that hold the report grant: they send records about
191 their own approvals and check runs, which the collector stores
192 under their name, stamped with when it received them, in a stream
193 of their own of at most 4 MiB. Reporting grants no reading: a
194 principal that should read its reports back is an observer too.
195 '';
196 };
197
198 remoteSources = lib.mkOption {
199 type = lib.types.listOf (
200 lib.types.submodule {
201 options = {
202 name = lib.mkOption {
203 type = lib.types.strMatching "[a-z0-9][a-z0-9._@-]*";
204 description = "The source's name in the catalog.";
205 };
206 description = lib.mkOption {
207 type = lib.types.str;
208 description = "What the source is.";
209 };
210 principal = lib.mkOption {
211 type = lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*";
212 description = "The reporter whose reports are the source's records.";
213 };
214 staleAfter = lib.mkOption {
215 type = lib.types.ints.positive;
216 default = 3600;
217 description = "Seconds without a report after which the source reads as stale.";
218 };
219 };
220 }
221 );
222 default = [ ];
223 description = ''
224 Components on other machines, such as a web receiver, that report
225 over the same interface with their own reporter principal. Each is
226 catalogued as remote, and reads as unavailable until it reports
227 and as stale once it stops.
228 '';
229 };
230
231 shellFlags = lib.mkOption {
232 type = lib.types.str;
233 default = "";
234 internal = true;
235 description = "The flags the git user's shell serves the telemetry command with; set by this module.";
236 };
237 };
238
239 options.services.valley.packages.telemetry = lib.mkOption {
240 type = lib.types.package;
241 internal = true;
242 default = (import ./packages.nix { inherit pkgs lib; }).telemetry;
243 description = "The collector, valley-telemetry serve.";
244 };
245
246 config = lib.mkIf (cfg.enable && tcfg.enable) {
247 assertions = [
248 {
249 assertion = lib.all (r: lib.elem r.principal tcfg.reporters) tcfg.remoteSources;
250 message = "services.valley.telemetry.remoteSources names a principal that is not in services.valley.telemetry.reporters.";
251 }
252 ];
253
254 services.valley.telemetry.shellFlags = "--telemetry-socket ${socket} --telemetry-grants ${grantsFile}";
255
256 users.users.valley-telemetry = {
257 isSystemUser = true;
258 group = "valley-telemetry";
259 description = "The valley telemetry collector";
260 };
261 users.groups.valley-telemetry = { };
262
263 systemd.tmpfiles.rules = [
264 "d ${root} 0755 root root - -"
265 "d ${collectorState} 0700 valley-telemetry valley-telemetry - -"
266 ]
267 ++ lib.optional cfg.worker.enable "d ${workerDir} 2750 valley-worker valley-telemetry - -"
268 ++ lib.optional cfg.integrator.enable "d ${integratorDir} 2750 ${cfg.integrator.user} valley-telemetry - -";
269
270 # The producers export into their own directories, and are told where
271 # by VALLEY_TELEMETRY_DIR. Each directory is setgid to the collector's
272 # group, so what a producer makes in it is the collector's to read, and
273 # a producer runs with a umask that keeps group read for it. Nothing
274 # else a producer makes is in a directory others can enter: the
275 # worker's own directory stays 0700, its key 0400.
276 systemd.services.valley-worker = lib.mkIf cfg.worker.enable {
277 environment.VALLEY_TELEMETRY_DIR = workerDir;
278 serviceConfig.ReadWritePaths = [ workerDir ];
279 serviceConfig.UMask = lib.mkForce "0027";
280 unitConfig.RequiresMountsFor = [ workerDir ];
281 };
282 systemd.services."valley-integrator@" = lib.mkIf cfg.integrator.enable {
283 environment.VALLEY_TELEMETRY_DIR = "${integratorDir}/stream";
284 # Its own umask, 0002, already keeps group read.
285 serviceConfig.ReadWritePaths = [ integratorDir ];
286 };
287
288 # One collector per connection, which only the git user can make. At
289 # most 16 at once; each principal's own slots are the collector's
290 # (four reads, one report). A connection that outlives the longest
291 # follow is killed.
292 systemd.sockets.valley-telemetry = {
293 description = "The valley telemetry collector's socket";
294 wantedBy = [ "sockets.target" ];
295 socketConfig = {
296 ListenStream = socket;
297 Accept = true;
298 SocketUser = cfg.user;
299 SocketGroup = cfg.group;
300 SocketMode = "0600";
301 MaxConnections = 16;
302 };
303 };
304
305 systemd.services."valley-telemetry@" = {
306 description = "The valley telemetry collector, for one connection";
307 path = [ config.systemd.package ];
308 serviceConfig = {
309 ExecStart = lib.concatStringsSep " " [
310 "${package}/bin/valley-telemetry serve"
311 "--manifest ${manifest}"
312 "--grants ${grantsFile}"
313 "--state ${collectorState}"
314 "--peer-user ${cfg.user}"
315 ];
316 StandardInput = "socket";
317 StandardOutput = "socket";
318 StandardError = "journal";
319 User = "valley-telemetry";
320 Group = "valley-telemetry";
321 SupplementaryGroups = [ "systemd-journal" ];
322 RuntimeMaxSec = 360;
323 MemoryMax = "256M";
324 TasksMax = 32;
325 CPUQuota = "50%";
326 NoNewPrivileges = true;
327 ProtectSystem = "strict";
328 ReadWritePaths = [ collectorState ];
329 ProtectHome = true;
330 PrivateTmp = true;
331 PrivateDevices = true;
332 PrivateNetwork = false;
333 ProtectClock = true;
334 ProtectHostname = true;
335 ProtectKernelLogs = true;
336 ProtectKernelModules = true;
337 ProtectKernelTunables = true;
338 ProtectControlGroups = true;
339 RestrictAddressFamilies = [ "AF_UNIX" ];
340 RestrictNamespaces = true;
341 RestrictRealtime = true;
342 RestrictSUIDSGID = true;
343 LockPersonality = true;
344 CapabilityBoundingSet = "";
345 SystemCallArchitectures = "native";
346 SystemCallFilter = [ "@system-service" ];
347 UMask = "0077";
348 };
349 };
350 };
351}