# data-perimeter / gcp / t1 "Standard" # # A VPC Service Controls perimeter around several projects, starting in dry # run, with an office network allowed in, named identities allowed in from # anywhere, named projects allowed to receive data, and an alert on every # violation across the organisation. locals { resources = [for n in var.project_numbers : "projects/${n}"] levels = length(var.office_ranges) > 0 ? [google_access_context_manager_access_level.office[0].name] : [] } resource "google_access_context_manager_access_level" "office" { count = length(var.office_ranges) > 0 ? 1 : 0 parent = var.access_policy name = "${var.access_policy}/accessLevels/${var.perimeter_name}_office" title = "${var.perimeter_name}-office" basic { conditions { ip_subnetworks = var.office_ranges } } } resource "google_access_context_manager_service_perimeter" "this" { parent = var.access_policy name = "${var.access_policy}/servicePerimeters/${var.perimeter_name}" title = var.perimeter_name use_explicit_dry_run_spec = var.dry_run # Enforced configuration, used when dry run is off. dynamic "status" { for_each = var.dry_run ? [] : [1] content { resources = local.resources restricted_services = var.restricted_services access_levels = local.levels dynamic "ingress_policies" { for_each = length(var.admin_identities) > 0 ? [1] : [] content { title = "admins" ingress_from { identities = var.admin_identities sources { access_level = "*" } } ingress_to { resources = ["*"] operations { service_name = "*" } } } } dynamic "egress_policies" { for_each = var.allowed_egress content { title = "to-${egress_policies.key}" egress_from { identity_type = "ANY_IDENTITY" } egress_to { resources = ["projects/${egress_policies.key}"] dynamic "operations" { for_each = egress_policies.value content { service_name = operations.value method_selectors { method = "*" } } } } } } } } # The same configuration, logged but not enforced, while dry run is on. dynamic "spec" { for_each = var.dry_run ? [1] : [] content { resources = local.resources restricted_services = var.restricted_services access_levels = local.levels dynamic "ingress_policies" { for_each = length(var.admin_identities) > 0 ? [1] : [] content { title = "admins" ingress_from { identities = var.admin_identities sources { access_level = "*" } } ingress_to { resources = ["*"] operations { service_name = "*" } } } } dynamic "egress_policies" { for_each = var.allowed_egress content { title = "to-${egress_policies.key}" egress_from { identity_type = "ANY_IDENTITY" } egress_to { resources = ["projects/${egress_policies.key}"] dynamic "operations" { for_each = egress_policies.value content { service_name = operations.value method_selectors { method = "*" } } } } } } } } } # Violations are written to the project that was called, so an organisation # sink gathers them from every project into one bucket first. locals { violations = "protoPayload.metadata.\"@type\"=\"type.googleapis.com/google.cloud.audit.VpcServiceControlAuditMetadata\"" } resource "google_logging_project_bucket_config" "violations" { project = var.security_project location = "global" bucket_id = "vpc-sc-violations" retention_days = 400 description = "Every VPC Service Controls violation in the organisation, dry run or enforced." } resource "google_logging_organization_sink" "violations" { org_id = var.org_id name = "vpc-sc-violations" destination = "logging.googleapis.com/${google_logging_project_bucket_config.violations.id}" filter = local.violations include_children = true } resource "google_project_iam_member" "sink_writes" { project = var.security_project role = "roles/logging.bucketWriter" member = google_logging_organization_sink.violations.writer_identity } resource "google_logging_metric" "violations" { project = var.security_project name = "vpc-sc-violations" bucket_name = google_logging_project_bucket_config.violations.id filter = local.violations metric_descriptor { metric_kind = "DELTA" value_type = "INT64" } } resource "google_monitoring_notification_channel" "email" { for_each = toset(var.alert_emails) project = var.security_project display_name = "Perimeter violations: ${each.value}" type = "email" labels = { email_address = each.value } } resource "google_monitoring_alert_policy" "violations" { project = var.security_project display_name = "VPC Service Controls violation" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = "any violation, dry run or enforced" condition_threshold { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.violations.name}\"" comparison = "COMPARISON_GT" threshold_value = 0 duration = "0s" aggregations { alignment_period = "300s" per_series_aligner = "ALIGN_SUM" cross_series_reducer = "REDUCE_SUM" } } } documentation { content = "A call crossed the perimeter, or would have in dry run. Open the vpc-sc-violations bucket in ${var.security_project}; each entry names the caller, the service, and the reason." } }