# evidence-capture / oci / t1 "Standard" # # The quick start's responder, with: a record of each instance and its hash # saved to a retention-ruled bucket under your own key before anything # changes, the instance taken out of its pool so the pool cannot replace it, # a choice of how to cut it off, and each step sent to a notification topic. # # Verify, on a test instance you do not mind isolating: # terraform output verify data "oci_objectstorage_namespace" "this" { compartment_id = local.evidence_compartment } locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) evidence_compartment = var.evidence_compartment_ocid != "" ? var.evidence_compartment_ocid : var.compartment_ocid dg = "dynamic-group id ${oci_identity_dynamic_group.responder.id}" } resource "random_id" "suffix" { byte_length = 3 } locals { package = "${path.module}/.build/responder.zip" package_present = fileexists("${path.module}/.build/responder.zip") } # Evidence: each instance's record and its hash, protected for the retention # period. Retention rules cannot be added to a bucket with versioning on. resource "oci_objectstorage_bucket" "evidence" { compartment_id = local.evidence_compartment namespace = data.oci_objectstorage_namespace.this.namespace name = "incident-evidence-${random_id.suffix.hex}" access_type = "NoPublicAccess" storage_tier = "Standard" versioning = "Disabled" kms_key_id = oci_kms_key.evidence.id freeform_tags = local.tags retention_rules { display_name = "evidence-retention" duration { time_amount = var.retention_days time_unit = "DAYS" } time_rule_locked = var.lock_at == "" ? null : var.lock_at } lifecycle { prevent_destroy = true } depends_on = [oci_identity_policy.storage_uses_key] } # The quarantine mark. A resource carrying a security attribute passes only # traffic that a Zero Trust Packet Routing policy allows. No policy names # this one, so nothing passes. Only made when isolation is by attribute, # because a tenancy without ZPR cannot have one. resource "oci_security_attribute_security_attribute_namespace" "incident" { count = var.isolation == "zpr" ? 1 : 0 compartment_id = var.tenancy_ocid name = "incident-${random_id.suffix.hex}" description = "Marks resources under incident response" freeform_tags = local.tags } resource "oci_security_attribute_security_attribute" "quarantine" { count = var.isolation == "zpr" ? 1 : 0 security_attribute_namespace_id = oci_security_attribute_security_attribute_namespace.incident[0].id name = "quarantine" description = "Set by the incident responder. No ZPR policy allows traffic for it, so the resource is cut off." } # Status: one message per step. resource "oci_ons_notification_topic" "status" { compartment_id = var.compartment_ocid name = "incident-responder-status-${random_id.suffix.hex}" description = "One message per step the incident responder takes" freeform_tags = local.tags } resource "oci_ons_subscription" "status" { for_each = toset(var.status_emails) compartment_id = var.compartment_ocid topic_id = oci_ons_notification_topic.status.id protocol = "EMAIL" endpoint = each.value freeform_tags = local.tags } # The function acts as itself, through a dynamic group. resource "oci_identity_dynamic_group" "responder" { compartment_id = var.tenancy_ocid name = "incident-responder-${random_id.suffix.hex}" description = "The incident responder function" matching_rule = "ALL {resource.type = 'fnfunc', resource.compartment.id = '${var.compartment_ocid}'}" freeform_tags = local.tags } # Read instances, back up their volumes, take them out of their pool, update # them, and publish status. No deletes. resource "oci_identity_policy" "responder" { compartment_id = var.compartment_ocid name = "incident-responder-${random_id.suffix.hex}" description = "Lets the responder read instances, back up their volumes, detach them from pools, and mark them. No deletes." freeform_tags = local.tags statements = concat([ "Allow ${local.dg} to read instances in compartment id ${var.compartment_ocid}", "Allow ${local.dg} to manage instances in compartment id ${var.compartment_ocid} where request.permission = 'INSTANCE_UPDATE'", "Allow ${local.dg} to inspect volume-attachments in compartment id ${var.compartment_ocid}", "Allow ${local.dg} to inspect vnic-attachments in compartment id ${var.compartment_ocid}", "Allow ${local.dg} to use volumes in compartment id ${var.compartment_ocid}", "Allow ${local.dg} to manage boot-volume-backups in compartment id ${var.compartment_ocid} where request.permission = 'BOOT_VOLUME_BACKUP_CREATE'", "Allow ${local.dg} to manage volume-backups in compartment id ${var.compartment_ocid} where request.permission = 'VOLUME_BACKUP_CREATE'", "Allow ${local.dg} to read instance-pools in compartment id ${var.compartment_ocid}", "Allow ${local.dg} to manage instance-pools in compartment id ${var.compartment_ocid} where request.permission = 'INSTANCE_POOL_INSTANCE_DETACH'", # Notifications supports no condition naming a topic, so this covers the # compartment's topics. "Allow ${local.dg} to use ons-topics in compartment id ${var.compartment_ocid}", ], var.isolation == "stop" ? [ "Allow ${local.dg} to manage instances in compartment id ${var.compartment_ocid} where request.permission = 'INSTANCE_POWER_ACTIONS'", ] : []) } # Add evidence, and never read, overwrite, or delete it. resource "oci_identity_policy" "responder_writes_evidence" { compartment_id = local.evidence_compartment name = "incident-responder-${random_id.suffix.hex}-evidence" description = "Lets the responder add evidence to the bucket. Not read, overwrite, or delete it." freeform_tags = local.tags statements = [ "Allow ${local.dg} to read buckets in compartment id ${local.evidence_compartment} where target.bucket.name = '${oci_objectstorage_bucket.evidence.name}'", "Allow ${local.dg} to manage objects in compartment id ${local.evidence_compartment} where all {target.bucket.name = '${oci_objectstorage_bucket.evidence.name}', request.permission = 'OBJECT_CREATE'}", ] } # Applying the attribute needs use on its namespace, which lives in the # tenancy, so that grant is a policy of its own. resource "oci_identity_policy" "responder_marks" { count = var.isolation == "zpr" ? 1 : 0 compartment_id = var.tenancy_ocid name = "incident-responder-${random_id.suffix.hex}-marks" description = "Lets the responder apply the quarantine security attribute, and no other." freeform_tags = local.tags statements = [ "Allow ${local.dg} to use security-attribute-namespace in tenancy where target.security-attribute-namespace.name = '${oci_security_attribute_security_attribute_namespace.incident[0].name}'", ] } # The function's package. build.sh writes it; Terraform uploads it. resource "oci_objectstorage_bucket" "source" { compartment_id = var.compartment_ocid namespace = data.oci_objectstorage_namespace.this.namespace name = "incident-responder-src-${random_id.suffix.hex}" access_type = "NoPublicAccess" freeform_tags = local.tags } resource "oci_objectstorage_object" "source" { bucket = oci_objectstorage_bucket.source.name namespace = data.oci_objectstorage_namespace.this.namespace object = "responder-${local.package_present ? filesha256(local.package) : "missing"}.zip" # Until build.sh has run, the plan is refused by the precondition below. source = local.package_present ? local.package : null content = local.package_present ? null : "run build.sh" lifecycle { precondition { condition = local.package_present error_message = "Run ./build.sh first. It packages function/ and the Oracle SDK into .build/responder.zip." } } } # The application reads its package as itself. Its principal type is fnapp, # which the dynamic group does not cover, so this names it directly. resource "oci_identity_policy" "application_reads_package" { compartment_id = var.compartment_ocid name = "incident-responder-${random_id.suffix.hex}-package" description = "Lets the responder's Functions application read its own code package." freeform_tags = local.tags statements = [ "Allow any-user to read objects in compartment id ${var.compartment_ocid} where all {request.principal.type = 'fnapp', request.principal.compartment.id = '${var.compartment_ocid}', target.bucket.name = '${oci_objectstorage_bucket.source.name}'}", ] } data "oci_functions_functions_runtime_versions" "python" { language_version = var.python_version is_current_version = true state = "ACTIVE" lifecycle { postcondition { condition = length(self.functions_runtime_version_collection) > 0 && length(self.functions_runtime_version_collection[0].items) > 0 error_message = "No current Python ${var.python_version} runtime here. oci fn functions-runtime-version list --all shows the versions offered; set python_version to one of them." } } } data "oci_functions_functions_runtime" "python" { functions_runtime_id = data.oci_functions_functions_runtime_versions.python.functions_runtime_version_collection[0].items[0].functions_runtime_id } resource "oci_functions_application" "responder" { compartment_id = var.compartment_ocid display_name = "incident-responder-${random_id.suffix.hex}" subnet_ids = [var.subnet_ocid] shape = "GENERIC_X86" freeform_tags = local.tags } resource "oci_functions_function" "responder" { application_id = oci_functions_application.responder.id display_name = "preserve" memory_in_mbs = "1024" timeout_in_seconds = 300 freeform_tags = local.tags config = { QUARANTINE_NAMESPACE = var.isolation == "zpr" ? oci_security_attribute_security_attribute_namespace.incident[0].name : "" QUARANTINE_ATTRIBUTE = var.isolation == "zpr" ? oci_security_attribute_security_attribute.quarantine[0].name : "" ISOLATION = var.isolation EVIDENCE_NAMESPACE = data.oci_objectstorage_namespace.this.namespace EVIDENCE_BUCKET = oci_objectstorage_bucket.evidence.name STATUS_TOPIC = oci_ons_notification_topic.status.id } source_details { source_type = "ARCHIVE" handler = "func.handler" archive_source_details { archive_source_type = "OBJECT_STORAGE_ARCHIVE" namespace = data.oci_objectstorage_namespace.this.namespace bucket = oci_objectstorage_bucket.source.name object = oci_objectstorage_object.source.object } runtime_config { functions_runtime_name = data.oci_functions_functions_runtime.python.name runtime_config_type = "FUNCTION_UPDATE" } } depends_on = [oci_identity_policy.responder, oci_identity_policy.responder_writes_evidence, oci_identity_policy.responder_marks, oci_identity_policy.application_reads_package] } resource "oci_events_rule" "instance_updates" { compartment_id = var.compartment_ocid display_name = "incident-responder-${random_id.suffix.hex}" description = "Instance updates to the incident responder" is_enabled = true freeform_tags = local.tags condition_details { event_types = ["com.oraclecloud.computeapi.updateinstance"] } actions { actions { action_type = "FAAS" function_id = oci_functions_function.responder.id is_enabled = true } } }