# brokered-shell-access / oci / t0 "Quick start"
#
# OCI Bastion in a private subnet, so machines there are reached through
# short-lived Bastion sessions with no public IP and no SSH port open to the
# internet. Every session created is recorded in Audit.
#
# Change the two values below. The provider reads everything else, region
# included, from the DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Put instances in the private subnet with the Bastion plugin of Oracle Cloud
# Agent enabled, then connect:
#   oci bastion session create-managed-ssh --bastion-id <bastion> \
#     --target-resource-id <instance> --target-os-username opc \
#     --ssh-public-key-file ~/.ssh/id_ed25519.pub --session-ttl 1800
#   oci bastion session get --session-id <session> --query 'data."ssh-metadata".command' --raw-output
#   Run the command it prints, with your private key.
#
# Verify: after a session, Audit shows the CreateSession event under your
# name, in the bastion's compartment.

locals {
  compartment_ocid = "ocid1.compartment.oc1..change-me" # change me
  client_cidr      = "203.0.113.0/24"                   # change me: where operators connect from

  tags = {
    owner               = "security-engineering"
    environment         = "prod"
    data_classification = "internal"
    managed_by          = "terraform"
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 9.0"
    }
  }
}

provider "oci" {}

resource "oci_core_vcn" "main" {
  compartment_id = local.compartment_ocid
  cidr_blocks    = ["10.70.0.0/16"]
  display_name   = "vcn-bastion"
  dns_label      = "bastion"
  freeform_tags  = local.tags
}

# Oracle Cloud Agent, and the Bastion plugin in it, reach Oracle's services
# through the service gateway. Nothing reaches the internet.
data "oci_core_services" "all" {
  filter {
    name   = "name"
    values = ["All .* Services In Oracle Services Network"]
    regex  = true
  }
}

resource "oci_core_service_gateway" "oracle" {
  compartment_id = local.compartment_ocid
  vcn_id         = oci_core_vcn.main.id
  display_name   = "sgw-oracle-services"
  freeform_tags  = local.tags

  services {
    service_id = data.oci_core_services.all.services[0].id
  }
}

resource "oci_core_route_table" "private" {
  compartment_id = local.compartment_ocid
  vcn_id         = oci_core_vcn.main.id
  display_name   = "rt-private"
  freeform_tags  = local.tags

  route_rules {
    destination       = data.oci_core_services.all.services[0].cidr_block
    destination_type  = "SERVICE_CIDR_BLOCK"
    network_entity_id = oci_core_service_gateway.oracle.id
  }
}

# SSH is accepted from inside the subnet only, which is where the bastion's
# private endpoint sits.
resource "oci_core_security_list" "private" {
  compartment_id = local.compartment_ocid
  vcn_id         = oci_core_vcn.main.id
  display_name   = "sl-private"
  freeform_tags  = local.tags

  ingress_security_rules {
    description = "SSH from the bastion's endpoint in this subnet"
    protocol    = "6"
    source      = "10.70.1.0/24"
    tcp_options {
      min = 22
      max = 22
    }
  }

  # The bastion's private endpoint sits in this subnet and connects out to
  # the instances on port 22.
  egress_security_rules {
    description = "SSH from the bastion's endpoint to instances in this subnet"
    protocol    = "6"
    destination = "10.70.1.0/24"
    tcp_options {
      min = 22
      max = 22
    }
  }

  egress_security_rules {
    description      = "HTTPS to Oracle services, for Oracle Cloud Agent"
    protocol         = "6"
    destination      = data.oci_core_services.all.services[0].cidr_block
    destination_type = "SERVICE_CIDR_BLOCK"
    tcp_options {
      min = 443
      max = 443
    }
  }
}

resource "oci_core_subnet" "private" {
  compartment_id             = local.compartment_ocid
  vcn_id                     = oci_core_vcn.main.id
  cidr_block                 = "10.70.1.0/24"
  display_name               = "private"
  dns_label                  = "private"
  prohibit_public_ip_on_vnic = true
  route_table_id             = oci_core_route_table.private.id
  security_list_ids          = [oci_core_security_list.private.id]
  freeform_tags              = local.tags
}

resource "oci_bastion_bastion" "main" {
  compartment_id               = local.compartment_ocid
  name                         = "bastion"
  bastion_type                 = "STANDARD"
  target_subnet_id             = oci_core_subnet.private.id
  client_cidr_block_allow_list = [local.client_cidr]
  max_session_ttl_in_seconds   = 3600
  freeform_tags                = local.tags
}

output "bastion_id" {
  description = "Use with oci bastion session create-managed-ssh."
  value       = oci_bastion_bastion.main.id
}

output "subnet_id" {
  description = "Put instances here, with the Bastion plugin enabled."
  value       = oci_core_subnet.private.id
}
