|
| 1 | +resource "google_service_account" "controller" { |
| 2 | + project = var.project_id |
| 3 | + account_id = "sysdig-ahs-${local.suffix}" |
| 4 | + display_name = "Sysdig Agentless Host Scanning" |
| 5 | +} |
| 6 | + |
| 7 | +resource "google_project_iam_custom_role" "controller" { |
| 8 | + project = var.project_id |
| 9 | + role_id = "${var.role_name}Controller${title(local.suffix)}" |
| 10 | + title = "Role for Sysdig Agentless Host Workers" |
| 11 | + permissions = [ |
| 12 | + # networks |
| 13 | + "compute.networks.list", |
| 14 | + "compute.networks.get", |
| 15 | + # instances |
| 16 | + "compute.instances.list", |
| 17 | + "compute.instances.get", |
| 18 | + # disks |
| 19 | + "compute.disks.list", |
| 20 | + "compute.disks.get", |
| 21 | + # workload identity federation |
| 22 | + "iam.serviceAccounts.getAccessToken", |
| 23 | + ] |
| 24 | +} |
| 25 | + |
| 26 | +resource "google_project_iam_binding" "controller_custom" { |
| 27 | + project = var.project_id |
| 28 | + role = google_project_iam_custom_role.controller.id |
| 29 | + |
| 30 | + members = [ |
| 31 | + "serviceAccount:${google_service_account.controller.email}", |
| 32 | + ] |
| 33 | +} |
| 34 | + |
| 35 | +resource "google_iam_workload_identity_pool" "agentless" { |
| 36 | + workload_identity_pool_id = "sysdig-ahs-${local.suffix}" |
| 37 | +} |
| 38 | + |
| 39 | +resource "google_iam_workload_identity_pool_provider" "agentless" { |
| 40 | + count = var.sysdig_backend != null ? 1 : 0 |
| 41 | + |
| 42 | + lifecycle { |
| 43 | + precondition { |
| 44 | + condition = (var.sysdig_backend != null && var.sysdig_account_id == null) |
| 45 | + error_message = "Cannot provide both sysdig_backend or sysdig_account_id" |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + workload_identity_pool_id = google_iam_workload_identity_pool.agentless.workload_identity_pool_id |
| 50 | + workload_identity_pool_provider_id = "sysdig-ahs-${local.suffix}" |
| 51 | + display_name = "Sysdig Agentless Controller" |
| 52 | + description = "AWS identity pool provider for Sysdig Secure Agentless Host Scanning" |
| 53 | + disabled = false |
| 54 | + |
| 55 | + attribute_condition = "attribute.aws_account==\"${var.sysdig_backend}\"" |
| 56 | + |
| 57 | + attribute_mapping = { |
| 58 | + "google.subject" = "assertion.arn" |
| 59 | + "attribute.aws_account" = "assertion.account" |
| 60 | + "attribute.role" = "assertion.arn.extract(\"/assumed-role/{role}/\")" |
| 61 | + "attribute.session" = "assertion.arn.extract(\"/assumed-role/{role_and_session}/\").extract(\"/{session}\")" |
| 62 | + } |
| 63 | + |
| 64 | + aws { |
| 65 | + account_id = var.sysdig_backend |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +resource "google_service_account_iam_member" "controller_custom" { |
| 70 | + count = var.sysdig_backend != null ? 1 : 0 |
| 71 | + |
| 72 | + lifecycle { |
| 73 | + precondition { |
| 74 | + condition = (var.sysdig_backend != null && var.sysdig_account_id == null) |
| 75 | + error_message = "Cannot provide both sysdig_backend or sysdig_account_id" |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + service_account_id = google_service_account.controller.name |
| 80 | + role = "roles/iam.workloadIdentityUser" |
| 81 | + member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.agentless.name}/attribute.aws_account/${var.sysdig_backend}" |
| 82 | +} |
| 83 | + |
| 84 | +resource "google_iam_workload_identity_pool_provider" "agentless_gcp" { |
| 85 | + count = var.sysdig_account_id != null ? 1 : 0 |
| 86 | + |
| 87 | + lifecycle { |
| 88 | + precondition { |
| 89 | + condition = (var.sysdig_backend == null && var.sysdig_account_id != null) |
| 90 | + error_message = "Cannot provide both sysdig_backend or sysdig_account_id" |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + workload_identity_pool_id = google_iam_workload_identity_pool.agentless.workload_identity_pool_id |
| 95 | + workload_identity_pool_provider_id = "sysdig-ahs-${local.suffix}-gcp" |
| 96 | + display_name = "Sysdig Agentless Controller" |
| 97 | + description = "GCP identity pool provider for Sysdig Secure Agentless Host Scanning" |
| 98 | + disabled = false |
| 99 | + |
| 100 | + attribute_condition = "google.subject == \"${var.sysdig_account_id}\"" |
| 101 | + |
| 102 | + attribute_mapping = { |
| 103 | + "google.subject" = "assertion.sub" |
| 104 | + "attribute.sa_id" = "assertion.sub" |
| 105 | + } |
| 106 | + |
| 107 | + oidc { |
| 108 | + issuer_uri = "https://accounts.google.com" |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +resource "google_service_account_iam_member" "controller_custom_gcp" { |
| 113 | + count = var.sysdig_account_id != null ? 1 : 0 |
| 114 | + |
| 115 | + lifecycle { |
| 116 | + precondition { |
| 117 | + condition = (var.sysdig_backend == null && var.sysdig_account_id != null) |
| 118 | + error_message = "Cannot provide both sysdig_backend or sysdig_account_id" |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + service_account_id = google_service_account.controller.name |
| 123 | + role = "roles/iam.workloadIdentityUser" |
| 124 | + member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.agentless.name}/attribute.sa_id/${var.sysdig_account_id}" |
| 125 | +} |
0 commit comments