hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.29k stars 1.72k forks source link

cloud_run_service cross-project secrets referencing #11385

Open marekaf opened 2 years ago

marekaf commented 2 years ago

Community Note

Terraform Version

terraform -v
Terraform v1.1.7
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v4.15.0
+ provider registry.terraform.io/hashicorp/google-beta v4.2.1
+ provider registry.terraform.io/hashicorp/random v3.1.2

Affected Resource(s)

Terraform Configuration Files

resource "google_cloud_run_service" "main" {
  name     = "main"
  location = var.region
  autogenerate_revision_name = true
  metadata {
    annotations = {
      "run.googleapis.com/secrets" = "db-password:projects/1234567890/secrets/db-password"
    }
  }

  template {
    spec {
      service_account_name = google_service_account.main.email
      timeout_seconds      = 60

      containers {
        image = var.image

        resources {
          limits = {
            cpu    = "1"
            memory = "128Mi"
          }
        }

        ports {
          name           = "http1"
          container_port = 80
        }

        env {
          name = "DB_PASSWORD"
          value_from {
            secret_key_ref {
              name = "db-password"
              key  = "1" // secret version
            }
          }
        }
      }
    }
    metadata {
      annotations = {}
    }
  }
}

Debug Output

Panic Output

Expected Behavior

Terraform should create the cloud run service and pull a secret from another GCP project without any errors.

Actual Behavior

Error waiting to create Service: resource is in failed state "Ready:False", message: spec.template.spec.container.env[0].value_from.secret_key_ref.name: Secret projects/3333333333/secrets/db-secret/versions/1 was not found

For some reason, terraform is trying to look for the secret in this GCP project, even though the annotation properly references the aliased secret & secret id of another project

Steps to Reproduce

  1. terraform apply

Important Factoids

doing this in console by hand works without any issues

if I take the random alias that is created by doing this manually in the console, the alias looks like this

secret-e0e199b5-8675-438c-a34a-3edcb93008a3:projects/1234567890/secrets/db-password

if I put this as the alias to my .tf file and run plan, drift is shown

If I have the same configuration in terraform, I do terraform plan and no drift

References

marekaf commented 2 years ago

Actually, nevermind... I just figured it out.

The annotations for secret alias has to be BOTH in metadata.annotations as well as template.metadata.annotations.

RIP my time. Hope this helps somebody before they blow their brains out.

resource "google_cloud_run_service" "main" {
  name     = "main"
  location = var.region
  autogenerate_revision_name = true
  metadata {
    annotations = {
      "run.googleapis.com/secrets" = "db-password:projects/1234567890/secrets/db-password"
    }
  }

  template {
    spec {
      service_account_name = google_service_account.main.email
      timeout_seconds      = 60

      containers {
        image = var.image

        resources {
          limits = {
            cpu    = "1"
            memory = "128Mi"
          }
        }

        ports {
          name           = "http1"
          container_port = 80
        }

        env {
          name = "DB_PASSWORD"
          value_from {
            secret_key_ref {
              name = "db-password"
              key  = "1" // secret version
            }
          }
        }
      }
    }
    metadata {
    annotations = {
      "run.googleapis.com/secrets" = "db-password:projects/1234567890/secrets/db-password"
      }
    }
  }
}
jketcham commented 1 year ago

Hope this helps somebody before they blow their brains out.

πŸ™Œ This really should be made more clear in the docs, adding an example would be great.

When I set this up with provider version 4.42.0, I only needed to add the annotation to the template block, and it returned with an error when I tried to add it to the top level metadata.annotations for the cloud run service itself.