chore(deploy): add least-privilege PR-Agent operator access
PR Check / Security scan (Snyk) (pull_request) Skipped
PR Check / Lint + typecheck (pull_request) Failing after 19s
PR Check / Secret scan (gitleaks) (pull_request) Successful in 6s
PR Check / Dependency audit (pull_request) Failing after 9s
PR Check / Dockerfile lint (droast) (pull_request) Successful in 5s

This commit is contained in:
Noel Sabia committed 2026-10-30 21:09:19 +01:00
1 parent 679c0b4858
commit f5810960a8
3 files changed
+235 -4

No files matched your search

+44 -4
View File
@@ -9,9 +9,11 @@ endpoint is not published to the internet.
| Image | `pragent/pr-agent:0.49.0-gitea_app` |
| Container | `pr-agent` |
| Webhook URL, from Gitea | `http://pr-agent:3000/api/v1/gitea_webhooks` |
| Config | `/home/noel/self-hosted/pr-agent/.env` (mode 600) |
| Logs | `docker logs -f pr-agent` |
| Upgrade | bump the image tag, then `docker compose up -d` in this directory |
| Config | `/opt/pr-agent/.env` (root, mode 600) |
| Logs | `ssh … agent@server0 logs` |
| Deploy / restart | `ssh … agent@server0 deploy` |
| Status | `ssh … agent@server0 status` |
| Upgrade | edit the root-owned compose file, then run `deploy` |
## Gitea side
@@ -24,6 +26,36 @@ endpoint is not published to the internet.
Gitea's default egress policy refuses webhook deliveries to private
addresses, including the Docker network.
## Operator access
The agent does not log in as `noel`. A dedicated `agent` account has one
forced command and three actions:
```sh
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 status
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 logs
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 deploy
```
The SSH key is restricted with `restrict,command=…`; the account has no
Docker group, no general sudo, and no shell. Sudo permits only the three
root-owned helper scripts in `/usr/local/sbin`, and those helpers operate on
the fixed `/opt/pr-agent/docker-compose.yml`. The compose file, `.env` and
helpers are root-owned, so the agent cannot turn “restart” into a different
container or a host mount.
`setup-agent-access.sh` creates the account and moves the stack to
`/opt/pr-agent`. `remove-noel-access.sh` then removes the inherited access
from `noel`: the `docker` and `sudo` groups, the keys in
`/home/noel/.ssh/authorized_keys`, and the old PR-Agent directory. Run both
on the server as root, and only run the second after the restricted command
works and you have another admin path.
For git, use a repository-scoped deploy key instead of the personal Gitea
key: `~/.ssh/pr_agent_gitea.pub` is added as a write deploy key on
`CV_Website/CV_Site_Nuxt`. The old personal key must be removed from Gitea,
or it still grants every repository the user can see.
## AI model
The agent uses OpenCode Go's OpenAI-compatible endpoint:
@@ -38,6 +70,14 @@ api base https://opencode.ai/inference/go/openai/v1
the model is not in its token table. The effective review budget is still
capped by PR-Agent's default `config.max_model_tokens` of 32000.
The transfer file used during installation was empty, so set the key as
root and redeploy:
```sh
sudoedit /opt/pr-agent/.env # set OPENAI__KEY=…
sudo /usr/local/sbin/pr-agent-deploy
```
## First check
Open a pull request in `CV_Website/CV_Site_Nuxt`. The server runs `/describe`
@@ -45,7 +85,7 @@ and `/review` on open/reopen; comment commands such as `/review`, `/improve`
and `/ask` are handled from the PR conversation. If nothing appears:
```sh
docker compose logs --tail=100 pr-agent
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 logs
```
A `403` in the Gitea webhook delivery log means the secret does not match or
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Final step: remove the agent's inherited noel access.
#
# Run on server0 only after the agent command works:
# ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 status
#
# This removes noel from docker and sudo, clears noel's authorized_keys,
# and deletes the old PR-Agent directory that held the secrets. Make sure
# you have another admin path (provider console or a new key) first.
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
cat <<'EOF'
This will:
- remove noel from the docker and sudo groups
- clear every key in /home/noel/.ssh/authorized_keys
- delete /home/noel/self-hosted/pr-agent (the /opt copy remains)
Existing noel SSH sessions keep their old groups until they log out.
Make sure you have another admin path before continuing.
EOF
read -r -p "Type REMOVE to continue: " answer
[ "$answer" = "REMOVE" ] || { echo "aborted"; exit 1; }
if id -nG noel | tr ' ' '\n' | grep -qx docker; then
gpasswd -d noel docker
fi
if id -nG noel | tr ' ' '\n' | grep -qx sudo; then
deluser noel sudo
fi
if [ -f /home/noel/.ssh/authorized_keys ]; then
cp -a /home/noel/.ssh/authorized_keys \
"/root/noel-authorized_keys.$(date +%Y%m%d-%H%M%S).bak"
: > /home/noel/.ssh/authorized_keys
chown noel:noel /home/noel/.ssh/authorized_keys
chmod 600 /home/noel/.ssh/authorized_keys
fi
rm -rf /home/noel/self-hosted/pr-agent
cat <<'EOF'
Done. Test the restricted path again:
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 status
Still to do outside this script:
- remove the old personal SSH key from Gitea (Settings > SSH / GPG Keys)
- add ~/.ssh/pr_agent_gitea.pub as a write deploy key on CV_Site_Nuxt
- put the OpenCode Go key in /opt/pr-agent/.env and run the deploy helper
EOF
+135
View File
@@ -0,0 +1,135 @@
#!/usr/bin/env bash
# One-time root setup for the least-privilege PR-Agent operator account.
#
# Run on server0:
# sudo bash /home/noel/agent-access-setup.sh
#
# This creates the agent user and moves the PR-Agent stack to /opt/pr-agent.
# It does NOT remove noel's current docker/sudo access. Verify the agent
# command printed at the end first, then run remove-noel-access.sh.
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
AGENT_USER=agent
AGENT_HOME=/home/$AGENT_USER
PR_AGENT_DIR=/opt/pr-agent
SRC_DIR=/home/noel/self-hosted/pr-agent
PUBKEY_FILE=/home/noel/.ssh/pr_agent_control.pub
DOCKER=/usr/bin/docker
for f in "$SRC_DIR/docker-compose.yml" "$SRC_DIR/README.md" "$SRC_DIR/.env" "$PUBKEY_FILE"; do
[ -f "$f" ] || { echo "missing $f" >&2; exit 1; }
done
case "$(cat "$PUBKEY_FILE")" in
ssh-*|ecdsa-*|sk-ssh-*) ;;
*) echo "$PUBKEY_FILE does not look like an SSH public key" >&2; exit 1 ;;
esac
grep -q '^Include /etc/ssh/sshd_config.d/\*.conf' /etc/ssh/sshd_config \
|| { echo "sshd does not include sshd_config.d" >&2; exit 1; }
if ! id "$AGENT_USER" >/dev/null 2>&1; then
useradd --create-home --shell /bin/bash "$AGENT_USER"
fi
passwd -l "$AGENT_USER" >/dev/null 2>&1 || true
chmod 700 "$AGENT_HOME"
install -d -m 700 -o "$AGENT_USER" -g "$AGENT_USER" "$AGENT_HOME/.ssh"
# The stack must not be writable by the agent: a changed compose file would
# turn "restart the service" into arbitrary root containers.
install -d -m 755 -o root -g root "$PR_AGENT_DIR"
if [ ! -f "$PR_AGENT_DIR/docker-compose.yml" ]; then
install -m 644 -o root -g root "$SRC_DIR/docker-compose.yml" "$PR_AGENT_DIR/docker-compose.yml"
fi
install -m 644 -o root -g root "$SRC_DIR/README.md" "$PR_AGENT_DIR/README.md"
if [ ! -f "$PR_AGENT_DIR/.env" ]; then
install -m 600 -o root -g root "$SRC_DIR/.env" "$PR_AGENT_DIR/.env"
fi
if ! grep -q '^name:' "$PR_AGENT_DIR/docker-compose.yml"; then
sed -i '1i name: pr-agent' "$PR_AGENT_DIR/docker-compose.yml"
fi
cat > /usr/local/sbin/pr-agent-deploy <<EOF
#!/bin/sh
set -eu
$DOCKER compose --project-directory $PR_AGENT_DIR -f $PR_AGENT_DIR/docker-compose.yml pull
exec $DOCKER compose --project-directory $PR_AGENT_DIR -f $PR_AGENT_DIR/docker-compose.yml up -d
EOF
cat > /usr/local/sbin/pr-agent-logs <<EOF
#!/bin/sh
set -eu
exec $DOCKER compose --project-directory $PR_AGENT_DIR -f $PR_AGENT_DIR/docker-compose.yml logs --tail=200 pr-agent
EOF
cat > /usr/local/sbin/pr-agent-status <<EOF
#!/bin/sh
set -eu
exec $DOCKER compose --project-directory $PR_AGENT_DIR -f $PR_AGENT_DIR/docker-compose.yml ps
EOF
cat > /usr/local/sbin/pr-agent-control <<'EOF'
#!/bin/sh
set -eu
case "${SSH_ORIGINAL_COMMAND:-}" in
deploy) exec sudo -n /usr/local/sbin/pr-agent-deploy ;;
logs) exec sudo -n /usr/local/sbin/pr-agent-logs ;;
status) exec sudo -n /usr/local/sbin/pr-agent-status ;;
*) echo "allowed: deploy, logs, status" >&2; exit 1 ;;
esac
EOF
chown root:root /usr/local/sbin/pr-agent-deploy /usr/local/sbin/pr-agent-logs \
/usr/local/sbin/pr-agent-status /usr/local/sbin/pr-agent-control
chmod 755 /usr/local/sbin/pr-agent-deploy /usr/local/sbin/pr-agent-logs \
/usr/local/sbin/pr-agent-status /usr/local/sbin/pr-agent-control
cat > /etc/sudoers.d/pr-agent-control <<'EOF'
agent ALL=(root) NOPASSWD: /usr/local/sbin/pr-agent-deploy, /usr/local/sbin/pr-agent-logs, /usr/local/sbin/pr-agent-status
EOF
chmod 440 /etc/sudoers.d/pr-agent-control
visudo -cf /etc/sudoers.d/pr-agent-control >/dev/null
PUB=$(cat "$PUBKEY_FILE")
printf 'restrict,command="/usr/local/sbin/pr-agent-control" %s\n' "$PUB" \
> "$AGENT_HOME/.ssh/authorized_keys"
chown "$AGENT_USER:$AGENT_USER" "$AGENT_HOME/.ssh/authorized_keys"
chmod 600 "$AGENT_HOME/.ssh/authorized_keys"
cat > /etc/ssh/sshd_config.d/60-pr-agent-agent.conf <<'EOF'
Match User agent
ForceCommand /usr/local/sbin/pr-agent-control
PermitTTY no
AllowTcpForwarding no
X11Forwarding no
PermitTunnel no
AllowAgentForwarding no
EOF
chmod 644 /etc/ssh/sshd_config.d/60-pr-agent-agent.conf
if ! sshd -t; then
rm -f /etc/ssh/sshd_config.d/60-pr-agent-agent.conf
echo "sshd config test failed; removed the new file" >&2
exit 1
fi
systemctl reload ssh
cd "$PR_AGENT_DIR"
"$DOCKER" compose pull
"$DOCKER" compose up -d
sleep 5
"$DOCKER" compose ps
cat <<'EOF'
Agent access is ready. Test from the laptop:
ssh -i ~/.ssh/pr_agent_control -o IdentitiesOnly=yes agent@91.99.207.62 status
Old access is still in place. Only after that command works, run:
sudo bash /home/noel/agent-access-remove.sh
EOF