diff --git a/bootstrap/oci/README.md b/bootstrap/oci/README.md index 9129630..cbeb2d8 100644 --- a/bootstrap/oci/README.md +++ b/bootstrap/oci/README.md @@ -91,6 +91,13 @@ the edge, an authorized guest SSH key, and an allowed private subnet. It does not require the OCI guest agent and never falls back to public SSH. A fresh edge without Tailscale uses Bastion to bootstrap it before reconciling subnet routes. Both paths use bounded SSH preflight and preserve known host keys. + +Before creating a Bastion session, Jeannie checks Debian's direct public IPv4 +against the Bastion allowlist. If it changed, Jeannie adds the current `/32`, +preserves existing ranges, stores the resulting list in the foundation config, +and creates a new session. The old ranges remain authorized until reviewed and +removed deliberately. If the public address, Bastion ETag, or update cannot be +verified, the operation stops without opening a broader range. Routine applies no longer restart caches; use `./jeannie edge-cache-flush` when a cache reset is intended. diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 891a513..547fd7e 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -8444,6 +8444,43 @@ for rule in json.load(sys.stdin).get("data", []): done <<<"${nsg_ids}" } +oci_reconcile_bastion_client_access() { + local oci_bin="$1" bastion_id="$2" public_ip current etag updated match + LAB_BASTION_ALLOWLIST_CHANGED=false + public_ip="$(curl --noproxy '*' -4 -fsS --connect-timeout 5 --max-time 10 https://api.ipify.org)" || { + echo "Cannot determine Debian's public IPv4 address for OCI Bastion." >&2; return 1; + } + if ! python3 -c 'import ipaddress,sys +try: + ip=ipaddress.ip_address(sys.argv[1]); sys.exit(0 if ip.version==4 and ip.is_global else 1) +except ValueError: sys.exit(1)' "${public_ip}"; then + echo "Bastion client address is not a public IPv4 address." >&2; return 1 + fi + current="$("${oci_bin}" bastion bastion get --bastion-id "${bastion_id}" --output json)" || return 1 + match="$(printf '%s' "${current}" | python3 -c 'import ipaddress,json,sys +d=json.load(sys.stdin)["data"]; ip=ipaddress.ip_address(sys.argv[1]); networks=d.get("client-cidr-block-allow-list") or [] +print("yes" if any(ip in ipaddress.ip_network(n, strict=False) for n in networks) else "no")' "${public_ip}")" || return 1 + if [[ "${match}" == yes ]]; then + printf 'Bastion already permits Debian public IP %s.\n' "${public_ip}" + return 0 + fi + etag="$(printf '%s' "${current}" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("etag") or "")')" || return 1 + [[ -n "${etag}" ]] || { echo "Bastion response has no ETag; refusing to overwrite its allowlist." >&2; return 1; } + updated="$(printf '%s' "${current}" | python3 -c 'import json,sys +d=json.load(sys.stdin)["data"]; cidrs=d.get("client-cidr-block-allow-list") or [] +cidrs.append(sys.argv[1]+"/32"); print(json.dumps(cidrs))' "${public_ip}")" || return 1 + printf 'Adding Debian public IP %s/32 to OCI Bastion; preserving existing ranges.\n' "${public_ip}" + "${oci_bin}" bastion bastion update --bastion-id "${bastion_id}" \ + --client-cidr-list "${updated}" --if-match "${etag}" --force \ + --wait-for-state SUCCEEDED --max-wait-seconds 180 >/dev/null || return 1 + export LAB_OCI_BASTION_CLIENT_CIDRS_JSON="${updated}" + oci_foundation_save_config || { + echo "Bastion was updated, but saving its allowlist to Jeannie config failed." >&2; return 1; + } + LAB_BASTION_ALLOWLIST_CHANGED=true + echo "Bastion allowlist updated; a new session is required. Review old ranges when convenient." +} + prepare_oci_edge_bastion() { local bastion_id="${LAB_OCI_BASTION_ID:-}" local target_id="${LAB_OCI_EDGE_INSTANCE_ID:-}" @@ -8500,6 +8537,10 @@ except ValueError: echo "OCI CLI is required to create the edge Bastion session." >&2 return 1 fi + oci_reconcile_bastion_client_access "${oci_bin}" "${bastion_id}" || return 1 + if [[ "${LAB_BASTION_ALLOWLIST_CHANGED}" == true ]]; then + session_name="${session_name}-$(date +%s)" + fi bastion_host="host.bastion.${region}.oci.oraclecloud.com" session_error="$(mktemp)" @@ -8551,7 +8592,7 @@ except ValueError: export TF_VAR_edge_bastion_user="${session_id}" export TF_VAR_edge_bastion_port=22 export TF_VAR_edge_bastion_private_key_path="${edge_key}" - printf -v proxy_command 'ssh -i %q -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -W %%h:%%p -p 22 %s@%s' \ + printf -v proxy_command 'ssh -i %q -o ProxyCommand=none -o ProxyJump=none -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -W %%h:%%p -p 22 %s@%s' \ "${edge_key}" "${session_id}" "${bastion_host}" export LAB_OCI_EDGE_BASTION_PROXY_COMMAND="${proxy_command}" export LAB_OCI_EDGE_BASTION_SESSION_ID="${session_id}"