0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cloud Run Domain Mapping — サービスアカウントのドメイン所有権エラー

0
Posted at

背景

Terraform で google_cloud_run_domain_mapping を作成する際に以下のエラーが発生した。

Error: Error waiting to create DomainMapping: resource is in failed state "Ready:False",
message: Caller is not authorized to administer the domain api.dev.example.com.

ブラウザで Search Console にてドメインの所有権確認は済んでいたが、エラーが解消されなかった。

原因

Cloud Run の Domain Mapping は 実行者(Terraform を動かすアカウント) が Google Site Verification 上でドメインの "Verified Owner" であることを要求するらしい。

Search Console での所有権確認は 個人アカウント に紐づくため、GitHub Actions 等で使うサービスアカウント(例: sa-cloud-build@PROJECT.iam.gserviceaccount.com)には所有権が伝わらない。

また、Search Console の UI はサービスアカウントのメールアドレスを Google アカウントとして検索するため、追加しようとすると「メールアドレスが見つかりませんでした」となる。

解決方法

Site Verification API を直接呼び、サービスアカウントを Delegated Owner として追加する。

1. Site Verification API を有効化

gcloud services enable siteverification.googleapis.com --project=PROJECT_ID

2. Application Default Credentials で再認証(siteverification スコープを追加)

gcloud auth application-default login \
  --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/siteverification"

gcloud auth application-default set-quota-project PROJECT_ID

実行アカウントは Search Console でドメインを確認済みの オーナーアカウント であること。

3. 現在の verified resource を確認

curl -s \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "X-Goog-User-Project: PROJECT_ID" \
  "https://www.googleapis.com/siteVerification/v1/webResource" | python3 -m json.tool

レスポンス例:

{
  "items": [
    {
      "id": "dns%3A%2F%2Fexample.com",
      "site": { "type": "INET_DOMAIN", "identifier": "example.com" },
      "owners": ["admin@example.com"]
    }
  ]
}

4. サービスアカウントを owners に追加(PUT でリソース全体を更新)

curl -s -X PUT \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "X-Goog-User-Project: PROJECT_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "site": {"type": "INET_DOMAIN", "identifier": "example.com"},
    "owners": [
      "admin@example.com",
      "sa-cloud-build@PROJECT.iam.gserviceaccount.com"
    ]
  }' \
  "https://www.googleapis.com/siteVerification/v1/webResource/dns%3A%2F%2Fexample.com" | python3 -m json.tool

レスポンスの owners にサービスアカウントが含まれていれば成功。

5. terraform apply を再実行

補足

  • owners の更新は /owners サブリソースへの POST ではなく、リソース本体への PUT で行う(/owners エンドポイントは 404)。
  • ルートドメイン(example.com)を所有者確認済みであれば、サブドメイン(api.dev.example.com 等)の Domain Mapping にも適用される。
  • サービスアカウントを追加した後は、Search Console の「ユーザーと権限」画面にも反映される。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?