|
1 | 1 | # get-azure-role-assignments-using-python
|
2 | 2 | This repository contains python code to get the role assignments from azure
|
| 3 | + |
| 4 | + |
| 5 | +# What this repository do |
| 6 | + |
| 7 | +```markdown |
| 8 | +This repo contains the source code which can be used to list the role assignments on a subscription level |
| 9 | +for Azure using Python azure SDK's. |
| 10 | +``` |
| 11 | + |
| 12 | +## parameters |
| 13 | + |
| 14 | +| inputs | description | mandatory | |
| 15 | +|--------|-------------|-----------| |
| 16 | +|subscription_id| azure subscription id| :heavy_check_mark: | |
| 17 | + |
| 18 | +## Authentication |
| 19 | + |
| 20 | +Authentication to Azure is done using service principal credentials |
| 21 | + |
| 22 | +have a `.env file` |
| 23 | + |
| 24 | +```yaml |
| 25 | +AZURE_CLIENT_ID="xxx" |
| 26 | +AZURE_CLIENT_SECRET="xxx" |
| 27 | +AZURE_TENANT_ID="xxx" |
| 28 | +AZURE_SUBSCRIPTION_ID="xxx" |
| 29 | +``` |
| 30 | +Where xxx refers to the actual values. These will vary for everyone |
| 31 | + |
| 32 | +using python-dotenv module and fucntion load_dotenv() uses it for local testing. |
| 33 | + |
| 34 | +# Final output |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "id": "/subscriptions/<subscription id>/providers/Microsoft.Authorization/roleAssignments/<assignment id>", |
| 39 | + "name": "<name>", |
| 40 | + "type": "Microsoft.Authorization/roleAssignments", |
| 41 | + "principal_id": "<principal id>", |
| 42 | + "principal_type": "< group or user or service principal>", |
| 43 | + "role_definition_id": "<role definition id>", |
| 44 | + "assignment_creation_time": "<when assignment was created>", |
| 45 | + "scope": "/subscriptions/<subscription id>", |
| 46 | + "principal_name": "<principal name>", |
| 47 | + "rbac_role_name": "<Azure Rbac or custom role namess>" |
| 48 | + } |
| 49 | +``` |
| 50 | + |
| 51 | +* for local testing the code creates this as a json file. |
| 52 | + |
| 53 | +# How to run the code locally |
| 54 | + |
| 55 | +```commandline |
| 56 | +python3 role_assignments.py --subscription_id <subscription id> |
| 57 | +``` |
| 58 | + |
| 59 | +### For package management poetry is used. |
| 60 | + |
| 61 | + |
| 62 | +## Refernces |
| 63 | + |
| 64 | +[role-assignments-list](https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest) |
| 65 | +[role-based-access-control built-in-roles](https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles) |
| 66 | +[AuthorizationManagementClient](https://learn.microsoft.com/en-us/rest/api/authorization/role-definitions/get?view=rest-authorization-2022-04-01&tabs=Python) |
| 67 | +[azure graph rbac](https://learn.microsoft.com/en-us/python/api/azure-graphrbac/azure.graphrbac.operations.service_principals_operations.serviceprincipalsoperations?view=azure-python-previous#azure-graphrbac-operations-service-principals-operations-serviceprincipalsoperations-get) |
0 commit comments