为什么某些Azure CLI命令需要az登录?

5
在我们的VSTS发布管道中,我们想调用一个PowerShell脚本,为我的Azure函数之一添加一个函数密钥(使用Key Management REST API)。
我根据这篇文章创建了一个脚本: https://www.markheath.net/post/managing-azure-function-keys
Param(
    [string] [Parameter(Mandatory=$true)] $ResourceGroup,
    [string] [Parameter(Mandatory=$true)] $AppName,
    [string] [Parameter(Mandatory=$true)] $FunctionName,
    [string] [Parameter(Mandatory=$true)] $KeyName,
    [string] [Parameter(Mandatory=$true)] $KeyValue
    )

    function getAuthenticationToken([string]$appName, [string]$resourceGroup)
    {
    $user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
            --query "[?publishMethod=='MSDeploy'].userName" -o tsv

    $pass = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
            --query "[?publishMethod=='MSDeploy'].userPWD" -o tsv

    $pair = "$($user):$($pass)"
    $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

    $jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $encodedCreds)} -Method GET

    return $jwt
}

function setFunctionKey([string]$appName, [string]$functionName, [string] $keyName, [string]$keyValue, [string]$jwt)
{
    $body = (@{
        "name" = $keyName
        "value" = $keyValue
    } | ConvertTo-Json)

    #Setting the SecurityProtocol is a workaround for calling Azure APIs, I think?
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    try {
        Invoke-RestMethod -Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/$keyName/" `
            -Headers @{Authorization=("Bearer $jwt")} `
            -Method PUT `
            -ContentType "application/json" `
            -Body $body
    } catch {
        $_.Exception | Format-List -Force
    }
}

$jwt = getAuthenticationToken $AppName $ResourceGroup
setFunctionKey $AppName $FunctionName $KeyName $KeyValue $jwt
Write-Host "Specified key '$KeyName' has been added to $FunctionName"

在本地工作正常,但在运行VSTS时调用时出现错误。

$user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
                --query "[?publishMethod=='MSDeploy'].userName" -o tsv

带有以下信息:

错误:请运行 'az login' 设置账户。

我们有其他可用的 Azure CLI 命令,例如 az cosmosdb database,因此我猜测我们的服务主体连接已经建立。这里可能出了什么问题?

2个回答

2

看起来我们使用的旧版PowerShell存在一个错误,当你创建一个新的服务连接身份验证时,它会保留旧的服务连接身份验证上下文,这就是本例中出现问题的原因。

所以我们在构建代理上更新了PowerShell,问题得到解决!


0

从您上面发布的文档中,有一些您可能会错过的细节。

第一步是我们需要获取调用Kudu API的凭据。如果您已经通过Azure CLI进行了身份验证,您可以通过调用az webapp deployment list-publishing-profiles命令并提取MSDeploy的userName和userPWD来实现。您需要提供函数应用程序名称和资源组名称。

看起来在使用它们之前,您应该对Azure CLI进行身份验证。


我认为所有影响环境的Azure CLI调用都需要进行身份验证?我猜在这种情况下有不同类型的身份验证“级别”(缺乏正确术语)? - Kimmen
@Kimmen 是的,如果您想使用 Azure CLI,首先需要进行身份验证。 - Charles Xu
我在其他PowerShell任务中成功运行了其他Azure CLI命令,例如“az cosmosdb database..”。设置服务连接选项,这不应该是用于验证PowerShell任务的吗? - Kimmen
@Kimmen,你是否在其他会话或同一会话中使用过az cosmosdb database命令?在执行该命令之前,请确保已经进行了身份验证。 - Charles Xu
同一会话。 - Kimmen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接