|
| 1 | +uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}') |
| 2 | + |
| 3 | +PREHOOK=$(cat <<'OUTER' |
| 4 | +#!/usr/bin/env bash |
| 5 | +set -Eeuo pipefail |
| 6 | + |
| 7 | +### ===== User-tunable variables ===== |
| 8 | + |
| 9 | +export NETWORK_NAME="podman" |
| 10 | +export SUBNET_CIDR="10.88.0.0/16" |
| 11 | +export GATEWAY_IP="10.88.0.1" |
| 12 | + |
| 13 | +export SQUID_IP="10.88.0.10" |
| 14 | +export SQUID_CONTAINER="http-proxy" |
| 15 | +export SQUID_IMAGE="docker.io/ubuntu/squid:latest" |
| 16 | +export SQUID_HTTP_PORT="3129" |
| 17 | + |
| 18 | +export WORKDIR="$PWD/podman-transparent-proxy-lab" |
| 19 | + |
| 20 | +### ===== Derived variables ===== |
| 21 | + |
| 22 | +SQUID_CONF_DIR="/etc/squid" |
| 23 | + |
| 24 | +mkdir -p "$SQUID_CONF_DIR" |
| 25 | + |
| 26 | +echo "==> Checking dependencies" |
| 27 | +command -v podman >/dev/null |
| 28 | +command -v sudo >/dev/null |
| 29 | +command -v iptables >/dev/null |
| 30 | + |
| 31 | +echo "==> Writing squid.conf" |
| 32 | +cat > "${SQUID_CONF_DIR}/squid.conf" <<INNER |
| 33 | +http_port 3128 |
| 34 | +http_port 3129 intercept |
| 35 | + |
| 36 | +acl localnet src ${SUBNET_CIDR} |
| 37 | +acl allowed_http dstdomain example.com |
| 38 | + |
| 39 | +acl Safe_ports port 80 |
| 40 | +acl CONNECT method CONNECT |
| 41 | + |
| 42 | +http_access deny !Safe_ports |
| 43 | +http_access allow localnet allowed_http |
| 44 | +http_access deny all |
| 45 | + |
| 46 | +pid_filename /tmp/squid.pid |
| 47 | +coredump_dir /tmp |
| 48 | +cache_log /dev/stderr |
| 49 | +access_log stdio:/dev/stdout |
| 50 | +# cache_dir ufs ${SQUID_CONF_DIR}/cache 100 16 256 |
| 51 | + |
| 52 | +cache deny all |
| 53 | +via off |
| 54 | +forwarded_for delete |
| 55 | +request_header_access X-Forwarded-For deny all |
| 56 | +request_header_access Via deny all |
| 57 | +visible_hostname localhost |
| 58 | + |
| 59 | +INNER |
| 60 | + |
| 61 | +echo "==> Creating Podman network if needed" |
| 62 | +if ! podman network exists "${NETWORK_NAME}"; then |
| 63 | + podman network create \ |
| 64 | + --subnet "${SUBNET_CIDR}" \ |
| 65 | + --gateway "${GATEWAY_IP}" \ |
| 66 | + "${NETWORK_NAME}" |
| 67 | +fi |
| 68 | + |
| 69 | +echo "==> Removing old containers if present" |
| 70 | +podman rm -f "${SQUID_CONTAINER}" 2>/dev/null || true |
| 71 | + |
| 72 | +echo "==> Preparing Squid cache and logs" |
| 73 | +mkdir -p "${SQUID_CONF_DIR}/cache" "${SQUID_CONF_DIR}/logs" |
| 74 | + |
| 75 | +echo "==> Starting Squid HTTP transparent proxy" |
| 76 | +podman run -d \ |
| 77 | + --name "${SQUID_CONTAINER}" \ |
| 78 | + --network host \ |
| 79 | + --entrypoint sh \ |
| 80 | + -v "${SQUID_CONF_DIR}/:/etc/squid/:Z,rw,rbind" \ |
| 81 | + "${SQUID_IMAGE}" \ |
| 82 | + -c 'exec squid -N -f /etc/squid/squid.conf' |
| 83 | + |
| 84 | +echo "==> Enabling IPv4 forwarding on host" |
| 85 | +sudo sysctl -w net.ipv4.ip_forward=1 >/dev/null |
| 86 | + |
| 87 | +echo "==> Preparing iptables chain" |
| 88 | +sudo iptables -t nat -N SQUID_PROXY 2>/dev/null || true |
| 89 | +sudo iptables -t nat -F SQUID_PROXY |
| 90 | + |
| 91 | +sudo iptables -t nat -A SQUID_PROXY -d 127.0.0.0/8 -j RETURN |
| 92 | +sudo iptables -t nat -A SQUID_PROXY -d ${SUBNET_CIDR} -j RETURN |
| 93 | +sudo iptables -t nat -A SQUID_PROXY -p tcp --dport 80 -j REDIRECT --to-ports 3129 |
| 94 | + |
| 95 | +sudo iptables -t nat -A PREROUTING -s ${SUBNET_CIDR} -p tcp -j SQUID_PROXY |
| 96 | +OUTER |
| 97 | +) |
| 98 | + |
| 99 | +ibmcloud ce fleet create --name "fleet-${uuid}" \ |
| 100 | + --tasks-state-store fleet-task-store \ |
| 101 | + --tasks 1 \ |
| 102 | + --image registry.access.redhat.com/ubi10/ubi-minimal \ |
| 103 | + --cpu "2" \ |
| 104 | + --memory "4G" \ |
| 105 | + --command curl --argument "--silent" --argument "--write-out" --argument "%{http_code}" --argument "--output" --argument "/dev/null" --argument "http://example.com" \ |
| 106 | + --max-scale 1 \ |
| 107 | + --retrylimit 0 \ |
| 108 | + --subnetpool-name fleet-subnetpool \ |
| 109 | + --env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}" |
| 110 | + |
0 commit comments