当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 Azure 托管身份

作者:精品下载站 日期:2024-12-14 03:44:22 浏览:16 分类:玩电脑

如何使用 Azure 托管身份


Azure 资源(例如 Azure 自动化或 Azure 虚拟机)通常需要访问其他资源。例如,从 Runbook 中访问 Azure AD 时。要授予访问权限,您通常需要帐户、密码或证书。但您不想将此信息存储在代码中。这就是 Azure 托管身份的用武之地。

Azure 托管标识为 Azure 资源提供 Azure AD 内的托管标识。这样,您就可以轻松管理权限并在 Azure 中进行身份验证,而无需存储凭据。

在本文中,我们将了解什么是 Azure 托管身份、如何创建它们以及如何使用它们。我还创建了一个 PowerShell 脚本,可让您轻松设置托管身份的权限。

什么是 Azure 托管身份

Azure 托管标识是安全管理对 Azure 资源的访问的重要工具。这些身份为 Azure 应用程序和服务提供了一种对自身进行身份验证和授权的方法,而无需存储和管理凭据。这降低了凭证被盗或滥用的风险,因为没有凭证可供窃取或管理。

它们的工作原理是在 Azure Active Directory (AAD) 中创建身份(企业应用程序)。然后,该标识与特定的 Azure 资源(例如 Azure 自动化帐户)相关联。您可以向 Azure 中的不同 API(例如 MsGraph 或 Exchange Online)授予身份特定权限,就像 Azure AD 中的常规企业应用程序一样。然后,资源使用该身份来验证和授权访问,而无需凭据。

托管身份类型

Azure 托管标识有两种类型,主要区别在于它们的创建和管理方式:

CreatedLife cycleSharingSystem-assignedPart of an Azure resourceDeleted when parent resource is deletedCan only be used for one Azure resourceUser-assignedStandalone resourceIndependent life cycleCan be shared with more than one Azure Resource

系统分配的托管身份

系统分配的托管标识与一种特定资源相关联。您可以为大多数 Azure 服务启用它们,例如虚拟机或自动化帐户。它在您的 Azure AD 中创建唯一标识,允许您为资源设置特定权限。

系统分配的标识的优点是,当您删除资源时,它们会自动删除。

用户分配的托管身份

另一方面,用户分配的托管标识是独立的 Azure 资源。它们可以与一项或多项 Azure 服务关联。这为身份管理提供了更大的灵活性和控制力,允许您创建和管理自己的身份并将其用于多种资源。

这也意味着当您删除资源时,用户分配的身份不会被删除,从而允许您重用该身份。

创建 Azure 托管身份

对于每个 Azure 服务,创建托管标识和分配权限的原则几乎相同。对于下面的示例,我们将为 Azure 自动化帐户创建托管标识。然后,我们可以使用 Runbook 中的身份来访问不同的 API,例如 MsGraph 或 Exchange online。

我假设您已经创建了一个自动化帐户,如果没有,请先阅读本文以开始使用。

步骤 1 - 启用系统分配的托管身份

第一步是启用系统分配的托管标识。如果您打开 Azure 服务(在本例中为自动化帐户),您将在左侧菜单中找到身份项(可能需要向下滚动一点)。选择此项并启用系统分配的身份,然后单击保存

[玩转系统] 如何使用 Azure 托管身份

第 2 步 - 分配角色

单击保存后,您将看到一个确认弹出窗口。确认要创建系统分配的标识后,您将看到对象 ID 和分配 Azure 角色的选项。此处重要的是,我们添加与当前运行方式帐户相同的角色(如果您要迁移 Runbook)。

  1. 在您的 Azure 自动化帐户中,单击作为帐户运行
  2. 打开运行方式帐户

[玩转系统] 如何使用 Azure 托管身份

  1. 记下当前分配给该帐户的角色

[玩转系统] 如何使用 Azure 托管身份

  1. 返回身份
  2. 单击Azure 角色分配

[玩转系统] 如何使用 Azure 托管身份

我们将向系统分配的身份分配与我们的 RunAs 帐户相同的角色。

  1. 点击添加角色分配
  2. 设置范围
  3. 添加所需的角色
  4. 点击保存

