[英]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.