How can I get I find the owners of a resources via API for Azure?

JPAN 1 Reputation point
2023-01-05T16:19:57.6+00:00

Hi,
I'm trying to get the owner (or list of owners) of a VM. I've investigated some of Azure API I am using to pull resource information, for example [getting VM (Compute) details][1] however I have not found any information for roles or owners associated to the resource. I have also tried going up a level from that to get information from the resource group and that API endpoint does not return any owner information as well, however I have found in the tags of the response for Subscriptions - Get that returns "c7n-contact" which has a DL but still no users that own the specific resources below. I would very much appreciate some assistance with how I could find the owner(s) or a VM or Resource Group that VM is a part of, not necessarily who created it.
[1]: https://learn.microsoft.com/en-us/rest/api/compute/virtual-machines/get?tabs=HTTP

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,035 questions
Azure Role-based access control
Azure Role-based access control
An Azure service that provides fine-grained access management for Azure resources, enabling you to grant users only the rights they need to perform their jobs.
976 questions
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. kobulloc-MSFT 26,811 Reputation points Microsoft Employee Moderator
    2023-01-05T18:47:32.833+00:00

    Hello, @JPAN !

    How do I find the owners of a resource via API?
    You can use the CLI command az role assignment list or the equivalent REST API below for roleAssignments. This works with a variety of scopes including the subscription, resource group, and resource:

    https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest#list-role-assignments

    To list the owners of a VM using the REST API, you would use this as your scope:

    subscriptions/{subscriptionId1}/resourceGroups/myresourcegroup1/providers/Microsoft.Compute/virtualMachines/MyVM

    Postman Example

    I created a service principle and configured Postman using a blog from Jon Gallant, an Azure developer at Microsoft.

    Once that was configured, I used the following request based on the documentation above:

    https://management.azure.com/subscriptions/mySubscriptionID/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/myVMName/providers/Microsoft.Authorization/roleAssignments?api-version=2022-04-01

    Example request:
    277105-image.png

    One of the nice things about the CLI command az role assignment list instead of the REST API is that it returns some extra values including principalName and roleDefinitionName which are the human readable versions of principalId and roleDefinitionId respectively.

    Once we have the response in Postman, we can search for the roleDefinitionId we are interested in such as the ID for Owner. Within that block, we can search for the principalId which we can use to get the name of the user.

    Extra values returned when using the CLI command az role assignment list can be easier to work with.
    277113-image.png

    The REST API will return roleDefinitionId and principalId which we can use to identify the role we are interested in and look up the user who has that role.
    277059-image.png

    There are a variety of methods for getting the roleDefinitionId and principalId. For example, you can find the roleDefinitionId of the role you are interested in within the portal by going to your resource > Access control (IAM) > Roles > then click on View under Details for the role name you are interested in (Owner) > JSON > id. A similar approach can be used for getting principalId by clicking on a user.
    277131-image.png

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.