[玩转系统] 如何使用 Azure 托管身份

例如,您现在已经可以从 Azure Runbook 中连接到 Azure 资源。所需的模块已加载到新的自动化帐户中。但是,对于现有的自动化帐户,您至少需要加载以下模块:

  • AZ.托管服务身份
  • AZ.账户
  • AZ.自动化

步骤 3 - 添加权限

Azure 角色仅处理对其他 Azure 资源的权限,但不会授予托管标识对 MsGraph 或 Exchange Online 等的访问权限。为此,我们需要向每项服务授予权限。

您可以在Azure门户中查看分配的权限,但我们无法分配权限。为此,我们需要使用 PowerShell,并且需要托管身份的对象 ID。

我们首先查找对象 ID:

  1. 在 Azure 门户中打开企业应用程序
  2. 选择所有应用程序
  3. 单击过滤器应用程序类型
  4. 选择托管身份并单击“应用”

[玩转系统] 如何使用 Azure 托管身份

现在仅列出托管身份。打开我们之前创建的身份而不是对象 ID:

[玩转系统] 如何使用 Azure 托管身份

在计算机上打开 PowerShell,确保安装了 Microsoft Graph 模块,并连接到具有以下范围的 Graph:

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Application.Read.All","AppRoleAssignment.ReadWrite.All,RoleManagement.ReadWrite.Directory"

# Select beta profile
Select-MgProfile Beta

我们首先将托管身份对象 ID 存储到一个变量中,因为我们将在下面的脚本中使用它几次:

$managedIdentityId = "3e4c2842-1778-407f-9b3f-2191f10874ec"

# We can also lookup the object id using the name of the managed identity:
$managedIdentityId = (Get-MgServicePrincipal -Filter "displayName eq 'LazyAutomationAccount'").id

MsGraph权限

我们首先根据标准 App ID 获取 Graph 的服务主体:

$graphApp = Get-MgServicePrincipal -Filter "AppId eq '00000003-0000-0000-c000-000000000000'"

对于 Graph,我们需要定义需要权限的范围。然后,我们可以通过查找应用程序角色来授予每个范围的权限:

$graphScopes = @(
  'UserAuthenticationMethod.Read.All',
  'Group.ReadWrite.All',
  'Directory.Read.All',
  'User.ReadWrite.All'
)

ForEach($scope in $graphScopes){
  $appRole = $graphApp.AppRoles | Where-Object {$_.Value -eq $scope}
  New-MgServicePrincipalAppRoleAssignment -PrincipalId $managedIdentityId -ServicePrincipalId $managedIdentityId -ResourceId $graphApp.Id -AppRoleId $appRole.Id
}

例如,我们现在可以在 Azure Runbook 中使用 MgGraph。对于 Runbook,请确保至少已将以下模块添加到 Azure 自动化帐户:

  • Microsoft.Graph.身份验证
  • Microsoft.Graph.用户

要连接到 Graph,我们首先需要连接到 Azure,以便获得访问令牌。

笔记

确保您的自动化帐户中加载了以下模块:
- Az.ManagedServiceIdentity
- Az.Accounts
- Az.Automation
否则您将无法使用 Connect- AzAccount cmdlet
try {
    # Logging in to Azure.
    Connect-AzAccount -Identity

    # Get token and connect to MgGraph
    Connect-MgGraph -AccessToken ((Get-AzAccessToken -ResourceTypeName MSGraph).token)
} catch {
    Write-Error -Message $_.Exception
    throw $_.Exception
}

# Test if we can get users:
Get-MgUser

通过即将推出的新 V2 模块,连接到 Microsoft Graph 将变得更加容易。目前处于预览阶段,但发布后,您可以使用 -identity 参数以托管身份连接到 Microsoft Graph。

# For System Assigned Managed Identies
Connect-MgGraph -Identity

# For User Assigned Managed Identies
Connect-MgGraph -Identity -ClientId "User_Assigned_Managed_identity_Client_Id"

SharePoint Online 权限

