feat: add OCI Bastion managed SSH access
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-27 08:03:35 -06:00
parent 82b6895151
commit 36730361d5
9 changed files with 204 additions and 13 deletions

View File

@ -72,3 +72,28 @@ The update is enabled by default when a token is present. Set
(Postfix, Dovecot, fail2ban, UFW, and node-exporter) is also installed
automatically after the new VM accepts SSH; set
`LAB_OCI_AUTO_CONFIGURE_EMAIL=false` to defer that step.
The foundation also creates an OCI Bastion service by default. It is a
service-managed, time-limited SSH entry point and does not consume a compute
VM. Its client allow-list defaults to `LAB_OCI_ADMIN_CIDR`. For a new VCN,
Jeannie permits SSH from the Bastion target subnet to the managed edge and
email instances. When reusing an existing VCN, set
`LAB_OCI_BASTION_SSH_SOURCE_CIDR` if its NSGs do not already allow that
subnet. Set `LAB_OCI_BASTION_TARGET_SUBNET_ID` to use a private target subnet.
After `oci-foundation apply`, the Bastion and discovered edge instance OCIDs
are persisted in the local OCI config. Open a managed SSH session with:
```text
./jeannie oci bastion ssh
```
Explicit OCIDs and key paths can also be supplied:
```text
./jeannie oci bastion ssh BASTION_OCID INSTANCE_OCID ubuntu ~/.ssh/id_ed25519.pub ~/.ssh/id_ed25519
```
The same operation is available through the remote API as
`./jeannie remote oci bastion ssh`; it executes on the Debian pipeline host,
keeping OCI credentials and the private SSH key off the client.

View File

