Skip to content

Commit c7b4d5b

Browse files
authored
[ZT] Tunnel token rotation (#18577)
* tunnel rotation instructions * fix link
1 parent 7c55212 commit c7b4d5b

File tree

1 file changed

+162
-2
lines changed

1 file changed

+162
-2
lines changed

src/content/docs/cloudflare-one/connections/connect-networks/configure-tunnels/remote-management.mdx

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,166 @@ To configure how `cloudflared` sends requests to your [public hostname](/cloudfl
148148

149149
## Tunnel permissions
150150

151-
A remotely-managed tunnel only requires the tunnel token to run. Anyone with access to the token will be able to run the tunnel. You can get a tunnel's token from the dashboard or via the [API](/api/operations/cloudflare-tunnel-get-a-cloudflare-tunnel-token).
151+
A remotely-managed tunnel only requires the tunnel token to run. Anyone with access to the token will be able to run the tunnel.
152152

153-
Account members with Cloudflare Access and DNS [permissions](/cloudflare-one/roles-permissions/) will be able to create, delete, and configure all tunnels for the account.
153+
### View the tunnel token
154+
155+
To get the token for a remotely-managed tunnel:
156+
157+
<Tabs syncKey="dashPlusAPI">
158+
<TabItem label="Dashboard">
159+
1. In [Zero Trust](https://one.dash.cloudflare.com/), go to **Networks** > **Tunnels**.
160+
2. Select a `cloudflared` tunnel and select **Edit**.
161+
3. Copy `cloudflared` installation command.
162+
4. Paste the installation command into any text editor. The token value is of the form `eyJhIjoiNWFiNGU5Z...`
163+
164+
</TabItem>
165+
<TabItem label="API">
166+
167+
Make a `GET` request to the [Cloudflare Tunnel token](/api/operations/cloudflare-tunnel-get-a-cloudflare-tunnel-token) endpoint:
168+
169+
```sh
170+
curl https://api.cloudflare.com/client/v4/accounts/{account_id}/cfd_tunnel/{tunnel_id}/token \
171+
--header "Authorization: Bearer <API_TOKEN>"
172+
```
173+
```sh output
174+
{
175+
"success": true,
176+
"errors": [],
177+
"messages": [],
178+
"result": "eyJhIjoiNWFiNGU5Z..."
179+
}
180+
```
181+
182+
</TabItem>
183+
</Tabs>
184+
185+
### Rotate a token without service disruption
186+
187+
Cloudflare recommends rotating the tunnel token at a regular cadence to reduce the risk of token compromise. You can rotate a token with minimal disruption to users as long as the tunnel is served by at least two [`cloudflared` replicas](/cloudflare-one/connections/connect-networks/deploy-tunnels/deploy-cloudflared-replicas/). To ensure service availability, We recommend performing token rotations outside of working hours or in a maintenance window.
188+
189+
To rotate a tunnel token:
190+
191+
1. Refresh the token on Cloudflare:
192+
193+
<Tabs syncKey="dashPlusAPI">
194+
<TabItem label="Dashboard">
195+
1. In [Zero Trust](https://one.dash.cloudflare.com/), go to **Networks** > **Tunnels**.
196+
2. Select a `cloudflared` tunnel and select **Edit**.
197+
3. Select **Refresh token**.
198+
4. Copy the `cloudflared` installation command for your operating system. This command contains the new token.
199+
200+
</TabItem>
201+
<TabItem label="API">
202+
203+
1. Generate a random base64 string (minimum size 32 bytes) to use as a tunnel secret:
204+
205+
```sh
206+
openssl rand -base64 32
207+
```
208+
209+
```sh output
210+
AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=
211+
```
212+
213+
2. Make a `PATCH` request to the [Cloudflare Tunnel](/api/operations/cloudflare-tunnel-update-a-cloudflare-tunnel) endpoint:
214+
```sh
215+
curl --request PATCH \
216+
https://api.cloudflare.com/client/v4/accounts/{account_id}/cfd_tunnel/{tunnel_id} \
217+
--header 'Content-Type: application/json' \
218+
--header "Authorization: Bearer <API_TOKEN>" \
219+
--data '{
220+
"name": "Example tunnel",
221+
"tunnel_secret": "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg="
222+
}'
223+
```
224+
225+
```sh output {18}
226+
{
227+
"success": true,
228+
"errors": [],
229+
"messages": [],
230+
"result": {
231+
"id": "f70ff985-a4ef-4643-bbbc-4a0ed4fc8415",
232+
"account_tag": "699d98642c564d2e855e9661899b7252",
233+
"created_at": "2024-12-04T22:03:26.291225Z",
234+
"deleted_at": null,
235+
"name": "Example tunnel",
236+
"connections": [],
237+
"conns_active_at": null,
238+
"conns_inactive_at": "2024-12-04T22:03:26.291225Z",
239+
"tun_type": "cfd_tunnel",
240+
"metadata": {},
241+
"status": "inactive",
242+
"remote_config": true,
243+
"token": "eyJhIjoiNWFiNGU5Z..."
244+
}
245+
}
246+
```
247+
3. Copy the `token` value shown in the output.
248+
249+
</TabItem>
250+
</Tabs>
251+
252+
After refreshing the token, `cloudflared` can no longer establish new connections to Cloudflare using the old token. However, existing connectors will remain active and the tunnel will continue serving traffic.
253+
254+
2. On half of your `cloudflared` replicas, update `cloudflared` to use the new token. For example, on a Linux host:
255+
256+
```sh
257+
sudo cloudflared service install <TOKEN>
258+
```
259+
260+
3. Restart `cloudflared`:
261+
262+
```sh
263+
sudo systemctl restart cloudflared.service
264+
```
265+
266+
4. Confirm that the service started correctly:
267+
```sh
268+
sudo systemctl status cloudflared
269+
```
270+
271+
While these replicas are connecting to Cloudflare with the new token, traffic will automatically route through the other replicas.
272+
273+
5. Wait 10 minutes for traffic to route through the new connectors.
274+
275+
6. Repeat steps 2, 3, and 4 for the second half of the replicas.
276+
277+
The tunnel token is now fully rotated. The old token is no longer in use.
278+
279+
### Rotate a compromised token
280+
281+
If your tunnel token is compromised, we recommend taking the following steps:
282+
283+
1. Refresh the token using the dashboard or API. Refer to Step 1 of [Rotate a token without service disruption](#rotate-a-token-without-service-disruption).
284+
2. [Delete all connections](/api/operations/cloudflare-tunnel-clean-up-cloudflare-tunnel-connections) between `cloudflared` and Cloudflare:
285+
```sh
286+
curl --request DELETE \
287+
https://api.cloudflare.com/client/v4/accounts/{account_id}/cfd_tunnel/{tunnel_id}/connections \
288+
--header "Authorization: Bearer <API_TOKEN>"
289+
```
290+
291+
This will clean up any unauthorized connections and prevent users from connecting to your network.
292+
293+
3. On each `cloudflared` replica, update `cloudflared` to use the new token. For example, on a Linux host:
294+
295+
```sh
296+
sudo cloudflared service install <TOKEN>
297+
```
298+
4. Restart `cloudflared`:
299+
300+
```sh
301+
sudo systemctl restart cloudflared.service
302+
```
303+
304+
5. Confirm that the service started correctly:
305+
```sh
306+
sudo systemctl status cloudflared
307+
```
308+
309+
The tunnel token is now fully rotated. The old token is no longer in use.
310+
311+
### Account-scoped roles
312+
313+
Account members with [Cloudflare Access](/cloudflare-one/roles-permissions/) and [DNS](/fundamentals/setup/manage-members/roles/) permissions will be able to create, delete, and configure all tunnels for the account.

0 commit comments

Comments
 (0)