Entra Administrative Units instead of Group.Read.All

What do you do if you have an app that needs to read a set of groups and lists its members? You can grant the app the Graph API Permission Group.Read.Alll and User.ReadBasic.All, but that would mean the app would be able to see all groups in your Entra tenant and that may be a security concern as it gives the app to much permission.

Instead, you can grant the app’s service principal (SP) the permission to only read groups that exists in an Administrative Unit (AU), meaning it can only see the groups it is intended to see. In order to achieve this the relevant groups needs to be added to an AU and the service principal assigned the permission of a custom role we create that can only read groups, not manage them.

If the other app can use SCIM, that would be a better choice, but in cases where SCIM is not an option, this can be a solution.

This post is a continuation from the the Administrative Units instead of Group.ReadWrite.All post.

Create the Service Principal that should hold the permissions

The application will need the API permissions User.ReadBasic.All and AdministrativeUnit.Read.All. User.ReadBasic.All is needed to get the user profile for each group member in the cases the users are not a member of the same AU. The AdministrativeUnit.Read.All is needed to enumerate all groups within the AU (shown further down).

Creating Custom Role Group Reader

We can not use the built-in role Group Administrator as that role gives the holder the right to create and manage groups. In this case we only want the holder to read groups. Therefor we create a custom role with the following permissions.

  • Name: Group Reader (Custom)
  • Role permissions:
    microsoft.directory/groups/standard/read
    microsoft.directory/groups/members/read
    microsoft.directory/groups/owners/read
    microsoft.directory/users/standard/read
    microsoft.directory/servicePrincipals/standard/read
    microsoft.directory/devices/standard/read

The users/servicePrincipals/devices permissions are needed to read the different types of group members. Without them you will just get a guid and the odata.type.

Assign the Group Reader custom role to the AdministrativeUnit

Once you have the custom role you need to assign it to the AU. Select you AU in Entra Admin Center > Roles & admins > Admin units > Roles and administrators. Then do +Add assignment and pick the AppThatNeedsToReadSomeGroups service principal and set it as an active assignment.

Listing groups and its members

First, authenticate the SP using client credentials

$auth = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" `
    -ContentType "application/x-www-form-urlencoded" `
    -Body @{ grant_type="client_credentials"; client_id=$ClientId; client_secret=$ClientSecret; scope="https://graph.microsoft.com/.default" }
$authHeader =@{ 'Content-Type'='application/json'; 'Authorization'='Bearer ' + $auth.access_token }    

Second, list groups available in the AU. Here we can not use https://graph.microsoft.com/groups as that would require a tenant wide scope which the service principal doesn’t have. Instead, we need to ask the AU what groups are members. The graph Uri is /directory/administrativeUnits/{id}/members/microsoft.graph.groups which means “give me all groups in this AU”. The crux is to know your AU id.

$groups = (Invoke-RestMethod -Method "GET" -Uri "https://graph.microsoft.com/v1.0/directory/administrativeUnits/$($au.id)/members/microsoft.graph.group" -Headers $authHeader).value

The above query requires the API permission AdministrativeUnit.Read.All which we assigned the SP earlier.

Once you have the group objects, you can use the standard graph group endpoint to get its members.

(Invoke-RestMethod -Method "GET" -Uri "https://graph.microsoft.com/v1.0/groups/$($groups[0].id)/members" -Headers $authHeader).value | select userPrincipalName, displayName

userPrincipalName              displayName
-----------------              -----------
basil@fawltytowers2.com        Basil Fawlty
sybil@fawltytowers2.com        Sybil Fawlty
rocky.balboa@fawltytowers2.com Rocky Balboa

Prove the SP can’t list other groups

If the SP try to execute a query on the groups endpoint that would operate on a tenant wide scope, the SP doesn’t have that permission and will receive an “Insufficient privileges” error.