#!/bin/bash # setup-identity.sh -- create the dynamic group and policy that let Spinifex nodes # authenticate to the OCI API as themselves, so no API key is installed on any node. # # Run once per tenancy, with a tenancy-admin principal. Every deployment afterwards # passes instance_principal = "adopt", which references these by nothing at all: the # matching rule covers the whole compartment, so it names no instance and needs no # OCID from here. # # Its own state file, deliberately. validate-topology.sh destroys its state at the # end of every run, and these two resources outlive every topology -- holding them # in a per-topology state would let a nightly teardown delete the tenancy's policy. # # MUST MAINTAIN BASH 3.X COMPATABILITY. Operators run this on macOS, which ships # bash 3.2: no declare -A, no mapfile, and under set -u an empty "${arr[@]}" is an # unbound variable, so guard every such expansion with ${#arr[@]}. set -uo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" STATE_DIR="${OCI_STATE_ROOT:-$HERE}/.identity" SSH_PUBLIC_KEY="${OCI_SSH_PUBLIC_KEY:-$HOME/.ssh/oci-spx.pub}" DESTROY=0 DRY_RUN=0 log() { printf '[setup-identity] %s\n' "$*"; } die() { printf '[setup-identity] ERROR: %s\n' "$*" >&2 exit 1 } usage() { cat >&2 </dev/null || die "terraform init failed" if [ "$DESTROY" = 1 ]; then log "destroying the dynamic group and policy" tf destroy -auto-approve -var 'instance_principal=create' \ -state "$STATE_DIR/terraform.tfstate" "${targets[@]}" \ || die "destroy failed" log "removed. Any node configured for instance principal can no longer allocate an address." exit 0 fi args=(-var 'instance_principal=create' -state "$STATE_DIR/terraform.tfstate" "${targets[@]}") if [ "$DRY_RUN" = 1 ]; then log "dry run: planning the dynamic group and policy only" tf plan "${args[@]}" || die "plan failed" exit 0 fi log "creating the dynamic group and policy (tenancy-root resources)" tf apply -auto-approve "${args[@]}" || die "apply failed -- a tenancy-admin principal is required" log "done. State in $STATE_DIR/terraform.tfstate -- keep it, it is the only record." log "Deployments can now use instance_principal = \"adopt\" with a compartment-scoped user."