# network-flow-logs / oci / t1 "Standard" # # Flow logs for several subnets, searchable for up to 180 days, copied by # Connector Hub to an Object Storage bucket whose retention rule keeps them # longer, and saved searches for the questions an investigation starts with. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) scope = "${var.compartment_ocid}/${oci_logging_log_group.flows.id}" searches = { flow-logs-rejected = { description = "Connections the network refused, newest first." query = "search \"${local.scope}\" | where data.action = 'REJECT' | sort by datetime desc" } flow-logs-rejected-by-source = { description = "Refused connections counted by source address." query = "search \"${local.scope}\" | where data.action = 'REJECT' | summarize count() as flows by data.sourceAddress | sort by flows desc" } flow-logs-one-address = { description = "Everything one address talked to. Replace REPLACE-WITH-IP." query = "search \"${local.scope}\" | where data.sourceAddress = 'REPLACE-WITH-IP' or data.destinationAddress = 'REPLACE-WITH-IP' | sort by datetime desc" } } } resource "oci_logging_log_group" "flows" { compartment_id = var.compartment_ocid display_name = "vcn-flow-logs" description = "Flow logs for the subnets named in the network flow logs template." freeform_tags = local.tags } resource "oci_logging_log" "subnet" { for_each = var.subnets display_name = "flows-${each.key}" log_group_id = oci_logging_log_group.flows.id log_type = "SERVICE" is_enabled = true retention_duration = var.search_days freeform_tags = local.tags configuration { compartment_id = var.compartment_ocid source { category = "all" resource = each.value service = "flowlogs" source_type = "OCISERVICE" } } } resource "oci_logging_log_saved_search" "investigation" { for_each = local.searches compartment_id = var.compartment_ocid name = each.key description = each.value.description query = each.value.query freeform_tags = local.tags } # --------------------------------------------------------------------------- # The archive # --------------------------------------------------------------------------- data "oci_objectstorage_namespace" "this" { compartment_id = var.compartment_ocid } resource "oci_objectstorage_bucket" "archive" { compartment_id = var.compartment_ocid namespace = data.oci_objectstorage_namespace.this.namespace name = "vcn-flow-logs-archive" access_type = "NoPublicAccess" storage_tier = "Standard" versioning = "Disabled" freeform_tags = local.tags # Retention rules and versioning cannot be on at the same time. The rule # already stops overwrites and deletes. retention_rules { display_name = "flow-log-retention" duration { time_amount = var.archive_days time_unit = "DAYS" } } } # Deleting this policy, or moving the connector out of this compartment, # stops the archive copy, and nothing here reports it. The daily drift plan # in Hardened is what notices. resource "oci_identity_policy" "connector" { compartment_id = var.compartment_ocid name = "vcn-flow-logs-archive" description = "Lets the flow log connector write into its archive bucket." freeform_tags = local.tags statements = [ "Allow any-user to manage objects in compartment id ${var.compartment_ocid} where all {request.principal.type='serviceconnector', target.bucket.name='${oci_objectstorage_bucket.archive.name}', request.principal.compartment.id='${var.compartment_ocid}'}", ] } resource "oci_sch_service_connector" "archive" { compartment_id = var.compartment_ocid display_name = "vcn-flow-logs-archive" description = "Copies every flow log record to the archive bucket." freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = oci_logging_log_group.flows.id } } target { kind = "objectStorage" bucket = oci_objectstorage_bucket.archive.name namespace = data.oci_objectstorage_namespace.this.namespace object_name_prefix = "flows" } depends_on = [oci_identity_policy.connector] }