gunk-dev/the-valley

telemetry.nix

sourcef69adc17996fmain's published tip history
13.7 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 = [
103 "each decision's outcome and its stages, from when it began judging the request"
104 "when its process first listed each request at its head, and the pass that decided it"
105 ];
106 not_instrumented = [
107 "when the host accepted a request ref, and anything before the integrator's process started"
108 ];
109 }
110 )
111 (source "restacker" "the per-project restackers" cfg.restacker.enable
112 (map (p: "valley-restacker@${p}.service") cfg.restacker.projects)
113 {
114 configured.restacker = "${derived.restackerPackage}";
115 not_instrumented = [ "no producer records: lifecycle and output only" ];
116 }
117 )
118 (source "identity" "the registry compiler" cfg.identity.enable [
119 "valley-identity.service"
120 "valley-identity.timer"
121 ] { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
122 (source "signers" "the known-signers publisher" derived.publishesSigners [
123 "valley-signers.service"
124 "valley-signers.path"
125 ] { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
126 (source "mirrors" "the publish queues and their drains" (published != [ ]) (
127 map (p: "valley-publish@${p}.service") published ++ map (p: "valley-publish@${p}.path") published
128 ) { not_instrumented = [ "no producer records: lifecycle and output only" ]; })
129 (source "bus" "the NATS bus" cfg.bus.enable [
130 "valley-bus.service"
131 "valley-bus-init.service"
132 ] { })
133 (source "web-publisher" "the web publisher" webPublisher [
134 "valley-web-publish.service"
135 "valley-web-publish.timer"
136 "valley-web-publish-policy.service"
137 ] { })
138 (source "web-receiver" "the web receiver, on this host" webReceiver [
139 "valley-web-receive.service"
140 "valley-web.service"
141 ] { })
142 (source "backup" "the offsite backup" derived.backupEnabled [
143 "restic-backups-valley.service"
144 "restic-backups-valley.timer"
145 ] { })
146 (source "convergence" "valley-init, which converges the repositories and hooks" true [
147 "valley-init.service"
148 ] { })
149 (source "build-slice" "the build slice and the nix daemon whose builds run in it" cfg.buildSlice.enable [
150 "valley-builds.slice"
151 "nix-daemon.service"
152 ] { })
153 (source "telemetry" "the collector's socket" true [ "valley-telemetry.socket" ] {
154 configured.collector = "${package}";
155 })
156 ]
157 ++ map (r: {
158 inherit (r) name description;
159 enabled = true;
160 location = "remote";
161 reporter = r.principal;
162 stale_after_s = r.staleAfter;
163 }) tcfg.remoteSources;
164 }
165 );
166in
167{
168 options.services.valley.telemetry = {
169 enable = lib.mkEnableOption ''
170 the telemetry collector: the host's diagnostics, read from a session
171 over ssh with the telemetry command (valley telemetry). Only the
172 principals in {option}`services.valley.telemetry.observers` can read,
173 and only those in {option}`services.valley.telemetry.reporters` can
174 send records'';
175
176 observers = lib.mkOption {
177 type = lib.types.listOf (lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*");
178 default = [ ];
179 example = [ "operator" ];
180 description = ''
181 Principals that hold the observe grant: they read every source on
182 the host, the shared units' output included, which spans every
183 project. It is a host-wide grant, apart from any project's read
184 consent. A principal is named by its key's tag, as a push is.
185 '';
186 };
187
188 reporters = lib.mkOption {
189 type = lib.types.listOf (lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*");
190 default = [ ];
191 example = [ "alice" ];
192 description = ''
193 Principals that hold the report grant: they send records about
194 their own approvals and check runs, which the collector stores
195 under their name, stamped with when it received them, in a stream
196 of their own of at most 4 MiB. Reporting grants no reading: a
197 principal that should read its reports back is an observer too.
198 '';
199 };
200
201 remoteSources = lib.mkOption {
202 type = lib.types.listOf (
203 lib.types.submodule {
204 options = {
205 name = lib.mkOption {
206 type = lib.types.strMatching "[a-z0-9][a-z0-9._@-]*";
207 description = "The source's name in the catalog.";
208 };
209 description = lib.mkOption {
210 type = lib.types.str;
211 description = "What the source is.";
212 };
213 principal = lib.mkOption {
214 type = lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*";
215 description = "The reporter whose reports are the source's records.";
216 };
217 staleAfter = lib.mkOption {
218 type = lib.types.ints.positive;
219 default = 3600;
220 description = "Seconds without a report after which the source reads as stale.";
221 };
222 };
223 }
224 );
225 default = [ ];
226 description = ''
227 Components on other machines, such as a web receiver, that report
228 over the same interface with their own reporter principal. Each is
229 catalogued as remote, and reads as unavailable until it reports
230 and as stale once it stops.
231 '';
232 };
233
234 shellFlags = lib.mkOption {
235 type = lib.types.str;
236 default = "";
237 internal = true;
238 description = "The flags the git user's shell serves the telemetry command with; set by this module.";
239 };
240 };
241
242 options.services.valley.packages.telemetry = lib.mkOption {
243 type = lib.types.package;
244 internal = true;
245 default = (import ./packages.nix { inherit pkgs lib; }).telemetry;
246 description = "The collector, valley-telemetry serve.";
247 };
248
249 config = lib.mkIf (cfg.enable && tcfg.enable) {
250 assertions = [
251 {
252 assertion = lib.all (r: lib.elem r.principal tcfg.reporters) tcfg.remoteSources;
253 message = "services.valley.telemetry.remoteSources names a principal that is not in services.valley.telemetry.reporters.";
254 }
255 ];
256
257 services.valley.telemetry.shellFlags = "--telemetry-socket ${socket} --telemetry-grants ${grantsFile}";
258
259 users.users.valley-telemetry = {
260 isSystemUser = true;
261 group = "valley-telemetry";
262 description = "The valley telemetry collector";
263 };
264 users.groups.valley-telemetry = { };
265
266 systemd.tmpfiles.rules = [
267 "d ${root} 0755 root root - -"
268 "d ${collectorState} 0700 valley-telemetry valley-telemetry - -"
269 ]
270 ++ lib.optional cfg.worker.enable "d ${workerDir} 2750 valley-worker valley-telemetry - -"
271 ++ lib.optional cfg.integrator.enable "d ${integratorDir} 2750 ${cfg.integrator.user} valley-telemetry - -";
272
273 # The producers export into their own directories, and are told where
274 # by VALLEY_TELEMETRY_DIR. Each directory is setgid to the collector's
275 # group, so what a producer makes in it is the collector's to read, and
276 # a producer runs with a umask that keeps group read for it. Nothing
277 # else a producer makes is in a directory others can enter: the
278 # worker's own directory stays 0700, its key 0400.
279 systemd.services.valley-worker = lib.mkIf cfg.worker.enable {
280 environment.VALLEY_TELEMETRY_DIR = workerDir;
281 serviceConfig.ReadWritePaths = [ workerDir ];
282 serviceConfig.UMask = lib.mkForce "0027";
283 unitConfig.RequiresMountsFor = [ workerDir ];
284 };
285 systemd.services."valley-integrator@" = lib.mkIf cfg.integrator.enable {
286 environment.VALLEY_TELEMETRY_DIR = "${integratorDir}/stream";
287 # Its own umask, 0002, already keeps group read.
288 serviceConfig.ReadWritePaths = [ integratorDir ];
289 };
290
291 # One collector per connection, which only the git user can make. At
292 # most 16 at once; each principal's own slots are the collector's
293 # (four reads, one report). A connection that outlives the longest
294 # follow is killed.
295 systemd.sockets.valley-telemetry = {
296 description = "The valley telemetry collector's socket";
297 wantedBy = [ "sockets.target" ];
298 socketConfig = {
299 ListenStream = socket;
300 Accept = true;
301 SocketUser = cfg.user;
302 SocketGroup = cfg.group;
303 SocketMode = "0600";
304 MaxConnections = 16;
305 };
306 };
307
308 systemd.services."valley-telemetry@" = {
309 description = "The valley telemetry collector, for one connection";
310 path = [ config.systemd.package ];
311 serviceConfig = {
312 ExecStart = lib.concatStringsSep " " [
313 "${package}/bin/valley-telemetry serve"
314 "--manifest ${manifest}"
315 "--grants ${grantsFile}"
316 "--state ${collectorState}"
317 "--peer-user ${cfg.user}"
318 ];
319 StandardInput = "socket";
320 StandardOutput = "socket";
321 StandardError = "journal";
322 User = "valley-telemetry";
323 Group = "valley-telemetry";
324 SupplementaryGroups = [ "systemd-journal" ];
325 RuntimeMaxSec = 360;
326 MemoryMax = "256M";
327 TasksMax = 32;
328 CPUQuota = "50%";
329 NoNewPrivileges = true;
330 ProtectSystem = "strict";
331 ReadWritePaths = [ collectorState ];
332 ProtectHome = true;
333 PrivateTmp = true;
334 PrivateDevices = true;
335 PrivateNetwork = false;
336 ProtectClock = true;
337 ProtectHostname = true;
338 ProtectKernelLogs = true;
339 ProtectKernelModules = true;
340 ProtectKernelTunables = true;
341 ProtectControlGroups = true;
342 RestrictAddressFamilies = [ "AF_UNIX" ];
343 RestrictNamespaces = true;
344 RestrictRealtime = true;
345 RestrictSUIDSGID = true;
346 LockPersonality = true;
347 CapabilityBoundingSet = "";
348 SystemCallArchitectures = "native";
349 SystemCallFilter = [ "@system-service" ];
350 UMask = "0077";
351 };
352 };
353 };
354}