21 lines
1007 B
Bash
21 lines
1007 B
Bash
#!/bin/sh
|
|||
|
|
# Run as root after reviewing the deployment privilege scope.
|
||
|
|
set -eu
|
||
|
|
[ "$(id -u)" -eq 0 ] || { echo "Run as root" >&2; exit 1; }
|
||
|
|
key=/home/codex/myfileagent-gitea/secrets/assignment8-deploy.pub
|
||
|
|
[ -s "$key" ] || { echo "Deployment public key missing" >&2; exit 1; }
|
||
|
|
if ! id a8deploy >/dev/null 2>&1; then useradd -m -s /bin/bash a8deploy; fi
|
||
|
|
home=$(getent passwd a8deploy | cut -d: -f6)
|
||
|
|
[ "$home" = /home/a8deploy ] || { echo "Unexpected account home" >&2; exit 1; }
|
||
|
|
install -d -o a8deploy -g a8deploy -m 700 "$home/.ssh"
|
||
|
|
touch "$home/.ssh/authorized_keys"
|
||
|
|
chown a8deploy:a8deploy "$home/.ssh/authorized_keys"
|
||
|
|
chmod 600 "$home/.ssh/authorized_keys"
|
||
|
|
grep -qxF "$(cat "$key")" "$home/.ssh/authorized_keys" || cat "$key" >> "$home/.ssh/authorized_keys"
|
||
|
|
tmp=$(mktemp)
|
||
|
|
trap 'rm -f "$tmp"' EXIT
|
||
|
|
printf '%s\n' 'a8deploy ALL=(root) NOPASSWD: ALL' > "$tmp"
|
||
|
|
visudo -cf "$tmp" >/dev/null
|
||
|
|
install -o root -g root -m 440 "$tmp" /etc/sudoers.d/90-assignment8-deploy
|
||
|
|
echo 'Assignment 8 deploy account prepared.'
|