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

[玩转系统] 从 PowerShell 调用 Azure 函数

作者:精品下载站 日期:2024-12-14 07:13:54 浏览:14 分类:玩电脑

从 PowerShell 调用 Azure 函数


这篇文章是有关 Azure Functions 和 PowerShell 的系列文章的一部分。查看该系列中其他帖子的列表!

到目前为止,我们已经设置了一个 Azure Function 应用程序,并使用 Azure Active Directory 配置了身份验证和授权。现在,只要我们期望用户使用浏览器点击我们的功能,一切就可以了。我经常使用函数来构建由代码调用的 API。就我而言,代码通常是 PowerShell 脚本,但我们暂时将代码称为应用程序。

注册调用 api 的客户端应用程序

由于在这种情况下我们的应用程序在客户端计算机上运行,因此我们不能依赖应用程序的身份,因为这需要应用程序具有某种秘密(密码或证书),并且必须嵌入该秘密代码中,我们的应用程序的用户可以提取该代码。一如既往,代码中没有秘密!

这也意味着我们不能依赖 Azure AD 将令牌发送到 RedirectURI 中指定的公共终结点,因为我们的客户端很可能不会暴露在 Internet 上。

为了解决这个问题,有一种叫做公共客户端的东西。基本上,我们注册的所有应用程序都可以设置为充当客户端(如果它们要调用其他服务)。用于 Web 服务器和 API 的常规应用程序称为机密客户端,因为它们的代码在受控环境中运行,我们可以安全地提供秘密(密码或证书)。桌面应用程序和脚本无法安全地存储秘密,这使得它们成为公共客户端类别的一部分。

在我们的例子中,我们需要一个使用本机客户端方法设置的应用程序(或应用程序注册)。本机客户端只是桌面应用程序或脚本等应用程序的另一种说法。现在我们可以将此配置添加到当前的应用程序中,但为了使事情更加干净和分离,我将介绍如何创建单独的客户端应用程序。

让我们看一个应用程序注册示例!

和以前一样,我将从 PowerShell 运行 Azure CLI。我们将需要我们之前创建的服务器应用程序的应用程序 ID(也称为 clientId)。这次我们将重定向 uri 设置为 http://localhost

通过服务器应用程序的 ID,我们可以从应用程序获取所有 oauth2Permissions,并使用权限 user_impersonation 授予客户端应用程序以用户身份登录服务器应用程序的权限。

创建应用程序后,我们需要将 requestsAccessTokenVersion 设置为 2,就像我们对服务器应用程序所做的那样。这告诉 Azure AD 我们需要版本 2 令牌,这是我们需要的,因为我们在应用服务上设置高级模式身份验证时在颁发者末尾指定了 v2.0。

$ClientAppDisplayName = 'MySuperApiClient'
$ServerAppId = 'c491d3f8-0d15-4ea5-96fd-957601d579fa'
$RedirectUri = 'https://localhost'

# Get the server app
$ServerApp = az ad app show --id $ServerAppId | ConvertFrom-Json

# Get oAuthPermission for user_impersonation from server app
$oAuthPermissionId = az ad app show --id $ServerApp.AppId --query "oauth2Permissions[?value=='user_impersonation'].id" -o tsv

# Build part of a manifest for requiredResourceAccess
$requiredResourceAccess = @{
    resourceAppId  = $ServerApp.AppId
    resourceAccess = @(
        @{
            id   = $oAuthPermissionId
            type = 'Scope'
        }
    ) 
} | ConvertTo-Json -AsArray -Depth 4 -Compress | ConvertTo-Json
# Pipe to ConvertTo-Json twice to escape all quotes, or az cli will remove them when parsing

# Register client application
$ClientApp = az ad app create --display-name $ClientAppDisplayName --native-app --reply-urls $RedirectUri --required-resource-accesses $requiredResourceAccess | ConvertFrom-Json
# Create a service principal for the application
$null = az ad sp create --id $ClientApp.appId | ConvertFrom-Json

# Consent the application for all users
$null = az ad app permission grant --id $ClientApp.AppId --api $ServerApp.AppId

# Disable implicit flow, we don't need this for authcode or device code flows
$null = az ad app update --id $ClientApp.AppId --set oauth2AllowIdTokenImplicitFlow=false *>&1

# Set application to use V2 access tokens
$Body = @{
    api = @{
        requestedAccessTokenVersion = 2
    }
} | ConvertTo-Json -Compress | ConvertTo-Json
# Pipe to ConvertTo-Json twice to escape all quotes, or az cli will remove them when parsing
$null = az rest --method PATCH --uri "https://graph.microsoft.com/v1.0/applications/$($ClientApp.objectId)" --body $Body --headers "Content-Type=application/json"

# Output the ClientId for use later
Write-Output $ClientApp.AppId

现在我们需要将客户端应用程序设置为要求为用户分配角色,就像我们为服务器应用程序所做的那样。

$ClientAppId = '479fe3c1-a9a4-4098-b343-db98e7c6e81b'
az ad sp update --id $ClientAppId --set appRoleAssignmentRequired=true

我们可以再次为用户分配默认访问角色,就像我们对上面的服务器应用程序所做的那样,这次使用我们的客户端 appId 而不是服务器。

$AppId = '479fe3c1-a9a4-4098-b343-db98e7c6e81b'
$UserPrincipalName = '[email '

$App = az ad sp show --id $AppId | ConvertFrom-Json
$principalId = az ad user show --id $UserPrincipalName --query 'objectId' -o tsv

$Body = @{
    appRoleId = [Guid]::Empty.Guid
    principalId = $principalId
    resourceId = $App.objectId
} | ConvertTo-Json -Compress | ConvertTo-Json
az rest --method post --uri "https://graph.microsoft.com/v1.0/users/$UserPrincipalName/appRoleAssignments" --body $Body --headers "Content-Type=application/json"

就是这样!现在我们有一个服务器和一个客户端应用程序,我们将它们设置为要求为用户分配角色。获得对服务器应用程序的访问权限将允许用户使用浏览器浏览服务器,授予对客户端应用程序的访问权限将允许用户使用客户端应用程序访问服务器。

现在,我们当然可以通过简单地将本机客户端平台添加到我们的服务器应用程序中,将这些应用程序合并为一个,如果您使用 Azure Functions 构建 API,这可能是更好的方法,但我选择在我的例子涵盖了双方,有时我们只需要设置一个服务器应用程序,有时我们只需要设置一个客户端应用程序,有时客户端应用程序需要多个API的权限。

让我们快速讨论一下这篇相当长的文章的最后部分。如何使用 PowerShell 访问我的 API?

使用 PowerShell 从 AzureAD 获取 OAuth 令牌

有多种方法可以从 Azure AD 获取 oauth 令牌。这次我将使用 PowerShell 模块 MSAL.PS。 MSAL.PS 是 dotnet 的 MSAL 客户端库的包装器,它包含我们需要的所有内容,可供使用!

首先安装模块。

Install-Module MSAL.PS

现在我们需要请求令牌的应用程序的 clientID、租户的租户 ID 以及范围列表。重定向 URI 需要与我们的应用程序上配置的重定向 URI 匹配。

$ClientAppId = '479fe3c1-a9a4-4098-b343-db98e7c6e81b'
$TenantId = "b5fbba89-c11c-4ba3-baf3-67fb6d5fb61f"
$Scopes = 'https://mysuperapi.azurewebsites.net/user_impersonation'
$RedirectUri = 'http://localhost'

Import-Module 'MSAL.PS' -ErrorAction 'Stop'
$PublicClient = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($ClientAppId).WithRedirectUri($RedirectUri).Build()
$token = Get-MsalToken -PublicClientApplication $PublicClient -TenantId $TenantId -Scopes $Scopes

Invoke-RestMethod -Uri "https://mysuperapi.azurewebsites.net/api/mysuperfunc" -Headers @{Authorization = "Bearer $($token.AccessToken)" }

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

取消回复欢迎 发表评论:

关灯