Originally published on kuryzhev.cloud
Your deploy pipeline needs to call an internal admin API that sits behind Cloudflare Access, and the first run ends with an HTML login page where JSON should be. Cloudflare Access service tokens are the documented way to let a non-human client through: a client ID and secret sent as two request headers, matched against a dedicated policy.
Short answer: create one service token per pipeline and app, add an Access policy with the Service Auth action that names that specific token, attach it to the application, and send CF-Access-Client-Id and CF-Access-Client-Secret from CI secrets. It fits HTTP(S) calls to apps already behind Access. If you need per-user identity or short-lived credentials, a service token is the wrong tool (see the last tip).
1. Issue one Cloudflare Access service token per pipeline and app
A shared "ci" token is a shared blast radius, so name tokens after the job and the target. If a token leaks, you revoke one thing and the audit log tells you which pipeline it was. The secret is shown only once at creation, so write it to your secret store in the same step.
Creating a token through the API (the account ID and an API token with Access edit permission are placeholders; check the current schema in the Cloudflare One docs):
curl -sS -X POST \
"https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT_ID/access/service_tokens" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"name":"ci-deploy-billing-admin","duration":"8760h"}' \
| jq '.result | {client_id, client_secret}' # secret is returned only now
2. Write the policy with Service Auth, not Allow
A token that exists but is not referenced by a Service Auth policy gets you nothing. The same is true of a policy that is not attached to an application. In the dashboard, add a policy to the application with the action Service Auth and an include rule selecting the specific token. Human policies (Allow, with email or IdP rules) will not admit a service token.
Watch out for the "Any Access Service Token" selector. It admits every token in the account, including ones created later for other teams. Pick the named token instead.
If you manage Access as code, the shape below is what the Terraform/OpenTofu Cloudflare provider v5 documents. In v5, policies are account-level, reusable objects that take effect only when an application lists them. Field names have shifted between provider majors, so verify against the provider docs before applying.
resource "cloudflare_zero_trust_access_service_token" "ci" {
account_id = var.account_id
name = "ci-deploy-billing-admin"
duration = "8760h"
}
resource "cloudflare_zero_trust_access_policy" "ci" {
account_id = var.account_id
name = "ci-deploy-billing-admin"
decision = "non_identity" # the API name for Service Auth
include = [{
service_token = { token_id = cloudflare_zero_trust_access_service_token.ci.id }
}]
}
# The policy only applies once an application references it
resource "cloudflare_zero_trust_access_application" "billing_deploy_api" {
account_id = var.account_id
name = "billing-admin-deploy-api"
type = "self_hosted"
domain = "admin.internal.example.com/api/deploy"
policies = [{
id = cloudflare_zero_trust_access_policy.ci.id
precedence = 1
}]
}
3. Prove it works with a with-and-without test
Always test both the allowed and the denied path. A 200 with headers proves little if the app was never protected. Run the same request without headers and confirm it is blocked.
URL="https://admin.internal.example.com/healthz"
# With the token: expect 200 (or your app's normal status)
curl -s -o /dev/null -w '%{http_code}\n' "$URL" \
-H "CF-Access-Client-Id: $CF_ACCESS_CLIENT_ID" \
-H "CF-Access-Client-Secret: $CF_ACCESS_CLIENT_SECRET"
# Without it: expect a redirect to the Access login or a 403
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' "$URL"
A typical failure is a 302 or 403 even with headers. That usually means one of four things:
- the policy uses the wrong action;
- it references a different token;
- it is not attached to the application;
- the hostname or path is not covered by the application.
The Access authentication logs in the Zero Trust dashboard show which policy evaluated and why.
4. Keep the secret out of logs, forks and shell history
Store both values as CI secrets and expose them only to jobs that need them. In GitHub Actions, store them as environment secrets (not repository secrets) on an environment with deployment branch rules. That way only jobs running from allowed branches can read them. Add required reviewers if you also want a human approval before the job starts.
For workflows triggered by pull_request from forks, secrets are not passed by default. pull_request_target does run with secrets, so do not use it to make a fork test pass.
jobs:
smoke:
runs-on: ubuntu-latest
environment: production-internal # environment secrets + branch rules gate access
steps:
- name: Call internal API through Access
env:
CF_ACCESS_CLIENT_ID: ${{ "{{" }} secrets.CF_ACCESS_CLIENT_ID }}
CF_ACCESS_CLIENT_SECRET: ${{ "{{" }} secrets.CF_ACCESS_CLIENT_SECRET }}
run: |
curl -fsS https://admin.internal.example.com/healthz \
-H "CF-Access-Client-Id: $CF_ACCESS_CLIENT_ID" \
-H "CF-Access-Client-Secret: $CF_ACCESS_CLIENT_SECRET"
Watch out for curl -v and set -x. Both print request headers, and masking only works on exact string matches in the log output.
5. Choose an expiry on purpose and rotate before it bites
An expired token fails like a missing one, and the failure often surfaces in the middle of a release. Pick a duration that matches your rotation habit. Do not reach for the non-expiring option just to avoid the chore. Cloudflare can send a notification before a service token expires. Make sure it goes to a team alias, not the person who created the token.
Put the expiry date in the token name or your inventory. Rotate by creating a new token, updating CI, then deleting the old one. The dashboard and API also offer refresh and rotate actions. Their exact behavior, including any grace period for the old secret, has changed over time, so read the current docs before relying on it.
6. Treat a service token as a bearer password
Anyone holding the two headers is the pipeline. There is no device posture, no IdP identity and no source IP check attached to it unless you add them. So narrow the policy: add your self-hosted runners' IP range as a Require rule alongside the Service Auth include, if the egress addresses are stable. A second Include rule would widen access rather than narrow it. GitHub-hosted runner IPs change, so this pairing mostly suits self-hosted runners.
Also scope the app, not just the token. Create a separate Access application for the CI-facing path (for example /api/deploy/*) so a leaked token cannot browse the whole admin UI. The cost is more applications and policies to manage, which is worth it for anything that can change production.
7. Know when Cloudflare Access service tokens are the wrong fit
Do not use them when you need to know who acted, or when the client is not speaking HTTP(S) through Access. An audit entry says "token ci-deploy-billing-admin", not which engineer triggered the run. Put that context in your CI logs and keep the correlation in your pipeline records.
For short-lived credentials, per-run identity or non-HTTP protocols, look at Cloudflare Tunnel with WARP or mTLS, or at a different access layer entirely. For SSH-style workloads, see the comparison of Tailscale-based access patterns elsewhere on this blog. The tradeoff is more client setup in exchange for stronger identity, so choose per workload rather than standardizing on one mechanism.
Top comments (0)