TerraformでGoogle Site Verificationする
前置き
GCPを使っているとドメインの所有権を確認されることがあります。
例えば Cloud Runのカスタムドメインを設定する 時は、ドメインの所有権を確認しておく必要があります。
ドメインの所有権を確認するために、手動でGoogle Search Consoleから検証トークンを取得して所有者を登録することもあるかもしれませんが、今回はそれをterraformで設定しようと思います。
Google Site Verification API
ドメインの所有権を確認する際に使用するAPIは Google Site Verification API です。名前の通りですね。
terraformでドメインの所有権を確認する際にはこのAPIについて把握することが重要になります。
ポイントとしては以下です。
- OAuth 2.0のスコープ情報を追加する
- ドメインの所有権を持たせたいユーザ(あるいはサービスアカウント)で検証トークンを取得する
-
google_site_verification_web_resource
で所有権の登録を行う
認証について
「terraformコマンドを実行するユーザ(あるいはサービスアカウント)にドメインの所有権を持たせたい」のか、「terraformコマンドを実行するユーザ(あるいはサービスアカウント)とは別のサービスアカウントにドメインの所有権を持たせたい」のかで必要な設定が変わります。
今回は後者の方法を紹介します。impersonateするために、 roles/iam.serviceAccountTokenCreator
が必要です。
前者の場合、ADCにscopesを付けたり、quota projectを設定したりする必要があり、意外と面倒です。
google_site_verification_token
検証トークンを取得するdata sourceです。
WebResource: getToken
に相当します。
google_site_verification_web_resource
ドメインの所有権の登録を行うリソースです。
WebResource: insert
に相当するもので、DNSレコードを確認し、検証に成功すれば所有者として登録されます。
コード
まずは provider.tf
です。
今回はDNSとしてRoute53を使用しているので、もしCloud DNSを使っている場合はaws providerは不要です。
terraform {
backend "local" {}
required_providers {
google = {
version = "~> 6.37.0"
}
aws = {
version = "~> 5.99.1"
}
}
}
provider "google" {
project = ${project_id}
region = "asia-northeast1"
zone = "asia-northeast1-c"
scopes = [
# Default scopes
# https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/sqlservice.login",
# Site Verification API scopes
# https://developers.google.com/site-verification/v1/getting_started?hl=ja#OAuth2Authorizing
"https://www.googleapis.com/auth/siteverification",
"https://www.googleapis.com/auth/siteverification.verify_only",
]
# 今回はサービスアカウントを登録するのでimpersonateする
impersonate_service_account = ${service_account_email}
}
provider "aws" {
region = "ap-northeast-1"
}
verification_method
では DNS_TXT
と DNS_CNAME
がありますが、今回は DNS_CNAME
を使ってみます。 DNS_CNAME
の場合、 token
の前半がサブドメインの名前、後半がトークンになることに注意しましょう。
Route53以外のDNSを使う場合は適宜読み替えてください。
data "google_site_verification_token" "default" {
type = "INET_DOMAIN"
identifier = ${domain}
verification_method = "DNS_CNAME"
}
locals {
# DNS_CNAME
# トークンはスペースで区切られた 2 つの部分で構成されます。最初の部分は新しい CNAME レコードの名前、2 番目の部分は新しい CNAME レコードの値です。
# https://developers.google.com/site-verification/v1/getting_started?hl=ja#tokens
token = split(" ", data.google_site_verification_token.default.token)
token_name = local.token[0]
token_value = local.token[1]
}
resource "aws_route53_record" "default" {
zone_id = var.hosted_zone_id
name = "${local.token_name}.${domain}"
type = "CNAME"
ttl = 300
records = [local.token_value]
}
resource "google_site_verification_web_resource" "default" {
verification_method = data.google_site_verification_token.default.verification_method
site {
type = data.google_site_verification_token.default.type
identifier = data.google_site_verification_token.default.identifier
}
depends_on = [
aws_route53_record.default,
]
}
ドメインの所有者の追加
既に登録されている所有者が追加の所有者を登録する場合は、 google_site_verification_owner
を使います。
Discussion