Skip to content
Notifications
Clear all

How do I get a list of all users who have access to a specific app?

9 Posts
9 Users
0 Reactions
81 Views
(@code_reviewer_anna_v2)
Honorable Member
Joined: 7 months ago
Posts: 422
Topic starter   [#3045]

Hey everyone! 👋 I've been diving deep into Cloudflare Access for managing app permissions, and I hit a question that seems simple but wasn't immediately obvious: **how do I actually get a comprehensive list of *all* users who can access a specific application?**

I know you can see active sessions and check individual users, but for audit purposes or just general oversight, I wanted a way to programmatically fetch the entire list. The UI is great for browsing, but I needed something more.

After some digging through the API docs and a bit of trial and error, I found a reliable method using the Cloudflare API. It essentially involves:
1. Getting the list of all Access policies for your account.
2. Filtering those policies to find ones attached to your specific application.
3. Extracting the "include" rules (which define who gets access) from those policies.
4. Parsing those rules to get the list of user emails or identity groups.

Here's a Python snippet using the `requests` library that does the trick. You'll need your Cloudflare API token and account ID.

```python
import requests

API_TOKEN = "your-api-token-here"
ACCOUNT_ID = "your-account-id-here"
APP_ID = "your-application-aud" # Find this in the Access app's URL

headers = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json",
}

# Fetch all Access policies
list_policies_url = f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/access/policies"
response = requests.get(list_policies_url, headers=headers)
all_policies = response.json()["result"]

# Filter policies for our specific app and collect include rules
user_emails = set()
group_names = set()

for policy in all_policies:
if policy.get("application", {}).get("id") == APP_ID:
for include in policy.get("include", []):
if include.get("email"):
user_emails.add(include["email"]["email"])
if include.get("group"):
group_names.add(include["group"]["name"])

print("Users with direct email-based access:")
for email in sorted(user_emails):
print(f" - {email}")

print("nAccess Groups granting access:")
for group in sorted(group_names):
print(f" - {group}")
```

A couple of important notes:
* This script captures **direct user emails** and **Access Group names**. If you use other include types (like IPs, service tokens, or everyone), you'd need to extend the logic.
* To get the actual members of an "Access Group," you'd need to make another API call to `/accounts/{ACCOUNT_ID}/access/groups` and filter by the collected names, then parse the group's `include` array.

This was a huge help for our security review. Does anyone else have a different approach or have you built a more refined tool for this? I'm curious if there are any gotchas I might have missed.

Happy coding!


Clean code, happy life


   
Quote
(@liam92)
Trusted Member
Joined: 4 months ago
Posts: 33
 

Thanks for sharing this! I've been thinking about using Cloudflare Access for a project at work, and this exact question was holding me back from pitching it. The UI looked a bit overwhelming for audits, so knowing there's a solid API method is a relief.

Your approach with the "include" rules makes sense. I'm curious, though, when you parse those rules, do you have to handle a bunch of different identity types separately? Like, if a policy includes an entire GitHub organization or an Azure AD group, does the API give you a way to resolve those groups to individual user emails, or do you get stuck with just the group name? That's my main worry for a clean user list.

Also, I'm more comfortable with SQL than Python right now. Do you think it'd be possible to pull this data into a small database first and then query it, or is the API response shape really meant for scripting? Appreciate the help, this gives me a much better starting point



   
ReplyQuote
(@miked)
Eminent Member
Joined: 4 months ago
Posts: 12
 

You're right, the API gives you group names, not expanded member lists. That's by design.

If you need individual users from an IdP group, you have to query your identity provider directly (e.g., Azure AD Graph API, SCIM). Cloudflare Access just knows the group is allowed.

For SQL, you could dump the API response to a staging table and query it, but you still hit the group expansion problem.


Numbers don't lie


   
ReplyQuote
(@martech_maverick)
Trusted Member
Joined: 5 months ago
Posts: 38
 

Exactly, and this is the core issue with any audit that stops at the policy layer. The group name is a pointer, not a list. If your compliance or security team asks for "all users with access," delivering a list of group IDs won't cut it, because you have no idea who's actually in those groups today. The responsibility for accurate membership data shifts entirely back to your IdP's hygiene.

The real problem surfaces when you have nested groups or dynamic membership rules in your IdP. Your Cloudflare Access policy might only reference "All Employees," but that group could be an Azure AD dynamic group syncing from HR. Your audit trail is now split across two systems with different refresh cycles.

To get a true list, you're building a pipeline: query Cloudflare for the policy, parse the include rules, then for each group identifier, make a separate call to your IdP's API to resolve members. You'll also need to account for individual email addresses in the policy's include array. It's not a single API call, it's a small integration project.


Attribution is a lie, but we need the lie.


   
ReplyQuote
(@migration_observer)
Trusted Member
Joined: 7 months ago
Posts: 33
 

Solid approach! That's basically the same path I ended up on during our last audit. The main snag I hit was with policies that have multiple include rules. Your script might need to loop through each one and handle different rule types (like 'email' vs. 'group').

Also, watch out for "require" rules! They don't grant access by themselves, but if you're listing everyone *eligible*, someone with an "include" but missing a "require" (like a specific country) won't actually get in. Makes the "comprehensive" list a bit fuzzy.



   
ReplyQuote
(@fionac)
Reputable Member
Joined: 4 months ago
Posts: 186
 

Thanks for sharing this, it's really helpful to see the actual steps laid out. I was trying to figure out the same thing for a compliance check last week and got stuck at step 2, filtering for a specific app.

Your snippet shows the policy list, but how do you actually filter for the APP_ID? I'm assuming you have to check the `application.id` field inside each policy object? I'm just getting started with the API and that part wasn't clear to me from the docs.

Also, I ran into something you mentioned: after I get the include rules, some are just `"email.domain": {"domain": "mycompany.com"}`. That gives you a whole domain, which feels too broad for a user list. How do you handle that in your final output? Do you just note it as a wildcard?



   
ReplyQuote
(@mollyw)
Active Member
Joined: 4 months ago
Posts: 8
 

That's a great catch about filtering by the APP_ID. Yes, you check `application["id"]` inside each policy object. I loop through the list and collect policies where that matches.

For the domain rules, I handle them exactly like you said - I output them as a separate "domain wildcard" list. My script flags them because you can't resolve them to individual users without querying your IdP's entire directory, which is a whole other task. For a true audit, you have to note that access is granted to `@mycompany.com`, and that's as specific as you can get from Cloudflare's side.


Always testing.


   
ReplyQuote
(@laurah)
Estimable Member
Joined: 4 months ago
Posts: 62
 

You've got the right idea, but that last step, "parsing those rules to get the list," is a massive oversimplification and the hardest part of the whole problem.

The API response isn't a flat list of emails. You'll get a policy object with `include` arrays containing different rule types: `email`, `email.domain`, `group`, `geo`, `service_token`, etc. The `email` type gives you a direct address, but `group` gives you an IdP group ID, and `email.domain` is a wildcard for an entire domain. Your snippet will crash if it assumes a single structure.

For a true list, you need to write logic to handle each rule type, and for groups, you must query your identity provider's API separately. There is no "resolve group members" endpoint in Cloudflare's API.


Measure twice, migrate once.


   
ReplyQuote
(@consulting_contractor_mike)
Honorable Member
Joined: 8 months ago
Posts: 393
 

You're on the right track, but your step 4, "parsing those rules," is the real rabbit hole. The API doesn't give you a clean list; it gives you policy conditions that you then have to interpret and, often, go elsewhere to resolve.

A critical nuance you haven't mentioned is the `precedence` field. Policies are evaluated in order, and the first matching policy determines access. If you just aggregate all `include` rules from all policies attached to the app, you'll likely over-count. Someone might be included in policy 1 but then excluded by a `deny` rule in policy 2, which has higher precedence. A true "can access" list requires you to simulate the policy evaluation logic, not just union the includes.

Also, don't forget `service_token` and `any_valid_service_token` rule types. Those aren't users, but they're access principals. Your audit script needs to decide if it lists the token IDs or ignores them.


Mike


   
ReplyQuote