リモート管理トンネルの実行に必要なのは、トンネルトークンだけです。トークンにアクセスできる人は、トンネルを実行できます。
リモート管理トンネルのトークンを取得する手順は次のとおりです。
-
Cloudflare ダッシュボードで Networking > Tunnels を開きます。
Tunnels を開く ↗ -
トンネルを選択して詳細ページを開きます。
-
Overview タブで Add a replica を選択し、インストールコマンドを表示します。コマンドをテキストエディターにコピーします(コマンドは実行しないでください)。トークンは
eyJ...の文字列です。
Cloudflare Tunnel token エンドポイントに GET リクエストを送ります。
Required API token permissions
At least one of the following token permissions is required:Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/token" \
--request GET \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"{
"success": true,
"errors": [],
"messages": [],
"result": "eyJhIjoiNWFiNGU5Z..."
}トークンの値は result にあります。
data "cloudflare_zero_trust_tunnel_cloudflared_token" "tunnel_token" {
account_id = var.cloudflare_account_id
tunnel_id = cloudflare_zero_trust_tunnel_cloudflared.example_tunnel.id
}ホストマシンを Terraform で管理していない場合、またはトンネルを手動でインストールする場合は、トークン値を CLI に出力できます。
例: CLI に出力する
- トンネルトークンを Terraform の state ファイルに出力します。
output "tunnel_token" { value = data.cloudflare_zero_trust_tunnel_cloudflared_token.tunnel_token.token sensitive = true } - 構成を適用します。
terraform apply - トンネルトークンを読み取ります。
terraform output -raw tunnel_tokeneyJhIj...
または、data.cloudflare_zero_trust_tunnel_cloudflared_token.tunnel_token.token をホストの Terraform 構成に直接渡すか、シークレット管理ツールにトークンを保存します。
例: HashiCorp Vault に保存する
resource "vault_generic_secret" "tunnel_token" {
path = "kv/cloudflare/tunnel_token"
data_json = jsonencode({
"TUNNEL_TOKEN" = data.cloudflare_zero_trust_tunnel_cloudflared_token.tunnel_token.token
})
}トークン漏洩のリスクを下げるため、トンネルトークンは定期的にローテーションしてください。トンネルが少なくとも 2 つの cloudflared レプリカ で提供されていれば、ユーザーへの影響を最小限にしてトークンをローテーションできます。サービス可用性を確保するため、トークンのローテーションは業務時間外またはメンテナンスウィンドウで実施してください。
トンネルトークンをローテーションする手順は次のとおりです。
-
Cloudflare 側でトークンを更新します。
-
Cloudflare ダッシュボードで Networking > Tunnels を開きます。
Tunnels を開く ↗ -
トンネルを選択して詳細ページを開きます。
-
Overview タブで Refresh token を選択します。
-
使用中の OS 向けの
cloudflaredインストールコマンドをコピーします。このコマンドに新しいトークンが含まれます。
-
トンネルシークレットとして使う、ランダムな base64 文字列(最小 32 バイト)を生成します。
openssl rand -base64 32AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg= -
Cloudflare Tunnel エンドポイントに
PATCHリクエストを送ります。
At least one of the following token permissions is required:Required API token permissions
Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
Update a Cloudflare Tunnelbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID" \ --request PATCH \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "name": "Example tunnel", "tunnel_secret": "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=" }'{ "success": true, "errors": [], "messages": [], "result": { "id": "f70ff985-a4ef-4643-bbbc-4a0ed4fc8415", "account_tag": "699d98642c564d2e855e9661899b7252", "created_at": "2024-12-04T22:03:26.291225Z", "deleted_at": null, "name": "Example tunnel", "connections": [], "conns_active_at": null, "conns_inactive_at": "2024-12-04T22:03:26.291225Z", "tun_type": "cfd_tunnel", "metadata": {}, "status": "inactive", "remote_config": true, "token": "eyJhIjoiNWFiNGU5Z..." } } -
出力に表示された
tokenの値をコピーします。
トークンを更新したあと、
cloudflaredは古いトークンで Cloudflare への新しい接続を確立できません。ただし、既存のコネクターは稼働したままで、トンネルはトラフィックの提供を続けます。 -
-
cloudflaredレプリカの半数で、新しいトークンを使ってcloudflaredサービスを再インストールします。たとえば Linux ホストでは、次を実行します。sudo cloudflared service uninstall sudo cloudflared service install <NEW_TOKEN> -
サービスが正しく起動したことを確認します。
sudo systemctl status cloudflaredこれらのレプリカが新しいトークンで Cloudflare に接続しているあいだ、トラフィックは残りのレプリカへ自動でルーティングされます。
-
トラフィックが新しいコネクター経由になるまで、10 分待ちます。
-
残りの半数のレプリカに対して、ステップ 2、3、4 を繰り返します。
トンネルトークンのローテーションは完了です。古いトークンは使われません。
トンネルトークンが漏洩した場合は、次の手順を推奨します。
-
ダッシュボードまたは API でトークンを更新します。サービスを止めずにトークンをローテーションする のステップ 1 を参照してください。
-
cloudflaredと Cloudflare のあいだの 接続をすべて削除します。
At least one of the following token permissions is required:Required API token permissions
Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
Clean up Cloudflare Tunnel connectionsbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/connections" \ --request DELETE \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"これにより、不正な接続が整理され、ユーザーがネットワークに接続できなくなります。
-
各
cloudflaredレプリカで、cloudflaredが新しいトークンを使うように更新します。たとえば Linux ホストでは、次を実行します。sudo cloudflared service uninstall sudo cloudflared service install <NEW_TOKEN> -
サービスが正しく起動したことを確認します。
sudo systemctl status cloudflared
トンネルトークンのローテーションは完了です。古いトークンは使われません。
アカウントでトンネルを作成、削除、設定するために必要な最小権限:
公開ホスト名へトラフィックをルーティング し、cloudflared login を実行するために追加で必要な権限:
アカウント全体のアクセスを付与する代わりに、個別の Cloudflare Tunnel インスタンスに権限をスコープすることもできます。Tunnels と Mesh ノードのきめ細かい権限 を参照してください。