|
| 1 | +######################################################################################## |
| 2 | +# The webhook-datasource module takes care of provisioning the necessary resources to make Sysdig's |
| 3 | +# backend able to ingest data from a single GCP project. |
| 4 | +# |
| 5 | +# Before applying the changes defined in this module, the following operations need to |
| 6 | +# be performed on the target GCP environment: |
| 7 | +# - Creating a GCP project |
| 8 | +# - Enabling the following APIs in the target environment (https://support.google.com/googleapi/answer/6158841?hl=en) |
| 9 | +# - Cloud Pub/Sub API |
| 10 | +# - Identity and Access Management (IAM) API |
| 11 | +# - IAM Service Account Credentials API |
| 12 | +# - Cloud Resource Manager API |
| 13 | +# |
| 14 | +# Given that, this module will take care of enabling the AuditLogs for the selected |
| 15 | +# project, direct them to a dedicated PubSub through a Sink and finally creating a Push |
| 16 | +# Subscription that will send the data to Sysdig's backend. This module takes also care |
| 17 | +# of creating the necessary service accounts along with the necessary policies to enable |
| 18 | +# pushing logs to Sysdig's system. |
| 19 | +######################################################################################## |
| 20 | + |
| 21 | +#-------------# |
| 22 | +# GCP Project # |
| 23 | +#-------------# |
| 24 | + |
| 25 | +data "google_project" "target_project" { |
| 26 | + project_id = var.project_id |
| 27 | +} |
| 28 | + |
| 29 | +#------------# |
| 30 | +# Audit Logs # |
| 31 | +#------------# |
| 32 | + |
| 33 | +resource "google_project_iam_audit_config" "audit_config" { |
| 34 | + project = var.project_id |
| 35 | + service = "allServices" |
| 36 | + |
| 37 | + audit_log_config { |
| 38 | + log_type = "ADMIN_READ" |
| 39 | + } |
| 40 | + |
| 41 | + audit_log_config { |
| 42 | + log_type = "DATA_READ" |
| 43 | + } |
| 44 | + |
| 45 | + audit_log_config { |
| 46 | + log_type = "DATA_WRITE" |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#------# |
| 51 | +# Sink # |
| 52 | +#------# |
| 53 | + |
| 54 | +resource "google_logging_project_sink" "ingestion_sink" { |
| 55 | + name = "${google_pubsub_topic.ingestion_topic.name}_sink" |
| 56 | + description = "Sysdig sink to direct the AuditLogs to the PubSub topic used for data gathering" |
| 57 | + |
| 58 | + # NOTE: The target destination is a PubSub topic |
| 59 | + destination = "pubsub.googleapis.com/projects/${var.project_id}/topics/${google_pubsub_topic.ingestion_topic.name}" |
| 60 | + |
| 61 | + filter = "protoPayload.@type = \"type.googleapis.com/google.cloud.audit.AuditLog\"" |
| 62 | + |
| 63 | + # NOTE: Used to create a dedicated writer identity and not using the default one |
| 64 | + unique_writer_identity = true |
| 65 | +} |
| 66 | + |
| 67 | +resource "google_pubsub_topic_iam_member" "publisher_iam_member" { |
| 68 | + project = google_pubsub_topic.ingestion_topic.project |
| 69 | + topic = google_pubsub_topic.ingestion_topic.name |
| 70 | + role = "roles/pubsub.publisher" |
| 71 | + member = google_logging_project_sink.ingestion_sink.writer_identity |
| 72 | +} |
| 73 | + |
| 74 | +#-----------------# |
| 75 | +# Ingestion topic # |
| 76 | +#-----------------# |
| 77 | + |
| 78 | +resource "google_pubsub_topic" "ingestion_topic" { |
| 79 | + name = "ingestion_topic" |
| 80 | + labels = var.labels |
| 81 | + project = var.project_id |
| 82 | + message_retention_duration = var.message_retention_duration |
| 83 | +} |
| 84 | + |
| 85 | +resource "google_pubsub_topic" "deadletter_topic" { |
| 86 | + name = "dl-${google_pubsub_topic.ingestion_topic.name}" |
| 87 | + |
| 88 | + project = var.project_id |
| 89 | + message_retention_duration = var.message_retention_duration |
| 90 | +} |
| 91 | + |
| 92 | +#-------------------# |
| 93 | +# Push Subscription # |
| 94 | +#-------------------# |
| 95 | + |
| 96 | +resource "google_service_account" "push_auth" { |
| 97 | + account_id = "ingestion-topic-push-auth" |
| 98 | + display_name = "Push Auth Service Account" |
| 99 | +} |
| 100 | + |
| 101 | +resource "google_service_account_iam_binding" "push_auth_binding" { |
| 102 | + service_account_id = google_service_account.push_auth.name |
| 103 | + role = "roles/iam.serviceAccountTokenCreator" |
| 104 | + |
| 105 | + members = [ |
| 106 | + "serviceAccount:${google_service_account.push_auth.email}", |
| 107 | + ] |
| 108 | +} |
| 109 | + |
| 110 | +resource "google_pubsub_subscription" "ingestion_topic_push_subscription" { |
| 111 | + name = "${google_pubsub_topic.ingestion_topic.name}_push_subscription" |
| 112 | + topic = google_pubsub_topic.ingestion_topic.name |
| 113 | + labels = var.labels |
| 114 | + ack_deadline_seconds = var.ack_deadline_seconds |
| 115 | + message_retention_duration = var.message_retention_duration |
| 116 | + |
| 117 | + push_config { |
| 118 | + push_endpoint = var.push_endpoint |
| 119 | + |
| 120 | + attributes = { |
| 121 | + x-goog-version = "v1" |
| 122 | + } |
| 123 | + |
| 124 | + oidc_token { |
| 125 | + service_account_email = google_service_account.push_auth.email |
| 126 | + audience = "sysdig_secure" |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + retry_policy { |
| 131 | + minimum_backoff = var.minimum_backoff |
| 132 | + maximum_backoff = var.maximum_backoff |
| 133 | + } |
| 134 | + |
| 135 | + dead_letter_policy { |
| 136 | + dead_letter_topic = google_pubsub_topic.deadletter_topic.id |
| 137 | + max_delivery_attempts = var.max_delivery_attempts |
| 138 | + } |
| 139 | +} |
| 140 | + |
0 commit comments