繁体   English   中英

为什么某些 Azure CLI 命令需要 az login

[英]Why does certain Azure CLI commands require az login

在我们的 VSTS 发布管道中,我们想要调用一个 powershell 脚本,该脚本为我的 Azure 函数之一(使用密钥管理其余 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 数据库,所以我猜我们的服务原则连接已经到位。 这里可能是什么问题?

似乎我们有一个旧的 powershell 版本,其中包含一个错误,当您创建新的服务连接身份验证时,该错误会保留旧的服务连接身份验证上下文,我在本例中就是这样做的。

因此,我们更新了构建代理上的 powershell,一切顺利!

从您上面发布的文档中,您可能会遗漏一些内容。

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

您似乎应该在使用 Azure CLI 之前对其进行身份验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM