interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / reach-reject.sh

67 lines · 2865 bytes Raw Blame History
1#!/usr/bin/env bash
2# Negative control for smoke.sh reach. A name or address that is not safe to paste into a
3# remote shell is refused before dig, ssh or curl run. A real IPv4 address is not refused,
4# and the certificate check passes the name as an argument instead of inside the script.
5# bash infra/test/reach-reject.sh
6set -u
7INFRA=$(cd "$(dirname "$0")/.." && pwd)
8T=$(mktemp -d)
9trap 'rm -rf "$T"' EXIT
10pass=0; fail=0
11ok() { pass=$((pass+1)); echo "PASS $*"; }
12bad() { fail=$((fail+1)); echo "FAIL $*"; }
13MARKER=$T/called
14mkdir -p "$T/bin"
15cat > "$T/bin/stub" <<EOF
16#!/bin/sh
17printf '%s\n' "\$0 \$*" >> "$MARKER"
18exit 0
19EOF
20chmod 755 "$T/bin/stub"
21ln -s stub "$T/bin/dig"
22ln -s stub "$T/bin/ssh"
23ln -s stub "$T/bin/curl"
24
25reject() { # reject LABEL EXPECT RC-CHECK...
26 local label=$1 expect=$2; shift 2
27 : > "$MARKER"
28 rm -f "$T/pwned"
29 out=$(PATH="$T/bin:$PATH" REACH_OUTSIDE=0 timeout 10 "$@" 2>&1); rc=$?
30 if [ "$rc" = 3 ] && printf '%s\n' "$out" | grep -q "$expect" && [ ! -s "$MARKER" ] && [ ! -e "$T/pwned" ]; then
31 ok "$label"
32 else
33 bad "$label rc $rc marker=$(wc -c < "$MARKER" 2>/dev/null || echo missing) out=$(printf '%s' "$out" | tail -n 1)"
34 fi
35}
36
37SMOKE=(bash "$INFRA/smoke.sh" reach)
38reject "quote in name refused, nothing queried" "not a DNS name" "${SMOKE[@]}" "x';touch $T/pwned;echo '"
39reject "space in name refused, nothing queried" "not a DNS name" "${SMOKE[@]}" "bad name"
40reject "quote in REACH_ADDR refused, nothing queried" "not an IPv4 address" \
41 env REACH_ADDR="1.2.3.4'" "${SMOKE[@]}" interplane.io
42reject "short REACH_ADDR refused, nothing queried" "not an IPv4 address" \
43 env REACH_ADDR=1.2.3 "${SMOKE[@]}" interplane.io
44reject "dot-only REACH_ADDR refused, nothing queried" "not an IPv4 address" \
45 env REACH_ADDR=.... "${SMOKE[@]}" interplane.io
46reject "octet 999 refused, nothing queried" "not an IPv4 address" \
47 env REACH_ADDR=999.1.1.1 "${SMOKE[@]}" interplane.io
48reject "five-part REACH_ADDR refused, nothing queried" "not an IPv4 address" \
49 env REACH_ADDR=192.0.2.1.9 "${SMOKE[@]}" interplane.io
50
51: > "$MARKER"
52out=$(PATH="$T/bin:$PATH" REACH_OUTSIDE=0 REACH_ADDR=192.0.2.1 timeout 15 bash "$INFRA/smoke.sh" reach interplane.io 2>&1); rc=$?
53if grep -q '/ssh ' "$MARKER" && ! grep -q '/dig ' "$MARKER"; then
54 ok "valid IPv4 is queried (ssh called, dig not)"
55else
56 bad "valid IPv4 query shape rc $rc marker=$(cat "$MARKER")"
57fi
58if grep -q 'servername' "$MARKER" && grep -q '\$2' "$MARKER" && ! grep -q "servername 'interplane.io'" "$MARKER" \
59 && grep -q '192.0.2.1' "$MARKER"; then
60 ok "cert check passes address and name as arguments"
61else
62 bad "cert command was interpolated: $(grep -m1 servername "$MARKER" || echo none)"
63fi
64[ "$rc" = 3 ] && ok "dead address still fails reach (rc 3)" || bad "dead address rc $rc (want 3)"
65
66echo "reach-reject: $pass passed, $fail failed"
67[ "$fail" = 0 ]