# brokered-shell-access / gcp / t1 "Standard" # # IAP tunnels limited to named ports by an IAM condition, OS Login without # admin rights, and every tunnel recorded in the audit log. # # IAP records who opened a tunnel, to which VM and port, and when. It does not # record what was typed inside. That needs a recorder on the VM, such as # auditd with its logs shipped by the host log shipping template. locals { port_condition = join(" || ", [for p in var.ports : "destination.port == ${p}"]) } resource "google_project_service" "iap" { project = var.project_id service = "iap.googleapis.com" disable_on_destroy = false } resource "google_compute_firewall" "iap" { project = var.project_id name = "allow-iap-tunnels-only" network = var.network direction = "INGRESS" priority = 1000 source_ranges = ["35.235.240.0/20"] target_tags = [var.network_tag] allow { protocol = "tcp" ports = [for p in var.ports : tostring(p)] } log_config { metadata = "INCLUDE_ALL_METADATA" } } # The condition means a tunnel to any other port is refused, whatever the # firewall allows. resource "google_project_iam_member" "tunnel" { for_each = toset(var.operators) project = var.project_id role = "roles/iap.tunnelResourceAccessor" member = each.value condition { title = "named-ports-only" description = "Tunnels to ${join(", ", var.ports)} only." expression = local.port_condition } depends_on = [google_project_service.iap] } # osLogin gives a normal account on the VM. osAdminLogin would add sudo, so # grant that separately, to fewer people, when it is needed. resource "google_project_iam_member" "os_login" { for_each = toset(var.operators) project = var.project_id role = "roles/compute.osLogin" member = each.value } resource "google_compute_project_metadata_item" "os_login" { project = var.project_id key = "enable-oslogin" value = "TRUE" } # Tunnel authorisations are Data Access logs, off by default. This resource # owns IAP's audit settings for the whole project; merge first if another tool # manages them. resource "google_project_iam_audit_config" "iap" { project = var.project_id service = "iap.googleapis.com" audit_log_config { log_type = "DATA_READ" } }