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
136 lines
4.5 KiB
Bash
Executable File
136 lines
4.5 KiB
Bash
Executable File
#!/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
|