|
| 1 | +#------------------------------------------------------------------# |
| 2 | +# Fetch and compute required data for Workload Identity Federation # |
| 3 | +#------------------------------------------------------------------# |
| 4 | + |
| 5 | +data "sysdig_secure_trusted_cloud_identity" "trusted_identity" { |
| 6 | + cloud_provider = "gcp" |
| 7 | +} |
| 8 | + |
| 9 | +data "google_project" "project" { |
| 10 | + project_id = var.project_id |
| 11 | +} |
| 12 | + |
| 13 | +// suffix to uniquely identify WIF pool and provider during multiple installs. If suffix value is not provided, this will generate a random value. |
| 14 | +resource "random_id" "suffix" { |
| 15 | + count = var.suffix == null ? 1 : 0 |
| 16 | + byte_length = 3 |
| 17 | +} |
| 18 | + |
| 19 | +locals { |
| 20 | + suffix = var.suffix == null ? random_id.suffix[0].hex : var.suffix |
| 21 | +} |
| 22 | + |
| 23 | +resource "google_service_account" "posture_auth" { |
| 24 | + account_id = "sysdig-posture-${local.suffix}" |
| 25 | + display_name = "Sysdig Config Posture Auth Service Account" |
| 26 | + project = var.project_id |
| 27 | +} |
| 28 | + |
| 29 | +resource "google_service_account_iam_binding" "posture_auth_binding" { |
| 30 | + service_account_id = google_service_account.posture_auth.name |
| 31 | + role = "roles/iam.workloadIdentityUser" |
| 32 | + |
| 33 | + members = [ |
| 34 | + "serviceAccount:${google_service_account.posture_auth.email}", |
| 35 | + ] |
| 36 | +} |
| 37 | + |
| 38 | +#------------------------------------------------------------# |
| 39 | +# Configure Workload Identity Federation for auth # |
| 40 | +# See https://cloud.google.com/iam/docs/access-resources-aws # |
| 41 | +#------------------------------------------------------------# |
| 42 | + |
| 43 | +resource "google_iam_workload_identity_pool" "posture_auth_pool" { |
| 44 | + project = var.project_id |
| 45 | + workload_identity_pool_id = "sysdig-posture-${local.suffix}" |
| 46 | +} |
| 47 | + |
| 48 | +resource "google_iam_workload_identity_pool_provider" "posture_auth_pool_provider" { |
| 49 | + project = var.project_id |
| 50 | + workload_identity_pool_id = google_iam_workload_identity_pool.posture_auth_pool.workload_identity_pool_id |
| 51 | + workload_identity_pool_provider_id = "sysdig-posture-${local.suffix}" |
| 52 | + display_name = "Sysdigcloud config posture auth" |
| 53 | + description = "AWS identity pool provider for Sysdig Secure Data Config Posture resources" |
| 54 | + disabled = false |
| 55 | + |
| 56 | + attribute_condition = "attribute.aws_role==\"arn:aws:sts::${data.sysdig_secure_trusted_cloud_identity.trusted_identity.aws_account_id}:assumed-role/${data.sysdig_secure_trusted_cloud_identity.trusted_identity.aws_role_name}/${var.external_id}\"" |
| 57 | + |
| 58 | + attribute_mapping = { |
| 59 | + "google.subject" = "assertion.arn", |
| 60 | + "attribute.aws_role" = "assertion.arn" |
| 61 | + } |
| 62 | + |
| 63 | + aws { |
| 64 | + account_id = data.sysdig_secure_trusted_cloud_identity.trusted_identity.aws_account_id |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#--------------------------------------------------------------------------------------------- |
| 69 | +# role permissions for CSPM (GCP Predefined Roles for Sysdig Cloud Secure Posture Management) |
| 70 | +#--------------------------------------------------------------------------------------------- |
| 71 | +resource "google_project_iam_member" "cspm" { |
| 72 | + for_each = var.is_organizational ? [] : toset(["roles/cloudasset.viewer", "roles/iam.workloadIdentityUser", "roles/logging.viewer", "roles/cloudfunctions.viewer", "roles/cloudbuild.builds.viewer", "roles/orgpolicy.policyViewer"]) |
| 73 | + |
| 74 | + project = var.project_id |
| 75 | + role = each.key |
| 76 | + member = "serviceAccount:${google_service_account.posture_auth.email}" |
| 77 | +} |
| 78 | + |
| 79 | +# attaching WIF as a member to the service account for auth |
| 80 | +resource "google_service_account_iam_member" "custom_auth" { |
| 81 | + service_account_id = google_service_account.posture_auth.name |
| 82 | + role = "roles/iam.workloadIdentityUser" |
| 83 | + member = "principalSet://iam.googleapis.com/projects/${data.google_project.project.number}/locations/global/workloadIdentityPools/${google_iam_workload_identity_pool.posture_auth_pool.workload_identity_pool_id}/attribute.aws_role/arn:aws:sts::${data.sysdig_secure_trusted_cloud_identity.trusted_identity.aws_account_id}:assumed-role/${data.sysdig_secure_trusted_cloud_identity.trusted_identity.aws_role_name}/${var.external_id}" |
| 84 | +} |
| 85 | + |
| 86 | +#-------------------------------------------------------------------------------------------------------------- |
| 87 | +# Call Sysdig Backend to add the service-principal integration for Config Posture to the Sysdig Cloud Account |
| 88 | +# |
| 89 | +# Note (optional): To ensure this gets called after all cloud resources are created, add |
| 90 | +# explicit dependency using depends_on |
| 91 | +#-------------------------------------------------------------------------------------------------------------- |
| 92 | +resource "sysdig_secure_cloud_auth_account_component" "google_service_principal" { |
| 93 | + account_id = var.sysdig_secure_account_id |
| 94 | + type = "COMPONENT_SERVICE_PRINCIPAL" |
| 95 | + instance = "secure-posture" |
| 96 | + verion = "v0.1.0" |
| 97 | + service_principal_metadata = jsonencode({ |
| 98 | + gcp = { |
| 99 | + service_principal = { |
| 100 | + workload_identity_federation = { |
| 101 | + pool_id = google_iam_workload_identity_pool.posture_auth_pool.workload_identity_pool_id |
| 102 | + pool_provider_id = google_iam_workload_identity_pool_provider.posture_auth_pool_provider.workload_identity_pool_provider_id |
| 103 | + project_number = data.google_project.project.number |
| 104 | + } |
| 105 | + email = google_service_account.posture_auth.email |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | +} |
0 commit comments