同样对于 SharePoint,我们首先需要获取服务主体并定义我们要授予权限的范围。使用以下 PowerShell 命令将 SharePoint 权限添加到您的托管标识:

$spoApp = Get-MgServicePrincipal -Filter "AppId eq '00000003-0000-0ff1-ce00-000000000000'"

$spoScopes = @(
  'Sites.FullControl.All',
  'TermStore.ReadWrite.All',
  'User.ReadWrite.All'
)

ForEach($scope in $spoScopes){
  $appRole = $spoApp.AppRoles | Where-Object {$_.Value -eq $scope}
  New-MgServicePrincipalAppRoleAssignment -PrincipalId $managedIdentityId -ServicePrincipalId $managedIdentityId -ResourceId $spoApp.Id -AppRoleId $appRole.Id
}

要连接到 Runbook 内的 SharePoint Online,您需要将 PnP-Online 模块添加到您的自动化帐户。通过添加 -ManagedIdentity 参数来连接到 PnPOnline:

Connect-PnPOnline -Url  lazydev-admin.sharepoint.com -ManagedIdentity

# Test connection
Get-PnPTeamsTeam

在线交流

对于Exchange,我们往往只需要ManageAsApp角色。所以我们可以简单地使用以下命令添加权限:

$exoApp = Get-MgServicePrincipal -Filter "AppId eq '00000002-0000-0ff1-ce00-000000000000'"

$appRole = $exoApp.AppRoles | Where-Object {$_.DisplayName -eq "Manage Exchange As Application"}

$AppRoleAssignment = @{
  "PrincipalId" = $managedIdentityId
  "ServicePrincipalId" = $managedIdentityId
  "ResourceId" = $exoApp.Id
  "AppRoleId" = $appRole.Id
}
New-MgServicePrincipalAppRoleAssignment @AppRoleAssignment

除了权限之外,我们还需要为我们的托管身份提供 Exchange 管理员角色。否则,您将收到以下错误:

The role assigned to application <id> isn't supported in this scenario. Please check online documentation for assigning correct Directory Roles to Azure AD Application for EXO App-Only Authentication.

我们可以使用 MgGraph 或使用 Azure 门户来添加角色。对于后者,请转到 Azure Active Directory > 角色和管理员。搜索 Exchange 管理员角色并将您的托管身份添加为成员。但我们当然也可以使用 PowerShell:

# Make sure that you connected to Graph with the scope RoleManagement.ReadWrite.Directory
$roleId = (Get-MgRoleManagementDirectoryRoleDefinition -Filter "DisplayName eq 'Exchange Administrator'").id

New-MgRoleManagementDirectoryRoleAssignment -PrincipalId $managedIdentityId -RoleDefinitionId $roleId -DirectoryScopeId "/""

若要在 Azure Runbook 中使用 Exchange Online 连接,需要确保自动化帐户中加载了最新的 Exchange Online (EXO V3) 模块。因为这将使使用托管身份连接到 Exchange Online 变得更加简单:

Connect-ExchangeOnline -ManagedIdentity -Organization a-d.onmicrosoft.com

验证权限

添加所有权限后,最好验证它们是否已按预期全部添加到您的托管身份中。我们可以查看Azure Active Directory中的权限:

  1. 打开 Portal.azure.com 并导航到 Azure AD 中的企业应用程序
  2. 点击所有应用程序
  3. 将过滤器应用程序类型更改为托管身份

[玩转系统] 如何使用 Azure 托管身份

  1. 选择托管身份
  2. 单击权限进行查看

[玩转系统] 如何使用 Azure 托管身份

托管身份权限脚本

我还创建了一个完整的脚本,它将为您的托管身份设置所有权限。您只需设置托管身份的名称,也可以使用身份的对象 ID。该脚本将设置所有必需的权限。您可以在我的 GitHub 存储库中找到该脚本的最新版本

# Change this to your Managed Identity app name:
$managedIdentityName = "LazyAutomationAccount"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Application.Read.All","AppRoleAssignment.ReadWrite.All,RoleManagement.ReadWrite.Directory"
Select-MgProfile Beta

