Skip to content

Commit 3ca8428

Browse files
Merge pull request #96379 from rolyon/rolyon-rbac-template-id
[Azure RBAC] principal identifiers for templates
2 parents e3a858b + 2be09e7 commit 3ca8428

File tree

3 files changed

+109
-46
lines changed

3 files changed

+109
-46
lines changed

articles/role-based-access-control/role-assignments-cli.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.devlang: na
1212
ms.topic: conceptual
1313
ms.tgt_pltfrm: na
1414
ms.workload: identity
15-
ms.date: 09/11/2019
15+
ms.date: 11/21/2019
1616
ms.author: rolyon
1717
ms.reviewer: bagovind
1818
---
@@ -261,6 +261,34 @@ az role assignment list --scope /providers/Microsoft.Management/managementGroups
261261
az role assignment list --scope /providers/Microsoft.Management/managementGroups/marketing-group --output json | jq '.[] | {"principalName":.principalName, "roleDefinitionName":.roleDefinitionName, "scope":.scope}'
262262
```
263263

264+
## Get object IDs
265+
266+
To list, add, or remove role assignments, you might need to specify the unique ID of an object. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal or Azure CLI.
267+
268+
### User
269+
270+
To get the object ID for an Azure AD user, you can use [az ad user show](/cli/azure/ad/user#az-ad-user-show).
271+
272+
```azurecli
273+
az ad user show --id "{email}" --query objectId --output tsv
274+
```
275+
276+
### Group
277+
278+
To get the object ID for an Azure AD group, you can use [az ad group show](/cli/azure/ad/group#az-ad-group-show) or [az ad group list](/cli/azure/ad/group#az-ad-group-list).
279+
280+
```azurecli
281+
az ad group show --group "{name}" --query objectId --output tsv
282+
```
283+
284+
### Application
285+
286+
To get the object ID for an Azure AD service principal (identity used by an application), you can use [az ad sp list](/cli/azure/ad/sp#az-ad-sp-list). For a service principal, use the object ID and **not** the application ID.
287+
288+
```azurecli
289+
az ad sp list --display-name "{name}" --query [].objectId --output tsv
290+
```
291+
264292
## Grant access
265293

266294
In RBAC, to grant access, you create a role assignment.
@@ -306,7 +334,7 @@ az role assignment create --role 9980e02c-c2be-4d73-94e8-173b1dc7cf3c --assignee
306334

307335
### Create a role assignment for a group
308336

309-
To grant access to a group, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). To get the ID of the group, you can use [az ad group list](/cli/azure/ad/group#az-ad-group-list) or [az ad group show](/cli/azure/ad/group#az-ad-group-show).
337+
To grant access to a group, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
310338

311339
```azurecli
312340
az role assignment create --role <role_name_or_id> --assignee-object-id <assignee_object_id> --resource-group <resource_group> --scope </subscriptions/subscription_id>
@@ -326,7 +354,7 @@ az role assignment create --role "Virtual Machine Contributor" --assignee-object
326354

327355
### Create a role assignment for an application at a resource group scope
328356