@ -1,13 +1,16 @@
locals {
target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id
email_compartment_id = var.enabled && var.email_create_compartment ? oci_identity_compartment.email[0].id : (trimspace(var.email_compartment_id) != "" ? var.email_compartment_id : local.target_compartment_id)
network_vcn_id = trimspace(var.existing_vcn_id) != "" ? var.existing_vcn_id : oci_core_vcn.homelab[0].id
public_subnet_id = trimspace(var.existing_public_subnet_id) != "" ? var.existing_public_subnet_id : oci_core_subnet.public[0].id
email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id
email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_id
public_route_table_id = try(oci_core_route_table.public[0].id, null)
private_route_table_id = try(oci_core_route_table.private[0].id, null)
base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" })
target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id
email_compartment_id = var.enabled && var.email_create_compartment ? oci_identity_compartment.email[0].id : (trimspace(var.email_compartment_id) != "" ? var.email_compartment_id : local.target_compartment_id)
network_vcn_id = trimspace(var.existing_vcn_id) != "" ? var.existing_vcn_id : oci_core_vcn.homelab[0].id
public_subnet_id = trimspace(var.existing_public_subnet_id) != "" ? var.existing_public_subnet_id : oci_core_subnet.public[0].id
email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id
email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_id
bastion_target_subnet_id = trimspace(var.bastion_target_subnet_id) != "" ? var.bastion_target_subnet_id : local.public_subnet_id
bastion_client_cidrs = length(var.bastion_client_cidr_block_allow_list) > 0 ? var.bastion_client_cidr_block_allow_list : [var.admin_cidr]
bastion_ssh_source_cidr = trimspace(var.bastion_ssh_source_cidr) != "" ? var.bastion_ssh_source_cidr : (trimspace(var.existing_vcn_id) != "" ? try(data.oci_core_subnet.existing_public[0].cidr_block, "") : var.public_subnet_cidr)
public_route_table_id = try(oci_core_route_table.public[0].id, null)
private_route_table_id = try(oci_core_route_table.private[0].id, null)
base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" })
}
data "oci_core_subnet" "existing_public" {
@ -152,6 +155,17 @@ resource "oci_core_network_security_group" "email" {
freeform_tags = local.base_tags
}
resource "oci_bastion_bastion" "homelab" {
count = var.enabled && var.bastion_enabled ? 1 : 0
bastion_type = "standard"
compartment_id = local.target_compartment_id
target_subnet_id = local.bastion_target_subnet_id
name = var.bastion_name
client_cidr_block_allow_list = local.bastion_client_cidrs
max_session_ttl_in_seconds = var.bastion_max_session_ttl_in_seconds
freeform_tags = local.base_tags
}
resource "oci_core_network_security_group_security_rule" "edge_ssh" {
count = var.enabled && var.edge_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
@ -167,6 +181,21 @@ resource "oci_core_network_security_group_security_rule" "edge_ssh" {
}
}
resource "oci_core_network_security_group_security_rule" "edge_bastion_ssh" {
count = var.enabled && var.edge_enabled && var.bastion_enabled && trimspace(local.bastion_ssh_source_cidr) != "" ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = local.bastion_ssh_source_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "edge_web" {
count = var.enabled && var.edge_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
@ -212,6 +241,21 @@ resource "oci_core_network_security_group_security_rule" "email_ssh" {
}
}
resource "oci_core_network_security_group_security_rule" "email_bastion_ssh" {
count = var.enabled && var.email_enabled && var.bastion_enabled && trimspace(local.bastion_ssh_source_cidr) != "" ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = local.bastion_ssh_source_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "email_ports" {
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id

View File

@ -7,6 +7,8 @@ output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, data.oci_c
output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, data.oci_core_instance.existing_edge[0].public_ip, null) }
output "email_instance_id" { value = try(oci_core_instance.email[0].id, null) }
output "email_public_ip" { value = try(oci_core_instance.email[0].public_ip, null) }
output "bastion_id" { value = try(oci_bastion_bastion.homelab[0].id, null) }
output "bastion_target_subnet_id" { value = try(oci_bastion_bastion.homelab[0].target_subnet_id, null) }
output "next_steps" {
value = [
"Set LAB_EDGE_HOST to edge_public_ip and run ./jeannie edge to configure the edge proxy.",

View File

@ -130,6 +130,40 @@ variable "monitoring_cidr" {
default = ""
}
variable "bastion_enabled" {
description = "Create an OCI Bastion service for time-limited SSH sessions."
type = bool
default = true
}
variable "bastion_name" {
type = string
default = "homelab-bastion"
}
variable "bastion_target_subnet_id" {
description = "Subnet the Bastion can reach; defaults to the foundation public subnet."
type = string
default = ""
}
variable "bastion_ssh_source_cidr" {
description = "CIDR allowed to SSH to managed targets from the Bastion subnet; defaults to the foundation public subnet CIDR."
type = string
default = ""
}
variable "bastion_client_cidr_block_allow_list" {
description = "CIDRs allowed to create Bastion sessions; defaults to admin_cidr."
type = list(string)
default = []
}
variable "bastion_max_session_ttl_in_seconds" {
type = number
default = 10800
}
variable "edge_enabled" {
type = bool
default = true

View File

@ -133,7 +133,7 @@ Observability And GitOps
oci-foundation {status|plan|apply|destroy --yes}
Create the opt-in OCI compartment, network, edge VM, and email VM.
oci-auth {install|init [--force]|check} Install/check OCI CLI and API-key authentication on Debian.
oci {status|doctor|cleanup} Inspect OCI resources or perform guarded infrastructure cleanup.
oci {status|doctor|bastion ssh|cleanup} Inspect OCI resources, open Bastion SSH, or clean up infrastructure.
edge-cache-flush Clear Nginx, Varnish, and Squid edge caches.
promote {plan|validate|rollback APP} Run canary promotion gates and rollback plan.
cert-check Check public DNS, TLS, and edge URL health.
@ -316,8 +316,9 @@ case "${1:-}" in
case "${2:-status}" in
status) oci_status ;;
doctor) oci_doctor ;;
bastion) oci_bastion_ssh "${@:3}" ;;
cleanup) oci_cleanup "${@:3}" ;;
*) echo "Usage: ./jeannie oci {status|doctor|cleanup plan|cleanup apply --confirm TENANCY-CLEANUP}" >&2; exit 2 ;;
*) echo "Usage: ./jeannie oci {status|doctor|bastion ssh|cleanup plan|cleanup apply --confirm TENANCY-CLEANUP}" >&2; exit 2 ;;
esac
;;
edge-cache-flush)

View File

