Cloudflare APIでPermission Group一覧を取得する
事象
CloudflareのAPIを実行するためにカスタムトークンを作成しました。
アクセス許可は以下のように設定しました。
アカウント | API Tokens | 編集 |
---|
curlでエラー
APIを実行するとUnauthorized to access requested resource
で弾かれてしまいました。
$ TOKEN=xxxxxxxxxxx$ curl -X GET \ --url https://api.cloudflare.com/client/v4/user/tokens/permission_groups \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" | jq{ "success": false, "errors": [ { "code": 9109, "message": "Unauthorized to access requested resource" } ], "messages": [], "result": null}
Terraformでエラー
Terraformでも同じく発生しました。
ファイルの内容は以下のように設定
$ tree.├── README.md├── backend.tf├── main.tf├── provider.tf├── terraform.tfvars└── variables.tf
terraform { backend "local" { path = "./terraform.tfstate" }}
data "cloudflare_api_token_permission_groups" "all" {}
terraform { required_version = "1.3.7"
required_providers { cloudflare = { source = "cloudflare/cloudflare" version = "4.0.0-rc2" } }}
provider "cloudflare" { api_token = var.cloudflare_api_token}
variable "cloudflare_api_token" {}
cloudflare_api_token = "api_token"
terraform planを実行すると同じくerror listing API Token Permission Groups: Unauthorized to access requested resource (9109)
で弾かれました。
$ terraform init$ terraform plandata.cloudflare_api_token_permission_groups.all: Reading...╷│ Error: error listing API Token Permission Groups: Unauthorized to access requested resource (9109)││ with data.cloudflare_api_token_permission_groups.all,│ on main.tf line 1, in data "cloudflare_api_token_permission_groups" "all":│ 1: data "cloudflare_api_token_permission_groups" "all" {}│╵
解決
カスタムトークンを作成する
ではなく追加のトークンを作成
を選択します。
アクセス許可は以下のように設定しました。
ユーザー | APIトークン | 読み取り |
---|
(なぜ、ユーザー、APIトークンはグレーアウトされているのだろうか・・)
curl
$ TOKEN=xxxxxxxxx$ curl -X GET \ --url https://api.cloudflare.com/client/v4/user/tokens/permission_groups \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" | jq{ "result": [ { "id": "6ced5d0d69b1422396909a62c38ab41b", "name": "API Gateway Read", "description": "Grants read access to API-Gateway Management", "scopes": [ "com.cloudflare.api.account.zone" ] }, { "id": "f0235726de25444a84f704b7c93afadf",
Terraform
$ terraform plandata.cloudflare_api_token_permission_groups.all: Reading...data.cloudflare_api_token_permission_groups.all: Read complete after 1s [id=xxxxxxx]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
$ terraform applydata.cloudflare_api_token_permission_groups.all: Reading...data.cloudflare_api_token_permission_groups.all: Read complete after 1s [id=xxxxxx]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
まとめ
最初はほぼ確実にハマると思います。
今だけ(2023年2月)このような仕様になっているだけかも知れません。
誰かのお役に立てればと思います。
参考
What permission do I need to be able to read permission groups?