329-
To grant access to an application, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). To get the object ID of the application, you can use [az ad app list](/cli/azure/ad/app#az-ad-app-list) or [az ad app show](/cli/azure/ad/app#az-ad-app-show).
357+
To grant access to an application, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). For information about how to get the object ID of the application, see [Get object IDs](#get-object-ids).
330358

331359
```azurecli
332360
az role assignment create --role <role_name_or_id> --assignee-object-id <assignee_object_id> --resource-group <resource_group>
@@ -346,7 +374,7 @@ To grant access to a user at a subscription scope, use [az role assignment creat
346374
az role assignment create --role <role_name_or_id> --assignee <assignee> --subscription <subscription_name_or_id>
347375
```
348376

349-
The following example assigns the *Reader* role to to the *annm\@example.com* user at a subscription scope.
377+
The following example assigns the *Reader* role to the *annm\@example.com* user at a subscription scope.
350378

351379
```azurecli
352380
az role assignment create --role "Reader" --assignee [email protected] --subscription 00000000-0000-0000-0000-000000000000
@@ -360,7 +388,7 @@ To grant access to a user at a management group scope, use [az role assignment c
360388
az role assignment create --role <role_name_or_id> --assignee <assignee> --scope /providers/Microsoft.Management/managementGroups/<group_id>
361389
```
362390

363-
The following example assigns the *Billing Reader* role to to the *alain\@example.com* user at a management group scope.
391+
The following example assigns the *Billing Reader* role to the *alain\@example.com* user at a management group scope.
364392

365393
```azurecli
366394
az role assignment create --role "Billing Reader" --assignee [email protected] --scope /providers/Microsoft.Management/managementGroups/marketing-group
@@ -396,7 +424,7 @@ The following example removes the *Virtual Machine Contributor* role assignment
396424
az role assignment delete --assignee [email protected] --role "Virtual Machine Contributor" --resource-group pharma-sales
397425
```
398426

399-
The following example removes the *Reader* role from the *Ann Mack Team* group with ID 22222222-2222-2222-2222-222222222222 at a subscription scope. To get the ID of the group, you can use [az ad group list](/cli/azure/ad/group#az-ad-group-list) or [az ad group show](/cli/azure/ad/group#az-ad-group-show).
427+
The following example removes the *Reader* role from the *Ann Mack Team* group with ID 22222222-2222-2222-2222-222222222222 at a subscription scope. For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
400428

401429
```azurecli
402430
az role assignment delete --assignee 22222222-2222-2222-2222-222222222222 --role "Reader" --subscription 00000000-0000-0000-0000-000000000000

articles/role-based-access-control/role-assignments-powershell.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.devlang: na
1212
ms.topic: conceptual
1313
ms.tgt_pltfrm: na
1414
ms.workload: identity
15-
ms.date: 10/22/2019
15+
ms.date: 11/21/2019
1616
ms.author: rolyon
1717
ms.reviewer: bagovind
1818
---
@@ -268,34 +268,41 @@ To list role assignments for the classic subscription administrator and co-admin
268268
Get-AzRoleAssignment -IncludeClassicAdministrators
269269
```
270270

271-
## Grant access
272-
273-
In RBAC, to grant access, you create a role assignment.
271+
## Get object IDs
274272

275-
### Search for object IDs
273+
To list, add, or remove role assignments, you might need to specify the unique ID of an object. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal or Azure PowerShell.
276274

277-
To assign a role, you need to identify both the object (user, group, or application) and the scope.
275+
### User
278276

279-
To get the subscription ID, you can find it on the **Subscriptions** blade in the Azure portal or you can use [Get-AzSubscription](/powershell/module/Az.Accounts/Get-AzSubscription).
280-
281-
To get the object ID for an Azure AD user, use [Get-AzADUser](/powershell/module/az.resources/get-azaduser).
277+
To get the object ID for an Azure AD user, you can use [Get-AzADUser](/powershell/module/az.resources/get-azaduser).
282278

283279
```azurepowershell
284280
Get-AzADUser -StartsWith <string_in_quotes>
281+
(Get-AzADUser -DisplayName <name_in_quotes>).id
285282
```
286283

287-
To get the object ID for an Azure AD group, use [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup).
284+
### Group
285+
286+
To get the object ID for an Azure AD group, you can use [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup).
288287

289288
```azurepowershell
290289
Get-AzADGroup -SearchString <group_name_in_quotes>
290+
(Get-AzADGroup -DisplayName <group_name_in_quotes>).id
291291
```
292292

293-
To get the object ID for an Azure AD service principal or application, use [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal).
293+
### Application
294+
295+
To get the object ID for an Azure AD service principal (identity used by an application), you can use [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal). For a service principal, use the object ID and **not** the application ID.
294296

295297
```azurepowershell
296298
Get-AzADServicePrincipal -SearchString <service_name_in_quotes>
299+
(Get-AzADServicePrincipal -DisplayName <service_name_in_quotes>).id
297300
```
298301

302+
## Grant access
303+
304+
In RBAC, to grant access, you create a role assignment.
305+
299306
### Create a role assignment for a user at a resource group scope
300307

301308
To grant access to a user at a resource group scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment).
@@ -339,7 +346,7 @@ To create a role assignment using the unique role ID instead of the role name, u
339346
New-AzRoleAssignment -ObjectId <object_id> -RoleDefinitionId <role_id> -ResourceGroupName <resource_group_name>
340347
```
341348

342-
The following example assigns the [Virtual Machine Contributor](built-in-roles.md#virtual-machine-contributor) role to *[email protected]* user at the *pharma-sales* resource group scope. To get the unique role ID, you can use [Get-AzRoleDefinition](/powershell/module/az.resources/get-azroledefinition) or see [Built-in roles for Azure resources](built-in-roles.md).
349+
The following example assigns the [Virtual Machine Contributor](built-in-roles.md#virtual-machine-contributor) role to *alain\@example.com* user at the *pharma-sales* resource group scope. To get the unique role ID, you can use [Get-AzRoleDefinition](/powershell/module/az.resources/get-azroledefinition) or see [Built-in roles for Azure resources](built-in-roles.md).
343350

344351
```Example
345352
PS C:\> New-AzRoleAssignment -ObjectId 44444444-4444-4444-4444-444444444444 -RoleDefinitionId 9980e02c-c2be-4d73-94e8-173b1dc7cf3c -Scope /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
@@ -357,7 +364,7 @@ CanDelegate : False
357364

358365
### Create a role assignment for a group at a resource scope
359366

360-
To grant access to a group at a resource scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment).
367+
To grant access to a group at a resource scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment). For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
361368

