VSTS Build和PowerShell以及AzureAD身份验证

问题描述 投票:5回答:3

我有一个VSTS项目通过服务主体通过Azure Resource Manager端点连接到Azure订阅。这适用于通过模板化,参数驱动的部署配置ARM资源的构建。

我还需要将Azure AD组设置为构建的一部分。我有一个脚本,可以在我的本地机器上正常工作。当我通过构建部署它并在托管构建控制器上执行时,脚本最初可能找不到AzureAD模块。我通过在git Repo中包含脚本并通过以下方式访问它来解决这个问题:

$adModulePath = $PSScriptRoot + "\PsModules\AzureAD\2.0.0.131\AzureAD.psd1"
Import-Module $adModulePath 

但是,在运行New-AzureADGroup时,我现在遇到了另一个问题。该脚本要求在发出命令之前运行Connect-AzureAD。通过硬编码凭证可以正常工作,但我不想这样做,我希望它在创建的SPN的上下文中运行,这是在托管构建控制器上运行脚本。

因此,问题是,我是否可以获取Azure PowerShell执行SPN的当前上下文并将其传递给Connect-AzureAD以避免以纯文本格式存储凭据?我错过了一招吗?还有其他选择吗?

我当前的代码如下所示,注释连接在命令中工作正常,就像硬编码值一样。没有参数的调用提供了登录UI,它终止了构建,因为它显然不是交互式的。

## Login to Azure
#$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
#$AdminCredential = New-Object System.Management.Automation.PSCredential ($AdminUserEmailAddress, $SecurePassword)
#Connect-AzureAD -Credential $AdminCredential

Connect-AzureAD

Write-Output "------------------ Start: Group Creation ------------------"

$TestForAdminGroup = Get-AzureADGroup -SearchString $AdminGroup
$TestForContributorGroup = Get-AzureADGroup -SearchString $ContributorGroup
$TestForReaderGroup = Get-AzureADGroup -SearchString $ReaderGroup

谢谢

powershell build azure-devops azure-active-directory
3个回答
5
投票

这个有可能。今天为我自己的VSTS extension工作了,我刚才发布了。我的扩展使用Azure Resource Manager endpoint作为输入。

现在使用以下代码在Microsoft Hosted Visual Studio 2017代理程序池上运行它。有关更多信息,请参阅我的post on how to use AzureAD PowerShell cmdlets on VSTS agent

Write-Verbose "Import AzureAD module because is not on default VSTS agent"
$azureAdModulePath = $PSScriptRoot + "\AzureAD\2.0.1.16\AzureAD.psd1"
Import-Module $azureAdModulePath 

# Workaround to use AzureAD in this task. Get an access token and call Connect-AzureAD
$serviceNameInput = Get-VstsInput -Name ConnectedServiceNameSelector -Require
$serviceName = Get-VstsInput -Name $serviceNameInput -Require
$endPointRM = Get-VstsEndpoint -Name $serviceName -Require

$clientId = $endPointRM.Auth.Parameters.ServicePrincipalId
$clientSecret = $endPointRM.Auth.Parameters.ServicePrincipalKey
$tenantId = $endPointRM.Auth.Parameters.TenantId

$adTokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$resource = "https://graph.windows.net/"

$body = @{
    grant_type    = "client_credentials"
    client_id     = $clientId
    client_secret = $clientSecret
    resource      = $resource
}

$response = Invoke-RestMethod -Method 'Post' -Uri $adTokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body
$token = $response.access_token

Write-Verbose "Login to AzureAD with same application as endpoint"
Connect-AzureAD -AadAccessToken $token -AccountId $clientId -TenantId $tenantId

1
投票

总之,Powershell模块不能共享相同的上下文,您需要将凭证存储在VSTS中的secret变量中。


-2
投票

为了更进一步,可以通过以下示例3使用服务主体:

https://docs.microsoft.com/en-us/powershell/module/azuread/connect-azuread?view=azureadps-2.0

创建自签名证书并将其附加后,您可以通过传递证书的指纹以及其他一些参数来连接到Azure AD:

Connect-AzureAD -TenantId $ tenantId -ApplicationId $ sp.AppId -CertificateThumbprint $ thumb

© www.soinside.com 2019 - 2024. All rights reserved.