rob-revisited/ide/tools/docker-ssh-e2e.sh

46 lines
3.2 KiB
Shell
Executable file

#!/usr/bin/env bash
# The Docker window's remote engine against real servers, all in Docker (ide/tests/DockerEngineTest.php testARealServer):
# a Docker-in-Docker engine, an sshd "server" whose user may use its socket, an sshd "bastion", and a client (PHP + the real
# ssh and docker clients) that runs the test twice: straight to the server, then through the bastion (-J). Everything is
# removed at the end. Usage: ide/tools/docker-ssh-e2e.sh (from the repository's root; pulls docker:27-dind, alpine, php)
set -euo pipefail
cd "$(dirname "$0")/../.."
P=jumphp-dse2e
cleanup() {
docker rm -f $P-dind $P-server $P-bastion >/dev/null 2>&1 || true
docker volume rm $P-sock $P-keys >/dev/null 2>&1 || true
docker network rm $P-net >/dev/null 2>&1 || true
}
trap cleanup EXIT
cleanup
docker network create $P-net >/dev/null
docker volume create $P-sock >/dev/null
docker volume create $P-keys >/dev/null
docker run -d --privileged --name $P-dind --network $P-net -e DOCKER_TLS_CERTDIR= -v $P-sock:/var/run docker:27-dind >/dev/null
docker run --rm -v $P-keys:/k alpine:3.20 sh -c 'apk add -q openssh-keygen && ssh-keygen -q -t ed25519 -N "" -f /k/id && chmod 644 /k/id /k/id.pub'
SSHD='apk add -q openssh >/dev/null && ssh-keygen -A >/dev/null && adduser -D -s /bin/sh deploy && echo "deploy:$(head -c 12 /dev/urandom | base64)" | chpasswd 2>/dev/null
mkdir -p /home/deploy/.ssh && cp /k/id.pub /home/deploy/.ssh/authorized_keys && chown -R deploy /home/deploy/.ssh && chmod 700 /home/deploy/.ssh
sed -i "/^AllowTcpForwarding/d;/^AllowStreamLocalForwarding/d" /etc/ssh/sshd_config
sed -i "1i AllowTcpForwarding yes\nAllowStreamLocalForwarding yes\nPasswordAuthentication no" /etc/ssh/sshd_config
if [ -d /var/run/dind ]; then
while [ ! -S /var/run/dind/docker.sock ]; do sleep 1; done
g=$(stat -c %g /var/run/dind/docker.sock); name=$(getent group "$g" | cut -d: -f1)
[ -n "$name" ] || { addgroup -g "$g" dsock; name=dsock; }
addgroup deploy "$name"; ln -sf /var/run/dind/docker.sock /var/run/docker.sock
fi
exec /usr/sbin/sshd -D -e'
docker run -d --name $P-server --hostname server --network $P-net -v $P-keys:/k:ro -v $P-sock:/var/run/dind alpine:3.20 sh -c "$SSHD" >/dev/null
docker run -d --name $P-bastion --hostname bastion --network $P-net -v $P-keys:/k:ro alpine:3.20 sh -c "$SSHD" >/dev/null
docker run --rm --network $P-net -v "$PWD":/w -w /w/ide -v $P-keys:/k:ro php:8.5-cli-alpine sh -c '
apk add -q openssh-client docker-cli >/dev/null
mkdir -p ~/.ssh && cp /k/id /tmp/id && chmod 600 /tmp/id && cp /tmp/id ~/.ssh/id_ed25519
for i in $(seq 90); do ssh-keyscan -T 2 server bastion > ~/.ssh/known_hosts 2>/dev/null
ssh-keygen -F server >/dev/null && ssh-keygen -F bastion >/dev/null && ssh -o BatchMode=yes -i /tmp/id deploy@server test -S /var/run/docker.sock 2>/dev/null && break; sleep 1; done
ssh -o BatchMode=yes -i /tmp/id deploy@server "id; ls -l /var/run/docker.sock /var/run/dind/docker.sock" || true
echo "== straight to the server"
JUMPHP_DOCKER_SSH_E2E=server:22:deploy:/tmp/id php vendor/bin/phpunit --filter "DockerEngineTest::testARealServer" --display-skipped
echo "== through the bastion (-J)"
JUMPHP_DOCKER_SSH_E2E=server:22:deploy:/tmp/id:deploy@bastion php vendor/bin/phpunit --filter "DockerEngineTest::testARealServer" --display-skipped
'