@ -3602,6 +3602,8 @@ oci_foundation_config_names=(
LAB_OCI_AUTO_CONFIGURE_EMAIL LAB_OCI_AUTO_UPDATE_DUCKDNS LAB_DUCKDNS_DOMAINS
LAB_OCI_FOUNDATION_STATE_FILE
LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR
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_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET
)
@ -3796,9 +3798,19 @@ oci_foundation_manage() {
tofu -chdir="${stack}" apply "${tofu_state_args[@]}" "$@" -auto-approve
local discovered_edge_ip
local discovered_email_ip
local discovered_bastion_id
local discovered_edge_instance_id
local post_apply_failures=0
discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)"
discovered_email_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw email_public_ip 2>/dev/null || true)"
discovered_bastion_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw bastion_id 2>/dev/null || true)"
discovered_edge_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_instance_id 2>/dev/null || true)"
if [[ -n "${discovered_bastion_id}" && "${discovered_bastion_id}" != "null" ]]; then
export LAB_OCI_BASTION_ID="${discovered_bastion_id}"
fi
if [[ -n "${discovered_edge_instance_id}" && "${discovered_edge_instance_id}" != "null" ]]; then
export LAB_OCI_EDGE_INSTANCE_ID="${discovered_edge_instance_id}"
fi
if [[ -n "${discovered_edge_ip}" && "${discovered_edge_ip}" != "null" ]]; then
export LAB_EDGE_HOST="${discovered_edge_ip}"
export TF_VAR_edge_host="${discovered_edge_ip}"
@ -3858,6 +3870,12 @@ oci_auth_manage() {
"${REPO_ROOT}/scripts/oci-auth" "$@"
}
oci_bastion_ssh() {
require_debian_server "oci bastion ssh"
oci_foundation_load_config
"${REPO_ROOT}/scripts/oci-bastion-ssh" "$@"
}
oci_cli_resolve() {
if command -v oci >/dev/null 2>&1; then
command -v oci
@ -3922,6 +3940,14 @@ oci_status() {
--query 'data[].{NAME:"display-name",SIZE_GB:"size-in-gbs",STATE:"lifecycle-state",AD:"availability-domain",ID:id}' \
--output table 2>/dev/null || echo 'unavailable'
done <<<"${resource_compartments}"
printf '%s\n' '-- bastions (all compartments)'
while IFS= read -r current_compartment; do
[[ -n "${current_compartment}" ]] || continue
printf 'compartment=%s\n' "${current_compartment}"
"${oci_bin}" bastion bastion list --compartment-id "${current_compartment}" --all \
--query 'data[].{NAME:name,TYPE:"bastion-type",STATE:"lifecycle-state",TARGET_SUBNET:"target-subnet-id",ID:id}' \
--output table 2>/dev/null || echo 'unavailable'
done <<<"${resource_compartments}"
printf '%s\n' '-- service limits'
"${oci_bin}" limits service list --compartment-id "${tenancy}" --all \
--query 'data[].{NAME:name,DESCRIPTION:description}' --output table 2>/dev/null || echo 'unavailable'

View File

@ -361,6 +361,12 @@ export_oci_foundation_tf_vars() {
export_if_unset TF_VAR_admin_cidr "${LAB_OCI_ADMIN_CIDR:-}"
export_if_unset TF_VAR_monitoring_enabled "${LAB_OCI_MONITORING_ENABLED:-}"
export_if_unset TF_VAR_monitoring_cidr "${LAB_OCI_MONITORING_CIDR:-${LAB_OCI_ADMIN_CIDR:-}}"
export_if_unset TF_VAR_bastion_enabled "${LAB_OCI_BASTION_ENABLED:-}"
export_if_unset TF_VAR_bastion_name "${LAB_OCI_BASTION_NAME:-}"
export_if_unset TF_VAR_bastion_target_subnet_id "${LAB_OCI_BASTION_TARGET_SUBNET_ID:-}"
export_if_unset TF_VAR_bastion_ssh_source_cidr "${LAB_OCI_BASTION_SSH_SOURCE_CIDR:-}"
export_if_unset TF_VAR_bastion_client_cidr_block_allow_list "${LAB_OCI_BASTION_CLIENT_CIDRS_JSON:-}"
export_if_unset TF_VAR_bastion_max_session_ttl_in_seconds "${LAB_OCI_BASTION_MAX_SESSION_TTL:-}"
export_if_unset TF_VAR_open_mail_ports "${LAB_OCI_OPEN_MAIL_PORTS:-}"
export_if_unset TF_VAR_edge_enabled "${LAB_OCI_EDGE_ENABLED:-}"
export_if_unset TF_VAR_edge_existing_instance_id "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}"

51
scripts/oci-bastion-ssh Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "${1:-}" == "ssh" ]]; then
shift
fi
oci_bin="${OCI_CLI_BIN:-}"
if [[ -z "${oci_bin}" ]]; then
if command -v oci >/dev/null 2>&1; then
oci_bin="$(command -v oci)"
else
oci_bin="${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci"
fi
fi
bastion_id="${1:-${LAB_OCI_BASTION_ID:-}}"
target_id="${2:-${LAB_OCI_TARGET_INSTANCE_ID:-${LAB_OCI_EDGE_INSTANCE_ID:-${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}}}}"
target_user="${3:-${LAB_OCI_BASTION_TARGET_USER:-ubuntu}}"
public_key="${4:-${LAB_OCI_SSH_PUBLIC_KEY_PATH:-${HOME}/.ssh/id_ed25519.pub}}"
private_key="${5:-${LAB_OCI_SSH_PRIVATE_KEY_PATH:-${public_key%.pub}}}"
session_ttl="${LAB_OCI_BASTION_SESSION_TTL:-3600}"
if [[ ! -x "${oci_bin}" ]]; then
echo "OCI CLI is not installed; run ./jeannie oci-auth install." >&2
exit 2
fi
if [[ -z "${bastion_id}" || -z "${target_id}" ]]; then
echo "Usage: ./jeannie oci bastion ssh [BASTION_OCID] [TARGET_INSTANCE_OCID] [OS_USER] [SSH_PUBLIC_KEY] [SSH_PRIVATE_KEY]" >&2
echo "Set LAB_OCI_BASTION_ID and LAB_OCI_EDGE_INSTANCE_ID to omit the OCIDs." >&2
exit 2
fi
[[ -f "${public_key}" ]] || { echo "SSH public key not found: ${public_key}" >&2; exit 2; }
[[ -f "${private_key}" ]] || { echo "SSH private key not found: ${private_key}" >&2; exit 2; }
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)"
session_id="$(printf '%s' "${session_json}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["id"])')"
ssh_command="$("${oci_bin}" bastion session get \
--session-id "${session_id}" \
--query 'data."ssh-metadata".command' \
--raw-output)"
ssh_command="${ssh_command//<privateKey>/${private_key}}"
printf 'Connecting through OCI Bastion session %s...\n' "${session_id}" >&2
exec bash -c "${ssh_command}"

