feat(host): root-owned read-only helpers behind a narrow sudo rule, and the target host's requirements
publish / publish (push) Successful in 1m4s

This commit is contained in:
sam committed 2026-11-15 23:11:18 +11:00
1 parent 645d514d11
commit b98620cc5b
6 files changed
+57 -2

No files matched your search

+14 -2
View File
@@ -23,6 +23,9 @@ type Target struct {
// HostKey is the host's public key in authorized_keys form, as
// ssh-keyscan prints it. A host presenting any other key is refused.
HostKey string `json:"host_key"`
// Helpers runs the lookups that walk directories through the root-owned
// helpers in target/, so they see what the account alone can't read.
Helpers bool `json:"helpers"`
key ssh.PublicKey
}
@@ -107,7 +110,7 @@ func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jso
if limit <= 0 {
limit = 20
}
command, parse, err := lookupCommand(name, p, limit, in.OlderThanHours)
command, parse, err := lookupCommand(name, p, limit, in.OlderThanHours, target.Helpers)
if err != nil {
return nil, false, err
}
@@ -129,13 +132,16 @@ func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jso
// lookupCommand builds the fixed command for a lookup. Every argument from
// the input is shell-quoted, because ssh hands the command to the remote
// shell.
func lookupCommand(name, p string, limit int, olderThan *int) (string, func([]byte) (any, error), error) {
func lookupCommand(name, p string, limit int, olderThan *int, helpers bool) (string, func([]byte) (any, error), error) {
q := quote(p)
n := strconv.Itoa(limit)
switch name {
case "filesystem_usage":
return "LC_ALL=C df -P -B1 -- " + q + " && LC_ALL=C df -P -i -- " + q, parseDF, nil
case "directory_sizes":
if helpers {
return helperDir + "directory-sizes " + q + " " + n, parseDU, nil
}
return "LC_ALL=C du -x -B1 --max-depth=1 -- " + q + " 2>/dev/null | sort -rn | head -n " + n, parseDU, nil
case "old_files":
hours := 24
@@ -143,12 +149,18 @@ func lookupCommand(name, p string, limit int, olderThan *int) (string, func([]by
hours = *olderThan
}
minutes := strconv.Itoa(hours * 60)
if helpers {
return helperDir + "old-files " + q + " " + minutes + " " + n, parseFind, nil
}
return "LC_ALL=C find " + q + " -xdev -type f -mmin +" + minutes +
" -printf '%s\\t%T@\\t%u\\t%p\\n' 2>/dev/null | sort -rn | head -n " + n, parseFind, nil
}
return "", nil, fmt.Errorf("no lookup named %q", name)
}
// helperDir is where target/install.sh puts the helpers, run through sudo.
const helperDir = "sudo -n /usr/local/libexec/archie/"
// checkPath accepts an absolute path with no control characters, cleaned.
func checkPath(p string) (string, error) {
if !path.IsAbs(p) {
+12
View File
@@ -0,0 +1,12 @@
# Host connector: what a target host needs
- An unprivileged account (`archie`) whose `authorized_keys` holds the public
half of the machine credential the connector is accepted with.
- `df`, `du`, `find`, `sort` and `head` (GNU coreutils and findutils).
- For lookups to see files the account can't read, run `install.sh` as root.
It installs two helpers under `/usr/local/libexec/archie` and a sudo rule
allowing exactly those, and then set `"helpers": true` on the host in the
connector's `hosts` setting. The helpers only read, check every argument,
and run fixed commands: sudo can't safely allow `du` or `find` themselves,
since `find` can delete and execute.
- The host's public key, as `ssh-keyscan` prints it, in the `hosts` setting.
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
# directory-sizes PATH LIMIT: the largest entries directly under PATH, in
# bytes, staying on its filesystem. Read-only; run by the host connector
# through sudo, so it trusts none of its arguments.
set -eu
[ "$#" -eq 2 ] || { echo "usage: directory-sizes PATH LIMIT" >&2; exit 2; }
case "$1" in /*) ;; *) echo "path must be absolute" >&2; exit 2 ;; esac
case "$2" in '' | *[!0-9]*) echo "limit must be a number" >&2; exit 2 ;; esac
LC_ALL=C du -x -B1 --max-depth=1 -- "$1" 2>/dev/null | sort -rn | head -n "$2"
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
# Installs the host connector's read-only helpers and their sudo rule. Run as
# root on each host the connector reaches.
set -eu
here=$(dirname "$0")
install -d -m 755 /usr/local/libexec/archie
install -m 755 -o root -g root "$here/directory-sizes" "$here/old-files" /usr/local/libexec/archie/
install -m 440 -o root -g root "$here/sudoers" /etc/sudoers.d/archie-host
visudo -cf /etc/sudoers.d/archie-host
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
# old-files PATH MINUTES LIMIT: the largest files under PATH not modified for
# MINUTES, staying on its filesystem, as size, mtime, owner and path.
# Read-only; run by the host connector through sudo, so it trusts none of its
# arguments.
set -eu
[ "$#" -eq 3 ] || { echo "usage: old-files PATH MINUTES LIMIT" >&2; exit 2; }
case "$1" in /*) ;; *) echo "path must be absolute" >&2; exit 2 ;; esac
case "$2" in '' | *[!0-9]*) echo "minutes must be a number" >&2; exit 2 ;; esac
case "$3" in '' | *[!0-9]*) echo "limit must be a number" >&2; exit 2 ;; esac
LC_ALL=C find "$1" -xdev -type f -mmin +"$2" -printf '%s\t%T@\t%u\t%p\n' 2>/dev/null | sort -rn | head -n "$3"
+2
View File
@@ -0,0 +1,2 @@
# The host connector's read-only helpers, and nothing else, as root.
archie ALL=(root) NOPASSWD: /usr/local/libexec/archie/directory-sizes, /usr/local/libexec/archie/old-files