Files
computer-playbook/scripts/system/tls/trust/wsl2.sh
kevinveenbirkenbachandClaude Opus 5 af605b049f refactor(domains): give local testing a .test domain, leave .example to docs
RFC 6761 reserves .test for testing and .example for documentation, and the
project's own domain was doing both jobs under the documentation name. It is
what default.env hands every local deploy and what the test fixtures assert
against, so it is a testing name.

Rename it wherever it stands for the running machine: the environment
defaults, the development inventory, the workspace and trust scripts, the test
suites, and the four documents that describe real behaviour - the DNS mapping
to loopback, the browser CA trust, WSL2 resolution. Those documents would have
started lying the moment default.env changed.

The sandbox allowlist moves with them, since egress is matched per pattern and
nothing resolves the old name any more, and the sandbox document that mirrors
that list follows it.

Two places keep .example on purpose: the link-style sample in the
documentation guide, which is documentation about documentation, and every
generic fixture name elsewhere in the suite - a.example, foo.example and their
kin. Those are a larger sweep of their own and are not touched here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 13:10:34 +02:00

83 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=scripts/meta/env/load.sh
source "${SCRIPT_DIR}/../../../meta/env/load.sh"
[[ "${INFINITO_IS_WSL2}" == "true" ]] || exit 0
CONTAINER="${INFINITO_CONTAINER}"
CA_SRC="${INFINITO_CA_CERT_HOST}"
CA_NAME="infinito-root-ca.crt"
if [[ -z "${CA_SRC}" ]]; then
echo ">>> ERROR: INFINITO_CA_CERT_HOST is not set" >&2
exit 2
fi
WIN_USER=""
for _dir in /mnt/c/Users/*/; do
_name="${_dir%/}"
_name="${_name##*/}"
case "${_name}" in
"All Users" | "Default" | "Default User" | "Public" | "desktop.ini") continue ;;
esac
WIN_USER="${_name}"
break
done
WIN_DOWNLOADS="/mnt/c/Users/${WIN_USER}/Downloads"
WIN_CA_PATH="${WIN_DOWNLOADS}/${CA_NAME}"
WIN_CA_PATH_WIN="C:\\Users\\${WIN_USER}\\Downloads\\${CA_NAME}"
POWERSHELL="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
if [[ ! -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then
echo ">>> Registering WSL interop binfmt handler"
printf ':WSLInterop:M::MZ::/init:PF\n' | sudo tee /proc/sys/fs/binfmt_misc/register >/dev/null
fi
echo ">>> Extracting CA from container: ${CONTAINER}"
# nocheck: container-cp - container-to-host CA extraction on the operator host
docker cp "${CONTAINER}:${CA_SRC}" "${WIN_CA_PATH}"
echo ">>> CA copied to Windows: ${WIN_CA_PATH_WIN}"
echo ">>> Importing CA into Windows CurrentUser trust store (no admin required)"
"${POWERSHELL}" -NonInteractive -Command "
\$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2('${WIN_CA_PATH_WIN}')
\$store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root','CurrentUser')
\$store.Open('ReadWrite')
\$store.Add(\$cert)
\$store.Close()
" >/dev/null
echo ">>> CA trusted in Windows (Chrome/Edge will trust the deployed vhosts)"
echo ">>> Discovering domains from nginx config"
DOMAINS=$(docker exec "${CONTAINER}" bash -c \
"find /etc/nginx/conf.d/servers -name '*.conf' | xargs grep -h 'server_name' 2>/dev/null | grep -v '#' | awk '{print \$2}' | tr -d ';' | sort -u")
echo ">>> Updating Windows hosts file (requires one UAC confirmation)"
WIN_TEMP="/mnt/c/Users/${WIN_USER}/AppData/Local/Temp"
PS1_FILE="${WIN_TEMP}/infinito-hosts-setup.ps1"
PS1_FILE_WIN="C:\\Users\\${WIN_USER}\\AppData\\Local\\Temp\\infinito-hosts-setup.ps1"
HOSTS_FILE='C:\Windows\System32\drivers\etc\hosts'
# nocheck: hardcoded-primary-domain the marker is matched verbatim against what an earlier run wrote into the Windows hosts file; deriving it would orphan that block instead of replacing it
MARKER='# infinito.test --- managed by infinito-nexus'
# shellcheck disable=SC2016
{
printf '$hostsFile = "%s"\n' "${HOSTS_FILE}"
printf '$marker = "%s"\n' "${MARKER}"
printf '$content = Get-Content $hostsFile -Raw -ErrorAction SilentlyContinue\n'
printf 'if ($content -notmatch [regex]::Escape($marker)) {\n'
printf ' $entries = "`r`n%s`r`n"\n' "${MARKER}"
while IFS= read -r domain; do
[[ -z "${domain}" ]] && continue
printf ' $entries += "127.0.0.1 %s`r`n"\n' "${domain}"
done <<<"${DOMAINS}"
printf ' Add-Content $hostsFile $entries\n'
printf '}\n'
} >"${PS1_FILE}"
"${POWERSHELL}" -NonInteractive -Command "Start-Process powershell -Verb RunAs -Wait -ArgumentList '-NonInteractive -File ${PS1_FILE_WIN}'"
echo ">>> Windows hosts file updated"
echo ">>> Done — restart your browser to apply CA trust"