Route edge provisioning through OCI Bastion
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-28 20:35:04 -06:00
parent ee6f2789fd
commit 95f59e6771
3 changed files with 180 additions and 10 deletions

View File

@ -123,10 +123,14 @@ resource "null_resource" "edge_services" {
}
connection {
type = "ssh"
user = self.triggers.user
private_key = file(var.edge_ssh_key_path)
host = self.triggers.host
type = "ssh"
user = self.triggers.user
private_key = file(var.edge_ssh_key_path)
host = var.edge_ssh_host != "" ? var.edge_ssh_host : self.triggers.host
bastion_host = var.edge_bastion_host != "" ? var.edge_bastion_host : null
bastion_user = var.edge_bastion_user != "" ? var.edge_bastion_user : null
bastion_port = var.edge_bastion_port
bastion_private_key = var.edge_bastion_private_key_path != "" ? file(var.edge_bastion_private_key_path) : null
}
provisioner "remote-exec" {

View File

@ -18,6 +18,36 @@ variable "edge_ssh_key_path" {
default = "/home/jv/.ssh/id_ed25519"
}
variable "edge_ssh_host" {
description = "Private edge address used for SSH when provisioning through OCI Bastion."
type = string
default = ""
}
variable "edge_bastion_host" {
description = "OCI Bastion endpoint used as the SSH jump host."
type = string
default = ""
}
variable "edge_bastion_user" {
description = "OCI Bastion managed-session OCID used as the SSH jump user."
type = string
default = ""
}
variable "edge_bastion_port" {
description = "OCI Bastion SSH endpoint port."
type = number
default = 22
}
variable "edge_bastion_private_key_path" {
description = "Private key path used to authenticate the OCI Bastion jump connection."
type = string
default = ""
}
variable "edge_install_dir" {
type = string
default = "/opt/homelab-edge"

View File

@ -7983,6 +7983,12 @@ edge_apply() {
fi
refresh_oci_edge_endpoint
refresh_oci_edge_host_key
if [[ -n "${LAB_OCI_BASTION_ID:-}" && -n "${LAB_OCI_EDGE_INSTANCE_ID:-}" &&
"${LAB_OCI_BASTION_ENABLED:-true}" != "false" ]] &&
declare -F oci_enable_bastion_plugin >/dev/null 2>&1; then
oci_enable_bastion_plugin "${LAB_OCI_EDGE_INSTANCE_ID}"
fi
prepare_oci_edge_bastion
run_tofu_stack "bootstrap/edge"
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
@ -7997,6 +8003,7 @@ refresh_oci_edge_endpoint() {
local state_file="${LAB_OCI_FOUNDATION_STATE_FILE:-}"
local -a tofu_state_args=()
local discovered_edge_ip=""
local discovered_edge_private_ip=""
local discovered_edge_instance_id=""
local oci_bin=""
local vnic_id=""
@ -8004,6 +8011,12 @@ refresh_oci_edge_endpoint() {
local candidate_compartment=""
local candidate_id=""
# These are derived per run; never retain a proxy/private address from a
# previous long-lived API invocation.
unset TF_VAR_edge_ssh_host TF_VAR_edge_bastion_host TF_VAR_edge_bastion_user
unset TF_VAR_edge_bastion_port TF_VAR_edge_bastion_private_key_path
unset LAB_OCI_EDGE_BASTION_PROXY_COMMAND LAB_OCI_EDGE_BASTION_SESSION_ID
# A recreated OCI instance can receive a new public IP. Prefer the
# foundation output over inventory so a standalone `edge` run is
# idempotent instead of trying the previous address.
@ -8015,6 +8028,10 @@ refresh_oci_edge_endpoint() {
fi
discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)"
discovered_edge_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_instance_id 2>/dev/null || true)"
if [[ -z "${LAB_OCI_BASTION_ID:-}" ]]; then
LAB_OCI_BASTION_ID="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw bastion_id 2>/dev/null || true)"
[[ -n "${LAB_OCI_BASTION_ID}" && "${LAB_OCI_BASTION_ID}" != "null" ]] && export LAB_OCI_BASTION_ID
fi
# The API service may not inherit the interactive shell's OCI compartment
# variables. Recover the managed compartment from foundation state so a
# recreated edge can still be discovered without manual exports.
@ -8045,6 +8062,9 @@ refresh_oci_edge_endpoint() {
discovered_edge_ip="$("${oci_bin}" network vnic get \
--vnic-id "${vnic_id}" \
--query 'data."public-ip"' --raw-output 2>/dev/null || true)"
discovered_edge_private_ip="$("${oci_bin}" network vnic get \
--vnic-id "${vnic_id}" \
--query 'data."private-ip"' --raw-output 2>/dev/null || true)"
fi
fi
fi
@ -8077,6 +8097,9 @@ refresh_oci_edge_endpoint() {
discovered_edge_ip="$("${oci_bin}" network vnic get \
--vnic-id "${vnic_id}" \
--query 'data."public-ip"' --raw-output 2>/dev/null || true)"
discovered_edge_private_ip="$("${oci_bin}" network vnic get \
--vnic-id "${vnic_id}" \
--query 'data."private-ip"' --raw-output 2>/dev/null || true)"
fi
break
fi
@ -8097,6 +8120,14 @@ refresh_oci_edge_endpoint() {
fi
export LAB_EDGE_HOST="${discovered_edge_ip}"
export TF_VAR_edge_host="${discovered_edge_ip}"
if [[ -n "${discovered_edge_private_ip}" && "${discovered_edge_private_ip}" != "null" &&
-n "${LAB_OCI_BASTION_ID:-}" && "${LAB_OCI_BASTION_ENABLED:-true}" != "false" ]]; then
export TF_VAR_edge_ssh_host="${discovered_edge_private_ip}"
export LAB_OCI_EDGE_PRIVATE_IP="${discovered_edge_private_ip}"
printf 'OCI edge private address discovered: %s (Bastion session will be prepared before SSH)\n' "${discovered_edge_private_ip}"
else
unset TF_VAR_edge_ssh_host LAB_OCI_EDGE_PRIVATE_IP
fi
if [[ -n "${discovered_edge_instance_id}" && "${discovered_edge_instance_id}" != "null" ]]; then
export LAB_OCI_EDGE_INSTANCE_ID="${discovered_edge_instance_id}"
fi
@ -8105,8 +8136,95 @@ refresh_oci_edge_endpoint() {
fi
}
prepare_oci_edge_bastion() {
local bastion_id="${LAB_OCI_BASTION_ID:-}"
local target_id="${LAB_OCI_EDGE_INSTANCE_ID:-}"
local target_host="${TF_VAR_edge_ssh_host:-}"
local target_user="${TF_VAR_edge_user:-${LAB_EDGE_USER:-ubuntu}}"
local edge_key="${TF_VAR_edge_ssh_key_path:-${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}}}"
local public_key="${edge_key}.pub"
local region="${LAB_OCI_REGION:-}"
local session_ttl="${LAB_OCI_BASTION_SESSION_TTL:-3600}"
local bastion_host
local oci_bin
local session_json=""
local session_id=""
local session_error
local attempt
local proxy_command
unset TF_VAR_edge_bastion_host TF_VAR_edge_bastion_user TF_VAR_edge_bastion_port
unset TF_VAR_edge_bastion_private_key_path LAB_OCI_EDGE_BASTION_PROXY_COMMAND
unset LAB_OCI_EDGE_BASTION_SESSION_ID
if [[ "${LAB_OCI_BASTION_ENABLED:-true}" == "false" || -z "${bastion_id}" ]]; then
return 0
fi
if [[ "${bastion_id}" != ocid1.bastion.* ]]; then
echo "Invalid OCI Bastion OCID '${bastion_id}'; refusing direct public SSH fallback." >&2
return 1
fi
if [[ -z "${target_id}" || -z "${target_host}" ]]; then
echo "OCI Bastion is enabled, but the managed edge instance/private address was not discovered." >&2
return 1
fi
if [[ -z "${region}" ]]; then
echo "LAB_OCI_REGION is required to create an OCI Bastion session for edge SSH." >&2
return 1
fi
if [[ ! -r "${edge_key}" || ! -r "${public_key}" ]]; then
echo "OCI Bastion edge SSH key pair is not readable: ${edge_key}" >&2
return 1
fi
if ! declare -F oci_cli_resolve >/dev/null 2>&1 || ! oci_bin="$(oci_cli_resolve 2>/dev/null)"; then
echo "OCI CLI is required to create the edge Bastion session." >&2
return 1
fi
bastion_host="host.bastion.${region}.oci.oraclecloud.com"
session_error="$(mktemp)"
for attempt in 1 2 3 4 5 6; do
session_json="$(${oci_bin} bastion session create-managed-ssh \
--bastion-id "${bastion_id}" \
--target-resource-id "${target_id}" \
--target-os-username "${target_user}" \
--ssh-public-key-file "${public_key}" \
--session-ttl "${session_ttl}" \
--wait-for-state SUCCEEDED --output json 2>"${session_error}" || true)"
if [[ -n "${session_json}" ]]; then
session_id="$(printf '%s' "${session_json}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["id"])' 2>/dev/null || true)"
fi
if [[ -n "${session_id}" && "${session_id}" != "null" ]]; then
break
fi
if (( attempt < 6 )); then
printf 'Waiting for OCI Bastion plugin/session readiness (retry %d/6)...\n' "${attempt}" >&2
sleep 10
fi
done
if [[ -z "${session_id}" || "${session_id}" == "null" ]]; then
echo "Unable to create an OCI Bastion managed SSH session for the edge instance." >&2
sed -n '1,80p' "${session_error}" >&2 || true
rm -f "${session_error}"
return 1
fi
rm -f "${session_error}"
export TF_VAR_edge_bastion_host="${bastion_host}"
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' \
"${edge_key}" "${session_id}" "${bastion_host}"
export LAB_OCI_EDGE_BASTION_PROXY_COMMAND="${proxy_command}"
export LAB_OCI_EDGE_BASTION_SESSION_ID="${session_id}"
printf 'Using OCI Bastion session %s for edge SSH via %s (target %s).\n' \
"${session_id}" "${bastion_host}" "${target_host}"
}
refresh_oci_edge_host_key() {
local edge_host="${LAB_EDGE_HOST:-${TF_VAR_edge_host:-}}"
local edge_private_host="${TF_VAR_edge_ssh_host:-}"
local known_hosts="${HOME}/.ssh/known_hosts"
# OCI instances may be destroyed and recreated with the same public IP.
@ -8117,11 +8235,26 @@ refresh_oci_edge_host_key() {
fi
if [[ -f "${known_hosts}" ]] && command -v ssh-keygen >/dev/null 2>&1; then
ssh-keygen -R "${edge_host}" -f "${known_hosts}" >/dev/null 2>&1 || true
if [[ -n "${edge_private_host}" && "${edge_private_host}" != "${edge_host}" ]]; then
ssh-keygen -R "${edge_private_host}" -f "${known_hosts}" >/dev/null 2>&1 || true
fi
fi
}
edge_ssh_args() {
local edge_key="${TF_VAR_edge_ssh_key_path:-${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}}}"
EDGE_SSH_ARGS=(-i "${edge_key}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new)
if [[ -n "${LAB_OCI_EDGE_BASTION_PROXY_COMMAND:-}" ]]; then
if [[ "${LAB_OCI_EDGE_BASTION_PROXY_COMMAND}" == *'%h:%p'* ]]; then
EDGE_SSH_ARGS+=( -o "ProxyCommand=${LAB_OCI_EDGE_BASTION_PROXY_COMMAND}" )
else
EDGE_SSH_ARGS+=( -o "ProxyCommand=${LAB_OCI_EDGE_BASTION_PROXY_COMMAND} %h %p" )
fi
fi
}
edge_flush_caches() {
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
local edge_host="${TF_VAR_edge_ssh_host:-${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}}"
local edge_user="${TF_VAR_edge_user:-${LAB_EDGE_USER:-ubuntu}}"
local edge_key="${TF_VAR_edge_ssh_key_path:-${LAB_EDGE_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}"
local edge_install_dir="${TF_VAR_edge_install_dir:-${LAB_EDGE_INSTALL_DIR:-/opt/homelab-edge}}"
@ -8134,7 +8267,8 @@ edge_flush_caches() {
fi
echo "Flushing edge caches on ${edge_user}@${edge_host}:${edge_install_dir}..."
ssh -i "${edge_key}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new "${edge_user}@${edge_host}" "set -eu
edge_ssh_args
ssh "${EDGE_SSH_ARGS[@]}" "${edge_user}@${edge_host}" "set -eu
cd '${edge_install_dir}'
if ! sudo docker compose ps >/dev/null 2>&1; then
echo 'Edge docker compose stack is not available.' >&2
@ -8173,7 +8307,7 @@ ensure_edge_tailscale_routes() {
local rpi_host="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}"
local rpi_user="${LAB_RPI_USER:-${LAB_RASPBERRY_USER:-jv}}"
local rpi_key="${LAB_RPI_SSH_KEY_PATH:-${LAB_RASPBERRY_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}"
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
local edge_host="${TF_VAR_edge_ssh_host:-${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}}"
local edge_user="${TF_VAR_edge_user:-${LAB_EDGE_USER:-ubuntu}}"
local edge_key="${TF_VAR_edge_ssh_key_path:-${LAB_EDGE_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}"
@ -8313,7 +8447,8 @@ sudo tailscale set --advertise-routes='${route}'
;;
esac
ssh -i "${edge_key}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new "${edge_user}@${edge_host}" "set -eu
edge_ssh_args
ssh "${EDGE_SSH_ARGS[@]}" "${edge_user}@${edge_host}" "set -eu
if ! command -v tailscale >/dev/null 2>&1; then
echo 'tailscale is not installed on the OCI edge host.' >&2
exit 1
@ -8358,7 +8493,7 @@ write_edge_haproxy_stats_credentials() {
}
recover_edge_haproxy_stats_password() {
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
local edge_host="${TF_VAR_edge_ssh_host:-${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}}"
local edge_user="${TF_VAR_edge_user:-${LAB_EDGE_USER:-ubuntu}}"
local edge_key="${TF_VAR_edge_ssh_key_path:-${LAB_EDGE_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}"
local edge_install_dir="${TF_VAR_edge_install_dir:-${LAB_EDGE_INSTALL_DIR:-/opt/homelab-edge}}"
@ -8369,7 +8504,8 @@ recover_edge_haproxy_stats_password() {
return 1
fi
remote_auth="$(ssh -i "${edge_key}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new "${edge_user}@${edge_host}" \
edge_ssh_args
remote_auth="$(ssh "${EDGE_SSH_ARGS[@]}" "${edge_user}@${edge_host}" \
"sudo sed -n 's/^[[:space:]]*stats auth //p' '${edge_install_dir}/config_files/haproxy.cfg' | head -n 1" 2>/dev/null || true)"
remote_auth="${remote_auth//$'\r'/}"
if [[ "${remote_auth}" != *:* ]]; then