【问题标题】:VSTS Build and PowerShell and AzureAD AuthenticationVSTS 构建和 PowerShell 和 AzureAD 身份验证
【发布时间】:2018-03-15 05:08:21
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

  • 不,您需要使用凭证脚本调用 Connect-AzureAD,您可以将凭证存储在秘密变量中(单击锁定图标,它是硬编码)
  • 谢谢 - 这是一个选择,我希望有更好的方法。
  • Connect-AzureAD 如果在本地机器上手动进行也是必需的。我尝试使用主密钥和 ID 生成凭据,但不能用于 Connect-AzureAD。你有办法用主键和 ID 来做吗?
  • 不,很遗憾没有。我很欣赏 Azure AD 独立于 Azure 订阅,但如果 Poweshell 模块可以共享相同的上下文会很好。这没什么大不了的,但现在我必须管理两组凭据才能为单个订阅运行我的 azure 构建。

标签: powershell build azure-devops azure-active-directory


【解决方案1】:

这是可能的。今天让它为我自己的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

【讨论】:

【解决方案2】:

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

【讨论】:

    【解决方案3】:

    为了更进一步,可以通过以下示例 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

    【讨论】:

    • 请将链接中的信息添加到您的答案中。
    • 示例 3 以 # Login to Azure AD PowerShell With Admin Account: Connect-AzureAD 开头,这完全忽略了无人值守执行的要点...
    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2021-04-09
    • 2020-05-15
    相关资源
    最近更新 更多