agents keep their github token in /run, fetched on every start, not in the state dir
This commit is contained in:
parent
adc61c9043
commit
4bd02ea14d
2 changed files with 310 additions and 78 deletions
|
|
@ -4,12 +4,18 @@
|
|||
# An operator links the token in the swarm UI; `swarm-controller` stores it at
|
||||
# `swarm/agents/<agent>/github-token` (`swarm_secret_client::github`). The
|
||||
# agent's own read grant covers that path, so this unit reads it and writes
|
||||
# `<state>/github-token`, which the `gh` wrapper, the git credential helper and
|
||||
# `hive-github-notify` read.
|
||||
# `services.hyperhive.agent.github.tokenFile`, under `/run`, which the `gh`
|
||||
# wrapper, the git credential helper and `hive-github-notify` read.
|
||||
#
|
||||
# It never deletes. A `github-token` already in place stays when the store has
|
||||
# none or cannot be read. The file is replaced by rename, and only when its
|
||||
# bytes changed.
|
||||
# `/run` is empty at every start, so the token is fetched anew each time and a
|
||||
# token removed from the store is gone at the next start. A store that does not
|
||||
# answer leaves the agent without a token until it does; the timer below asks
|
||||
# again. Nothing reads the `github-token` file an earlier version kept in the
|
||||
# state dir, and nothing deletes it.
|
||||
#
|
||||
# Within one run, a token already fetched stays when the store has none or
|
||||
# cannot be read. The file is replaced by rename, and only when its bytes
|
||||
# changed.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -20,7 +26,6 @@ let
|
|||
cfg = config.services.hyperhive.agent.bao;
|
||||
|
||||
agentName = config.services.hyperhive.agent.user.name;
|
||||
stateDir = "/agents/${agentName}/state";
|
||||
|
||||
# The server CA id ./bao.nix loads.
|
||||
serverCaCredential = "hive-agent-bao-server-ca";
|
||||
|
|
@ -32,19 +37,36 @@ let
|
|||
tokenPath = "secret/swarm/agents/${agentName}/github-token";
|
||||
|
||||
runtimeDir = unitName;
|
||||
tokenFile = "/run/${runtimeDir}/token";
|
||||
# Beside the token, so the rename that replaces it stays in one directory.
|
||||
stagedFile = "/run/${runtimeDir}/token.new";
|
||||
# The store's whole answer, token included: kept in the unit's own `0700`
|
||||
# directory, never in the state dir.
|
||||
# directory.
|
||||
rawFile = "/run/${runtimeDir}/account.json";
|
||||
errFile = "/run/${runtimeDir}/bao.err";
|
||||
|
||||
tokenFile = "${stateDir}/github-token";
|
||||
stagedFile = "${stateDir}/.github-token.new";
|
||||
|
||||
configured = cfg.addr != null && config.services.hyperhive.agent.github.enable;
|
||||
|
||||
storeRetry = import ../host-modules/lib/store-retry.nix { };
|
||||
in
|
||||
{
|
||||
options.services.hyperhive.agent.github.tokenFile = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
default = tokenFile;
|
||||
description = ''
|
||||
Path this agent's GitHub token is fetched to. Read-only: it is a fact
|
||||
about where `${unitName}.service` writes, not a knob.
|
||||
|
||||
🩸 A PATH and never a value. The file is `0400` to the agent user and
|
||||
lives on `/run`, so it is gone whenever the agent's container restarts.
|
||||
|
||||
The file exists only once an operator has linked a token and the agent
|
||||
has a store identity to fetch it with. Until then `gh` and `git push`
|
||||
run unauthenticated.
|
||||
'';
|
||||
};
|
||||
|
||||
config = lib.mkIf configured {
|
||||
systemd.services.${unitName} = {
|
||||
description = "fetch this agent's GitHub token from the secret store";
|
||||
|
|
@ -67,14 +89,19 @@ in
|
|||
serviceConfig = storeRetry.serviceConfig // {
|
||||
Type = "oneshot";
|
||||
# Not `RemainAfterExit`, so the timer below can start it again.
|
||||
# `RuntimeDirectoryPreserve` is what keeps the directory, and the
|
||||
# token in it, alive between runs instead.
|
||||
RemainAfterExit = false;
|
||||
TimeoutStartSec = 30;
|
||||
User = agentName;
|
||||
Group = agentName;
|
||||
RuntimeDirectory = runtimeDir;
|
||||
RuntimeDirectoryMode = "0700";
|
||||
# `0600`, the mode `github-token` has.
|
||||
UMask = "0077";
|
||||
RuntimeDirectoryPreserve = "yes";
|
||||
# Every file this script creates is `0400`, so only the redirect that
|
||||
# creates a file can write to it: each is removed before it is
|
||||
# written.
|
||||
UMask = "0377";
|
||||
LoadCredential = [ serverCaCredential ];
|
||||
};
|
||||
environment = {
|
||||
|
|
@ -88,7 +115,7 @@ in
|
|||
# No identity delivered: ./bao.nix's check reports that.
|
||||
for f in "$BAO_CLIENT_CERT" "$BAO_CLIENT_KEY"; do
|
||||
if [ ! -s "$f" ]; then
|
||||
echo "this agent has no store identity, so it cannot fetch its GitHub token." >&2
|
||||
echo "this agent has no store identity, so it runs without a GitHub token." >&2
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
|
@ -103,17 +130,19 @@ in
|
|||
token=${lib.escapeShellArg tokenFile}
|
||||
trap 'rm -f "$err" "$raw" "$staged"' EXIT
|
||||
|
||||
rm -f "$err"
|
||||
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
|
||||
echo "the swarm secret store at $BAO_ADDR did not accept this agent's certificate login:" >&2
|
||||
echo "the swarm secret store at $BAO_ADDR did not accept this agent's certificate login; this agent runs without a GitHub token until it does:" >&2
|
||||
if [ -s "$err" ]; then cat "$err" >&2; fi
|
||||
exit 1
|
||||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
# No token linked and a store that cannot answer look alike here, and
|
||||
# either way the file in place, if any, is kept.
|
||||
# either way a token fetched earlier in this run, if any, is kept.
|
||||
rm -f "$err" "$raw"
|
||||
if ! bao kv get -format=json ${lib.escapeShellArg tokenPath} >"$raw" 2>"$err"; then
|
||||
echo "no GitHub token read from ${tokenPath}; github-token left as it is:" >&2
|
||||
echo "no GitHub token read from ${tokenPath}; this agent runs without one until the store has one:" >&2
|
||||
if [ -s "$err" ]; then cat "$err" >&2; fi
|
||||
exit 0
|
||||
fi
|
||||
|
|
@ -123,7 +152,7 @@ in
|
|||
# failing the unit would only restart it into the same answer.
|
||||
rm -f "$staged"
|
||||
if ! jq -er '.data.data.value | strings' "$raw" >"$staged"; then
|
||||
echo "${tokenPath} holds no string value; github-token left as it is." >&2
|
||||
echo "${tokenPath} holds no string value; this agent runs without a GitHub token." >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
# GitHub integration (services.hyperhive.agent.github.enable): a `gh` wrapper + a git
|
||||
# credential helper, both reading the PAT from the agent's
|
||||
# `github-token` state file at invocation, so a token ./github-token.nix
|
||||
# fetches takes effect with no rebuild. The token PATH is baked in at build
|
||||
# time (nix knows `userName`) — NOT read from `$HIVE_GITHUB_TOKEN_FILE`,
|
||||
# because claude's Bash tool runs `bash -c` in a minimal env that
|
||||
# doesn't source `/etc/set-environment`, so the env var isn't present
|
||||
# where `gh`/`git` actually run. The token value never enters the nix
|
||||
# store (only its path). github.com only; git auths as `x-access-token`
|
||||
# + PAT.
|
||||
# credential helper, both reading the PAT from `github.tokenFile` (the file
|
||||
# ./github-token.nix fetches into under `/run`) at invocation, so a fetched
|
||||
# token takes effect with no rebuild. The token PATH is baked in at build
|
||||
# time — NOT read from an env var, because claude's Bash tool runs `bash -c`
|
||||
# in a minimal env that doesn't source `/etc/set-environment`, so the env var
|
||||
# isn't present where `gh`/`git` actually run. The token value never enters
|
||||
# the nix store (only its path). github.com only; git auths as
|
||||
# `x-access-token` + PAT.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -16,9 +15,10 @@
|
|||
}:
|
||||
let
|
||||
userName = config.services.hyperhive.agent.user.name;
|
||||
tokenFile = config.services.hyperhive.agent.github.tokenFile;
|
||||
ghWrapper = pkgs.writeShellScriptBin "gh" ''
|
||||
if [ -r "/agents/${userName}/state/github-token" ]; then
|
||||
GH_TOKEN="$(cat "/agents/${userName}/state/github-token")"
|
||||
if [ -r "${tokenFile}" ]; then
|
||||
GH_TOKEN="$(cat "${tokenFile}")"
|
||||
export GH_TOKEN
|
||||
fi
|
||||
exec ${pkgs.gh}/bin/gh "$@"
|
||||
|
|
@ -26,11 +26,11 @@ let
|
|||
gitCredHelper = pkgs.writeShellScriptBin "git-credential-hive-github" ''
|
||||
# git credential-helper protocol: only the `get` action needs an answer.
|
||||
[ "''${1:-}" = "get" ] || exit 0
|
||||
if [ -r "/agents/${userName}/state/github-token" ]; then
|
||||
if [ -r "${tokenFile}" ]; then
|
||||
# GitHub ignores the username for PAT auth — `x-access-token` is the
|
||||
# conventional placeholder; the PAT is the password.
|
||||
printf 'username=x-access-token\n'
|
||||
printf 'password=%s\n' "$(cat "/agents/${userName}/state/github-token")"
|
||||
printf 'password=%s\n' "$(cat "${tokenFile}")"
|
||||
fi
|
||||
'';
|
||||
in
|
||||
|
|
@ -43,7 +43,7 @@ in
|
|||
git credential helper for `https://github.com`, both authenticated from
|
||||
an operator-supplied personal access token (PAT). The operator links the
|
||||
PAT in the swarm UI, and the agent fetches it from the swarm secret store
|
||||
into `<state>/github-token` (needs `services.hyperhive.agent.bao.addr`),
|
||||
into `/run` (needs `services.hyperhive.agent.bao.addr`),
|
||||
so giving an agent GitHub is a runtime action, no per-agent config or
|
||||
rebuild. The wrappers read the token file at invocation, so a fetched
|
||||
PAT takes effect immediately; until one exists, `gh` / `git push` just
|
||||
|
|
@ -72,7 +72,7 @@ in
|
|||
# Wire the GitHub credential helper for `git push` over HTTPS. Host-scoped
|
||||
# to `https://github.com`, so it never touches the forge (localhost:3000)
|
||||
# or any other remote. The helper reads the PAT from the agent's
|
||||
# `github-token` state file at invocation and auths as `x-access-token` +
|
||||
# `github.tokenFile` at invocation and auths as `x-access-token` +
|
||||
# the PAT. System /etc/gitconfig merges under the agent's ~/.gitconfig
|
||||
# (safe.directory), so this is additive.
|
||||
# Nested-path binding + mkIf (matching the other `environment.etc."…"`
|
||||
|
|
@ -110,8 +110,8 @@ in
|
|||
# (agent-service.nix) — one todo stream, two producers.
|
||||
HIVE_AGENT_SOCKET = "/run/hive-agent/${userName}/agent.sock";
|
||||
RUST_LOG = "info";
|
||||
# HYPERHIVE_STATE_DIR comes from systemd.globalEnvironment; the
|
||||
# poller reads the agent's `github-token` from under it.
|
||||
# The file ./github-token.nix fetches the PAT into; a path, never the value.
|
||||
HIVE_GITHUB_TOKEN_FILE = tokenFile;
|
||||
};
|
||||
serviceConfig = {
|
||||
ExecStart = "${config.services.hyperhive.agent.packages.hive-github-notify}/bin/hive-github-notify";
|
||||
|
|
@ -138,7 +138,7 @@ in
|
|||
systemd.paths.hive-github-notify = lib.mkIf config.services.hyperhive.agent.github.enable {
|
||||
description = "start hive-github-notify when the GitHub token appears";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
pathConfig.PathChanged = "/agents/${userName}/state/github-token";
|
||||
pathConfig.PathChanged = tokenFile;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue