【问题标题】:Read AD group from azure function using service principal authentication使用服务主体身份验证从 azure 函数读取 AD 组
【发布时间】:2019-10-30 17:13:00
【问题描述】:

我想使用 azure 函数的 powershell 脚本读取和列出特定 AD 组的成员。要连接 AD,我使用的是服务主体。连接到 AzureAD 是成功的,但是尝试访问 AD 组给我一个错误(在这个阶段我只想得到一个特定的组并回显它):

System.Management.Automation.RemoteException: Error occurred while executing GetGroups 
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
RequestId: <requestID>
DateTimeStamp: Mon, 14 Oct 2019 20:40:26 GMT
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed

为什么会这样?有人在 azure 函数中使用 azuread 模块命令吗?我已为此应用授予 ms 图表权限:

$Script={
    param ()
    ##Save AzureAD module to the modules folder before publishing
    Import-Module .\modules\AzureAD

    $appId = "<AppId>"
    $thumb = "<CertThumb>"
    $tenantId = "TenantID"
    Connect-AzureAD -TenantId $tenantId -ApplicationId  $appId -CertificateThumbprint $thumb

    $groupName = "<Name of the group>"

    $group = Get-AzureADGroup -SearchString $groupName 
    #or
    #$group = Get-AzureADGroup -ObjectId "<object id>"

    echo $group
}

&$env:64bitPowerShellPath -WindowStyle Hidden -NonInteractive -Command $Script

请注意,我的代码被包装到 $Script 变量中,并添加了最后一行以使代码作为临时解决方法工作,直到将 AD 模块添加到 PS Core: https://github.com/Azure/azure-functions-powershell-worker/issues/232

【问题讨论】:

  • 您可以尝试在 AAD Graph API 上授予它类似的权限吗? (Windows Azure Active Directory)只是一种预感,但我记得一些 PowerShell cmdlet 使用了这个旧 API。

标签: azure powershell azure-active-directory azure-functions azure-ad-graph-api


【解决方案1】:

正如另一个回复中提到的,将服务主体添加为目录角色是一种方法,但您应该注意它会给您的服务主体提供其他权限,例如create groupdelete group

其实问题是你授予了错误的权限,你需要授予Azure Active Directory GraphDirectory.Read.All应用权限而不是Microsoft Graph,因为命令Get-AzureADGroup本质上调用了Azure Active Directory Graph

注意:在本地测试命令时,授予权限后,关闭你的powershell会话并打开一个新的,再次登录并运行命令。如果您在函数中运行它,可能需要重新启动函数应用以确保权限受到影响。

【讨论】:

  • 感谢@Joy Wang 的回复,我会试试这个,明天我会得到管理员权限的时候告诉你结果。
【解决方案2】:

根据您提供的图片,您已为服务主体分配了一些图形 API 权限。完成此操作后,您只需使用服务主体调用一些图形 API。如果要使用 Azure AD PowerShell 模块和服务主体来管理 Azure AD,则需要将 Azure AD 角色分配给服务主体。更多详情请参考https://docs.microsoft.com/en-us/powershell/azure/active-directory/signing-in-service-principal?view=azureadps-2.0

关于如何创建服务主体和分配角色,请参考以下脚本。

# Login to Azure AD PowerShell With Admin Account
Connect-AzureAD 

# Create the self signed cert
$currentDate = Get-Date
$endDate  = $currentDate.AddYears(1)
$notAfter  = $endDate.AddYears(1)
$pwd  = "<password>"
$thumb = (New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -DnsName com.foo.bar -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $notAfter).Thumbprint
$pwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
Export-PfxCertificate -cert "cert:\localmachine\my\$thumb" -FilePath c:\temp\examplecert.pfx -Password $pwd

# Load the certificate
$cert  = New-Object System.Security.Cryptography.X509Certificates.X509Certificate("C:\temp\examplecert.pfx", $pwd)
$keyValue = [System.Convert]::ToBase64String($cert.GetRawCertData())


# Create the Azure Active Directory Application
$application = New-AzureADApplication -DisplayName "test123" -IdentifierUris "https://test123"
New-AzureADApplicationKeyCredential -ObjectId $application.ObjectId -CustomKeyIdentifier "Test123" -StartDate $currentDate -EndDate $endDate -Type AsymmetricX509Cert -Usage Verify -Value $keyValue

# Create the Service Principal and connect it to the Application
$sp = New-AzureADServicePrincipal -AppId $application.AppId

# Give the Service Principal Reader access to the current tenant (Get-AzureADDirectoryRole)
#Regarding the Azure AD role, please refer to https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/roles-delegate-by-task
$role = Get-AzureADDirectoryRole | where-object {$_.DisplayName -eq "role name"}
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId $sp.ObjectId

【讨论】:

  • 已经试过了,我没有足够的权限。
  • @bryniek 如果你想运行脚本,我需要是全局管理员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多