Closed toadjaune closed 2 weeks ago
Similar issue was closed before: https://github.com/terraform-providers/terraform-provider-google/issues/1724
But I think it would be a good addition to this provider!
In the meantime, you can use this: https://github.com/hectorj/terraform-provider-googlesiteverification
Edit: I'm interested in trying to contribute it to this repo. I'll take a shot at it soon-ish. Edit2: I do not have much free time for this at the moment. If someone else feels like taking a shot at it, do not hesitate.
@danawillow @hectorj and I are perfectly willing to assist in providing the required resources. If you feel it can be auto-generated, then that would be awesome. I do think it needs some work as the API is not orthogonal. ie. the create and update have a different signature.
If you already have handwritten code for the resource, then it's not mandatory to do it generated, especially if it's unusual. The one thing I would be very careful about is authentication- our guideline right now is that you should be able to use the same authentication method for all your resources, and based on https://github.com/hashicorp/terraform-provider-google/issues/1724#issuecomment-403012664 I'm not 100% sure you can.
@hectorj actually programmed it to work properly; if you look at the code at https://github.com/hectorj/terraform-provider-googlesiteverification, you can specify JSON service account key to authenticate with and that works. the site verification API did not work with the default Oauth 2.0 credentials, I actually created a separate service account to verify the domain with. See:
So this issue can be documented or even resolved in the resource definition.
This is absolutely required from an automation stand point and I am actually incredulous that this isn't standard in the provider or that the APIs aren't set up in such a way at GCP.
The above module doesn't work (at least not with latest versions of Terraform) and there is no documentation - please get this supported in the provider asap
At 2 years old and 44 upvotes, is there any progress on this or plans to add it? This totally breaks automation when creating GCS buckets. I managed to create a workaround which manually invokes the site verification APIs through http
data requests. The I use the following module to verify a domain. The downside is that the access token appears in the logs, but using a short lived one (2s) should help.
variable "project_id" { type = string }
variable "dns_name" { type = string }
variable "managed_dns_zone" { type = string }
variable "service_account_email" { type = string }
// Create an access token.
data "google_service_account_access_token" "verification" {
provider = google
target_service_account = "${var.service_account_email}"
scopes = ["https://www.googleapis.com/auth/siteverification", "https://www.googleapis.com/auth/siteverification.verify_only"]
lifetime = "2s"
}
// Invokes the Site Verification API to get a token.
// https://developers.google.com/site-verification/v1/invoking#verify
data "http" "api_verification" {
url = "https://www.googleapis.com/siteVerification/v1/token?access_token=${data.google_service_account_access_token.verification.access_token}"
method = "POST"
request_body = <<EOT
{
"verificationMethod": "DNS_TXT",
"site": {
"identifier": "${var.dns_name}",
"type": "INET_DOMAIN"
}
}
EOT
# Optional request headers
request_headers = {
Content-Type = "application/json"
}
lifecycle {
postcondition {
condition = contains([201, 204, 200], self.status_code)
error_message = self.body
}
}
depends_on = [
data.google_service_account_access_token.verification
]
}
// Domain verification for static storage url.
resource "google_dns_record_set" "storage_static_verification" {
project = var.project_id
name = var.dns_name
type = "TXT"
ttl = 300
managed_zone = var.managed_dns_zone
rrdatas = [jsondecode(data.http.api_verification.response_body)["token"]]
depends_on = [data.http.api_verification]
}
data "google_service_account_access_token" "verification_confirm" {
provider = google
target_service_account = "${var.service_account_email}"
scopes = ["https://www.googleapis.com/auth/siteverification", "https://www.googleapis.com/auth/siteverification.verify_only"]
lifetime = "2s"
}
// From https://developers.google.com/site-verification/v1/invoking#exampleInsert
data "http" "api_verification_insert" {
url = "https://www.googleapis.com/siteVerification/v1/webResource?verificationMethod=DNS_TXT&access_token=${data.google_service_account_access_token.verification_confirm.access_token}"
method = "POST"
request_body = <<EOT
{
"site": {
"identifier": "${var.dns_name}",
"type": "INET_DOMAIN"
}
}
EOT
request_headers = {
Content-Type = "application/json"
}
lifecycle {
postcondition {
condition = contains([201, 204, 200], self.status_code)
error_message = self.body
}
}
depends_on = [
data.google_service_account_access_token.verification_confirm
]
}
Any chance this could be added ? Ideally with OpenID Connect as the authentication method.
I was able to confirm that this API can use the same OAuth 2.0 credentials that the Terraform provider operates on, so this seems like it would make sense in the google
and google-beta
providers.
đź‘‹
We are also facing the same issue currently - a fix would be much apppreciated
Community Note
Description
Several operations on Google Cloud require you to validate domain ownership. For example, creating a GCS bucket for static website hosting. This is the usecase I met and the only one I know of, I suppose however there should be other affected resources.
There are currently no terraform resources for automating the validation process, nor to delegate domain ownership to a service account, which has several implications :
TL;DR : a lot of friction and manual work no matter what you do.
There is an API for automating site verification. I don't know if it can be used when authenticated as a service account, but even if not, being able to automate this would be a great step forward.
New or Affected Resource(s)
AWS have several resources requiring similar validation processes, my API proposal is based on the one used by aws_ses_domain_identity_verification
Potential Terraform Configuration
References
Relevant documentation was referenced above