繁体   English   中英

使用 Azure Devops 管道连接 AzAccount?

[英]Connect-AzAccount with Azure Devops Pipeline?

我发现很难找到将 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 任务找到了几种解决方案。 这是 Yaml 显示 powershell 任务运行脚本:

  - 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 = "xxx@xxxx.onmicrosoft.com"
$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

我不知道创建服务主体是否会产生任何费用以及我应该采取哪些步骤才能使其正常工作。

老实说,我对这一切都很陌生,有人可以告诉我实现这一目标的确切步骤是什么。 谢谢您的回答:)

最安全的方法是创建 Azure 资源管理器服务连接并在您的管道中使用它。 您可以使用自动方式创建它,也可以使用以前创建的服务主体手动创建它。

暂无
暂无

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

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