【问题标题】:Azure devops terraform pipeline generate client id and secretAzure devops terraform 管道生成客户端 ID 和密码
【发布时间】:2019-08-17 23:11:52
【问题描述】:

我正在使用此 terraform 清单在 Azure 上部署 AKS。我可以通过命令行很好地做到这一点并且它可以工作,因为我在我的机器上配置了 azure cli 来生成客户端 ID 和机密

https://github.com/anubhavmishra/terraform-azurerm-aks

但是,我现在正在 Azure Devops Pipeline 上构建它

所以,到目前为止,我已经设法在 Azure 上运行 terraform init 并规划后端存储,使用 Azure Devops 并使用此扩展

https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform

问题:如何在 Azure devops 管道上获取客户端 ID 和密码并将其设置为 terraform 的环境变量?我尝试在管道中创建一个 bash az 命令

> az ad sp create-for-rbac --role="Contributor"
> --scopes="/subscriptions/YOUR_SUBSCRIPTION_ID"

但因此错误而失败

> 2019-03-27T10:41:58.1042923Z 
2019-03-27T10:41:58.1055624Z Setting AZURE_CONFIG_DIR env variable to: /home/vsts/work/_temp/.azclitask
2019-03-27T10:41:58.1060006Z Setting active cloud to: AzureCloud
2019-03-27T10:41:58.1069887Z [command]/usr/bin/az cloud set -n AzureCloud
2019-03-27T10:41:58.9004429Z [command]/usr/bin/az login --service-principal -u *** -p *** --tenant ***
2019-03-27T10:42:00.0695154Z [
2019-03-27T10:42:00.0696915Z   {
2019-03-27T10:42:00.0697522Z     "cloudName": "AzureCloud",
2019-03-27T10:42:00.0698958Z     "id": "88bfee03-551c-4ed3-98b0-be68aee330bb",
2019-03-27T10:42:00.0704752Z     "isDefault": true,
2019-03-27T10:42:00.0705381Z     "name": "Visual Studio Enterprise",
2019-03-27T10:42:00.0706362Z     "state": "Enabled",
2019-03-27T10:42:00.0707434Z     "tenantId": "***",
2019-03-27T10:42:00.0716107Z     "user": {
2019-03-27T10:42:00.0717485Z       "name": "***",
2019-03-27T10:42:00.0718161Z       "type": "servicePrincipal"
2019-03-27T10:42:00.0718675Z     }
2019-03-27T10:42:00.0719185Z   }
2019-03-27T10:42:00.0719831Z ]
2019-03-27T10:42:00.0728173Z [command]/usr/bin/az account set --subscription 88bfee03-551c-4ed3-98b0-be68aee330bb
2019-03-27T10:42:00.8569816Z [command]/bin/bash /home/vsts/work/_temp/azureclitaskscript1553683312219.sh
2019-03-27T10:42:02.4431342Z ERROR: Directory permission is needed for the current user to register the application. For how to configure, please refer 'https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal'. Original error: Insufficient privileges to complete the operation.
2019-03-27T10:42:02.5271752Z [command]/usr/bin/az account clear
2019-03-27T10:42:03.3092558Z ##[error]Script failed with error: Error: /bin/bash failed with return code: 1
2019-03-27T10:42:03.3108490Z ##[section]Finishing: Azure CLI 

【问题讨论】:

    标签: bash azure terraform az


    【解决方案1】:

    这是我使用 Azure Pipelines 的方法。

    1. 为 Terraform 创建服务主体。
    2. 在您的管道中创建以下变量
      • ARM_CLIENT_ID
      • ARM_CLIENT_SECRET
      • ARM_SUBSCRIPTION_ID
      • ARM_TENANT_ID

    如果您选择将 ARM_CLIENT_SECRET 作为机密存储在 Azure DevOps 中,则需要在任务的 环境变量 部分下的任务中执行以下操作,以将其解密,以便 terraform 可以读取它。

    【讨论】:

      【解决方案2】:

      您只需授予服务连接权限即可创建服务主体。但我通常建议不要这样做,只需预先创建一个服务主体并在您的管道中使用它。在每次运行时创建一个新的服务主体似乎太过分了。

      您可以使用 build\release 变量并使用客户端 id\secret 填充这些变量

      【讨论】:

      • 所以我在 Azure 上创建服务主体,然后附加到管道,是否每次需要运行管道时都需要填充客户端 ID 和密码?
      • 不,只需将它们放入 build\release 变量并在脚本中引用它们即可。 docs.microsoft.com/en-us/azure/devops/pipelines/process/…
      • 是否存在安全风险?
      • 可能少于每次运行生成新的服务主体
      • 我已经在发布管道上添加了变量,但是它们对 terraform 计划任务不可用,所以我需要做任何其他事情,基本上我希望它们在 terraform 运行时作为 bash 环境变量
      【解决方案3】:

      帖子https://medium.com/@maninder.bindra/creating-a-single-azure-devops-yaml-pipeline-to-provision-multiple-environments-using-terraform-e6d05343cae2中定义的方法? 也可以考虑。这里 Keyvault 任务用于从 Azure Vault 获取机密(包括 terraform 后端访问机密以及 aks sp 机密):

      #KEY VAULT TASK
      - task: AzureKeyVault@1
        inputs:
          azureSubscription: '$(environment)-sp'
          KeyVaultName: '$(environment)-pipeline-secrets-kv'
          SecretsFilter: 'tf-sp-id,tf-sp-secret,tf-tenant-id,tf-subscription-id,tf-backend-sa-access-key,aks-sp-id,aks-sp-secret'
        displayName: 'Get key vault secrets as pipeline variables'
      

      然后,您可以在管道的其余部分中将秘密用作变量。例如 aks-sp-id 可以称为 $(aks-sp-id)。所以 bash/azure-cli 任务可能类似于

      # AZ LOGIN USING TERRAFORM SERVICE PRINCIPAL
      - script: |
          az login --service-principal -u $(tf-sp-id) -p $(tf-sp-secret) --tenant $(tf-tenant-id)
          cd $(System.DefaultWorkingDirectory)/tf-infra-provision
      

      随后是 terraform init 和计划(计划如下所示,完整的管道详细信息请参阅帖子)

      # TERRAFORM PLAN    
          echo '#######Terraform Plan########'
          terraform plan -var-file=./tf-vars/$(tfvarsFile) -var="client_id=$(tf-sp-id)" -var="client_secret=$(tf-sp-secret)" -var="tenant_id=$(tf-tenant-id)" -var="subscription_id=$(tf-subscription-id)" -var="aks_sp_id=$(aks-sp-id)" -var="aks_sp_secret=$(aks-sp-secret)" -out="out.plan"
      
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-02
        • 2012-02-15
        • 1970-01-01
        相关资源
        最近更新 更多