Skip to content

Cloudflare APIでPermission Group一覧を取得する

事象

CloudflareのAPIを実行するためにカスタムトークンを作成しました。 create-api-token-button

select-custom-token

アクセス許可は以下のように設定しました。

アカウントAPI Tokens編集

account-api-token

curlでエラー

APIを実行するとUnauthorized to access requested resourceで弾かれてしまいました。

Terminal window
$ 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でも同じく発生しました。

ファイルの内容は以下のように設定

Terminal window
$ tree
.
├── README.md
├── backend.tf
├── main.tf
├── provider.tf
├── terraform.tfvars
└── variables.tf
backend.tf
terraform {
backend "local" {
path = "./terraform.tfstate"
}
}
main.tf
data "cloudflare_api_token_permission_groups" "all" {}
provider.tf
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
}
variables.tf
variable "cloudflare_api_token" {}
terraform.tfvars
cloudflare_api_token = "api_token"

terraform planを実行すると同じくerror listing API Token Permission Groups: Unauthorized to access requested resource (9109)で弾かれました。

Terminal window
$ terraform init
$ terraform plan
data.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" {}

解決

カスタムトークンを作成するではなく追加のトークンを作成を選択します。

select-create-additional-token

アクセス許可は以下のように設定しました。

ユーザーAPIトークン読み取り

(なぜ、ユーザー、APIトークンはグレーアウトされているのだろうか・・)

create-additional-token

curl

Terminal window
$ 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

Terminal window
$ terraform plan
data.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 apply
data.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?