| 1 | # The web publisher (webui/cmd/valley-web-publish): a oneshot on a timer |
| 2 | # that reads this host's public projects and pushes signed exports to a web |
| 3 | # receiver, which never connects back. |
| 4 | # |
| 5 | # A module of its own beside the host module (valley-host.nix), which it |
| 6 | # needs: it reads that module's options for the repositories the host |
| 7 | # serves, their group, and the instance repository. Which projects exist, |
| 8 | # and the signers that verify their landings, it reads at run time from |
| 9 | # what the instance repository publishes, through the valley CLI. |
| 10 | # |
| 11 | # The service runs as a dynamic user in the git group, so it can read the |
| 12 | # repositories and own nothing on disk but its state and cache. It cannot |
| 13 | # write the repositories, and the files that hold the host's mirror, |
| 14 | # backup and signing keys are made inaccessible to it. |
| 15 | { |
| 16 | config, |
| 17 | pkgs, |
| 18 | lib, |
| 19 | ... |
| 20 | }: |
| 21 | |
| 22 | let |
| 23 | cfg = config.services.valley.webPublisher; |
| 24 | host = config.services.valley; |
| 25 | |
| 26 | # The repositories this host serves, read from its declaration the way |
| 27 | # the host module reads them. The derivation is the host module's own, |
| 28 | # with the same name, schema and declaration, so it is built once. |
| 29 | schema = ../schema/valley.cue; |
| 30 | configJSON = |
| 31 | pkgs.runCommand "valley-config.json" |
| 32 | { |
| 33 | nativeBuildInputs = [ pkgs.cue ]; |
| 34 | } |
| 35 | '' |
| 36 | cue vet -c ${schema} ${host.config} |
| 37 | cue export ${schema} ${host.config} > $out |
| 38 | ''; |
| 39 | declared = lib.importJSON configJSON; |
| 40 | repositories = map (name: "${toString host.dataDir}/${name}.git") ( |
| 41 | lib.attrNames (lib.filterAttrs (_: p: p.git.enable) declared.projects) |
| 42 | ); |
| 43 | |
| 44 | instanceRepo = "${toString host.dataDir}/${toString host.integrator.instanceProject}.git"; |
| 45 | |
| 46 | # The repositories belong to the git user and the service does not, so |
| 47 | # git refuses them as dubiously owned. One exact safe.directory entry per |
| 48 | # repository rides in the unit's environment, which every git the |
| 49 | # publisher and the valley CLI run inherits. GIT_CONFIG_* is command |
| 50 | # scope, which git reads safe.directory from. Never a wildcard. |
| 51 | safeDirectories = { |
| 52 | GIT_CONFIG_COUNT = toString (lib.length repositories); |
| 53 | } |
| 54 | // lib.listToAttrs ( |
| 55 | lib.concatLists ( |
| 56 | lib.imap0 (i: repo: [ |
| 57 | (lib.nameValuePair "GIT_CONFIG_KEY_${toString i}" "safe.directory") |
| 58 | (lib.nameValuePair "GIT_CONFIG_VALUE_${toString i}" repo) |
| 59 | ]) repositories |
| 60 | ) |
| 61 | ); |
| 62 | |
| 63 | # What the service must never read: the git user's ssh identity, which |
| 64 | # pushes the mirrors, and the key files the host module is given. A path |
| 65 | # that does not exist is skipped (the leading -). |
| 66 | keys = [ |
| 67 | "${toString host.dataDir}/.ssh" |
| 68 | "/var/lib/valley-integrator" |
| 69 | ] |
| 70 | ++ lib.optional (host.integrator.signingKeyFile != null) (toString host.integrator.signingKeyFile) |
| 71 | ++ lib.optional host.worker.enable (toString host.worker.stateDir) |
| 72 | ++ map toString ( |
| 73 | lib.filter (p: p != null) [ |
| 74 | host.backup.sshKeyFile |
| 75 | host.backup.passwordFile |
| 76 | host.backup.repositoryFile |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | stateName = lib.removePrefix "/var/lib/" (toString cfg.stateDir); |
| 81 | cacheDir = "/var/cache/${stateName}"; |
| 82 | |
| 83 | principalsFile = pkgs.writeText "valley-web-principals.json" (builtins.toJSON cfg.principals); |
| 84 | |
| 85 | command = lib.concatStringsSep " " [ |
| 86 | (lib.getExe cfg.package) |
| 87 | "--instance-repo ${lib.escapeShellArg instanceRepo}" |
| 88 | "--instance ${lib.escapeShellArg config.networking.hostName}" |
| 89 | "--destination ${lib.escapeShellArg cfg.destination}" |
| 90 | "--receiver ${lib.escapeShellArg cfg.receiver}" |
| 91 | "--principals ${principalsFile}" |
| 92 | "--state ${lib.escapeShellArg (toString cfg.stateDir)}" |
| 93 | ]; |
| 94 | in |
| 95 | { |
| 96 | options.services.valley.webPublisher = { |
| 97 | enable = lib.mkEnableOption '' |
| 98 | the valley web publisher: a timer that uploads this host's public |
| 99 | projects, as signed exports, to a web receiver. The manifest names |
| 100 | {option}`networking.hostName` as the instance, and is signed under |
| 101 | `<hostName>/web-export` |
| 102 | ''; |
| 103 | |
| 104 | destination = lib.mkOption { |
| 105 | type = lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}"; |
| 106 | example = "example.org"; |
| 107 | description = '' |
| 108 | The receiver's name, which every manifest is signed for. The |
| 109 | receiver admits only manifests that name it. |
| 110 | ''; |
| 111 | }; |
| 112 | |
| 113 | receiver = lib.mkOption { |
| 114 | type = lib.types.strMatching "https?://[^/?#@]+(/[^?#]*)?"; |
| 115 | example = "https://example.org"; |
| 116 | description = '' |
| 117 | The receiver's base URL. The upload's paths are appended to it. |
| 118 | ''; |
| 119 | }; |
| 120 | |
| 121 | principals = lib.mkOption { |
| 122 | type = lib.types.attrsOf lib.types.str; |
| 123 | default = { }; |
| 124 | example = { |
| 125 | operator = "operator"; |
| 126 | alice = "agent"; |
| 127 | }; |
| 128 | description = '' |
| 129 | The display mapping: what an export shows in place of each |
| 130 | principal's name. A principal it does not name is shown as |
| 131 | `contributor`. |
| 132 | ''; |
| 133 | }; |
| 134 | |
| 135 | stateDir = lib.mkOption { |
| 136 | type = lib.types.strMatching "/var/lib/[^/].*"; |
| 137 | default = "/var/lib/valley-web-publish"; |
| 138 | description = '' |
| 139 | The publisher's state, under /var/lib: its export key, made on the |
| 140 | first start, the key's verifier line, which the receiver pins, and |
| 141 | the last acknowledgement. The cache is the directory of the same |
| 142 | name under /var/cache. |
| 143 | ''; |
| 144 | }; |
| 145 | |
| 146 | # The publisher as this flake packages it. Internal, like the host |
| 147 | # module's packages: module-eval sets it to a stand-in. |
| 148 | package = lib.mkOption { |
| 149 | type = lib.types.package; |
| 150 | internal = true; |
| 151 | default = (import ./packages.nix { inherit pkgs lib; }).valley-web-publish; |
| 152 | defaultText = lib.literalExpression "(import ./packages.nix { inherit pkgs lib; }).valley-web-publish"; |
| 153 | description = "The web publisher."; |
| 154 | }; |
| 155 | }; |
| 156 | |
| 157 | config = lib.mkIf cfg.enable { |
| 158 | assertions = [ |
| 159 | { |
| 160 | assertion = host.enable; |
| 161 | message = "services.valley.webPublisher needs services.valley: it reads the repositories that host serves."; |
| 162 | } |
| 163 | { |
| 164 | assertion = host.integrator.instanceProject != null; |
| 165 | message = "services.valley.webPublisher needs services.valley.integrator.instanceProject: the publisher discovers the projects, and the signers that verify their landings, through what the instance repository publishes."; |
| 166 | } |
| 167 | ]; |
| 168 | |
| 169 | # One pass, as a oneshot the timer below starts. A switch never starts |
| 170 | # it: nothing wants the service, and a changed unit is not restarted. |
| 171 | # The publisher's own lock serializes a pass started by hand with the |
| 172 | # timer's. |
| 173 | systemd.services.valley-web-publish = { |
| 174 | description = "One pass of the valley web publisher"; |
| 175 | after = [ |
| 176 | "network-online.target" |
| 177 | "valley-init.service" |
| 178 | ]; |
| 179 | wants = [ "network-online.target" ]; |
| 180 | restartIfChanged = false; |
| 181 | unitConfig.RequiresMountsFor = toString host.dataDir; |
| 182 | environment = safeDirectories // { |
| 183 | HOME = toString cfg.stateDir; |
| 184 | XDG_CACHE_HOME = cacheDir; |
| 185 | }; |
| 186 | serviceConfig = { |
| 187 | Type = "oneshot"; |
| 188 | ExecStart = command; |
| 189 | # A pass that hangs is ended, and the timer starts the next. |
| 190 | TimeoutStartSec = "1h"; |
| 191 | DynamicUser = true; |
| 192 | SupplementaryGroups = [ host.group ]; |
| 193 | StateDirectory = stateName; |
| 194 | StateDirectoryMode = "0700"; |
| 195 | CacheDirectory = stateName; |
| 196 | CacheDirectoryMode = "0700"; |
| 197 | WorkingDirectory = toString cfg.stateDir; |
| 198 | # ProtectSystem=strict leaves the whole filesystem read-only but |
| 199 | # the state and cache directories, which systemd exempts. So the |
| 200 | # repositories, readable through the git group, are read-only, |
| 201 | # and named here so a reader of the unit sees it. A repository |
| 202 | # valley-init has not made yet is skipped (the leading -). |
| 203 | ProtectSystem = "strict"; |
| 204 | ReadOnlyPaths = map (p: "-${p}") repositories; |
| 205 | InaccessiblePaths = map (p: "-${p}") keys; |
| 206 | ProtectHome = true; |
| 207 | PrivateTmp = true; |
| 208 | PrivateDevices = true; |
| 209 | NoNewPrivileges = true; |
| 210 | ProtectClock = true; |
| 211 | ProtectHostname = true; |
| 212 | ProtectKernelLogs = true; |
| 213 | ProtectKernelModules = true; |
| 214 | ProtectKernelTunables = true; |
| 215 | ProtectControlGroups = true; |
| 216 | ProtectProc = "invisible"; |
| 217 | RestrictAddressFamilies = [ |
| 218 | "AF_INET" |
| 219 | "AF_INET6" |
| 220 | "AF_UNIX" |
| 221 | ]; |
| 222 | RestrictNamespaces = true; |
| 223 | RestrictRealtime = true; |
| 224 | RestrictSUIDSGID = true; |
| 225 | LockPersonality = true; |
| 226 | CapabilityBoundingSet = ""; |
| 227 | SystemCallArchitectures = "native"; |
| 228 | SystemCallFilter = [ "@system-service" ]; |
| 229 | UMask = "0077"; |
| 230 | }; |
| 231 | }; |
| 232 | |
| 233 | # Every five minutes, counted from the end of each pass. The first |
| 234 | # pass waits five minutes after the timer is started, so it never runs |
| 235 | # inside the switch that started it. |
| 236 | systemd.timers.valley-web-publish = { |
| 237 | description = "Run the valley web publisher"; |
| 238 | wantedBy = [ "timers.target" ]; |
| 239 | timerConfig = { |
| 240 | OnActiveSec = "5min"; |
| 241 | OnUnitInactiveSec = "5min"; |
| 242 | }; |
| 243 | }; |
| 244 | }; |
| 245 | } |