# evidence-capture / oci / t0 "Quick start" # # Add the freeform tag incident= to a Compute instance, and a function: # 1. backs up its boot volume and every attached block volume # 2. cuts it off, with a Zero Trust Packet Routing security attribute that # no policy allows traffic for # # The tag is the trigger because anyone on the incident can add one from the # console or the command line, and the Audit service records who did. # # Oracle Cloud has no termination protection for an instance, and no managed # way to capture memory. The backups are fixed the moment they are requested, # so take them before anyone touches the instance. # # The function's code is inline below. Its archive needs the Oracle SDK, too # large to send through the API, so apply packages it on your machine and # uploads it with the OCI CLI. Needs python3 with pip, zip, and the OCI CLI. # Change the four values below, then: # terraform init && terraform apply (credentials from ~/.oci/config) # # Verify, on a test instance you do not mind isolating: # terraform output verify locals { tenancy_ocid = "ocid1.tenancy.oc1..replace-me" # change me compartment_ocid = "ocid1.compartment.oc1..replace-me" # change me: where your instances run region = "us-ashburn-1" # change me subnet_ocid = "ocid1.subnet.oc1..replace-me" # change me: a private subnet with a service gateway, for the function tags = { owner = "security-operations" environment = "prod" data_classification = "restricted" managed_by = "terraform" } } terraform { required_version = ">= 1.6" required_providers { oci = { source = "oracle/oci" version = ">= 9.3.0, < 10.0.0" # code-only functions arrived in 9.3.0 } random = { source = "hashicorp/random" version = "~> 3.6" } } } provider "oci" { region = local.region } data "oci_objectstorage_namespace" "this" { compartment_id = local.compartment_ocid } resource "random_id" "suffix" { byte_length = 3 } # The quarantine mark. A resource carrying a security attribute passes only # traffic that a Zero Trust Packet Routing policy allows, whatever its # security lists and network security groups say. No policy names this one, # so nothing passes. resource "oci_security_attribute_security_attribute_namespace" "incident" { compartment_id = local.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" { security_attribute_namespace_id = oci_security_attribute_security_attribute_namespace.incident.id name = "quarantine" description = "Set by the incident responder. No ZPR policy allows traffic for it, so the resource is cut off." } # The function acts as itself, through a dynamic group. resource "oci_identity_dynamic_group" "responder" { compartment_id = local.tenancy_ocid name = "incident-responder-${random_id.suffix.hex}" description = "The incident responder function" matching_rule = "ALL {resource.type = 'fnfunc', resource.compartment.id = '${local.compartment_ocid}'}" freeform_tags = local.tags } resource "oci_identity_policy" "responder" { compartment_id = local.compartment_ocid name = "incident-responder-${random_id.suffix.hex}" description = "Lets the responder read instances, back up their volumes, and mark them. No deletes, no power actions." freeform_tags = local.tags statements = [ "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to read instances in compartment id ${local.compartment_ocid}", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to manage instances in compartment id ${local.compartment_ocid} where request.permission = 'INSTANCE_UPDATE'", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to inspect volume-attachments in compartment id ${local.compartment_ocid}", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to inspect vnic-attachments in compartment id ${local.compartment_ocid}", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to use volumes in compartment id ${local.compartment_ocid}", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to manage boot-volume-backups in compartment id ${local.compartment_ocid} where request.permission = 'BOOT_VOLUME_BACKUP_CREATE'", "Allow dynamic-group id ${oci_identity_dynamic_group.responder.id} to manage volume-backups in compartment id ${local.compartment_ocid} where request.permission = 'VOLUME_BACKUP_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" { compartment_id = local.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 dynamic-group id ${oci_identity_dynamic_group.responder.id} to use security-attribute-namespace in tenancy where target.security-attribute-namespace.name = '${oci_security_attribute_security_attribute_namespace.incident.name}'", ] } # The function's package: the code below plus the Oracle SDK, built and # uploaded during apply. A change to the code builds and uploads a new one. resource "oci_objectstorage_bucket" "source" { compartment_id = local.compartment_ocid namespace = data.oci_objectstorage_namespace.this.namespace name = "incident-responder-src-${random_id.suffix.hex}" access_type = "NoPublicAccess" freeform_tags = local.tags } locals { package = "responder-${sha256(local.code)}.zip" } resource "terraform_data" "package" { triggers_replace = local.package input = { object = local.package bucket = oci_objectstorage_bucket.source.name namespace = data.oci_objectstorage_namespace.this.namespace region = local.region } provisioner "local-exec" { interpreter = ["bash", "-c"] environment = { CODE = local.code OBJECT = local.package BUCKET = oci_objectstorage_bucket.source.name NAMESPACE = data.oci_objectstorage_namespace.this.namespace REGION = local.region } command = <<-SH set -euo pipefail dir=$(mktemp -d) mkdir -p "$dir/archive/function" "$dir/archive/python" printf '%s' "$CODE" > "$dir/archive/function/func.py" # Wheels for the runtime's platform, whatever machine this runs on. python3 -m pip install --quiet --disable-pip-version-check --no-compile --only-binary=:all: \ --platform manylinux2014_x86_64 --implementation cp --python-version 3.12 \ --target "$dir/archive/python" oci (cd "$dir/archive" && zip -qr ../responder.zip .) oci os object put --region "$REGION" -ns "$NAMESPACE" -bn "$BUCKET" --name "$OBJECT" --file "$dir/responder.zip" --force rm -rf "$dir" SH } # On destroy, or before a new package replaces this one, remove the object, # so the bucket can go too. provisioner "local-exec" { when = destroy interpreter = ["bash", "-c"] command = "oci os object delete --region ${self.input.region} -ns ${self.input.namespace} -bn ${self.input.bucket} --name ${self.input.object} --force || true" } } # The application reads its package as itself. Its principal type is fnapp, # which the dynamic group above does not cover, so this names it directly. resource "oci_identity_policy" "application_reads_package" { compartment_id = local.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 ${local.compartment_ocid} where all {request.principal.type = 'fnapp', request.principal.compartment.id = '${local.compartment_ocid}', target.bucket.name = '${oci_objectstorage_bucket.source.name}'}", ] } # The current managed Python runtime. Change the version if the lookup finds # nothing; oci fn functions-runtime-version list --all shows them. data "oci_functions_functions_runtime_versions" "python" { language_version = "3.12" is_current_version = true state = "ACTIVE" } 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 = local.compartment_ocid display_name = "incident-responder-${random_id.suffix.hex}" subnet_ids = [local.subnet_ocid] shape = "GENERIC_X86" freeform_tags = local.tags } locals { code = <<-PY """Preserve, then isolate, a Compute instance tagged incident=. Order of work: 1. back up the boot volume and every attached block volume 2. cut it off: the quarantine security attribute, which no ZPR policy allows 3. mark it done, so the tag change this makes does not start it again """ import io import json import logging import os import oci NAMESPACE = os.environ["QUARANTINE_NAMESPACE"] ATTRIBUTE = os.environ["QUARANTINE_ATTRIBUTE"] DONE = "preserved-and-isolated" def handler(ctx, data: io.BytesIO = None): event = json.loads(data.getvalue()) instance_id = event.get("data", {}).get("resourceId", "") if not instance_id.startswith("ocid1.instance."): return signer = oci.auth.signers.get_resource_principals_signer() compute = oci.core.ComputeClient({}, signer=signer) storage = oci.core.BlockstorageClient({}, signer=signer) instance = compute.get_instance(instance_id).data tags = dict(instance.freeform_tags or {}) incident = tags.get("incident") # Marking the instance done changes its tags, which fires this again. Stop there. if not incident or tags.get("incident-status") == DONE: return logging.getLogger().info("incident %s: preserving %s", incident, instance_id) marks = {"incident": incident, "source-instance": instance_id} # 1. Back up every volume. A backup is fixed when it is requested, and a # full one stands alone. boots = compute.list_boot_volume_attachments(instance.availability_domain, instance.compartment_id, instance_id=instance_id).data for a in boots: storage.create_boot_volume_backup(oci.core.models.CreateBootVolumeBackupDetails( boot_volume_id=a.boot_volume_id, type="FULL", display_name=f"ir-{incident}-boot", freeform_tags=marks)) for a in compute.list_volume_attachments(instance.compartment_id, instance_id=instance_id).data: if a.lifecycle_state == "ATTACHED": storage.create_volume_backup(oci.core.models.CreateVolumeBackupDetails( volume_id=a.volume_id, type="FULL", display_name=f"ir-{incident}-{a.volume_id[-8:]}", freeform_tags=marks)) # 2. Cut it off. Tags and attributes are replaced whole on update, so the # ones already there are kept. attributes = dict(instance.security_attributes or {}) attributes.setdefault(NAMESPACE, {})[ATTRIBUTE] = {"value": "true", "mode": "enforce"} tags["incident-status"] = DONE compute.update_instance(instance_id, oci.core.models.UpdateInstanceDetails( security_attributes=attributes, freeform_tags=tags)) logging.getLogger().info("incident %s: %s preserved and isolated", incident, instance_id) PY } resource "oci_functions_function" "responder" { application_id = oci_functions_application.responder.id display_name = "preserve" memory_in_mbs = "512" timeout_in_seconds = 300 freeform_tags = local.tags config = { QUARANTINE_NAMESPACE = oci_security_attribute_security_attribute_namespace.incident.name QUARANTINE_ATTRIBUTE = oci_security_attribute_security_attribute.quarantine.name } 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 = local.package } 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_marks, oci_identity_policy.application_reads_package, terraform_data.package] } # Every update to an instance in the compartment reaches the function, which # reads the instance's tags and returns at once when incident is not among # them. resource "oci_events_rule" "instance_updates" { compartment_id = local.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 } } } output "verify" { description = "Tag a test instance, then confirm the backups and the quarantine attribute." value = <<-EOT oci compute instance update --instance-id --freeform-tags '{"incident":"drill-1"}' --force # this replaces the instance's freeform tags; merge your own in oci bv boot-volume-backup list --compartment-id ${local.compartment_ocid} --query "data[?\"freeform-tags\".incident=='drill-1'].\"display-name\"" oci compute instance get --instance-id --query 'data."security-attributes"' # ${oci_security_attribute_security_attribute_namespace.incident.name}.quarantine ssh # times out oci compute instance get --instance-id --query 'data."freeform-tags"' # incident-status: preserved-and-isolated EOT }