Allow Bastion SSH to managed edge
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 21:58:27 -06:00
parent b5d72aad3a
commit 8584355afc
2 changed files with 85 additions and 1 deletions

View File

@ -3604,7 +3604,7 @@ oci_foundation_config_names=(
LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR
LAB_OCI_BACKUP_BUCKET_ENABLED LAB_OCI_BACKUP_BUCKET LAB_OCI_BACKUP_BUCKET_NAME LAB_OCI_BACKUP_BUCKET_NAMESPACE LAB_OCI_BACKUP_BUCKET_ID
LAB_OCI_BASTION_ENABLED LAB_OCI_BASTION_NAME LAB_OCI_BASTION_TARGET_SUBNET_ID LAB_OCI_BASTION_SSH_SOURCE_CIDR
LAB_OCI_BASTION_CLIENT_CIDRS_JSON LAB_OCI_BASTION_MAX_SESSION_TTL LAB_OCI_BASTION_ID LAB_OCI_EDGE_INSTANCE_ID LAB_OCI_EDGE_INSTANCE_MANAGED
LAB_OCI_BASTION_CLIENT_CIDRS_JSON LAB_OCI_BASTION_MAX_SESSION_TTL LAB_OCI_BASTION_ID LAB_OCI_EDGE_COMPARTMENT_ID LAB_OCI_EDGE_INSTANCE_ID LAB_OCI_EDGE_INSTANCE_MANAGED
LAB_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET
)

View File

@ -7982,6 +7982,7 @@ edge_apply() {
export TF_VAR_edge_ssh_key_path="${configured_edge_key}"
fi
refresh_oci_edge_endpoint || return 1
oci_ensure_edge_bastion_ingress || return 1
refresh_oci_edge_host_key
if [[ -n "${LAB_OCI_BASTION_ID:-}" && -n "${LAB_OCI_EDGE_INSTANCE_ID:-}" &&
"${LAB_OCI_BASTION_ENABLED:-true}" != "false" ]] &&
@ -8240,11 +8241,94 @@ for item in json.load(sys.stdin).get("data", []):
if [[ "${discovered_edge_instance_id}" == ocid1.instance.* ]]; then
export LAB_OCI_EDGE_INSTANCE_ID="${discovered_edge_instance_id}"
fi
if [[ "${instance_compartment}" == ocid1.compartment.* ]]; then
export LAB_OCI_EDGE_COMPARTMENT_ID="${instance_compartment}"
fi
if declare -F oci_foundation_save_config >/dev/null 2>&1; then
oci_foundation_save_config
fi
}
oci_ensure_edge_bastion_ingress() {
local oci_bin=""
local edge_instance="${LAB_OCI_EDGE_INSTANCE_ID:-}"
local edge_compartment="${LAB_OCI_EDGE_COMPARTMENT_ID:-${LAB_OCI_COMPARTMENT_ID:-}}"
local vnic_id=""
local nsg_ids=""
local bastion_subnet="${LAB_OCI_BASTION_TARGET_SUBNET_ID:-}"
local source_cidr="${LAB_OCI_BASTION_SSH_SOURCE_CIDR:-}"
local nsg rule_file has_rule
if [[ "${LAB_OCI_BASTION_ENABLED:-true}" == "false" || -z "${LAB_OCI_BASTION_ID:-}" ]]; then
return 0
fi
if [[ -z "${edge_instance}" || -z "${edge_compartment}" ]]; then
echo "Cannot ensure Bastion ingress: edge instance compartment is unknown." >&2
return 1
fi
oci_bin="$(oci_cli_resolve 2>/dev/null || true)"
if [[ -z "${oci_bin}" ]]; then
echo "OCI CLI is required to ensure Bastion ingress." >&2
return 1
fi
if [[ -z "${bastion_subnet}" ]]; then
bastion_subnet="$("${oci_bin}" bastion bastion get \
--bastion-id "${LAB_OCI_BASTION_ID}" \
--query 'data."target-subnet-id"' --raw-output 2>/dev/null || true)"
fi
if [[ -z "${source_cidr}" && "${bastion_subnet}" != "null" ]]; then
source_cidr="$("${oci_bin}" network subnet get \
--subnet-id "${bastion_subnet}" \
--query 'data."cidr-block"' --raw-output 2>/dev/null || true)"
fi
if [[ -z "${source_cidr}" || "${source_cidr}" == "null" ]]; then
echo "Cannot ensure Bastion ingress: target subnet CIDR is unavailable." >&2
return 1
fi
vnic_id="$("${oci_bin}" compute vnic-attachment list \
--compartment-id "${edge_compartment}" \
--instance-id "${edge_instance}" \
--query 'data[0]."vnic-id"' --raw-output 2>/dev/null || true)"
if [[ -z "${vnic_id}" || "${vnic_id}" == "null" ]]; then
echo "Cannot ensure Bastion ingress: edge VNIC was not found." >&2
return 1
fi
nsg_ids="$("${oci_bin}" network vnic get --vnic-id "${vnic_id}" --output json 2>/dev/null \
| python3 -c 'import json,sys
data=json.load(sys.stdin).get("data", {})
print("\\n".join(data.get("nsg-ids") or []))' 2>/dev/null || true)"
if [[ -z "${nsg_ids}" ]]; then
echo "Cannot ensure Bastion ingress: edge VNIC has no NSG." >&2
return 1
fi
while IFS= read -r nsg; do
[[ -n "${nsg}" ]] || continue
has_rule="$("${oci_bin}" network nsg rules list \
--nsg-id "${nsg}" --direction INGRESS --all --output json 2>/dev/null \
| SOURCE_CIDR="${source_cidr}" python3 -c 'import json,os,sys
for rule in json.load(sys.stdin).get("data", []):
tcp=rule.get("tcpOptions") or {}
port=tcp.get("destinationPortRange") or {}
if (rule.get("direction") == "INGRESS" and rule.get("protocol") == "6" and
rule.get("source") in (os.environ["SOURCE_CIDR"], "0.0.0.0/0") and
str(port.get("min")) == "22" and str(port.get("max")) == "22"):
print("yes")
break' 2>/dev/null || true)"
[[ "${has_rule}" == yes ]] && continue
rule_file="$(mktemp)"
printf '[{"description":"Jeannie Bastion SSH access","direction":"INGRESS","protocol":"6","source":"%s","sourceType":"CIDR_BLOCK","tcpOptions":{"destinationPortRange":{"min":22,"max":22}}}]\n' \
"${source_cidr}" >"${rule_file}"
if ! "${oci_bin}" network nsg rules add --nsg-id "${nsg}" \
--security-rules "file://${rule_file}" >/dev/null 2>&1; then
rm -f "${rule_file}"
echo "Failed to add Bastion TCP/22 ingress to NSG ${nsg}." >&2
return 1
fi
rm -f "${rule_file}"
printf 'Added Bastion TCP/22 ingress to edge NSG %s from %s.\n' "${nsg}" "${source_cidr}"
done <<<"${nsg_ids}"
}
prepare_oci_edge_bastion() {
local bastion_id="${LAB_OCI_BASTION_ID:-}"
local target_id="${LAB_OCI_EDGE_INSTANCE_ID:-}"