我發現很難找到將connect-azaccount 與 azure devops 管道一起使用的最佳和安全方式。我在管道中有以下這個簡單的 powershell 腳本,它用于創建 azure 資源。只是為了簡化事情,我只使用了資源組的創建:
$Location = "Location Name"
$resourceGroupName = "Resource Group Name"
try {
#Creation of Resource Group
$resourceGroup = Get-AzResourceGroup -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
if($null -eq $resourceGroup)
{
New-AzResourceGroup -Name $resourceGroupName -Location $Location
}
else
{
Write-Host "The ResourceGroup with the name: $resourceGroupName already exists."
}
}
catch
{
Write-Host "Error occurred: $_"
}
這里的問題是當管道正在運行并到達 Powershell 任務時,它給了我一個錯誤,錯誤發生:運行 Connect-AzAccount 登錄。
我的問題是,老實說,我不知道在不輸入任何用戶憑據的情況下哪種方式是最安全的連接方式。它應該直接連接和創建資源。請注意,我使用的是 Multi-Factor Authentication。為了實作這一點,我找到了幾種解決方案,但在選擇最佳方式時我需要幫助。我通過在 Yaml 檔案中添加一個 powershell 任務找到了幾個解決方案。這是顯示運行腳本的 powershell 任務的 Yaml:
- task: PowerShell@2
inputs:
filePath: '$(Pipeline.Workspace)/Deploy/functionapp.ps1'
選項1:
Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx' -SubscriptionId 'yyyy-yyyy-yyyy-yyyy'
現在這里的問題是租戶 ID 和訂閱將在代碼中可見,這是一個非常糟糕的做法
選項 2 是使用以下腳本:
$User = "[email protected]"
$PWord = ConvertTo-SecureString -String "<Password>" -AsPlainText -Force
$tenant = "<tenant id>"
$subscription = "<subscription id>"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
Connect-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription
這與第一個非常相似,但如果我沒記錯的話,它僅限于特定用戶。
選項 3 是使用服務主體:
$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal
I don't know if creating a service principal will incur any costs and what steps should I do to make it work.
I am honestly new to all this, can someone please provide me what are the exact steps to achieve this. Thank you for your answers :)
uj5u.com熱心網友回復:
最安全的方法是創建 Azure 資源管理器服務連接并在管道中使用它。您可以使用自動方式創建它,也可以使用以前創建的服務主體手動創建它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405511.html
標籤:
