[deploy] Sockets: services.ianitor.edge.proxySocket (Caddy beside Ianitor reaches edge on a 0660 socket of the ianitor-proxy group) and ui.socket (control's UI socket, the proof trusted only there); the Configuration_Server patch, beside-caddy.md and api.md follow; a VM test for both
This commit is contained in:
+54
-7
@@ -20,6 +20,14 @@ let
|
||||
pkg = cfg.package;
|
||||
stateRoot = "/var/lib/ianitor";
|
||||
runProof = "/run/ianitor-proof";
|
||||
# Control's UI socket (G14 option b): its directory is control's RuntimeDirectory, 0750
|
||||
# ianitor-control:ianitor, so only the ianitor group (edge, Caddy) can reach it and no other
|
||||
# user can put a socket there while control restarts.
|
||||
uiSocket = "/run/ianitor-control/ui.sock";
|
||||
# The front proxy's socket (Caddy beside Ianitor): its directory is setgid ianitor-proxy, so
|
||||
# the socket either engine makes there belongs to that group.
|
||||
proxyDir = "/run/ianitor-proxy";
|
||||
proxySocket = "${proxyDir}/edge.sock";
|
||||
toml = pkgs.formats.toml { };
|
||||
|
||||
isStore = p: lib.hasPrefix builtins.storeDir (toString p);
|
||||
@@ -42,7 +50,7 @@ let
|
||||
access_log = "/var/log/ianitor/access.log";
|
||||
caddy_credentials_dir = "/run/credentials/caddy-fallback.service";
|
||||
trusted_proxies = cfg.edge.trustedProxies;
|
||||
};
|
||||
} // lib.optionalAttrs cfg.edge.proxySocket.enable { proxy_socket = proxySocket; };
|
||||
|
||||
controlToml = toml.generate "control.toml" (
|
||||
{
|
||||
@@ -78,6 +86,7 @@ let
|
||||
static_dir = "${pkg}/share/ianitor/web";
|
||||
}
|
||||
// lib.optionalAttrs (cfg.ui.hostname != null) { hostname = cfg.ui.hostname; }
|
||||
// lib.optionalAttrs cfg.ui.socket { socket = uiSocket; }
|
||||
// lib.optionalAttrs (cfg.ui.logoutUrl != null) { logout_url = cfg.ui.logoutUrl; };
|
||||
engine = {
|
||||
file = "${stateRoot}/engine";
|
||||
@@ -147,11 +156,24 @@ in
|
||||
default = false;
|
||||
description = "Open edge's two ports in the firewall.";
|
||||
};
|
||||
proxySocket = {
|
||||
enable = mkEnableOption ''
|
||||
a Unix socket (${proxySocket}, 0660, group ianitor-proxy) on which edge (or the Caddy
|
||||
fallback) serves the HTTPS hosts in plain HTTP to a front proxy that terminated TLS,
|
||||
such as Caddy beside Ianitor. Only that group can connect, so its X-Forwarded-For is
|
||||
believed; no loopback TCP port is involved'';
|
||||
users = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
example = [ "caddy" ];
|
||||
description = "The front proxy's users, added to the ianitor-proxy group so they can connect.";
|
||||
};
|
||||
};
|
||||
trustedProxies = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
example = [ "127.0.0.1" ];
|
||||
description = "Proxies in front of edge whose X-Forwarded-For names the client (beside Caddy: 127.0.0.1), for the share-link throttle and the access log. Off by default: on a loopback listener any local process could then claim any address (docs/deploy/beside-caddy.md).";
|
||||
description = "Proxies in front of edge whose X-Forwarded-For names the client, for the share-link throttle and the access log. Off by default: on a loopback listener any local process could then claim any address. Beside Caddy, use edge.proxySocket instead (docs/deploy/beside-caddy.md).";
|
||||
};
|
||||
};
|
||||
|
||||
@@ -229,6 +251,15 @@ in
|
||||
example = "ianitor.example";
|
||||
description = "The admin host edge serves the UI on (a host in the routes repository with forward-auth and the X-Ianitor-Proof rule, docs/api.md).";
|
||||
};
|
||||
socket = mkOption {
|
||||
type = types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
Serve the API for the admin host on ${uiSocket} as well, and trust the UI proof only
|
||||
there (G14 option b): the admin host's upstream is then `unix:${uiSocket}`. Over the
|
||||
loopback port, which every local process can reach, only API tokens work.
|
||||
'';
|
||||
};
|
||||
logoutUrl = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
@@ -255,8 +286,19 @@ in
|
||||
# Only control (and Caddy itself) may reach Caddy's admin socket; edge is not in it.
|
||||
users.groups.ianitor-caddy-admin = { };
|
||||
users.users.ianitor-control = { isSystemUser = true; group = "ianitor"; extraGroups = [ "ianitor-caddy-admin" ]; home = "${stateRoot}/control"; };
|
||||
users.users.ianitor-edge = { isSystemUser = true; group = "ianitor"; };
|
||||
users.users.caddy-fallback = { isSystemUser = true; group = "ianitor"; };
|
||||
users.users.ianitor-edge = {
|
||||
isSystemUser = true;
|
||||
group = "ianitor";
|
||||
extraGroups = lib.optional cfg.edge.proxySocket.enable "ianitor-proxy";
|
||||
};
|
||||
users.users.caddy-fallback = {
|
||||
isSystemUser = true;
|
||||
group = "ianitor";
|
||||
extraGroups = lib.optional cfg.edge.proxySocket.enable "ianitor-proxy";
|
||||
};
|
||||
users.groups.ianitor-proxy = mkIf cfg.edge.proxySocket.enable {
|
||||
members = cfg.edge.proxySocket.users;
|
||||
};
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${stateRoot} 0755 ianitor-control ianitor - -"
|
||||
@@ -268,7 +310,7 @@ in
|
||||
"d ${stateRoot}/caddy-state 0700 caddy-fallback ianitor - -"
|
||||
"f ${stateRoot}/engine 0644 ianitor-control ianitor - ${cfg.defaultEngine}"
|
||||
"d /var/log/ianitor 0770 ianitor-edge ianitor - -"
|
||||
];
|
||||
] ++ lib.optional cfg.edge.proxySocket.enable "d ${proxyDir} 2770 root ianitor-proxy - -";
|
||||
|
||||
environment.systemPackages = [ pkg ];
|
||||
# The non-secret config, so the CLI (token, pki) finds it: --config /etc/ianitor/control.toml.
|
||||
@@ -308,6 +350,9 @@ in
|
||||
ExecStopPost = "${pkg}/bin/ianitor-control caddy-revert --config ${controlToml}";
|
||||
LoadCredential = loadCreds controlCreds ++ [ "ui-proof:${runProof}/ui-proof" ];
|
||||
ReadWritePaths = [ stateRoot ];
|
||||
# The UI socket's directory: 0750, so the ianitor group may connect and nobody else.
|
||||
RuntimeDirectory = "ianitor-control";
|
||||
RuntimeDirectoryMode = "0750";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 2;
|
||||
};
|
||||
@@ -334,7 +379,8 @@ in
|
||||
LoadCredential = loadCreds edgeCreds ++ [ "ui-proof:${runProof}/ui-proof" ];
|
||||
RuntimeDirectory = "ianitor-edge";
|
||||
RuntimeDirectoryMode = "0750";
|
||||
ReadWritePaths = [ "${stateRoot}/edge-state" "/var/log/ianitor" ];
|
||||
ReadWritePaths = [ "${stateRoot}/edge-state" "/var/log/ianitor" ]
|
||||
++ lib.optional cfg.edge.proxySocket.enable proxyDir;
|
||||
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
||||
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
|
||||
# The first start may come before control has written edge.json.
|
||||
@@ -366,7 +412,8 @@ in
|
||||
LoadCredential = loadCreds cfg.credentials ++ [ "ui-proof:${runProof}/ui-proof" ];
|
||||
RuntimeDirectory = "caddy-fallback";
|
||||
RuntimeDirectoryMode = "0750";
|
||||
ReadWritePaths = [ "${stateRoot}/caddy-state" "/var/log/ianitor" ];
|
||||
ReadWritePaths = [ "${stateRoot}/caddy-state" "/var/log/ianitor" ]
|
||||
++ lib.optional cfg.edge.proxySocket.enable proxyDir;
|
||||
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
||||
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
|
||||
# Go needs W^X for its runtime on some platforms.
|
||||
|
||||
@@ -226,6 +226,56 @@ in
|
||||
server.fail("find /var/lib/ianitor -name 'domi-root.key' | grep -q .")
|
||||
'';
|
||||
|
||||
# G14 option b and the proxy socket (phase 4): another local user can neither open control's
|
||||
# UI socket nor use the proof over the loopback port; the front proxy's socket is for the
|
||||
# ianitor-proxy group only.
|
||||
vm-sockets = pkgs.testers.runNixOSTest {
|
||||
name = "ianitor-sockets";
|
||||
nodes.server = {
|
||||
imports = [ server ];
|
||||
services.ianitor.edge.proxySocket = { enable = true; users = [ "front" ]; };
|
||||
services.ianitor.ui.hostname = "ui.test";
|
||||
users.users.front.isNormalUser = true;
|
||||
users.users.mallory.isNormalUser = true;
|
||||
};
|
||||
testScript = helpers + ''
|
||||
start_all()
|
||||
t = setup(server)
|
||||
install_intermediate(server, t)
|
||||
server.succeed("test -S /run/ianitor-control/ui.sock")
|
||||
assert server.succeed("stat -c %a /run/ianitor-control/ui.sock").strip() == "660"
|
||||
assert server.succeed("stat -c %a /run/ianitor-control").strip() == "750"
|
||||
# Another local user cannot reach the UI socket ...
|
||||
server.fail("runuser -u mallory -- curl -sf -m 5 --unix-socket /run/ianitor-control/ui.sock http://localhost/api/v1/overview")
|
||||
# ... nor sign in over the loopback port, even with the real proof (read here as root).
|
||||
proof = server.succeed("cat /run/ianitor-proof/ui-proof").strip()
|
||||
code = server.succeed(
|
||||
"runuser -u mallory -- curl -s -o /dev/null -w '%{http_code}' -H 'Remote-User: mallory' "
|
||||
+ "-H 'X-Ianitor-Proof: " + proof + "' http://127.0.0.1:7878/api/v1/overview"
|
||||
).strip()
|
||||
assert code == "401", code
|
||||
# The same over the socket, as edge (group ianitor): signed in.
|
||||
code = server.succeed(
|
||||
"runuser -u ianitor-edge -- curl -s -o /dev/null -w '%{http_code}' -H 'Host: ui.test' "
|
||||
+ "-H 'Remote-User: mark' -H 'X-Ianitor-Proof: " + proof + "' "
|
||||
+ "--unix-socket /run/ianitor-control/ui.sock http://ui.test/api/v1/overview"
|
||||
).strip()
|
||||
assert code == "200", code
|
||||
|
||||
# The front proxy's socket: the front user may connect, mallory may not.
|
||||
r = api(server, t, "POST", "/hosts", {"host": {
|
||||
"hostname": "app.test", "upstreams": ["http://127.0.0.1:8000"], "tls": "domi", "auth": "none"}})
|
||||
assert wait_apply(server, t, r["apply"]["id"])["state"] == "kept"
|
||||
server.wait_until_succeeds("test -S /run/ianitor-proxy/edge.sock")
|
||||
assert server.succeed("stat -c '%a %G' /run/ianitor-proxy/edge.sock").strip() == "660 ianitor-proxy"
|
||||
code = server.succeed(
|
||||
"runuser -u front -- curl -s -o /dev/null -w '%{http_code}' --unix-socket /run/ianitor-proxy/edge.sock http://app.test/"
|
||||
).strip()
|
||||
assert code == "200", code
|
||||
server.fail("runuser -u mallory -- curl -sf -m 5 --unix-socket /run/ianitor-proxy/edge.sock http://app.test/")
|
||||
'';
|
||||
};
|
||||
|
||||
vm-control-loopback = pkgs.testers.runNixOSTest {
|
||||
name = "ianitor-control-loopback";
|
||||
nodes.server = server;
|
||||
|
||||
Reference in New Issue
Block a user