362369
```azurepowershell
363370
New-AzRoleAssignment -ObjectId <object_id> -RoleDefinitionName <role_name> -ResourceName <resource_name> -ResourceType <resource_type> -ParentResource <parent resource> -ResourceGroupName <resource_group_name>
@@ -388,10 +395,10 @@ CanDelegate : False
388395

389396
### Create a role assignment for an application at a subscription scope
390397

391-
To grant access to an application at a subscription scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment).
398+
To grant access to an application at a subscription scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment). For information about how to get the object ID of the application, see [Get object IDs](#get-object-ids).
392399

393400
```azurepowershell
394-
New-AzRoleAssignment -ObjectId <application_id> -RoleDefinitionName <role_name> -Scope /subscriptions/<subscription_id>
401+
New-AzRoleAssignment -ObjectId <object_id> -RoleDefinitionName <role_name> -Scope /subscriptions/<subscription_id>
395402
```
396403

397404
```Example

articles/role-based-access-control/role-assignments-template.md

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,54 @@ ms.devlang: na
1111
ms.topic: conceptual
1212
ms.tgt_pltfrm: na
1313
ms.workload: identity
14-
ms.date: 09/20/2019
14+
ms.date: 11/21/2019
1515
ms.author: rolyon
1616
ms.reviewer: bagovind
1717
---
1818
# Manage access to Azure resources using RBAC and Azure Resource Manager templates
1919

2020
[Role-based access control (RBAC)](overview.md) is the way that you manage access to Azure resources. In addition to using Azure PowerShell or the Azure CLI, you can manage access to Azure resources using [Azure Resource Manager templates](../azure-resource-manager/resource-group-authoring-templates.md). Templates can be helpful if you need to deploy resources consistently and repeatedly. This article describes how you can manage access using RBAC and templates.
2121

