|
| 1 | +# Managed Identity Best Practices |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document provides a cmdlet best practice for supporting Managed Identity in Azure PowerShell. |
| 5 | +- New cmdlets are required to follow the best practices. If any further discussion is needed, please contact [Azure PowerShell team ](mailto: [email protected]); |
| 6 | +- For existing cmdlets, we are strongly recommended to keep aligned with the best practice. Please contact [Azure PowerShell team ](mailto: [email protected]) to make a proper migration plan. |
| 7 | + |
| 8 | +## Applicability |
| 9 | +Resources supported Managed Identity in management plane with common type definition in [managedidentity.json](https://github.com/Azure/azure-rest-api-specs/blob/main/specification/common-types/resource-management/v5/managedidentity.json). |
| 10 | + |
| 11 | +## Cmdlet Syntax |
| 12 | + |
| 13 | +### New- Cmdlet Design Practices |
| 14 | +Use `[EnableSystemAssignedIdentity <SwitchParameter>]` to enable system-assigned identity and `[UserAssignedIdentity <string[]>]` to add user-assigned identities. |
| 15 | + |
| 16 | +```powershell |
| 17 | +New-AzResource ... -EnableSystemAssignedIdentity -UserAssignedIdentity <id1>, <id2> |
| 18 | +``` |
| 19 | + |
| 20 | +- If `EnableSystemAssignedIdentity` is present, IdentityType is set up as `SystemAssigned`, which enables system-assigned identity; |
| 21 | +- If `UserAssignedIdentity` is provided, IdentityType is set up as `UserAssigned`, which adds user-assigned identities by provided value; |
| 22 | +- If `EnableSystemAssignedIdentity` and `UserAssignedIdentity` both are not presented, IdentityType is set up as `None`; |
| 23 | +- If `EnableSystemAssignedIdentity` is present and `UserAssignedIdentity` is provided, IdentityType is set up as `SystemAssigned,UserAssigned`, which enables system-assigned identity and adds user-assigned identities by provided value; |
| 24 | + |
| 25 | +### Update- Cmdlet Design Practices |
| 26 | +Use `[EnableSystemAssignedIdentity <bool>]` to enable or disable system-assigned identity and `[UserAssignedIdentity <string[]>]` to set user-assigned identities. |
| 27 | +#### Update System-assigned Identity |
| 28 | +```powershell |
| 29 | +Update-AzResource ... -EnableSystemAssignedIdentity $false |
| 30 | +``` |
| 31 | +If `EnableSystemAssignedIdentity` is provided, $false disables system-assigned identity and $true enables system-assigned identity. If `EnableSystemAssignedIdentity` is not provided, it means no change happens on system-assigned identity. |
| 32 | + |
| 33 | +#### Update User-assigned Identity |
| 34 | +```powershell |
| 35 | +Update-AzResource ... -UserAssignedIdentity <id1>, <id2> |
| 36 | +``` |
| 37 | +If `UserAssignedIdentity` is provided, user-assigned identities will be overridden as the value of `UserAssignedIdentity`; If `UserAssignedIdentity` is not provided, it means keep user-assigned identity as previous value. |
| 38 | + |
| 39 | +#### Remove User-assigned Identity |
| 40 | +```powershell |
| 41 | +Update-AzResource ... -UserAssignedIdentity @() |
| 42 | +``` |
| 43 | +Especially, setting `UserAssignedIdentity` as empty collection removes all existing user-assigned identities. |
| 44 | + |
| 45 | +### Set- Cmdlet Design Practices |
| 46 | +The design practices of Set- Cmdlet depends on if cmdlet will set properties that are not provided as empty or default value, |
| 47 | +- If yes, please follow [New- Cmdlet Design Practices](#New--Cmdlet-Design-Practices); |
| 48 | +- Otherwise, please follow [Update- Cmdlet Design Practices](#Update--Cmdlet-Design-Practices). |
| 49 | + |
| 50 | +## Migration for Existing Cmdlets |
| 51 | +Please contact [Azure PowerShell team ](mailto: [email protected]) to make a proper migration plan. |
| 52 | + |
| 53 | +## Frequently Asked Question |
| 54 | +### Is it required to set the type of UserAssignedIdentity as string array if service only supports one user assigned identity? |
| 55 | +We are recommended to use string array as the type of UserAssignedIdentity with following reasons: |
| 56 | +- string array matches the swagger definition of `UserAssignedIdentity`; |
| 57 | +- string array is inclusive of a single string; |
| 58 | +- No syntax changes if service supports one more user assigned identity in future; |
| 59 | +- Service will provide correct error response if customer reaches the count limitation of `UserAssignedIdentity` ideally, which means no harm. |
| 60 | + |
0 commit comments