View File

@ -36,7 +36,7 @@ mapfile -t compartments < <(
)
declare -a instances=() boot_volumes=() block_volumes=() public_ips=() vcns=()
declare -a subnets=() gateways=() route_tables=() security_lists=() dhcp_options=() nsgs=()
declare -a subnets=() gateways=() route_tables=() security_lists=() dhcp_options=() nsgs=() bastions=()
for compartment in "${compartments[@]}"; do
[[ -n "${compartment}" ]] || continue
while IFS= read -r id; do [[ -n "${id}" ]] && instances+=("${compartment}|${id}"); done < <("${oci_bin}" compute instance list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
@ -52,11 +52,12 @@ for compartment in "${compartments[@]}"; do
while IFS= read -r id; do [[ -n "${id}" ]] && security_lists+=("${compartment}|${id}"); done < <("${oci_bin}" network security-list list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && dhcp_options+=("${compartment}|${id}"); done < <("${oci_bin}" network dhcp-options list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && nsgs+=("${compartment}|${id}"); done < <("${oci_bin}" network nsg list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && bastions+=("${compartment}|${id}"); done < <("${oci_bin}" bastion bastion list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
done
echo "OCI infrastructure cleanup (${action})"
echo "Preserved: tenancy, root compartment, IAM users/groups/policies/API keys."
printf 'compartments=%d instances=%d boot_volumes=%d block_volumes=%d public_ips=%d vcns=%d subnets=%d\n' "${#compartments[@]}" "${#instances[@]}" "${#boot_volumes[@]}" "${#block_volumes[@]}" "${#public_ips[@]}" "${#vcns[@]}" "${#subnets[@]}"
printf 'compartments=%d instances=%d boot_volumes=%d block_volumes=%d public_ips=%d vcns=%d subnets=%d bastions=%d\n' "${#compartments[@]}" "${#instances[@]}" "${#boot_volumes[@]}" "${#block_volumes[@]}" "${#public_ips[@]}" "${#vcns[@]}" "${#subnets[@]}" "${#bastions[@]}"
if [[ "${action}" == plan ]]; then
printf '%s\n' "Re-run with: ./jeannie oci cleanup apply --confirm TENANCY-CLEANUP"
@ -86,6 +87,7 @@ for item in "${instances[@]}"; do
delete_resource "instance ${id}" "${oci_bin}" compute instance terminate --instance-id "${id}" --preserve-boot-volume false --force
wait_instance_terminated "${id}"
done
for item in "${bastions[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "Bastion ${id}" "${oci_bin}" bastion bastion delete --bastion-id "${id}"; done
for item in "${subnets[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "subnet ${id}" "${oci_bin}" network subnet delete --subnet-id "${id}" --force; done
# OCI does not allow deletion of default route tables, security lists, or DHCP