# Get the Managed Identity Object id
# You can find the name or object id in Azure > Automation Account > Identity
# $managedIdentityId = "<id-number-goes-here>"
$managedIdentityId = (Get-MgServicePrincipal -Filter "displayName eq $managedIdentityName").id

#
# Adding Microsoft Graph permissions
#
Write-host "Adding Microsoft Graph Permissions" -ForegroundColor Cyan

$graphApp = Get-MgServicePrincipal -Filter "AppId eq '00000003-0000-0000-c000-000000000000'"

# Add the required Graph scopes
$graphScopes = @(
  'UserAuthenticationMethod.Read.All',
  'Group.ReadWrite.All',
  'Directory.Read.All',
  'User.ReadWrite.All'
)
ForEach($scope in $graphScopes){
  $appRole = $graphApp.AppRoles | Where-Object {$_.Value -eq $scope}

  if ($null -eq $appRole) { Write-Warning "Unable to find App Role for scope $scope"; continue; }

  # Check if permissions isn't already assigned
  $assignedAppRole = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentityId | Where-Object { $_.AppRoleId -eq $appRole.Id -and $_.ResourceDisplayName -eq "Microsoft Graph" }

  if ($null -eq $assignedAppRole) {
    New-MgServicePrincipalAppRoleAssignment -PrincipalId $managedIdentityId -ServicePrincipalId $managedIdentityId -ResourceId $graphApp.Id -AppRoleId $appRole.Id
  }else{
    write-host "Scope $scope already assigned"
  }
}

#
# SharePoint Online Permissions
#
Write-host "Adding SharePoint Online Permissions" -ForegroundColor Cyan

$spoApp = Get-MgServicePrincipal -Filter "AppId eq '00000003-0000-0ff1-ce00-000000000000'"

# Add the required SPO scopes
$spoScopes = @(
  'Sites.FullControl.All',
  'TermStore.ReadWrite.All',
  'User.ReadWrite.All'
)
ForEach($scope in $spoScopes){
  $appRole = $spoApp.AppRoles | Where-Object {$_.Value -eq $scope}

  if ($null -eq $appRole) { Write-Warning "Unable to find App Role for scope $scope"; continue; }

  # Check if permissions isn't already assigned
  $assignedAppRole = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentityId | Where-Object { $_.AppRoleId -eq $appRole.Id -and $_.ResourceDisplayName -eq "Office 365 SharePoint Online"}

  if ($null -eq $assignedAppRole) {
    New-MgServicePrincipalAppRoleAssignment -PrincipalId $managedIdentityId -ServicePrincipalId $managedIdentityId -ResourceId $spoApp.Id -AppRoleId $appRole.Id
  }else{
    write-host "Scope $scope already assigned"
  }
}


#
# Adding Exchange Online permissions
#
Write-host "Adding Exchange Online Permissions" -ForegroundColor Cyan

$exoApp = Get-MgServicePrincipal -Filter "AppId eq '00000002-0000-0ff1-ce00-000000000000'"
$appRole = $exoApp.AppRoles | Where-Object {$_.DisplayName -eq "Manage Exchange As Application"}

$AppRoleAssignment = @{
  "PrincipalId" = $managedIdentityId
  "ServicePrincipalId" = $managedIdentityId
  "ResourceId" = $exoApp.Id
  "AppRoleId" = $appRole.Id
}
New-MgServicePrincipalAppRoleAssignment @AppRoleAssignment

# Add Exchange Administrator Role
$roleId = (Get-MgRoleManagementDirectoryRoleDefinition -Filter "DisplayName eq 'Exchange Administrator'").id
New-MgRoleManagementDirectoryRoleAssignment -PrincipalId $managedIdentityId -RoleDefinitionId $roleId -DirectoryScopeId "/"

总结

托管身份非常适合安全地管理对 Azure 资源的访问。它提供了一种安全、简单的方法来管理对资源的访问,无需凭据或复杂的安全策略,从而降低了凭据被盗或滥用的风险。

更新脚本时,请确保还切换到最新版本的 Exchange Online 模块,并将 Azure AD 模块替换为新的 Microsoft Graph 模块。

希望您喜欢这篇文章,如果您有任何疑问,请在下面发表评论。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