22+
## Get object IDs
23+
24+
To assign a role, you need to specify the ID of the user, group, or application you want to assign the role to. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal, Azure PowerShell, or Azure CLI.
25+
26+
### User
27+
28+
To get the ID of a user, you can use the [Get-AzADUser](/powershell/module/az.resources/get-azaduser) or [az ad user show](/cli/azure/ad/user#az-ad-user-show) commands.
29+
30+
```azurepowershell
31+
$objectid = (Get-AzADUser -DisplayName "{name}").id
32+
```
33+
34+
```azurecli
35+
objectid=$(az ad user show --id "{email}" --query objectId --output tsv)
36+
```
37+
38+
### Group
39+
40+
To get the ID of a group, you can use the [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup) or [az ad group show](/cli/azure/ad/group#az-ad-group-show) commands.
41+
42+
```azurepowershell
43+
$objectid = (Get-AzADGroup -DisplayName "{name}").id
44+
```
45+
46+
```azurecli
47+
objectid=$(az ad group show --group "{name}" --query objectId --output tsv)
48+
```
49+
50+
### Application
51+
52+
To get the ID of a service principal (identity used by an application), you can use the [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal) or [az ad sp list](/cli/azure/ad/sp#az-ad-sp-list) commands. For a service principal, use the object ID and **not** the application ID.
53+
54+
```azurepowershell
55+
$objectid = (Get-AzADServicePrincipal -DisplayName "{name}").id
56+
```
57+
58+
```azurecli
59+
objectid=$(az ad sp list --display-name "{name}" --query [].objectId --output tsv)
60+
```
61+
2262
## Create a role assignment at a resource group scope (without parameters)
2363

2464
In RBAC, to grant access, you create a role assignment. The following template shows a basic way to create a role assignment. Some values are specified within the template. The following template demonstrates:
@@ -28,7 +68,7 @@ In RBAC, to grant access, you create a role assignment. The following template s
2868
To use the template, you must do the following:
2969

3070
- Create a new JSON file and copy the template
31-
- Replace `<your-principal-id>` with the unique identifier of a user, group, or application to assign the role to. The identifier has the format: `11111111-1111-1111-1111-111111111111`
71+
- Replace `<your-principal-id>` with the ID of a user, group, or application to assign the role to
3272

3373
```json
3474
{
@@ -71,9 +111,8 @@ The previous template isn't very flexible. The following template uses parameter
71111

72112
To use the template, you must specify the following inputs:
73113

74-
- The unique identifier of a user, group, or application to assign the role to
75-
- The role to assign
76-
- A unique identifier that will be used for the role assignment, or you can use the default identifier
114+
- The ID of a user, group, or application to assign the role to
115+
- A unique ID that will be used for the role assignment, or you can use the default ID
77116

78117
```json
79118
{
@@ -124,39 +163,29 @@ To use the template, you must specify the following inputs:
124163
}
125164
```
126165

127-
To get the unique identifier of a user to assign the role to, you can use the [Get-AzADUser](/powershell/module/az.resources/get-azaduser) or [az ad user show](/cli/azure/ad/user#az-ad-user-show) commands.
128-
129-
```azurepowershell
130-
$userid = (Get-AzADUser -DisplayName "{name}").id
131-
```
132-
133-
```azurecli
134-
userid=$(az ad user show --upn-or-object-id "{email}" --query objectId --output tsv)
135-
```
166+
> [!NOTE]
167+
> This template is not idempotent unless the same `roleNameGuid` value is provided as a parameter for each deployment of the template. If no `roleNameGuid` is provided, by default a new GUID is generated on each deployment and subsequent deployments will fail with a `Conflict: RoleAssignmentExists` error.
136168
137169
The scope of the role assignment is determined from the level of the deployment. Here are example [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment) and [az group deployment create](/cli/azure/group/deployment#az-group-deployment-create) commands for how to start the deployment at a resource group scope.
138170

139171
```azurepowershell
140-
New-AzResourceGroupDeployment -ResourceGroupName ExampleGroup -TemplateFile rbac-test.json -principalId $userid -builtInRoleType Reader
172+
New-AzResourceGroupDeployment -ResourceGroupName ExampleGroup -TemplateFile rbac-test.json -principalId $objectid -builtInRoleType Reader
141173
```
142174

143175
```azurecli
144-
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$userid builtInRoleType=Reader
176+
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$objectid builtInRoleType=Reader
145177
```
146178

147179
Here are example [New-AzDeployment](/powershell/module/az.resources/new-azdeployment) and [az deployment create](/cli/azure/deployment#az-deployment-create) commands for how to start the deployment at a subscription scope and specify the location.
148180

149181
```azurepowershell
150-
New-AzDeployment -Location centralus -TemplateFile rbac-test.json -principalId $userid -builtInRoleType Reader
182+
New-AzDeployment -Location centralus -TemplateFile rbac-test.json -principalId $objectid -builtInRoleType Reader
151183
```
152184

153185
```azurecli
154-
az deployment create --location centralus --template-file rbac-test.json --parameters principalId=$userid builtInRoleType=Reader
186+
az deployment create --location centralus --template-file rbac-test.json --parameters principalId=$objectid builtInRoleType=Reader
155187
```
156188

157-
> [!NOTE]
158-
> This template is not idempotent unless the same `roleNameGuid` value is provided as a parameter for each deployment of the template. If no `roleNameGuid` is provided, by default a new GUID is generated on each deployment and subsequent deployments will fail with a `Conflict: RoleAssignmentExists` error.
159-
160189
## Create a role assignment at a resource scope
161190

162191
If you need to create a role assignment at the level of a resource, the format of the role assignment is different. You provide the resource provider namespace and resource type of the resource to assign the role to. You also include the name of the resource in the name of the role assignment.
@@ -176,8 +205,7 @@ The following template demonstrates:
176205

177206
To use the template, you must specify the following inputs:
178207

179-
- The unique identifier of a user, group, or application to assign the role to
180-
- The role to assign
208+
- The ID of a user, group, or application to assign the role to
181209

182210
```json
183211
{
@@ -243,11 +271,11 @@ To use the template, you must specify the following inputs:
243271
To deploy the previous template, you use the resource group commands. Here are example [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment) and [az group deployment create](/cli/azure/group/deployment#az-group-deployment-create) commands for how to start the deployment at a resource scope.
244272

245273
```azurepowershell
246-
New-AzResourceGroupDeployment -ResourceGroupName ExampleGroup -TemplateFile rbac-test.json -principalId $userid -builtInRoleType Contributor
274+
New-AzResourceGroupDeployment -ResourceGroupName ExampleGroup -TemplateFile rbac-test.json -principalId $objectid -builtInRoleType Contributor
247275
```
248276

249277
```azurecli
250-
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$userid builtInRoleType=Contributor
278+
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$objectid builtInRoleType=Contributor
251279
```
252280

253281
The following shows an example of the Contributor role assignment to a user for a storage account after deploying the template.

0 commit comments

Comments
 (0)