# brokered-shell-access / oci / t1 "Standard" # # OCI Bastion reachable from named address ranges only, a network security # group that accepts SSH from the bastion's own address and nothing else, an # operator group that can open sessions as one OS user and do little more, # short sessions, and an email for every session created. # # Bastion does not record what is typed in a session. The record is who # opened a session, to which instance, as which user, and when. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) subnet_cidr = cidrsubnet(var.cidr, 8, 1) compartment = "compartment id ${var.compartment_ocid}" } resource "oci_core_vcn" "main" { compartment_id = var.compartment_ocid cidr_blocks = [var.cidr] display_name = "vcn-bastion" dns_label = "bastion" freeform_tags = local.tags } 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 = var.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 = var.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 } } # The subnet's list allows no inbound traffic. SSH comes in through the # network security group below, from the bastion's address only. resource "oci_core_security_list" "private" { compartment_id = var.compartment_ocid vcn_id = oci_core_vcn.main.id display_name = "sl-private" freeform_tags = local.tags # 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 = local.subnet_cidr 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 = var.compartment_ocid vcn_id = oci_core_vcn.main.id cidr_block = local.subnet_cidr 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 = var.compartment_ocid name = "bastion" bastion_type = "STANDARD" target_subnet_id = oci_core_subnet.private.id client_cidr_block_allow_list = var.client_cidrs max_session_ttl_in_seconds = var.max_session_minutes * 60 freeform_tags = local.tags } # Attach this group to every instance operators reach. resource "oci_core_network_security_group" "workloads" { compartment_id = var.compartment_ocid vcn_id = oci_core_vcn.main.id display_name = "nsg-bastion-targets" freeform_tags = local.tags } resource "oci_core_network_security_group_security_rule" "ssh_from_bastion" { network_security_group_id = oci_core_network_security_group.workloads.id direction = "INGRESS" protocol = "6" source = "${oci_bastion_bastion.main.private_endpoint_ip_address}/32" source_type = "CIDR_BLOCK" description = "SSH from the bastion's private endpoint only" tcp_options { destination_port_range { min = 22 max = 22 } } } # The statements Oracle lists for creating and using sessions, with sessions # limited to this bastion and one OS user. resource "oci_identity_policy" "operators" { compartment_id = var.tenancy_ocid name = "bastion-operators" description = "Lets operators open Bastion sessions as ${var.os_username}, and little else." freeform_tags = local.tags statements = [ "Allow group ${var.operator_group} to use bastion in ${local.compartment} where target.bastion.ocid = '${oci_bastion_bastion.main.id}'", "Allow group ${var.operator_group} to manage bastion-session in ${local.compartment} where all {target.bastion.ocid = '${oci_bastion_bastion.main.id}', target.bastion-session.username = '${var.os_username}'}", "Allow group ${var.operator_group} to read instances in ${local.compartment}", "Allow group ${var.operator_group} to read instance-agent-plugins in ${local.compartment}", "Allow group ${var.operator_group} to read vcns in ${local.compartment}", "Allow group ${var.operator_group} to read subnets in ${local.compartment}", "Allow group ${var.operator_group} to read vnic-attachments in ${local.compartment}", "Allow group ${var.operator_group} to read vnics in ${local.compartment}", ] } # --------------------------------------------------------------------------- # An email for every session created # --------------------------------------------------------------------------- resource "oci_ons_notification_topic" "sessions" { compartment_id = var.compartment_ocid name = "bastion-sessions" description = "Every Bastion session created." freeform_tags = local.tags } resource "oci_ons_subscription" "email" { compartment_id = var.compartment_ocid topic_id = oci_ons_notification_topic.sessions.id protocol = "EMAIL" endpoint = var.alert_email freeform_tags = local.tags } # Deleting this policy or the topic stops the session email, and nothing # reports it. Hardened refuses both deletes, and its daily drift plan notices # one made by hand. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "bastion-sessions-connector" description = "Lets the session connector publish to its topic." freeform_tags = local.tags statements = [ "Allow any-user to use ons-topics in ${local.compartment} where all {request.principal.type='serviceconnector', target.topic.id='${oci_ons_notification_topic.sessions.id}', request.principal.compartment.id='${var.compartment_ocid}'}", ] } resource "oci_sch_service_connector" "sessions" { compartment_id = var.compartment_ocid display_name = "bastion-sessions" description = "Audit events for each Bastion session created in the compartment." freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = "_Audit" } } # One log rule and no other task. Hardened refuses a second task, which # could drop events before they reach the topic. tasks { kind = "logRule" condition = "data.eventName='CreateSession'" } target { kind = "notifications" topic_id = oci_ons_notification_topic.sessions.id enable_formatted_messaging = true } depends_on = [oci_identity_policy.connector] }