【问题标题】:What is the best way to use Hashicorp Vault with GitLab pipelines?将 Hashicorp Vault 与 GitLab 管道一起使用的最佳方式是什么?
【发布时间】:2019-11-17 00:17:24
【问题描述】:

假设我想用 Vault 中的值创建一个变量。

variables:
  $SSH_PRIVATE_KEY: `vault kv get -field=private_key project/production`
before_script:
  - echo "$SSH_PRIVATE_KEY"

有可能吗?

还有其他方法可以在管道中使用 Vault 机密吗?

【问题讨论】:

    标签: gitlab gitlab-ci hashicorp-vault


    【解决方案1】:

    2019 年 7 月的原始答案:

    您可以看到它在脚本步骤之前/之后使用,最后带有一个已撤销的令牌。
    gitlab.eng.cleardata.com pub/pipelines/gcp-ci.yml为例:

    # Obtains credentials via vault (the gitlab-runner authenticates to vault using its AWS credentials)
    # Configures the `gcloud` sdk and `kubectl` to authenticate to our *production* cluster
    #
    # Note: Do not override the before_script or the after_script in your job
    #
    .auth-prod: &auth-prod
      image: cleardata/bionic
      before_script:
        - |
          export CLUSTER_NAME=production
          export CLUSTER_LOCATION=us-central1
          export CLUSTER_PROJECT_ID=cleardata-production-cluster
        - vault login -method=aws -path=gitlab-ci -no-print header_value=gitlab.eng.cleardata.com
        - GCP_CREDS=$(vault read -field=private_key_data gitlab-ci/gcp/cleardata-production-cluster/key/deployment-key)
        - gcloud auth activate-service-account --key-file=<(base64 -d <<<$GCP_CREDS)
        - gcloud auth configure-docker
        - gcloud beta container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_LOCATION --project $CLUSTER_PROJECT_ID
      after_script:
        - vault token revoke -self
    

    2020 年 3 月更新:GitLab 12.9 支持此功能

    HashiCorp Vault GitLab CI/CD Managed Application

    GitLab 希望让用户能够轻松地进行现代机密管理。作为 GitLab CI 托管应用程序流程的一部分,我们现在为用户提供在 Kubernetes 集群中安装 Vault 的能力。
    这将支持在 Helm 图表安装中的项目级别安全管理密钥、令牌和其他机密。

    参见documentationissue


    2020 年 4 月:GitLab 12.10:

    Retrieve CI/CD secrets from HashiCorp Vault

    在此版本中,GitLab 添加了对轻量级 JSON Web 令牌 (JWT) 身份验证的支持,以与您现有的 HashiCorp Vault 集成

    现在,您可以利用 HashiCorp 的 JWT 身份验证方法无缝地为 CI/CD 作业提供机密,而不必在 GitLab 中手动将机密作为变量提供。

    请参阅 documentationissue


    GitLab 13.4(2020 年 9 月)

    仅适用于高级/白银:

    在 CI 作业中使用 HashiCorp Vault 机密

    在 GitLab 12.10 中,GitLab 为 GitLab Runner 引入了获取机密并将其注入 CI 作业的功能。 GitLab 现在通过在 .gitlab-ci.yml 文件中构建新的 secrets 语法来扩展 JWT Vault Authentication method。这使您可以更轻松地通过 GitLab 配置和使用 HashiCorp Vault。

    https://about.gitlab.com/images/13_4/vault_ci.png -- 在 CI 作业中使用 HashiCorp Vault 机密

    请参阅 DocumentationIssue


    GitLab 13.9(2021 年 2 月)

    Vault JWT (JSON Web Token) 支持 GitLab 环境。

    为了简化与 HashiCorp Vault 的集成,我们推出了 Vault JWT 令牌支持。从发布开始,您可以根据以下条件限制访问 JWT 中的数据。此版本为您提供了一个新的限制维度 访问凭据:作业所针对的环境。

    此版本扩展了现有的 Vault JWT 令牌以支持基于环境的 也有限制。因为environment 名称可以由运行的用户提供 管道,我们建议您将新的基于环境的限制与 已经存在的ref_type 值以获得最大的安全性。

    参见DocumentationIssue

    【讨论】:

      【解决方案2】:

      我们在构建器映像中嵌入了一个帮助脚本,它可以将指向机密的 GitLab CI/CD 作业变量转换为包含 Vault 机密的作业环境变量。在我们的例子中,我们还使用 appRole auth 方法来限制临时 Vault 访问令牌的有效性。

      一个示例用例是:

      I want a job env var "MY_SECRET_TOKEN" with a value from a Vault secret.
      So I add a CI/CD variable called V_MY_SECRET_TOKEN="secret/<path>/<key>"
      Then I insert a job step to retrieve the secret value and populate
        the MY_SECRET_TOKEN with the value associated with the key.
      

      在 GitLab 中添加到 CICD 作业设置的变量。

      VAULT_ADDR=https://vault.example.com:8200
      VAULT_ROLE_ID=db02de05-fa39-4855-059b-67221c5c2f63
      VAULT_SECRET_ID=6a174c20-f6de-a53c-74d2-6018fcceff64
      VAULT_VAR_FILE=/var/tmp/vault-vars.sh
      

      添加到 .gitlab-ci.yml 作业定义的步骤。

      script:
        - get-vault-secrets-by-approle > ${VAULT_VAR_FILE}
        - source ${VAULT_VAR_FILE} && rm ${VAULT_VAR_FILE}
      

      这里引用了我们使用的get-vault-secrets-by-approle 帮助脚本。
      这是thinking behind the design 的一篇文章。

      “before_script”选项不适合我们的工作流程,因为我们在 gitlab-ci.yml 定义中定义了特权阶段和非特权阶段的组合。非特权作业构建和 QA 代码,而特权作业打包和发布代码。 VAULT_ROLE_ID 和 VAULT_SECRET_ID 作业变量应该只对特权打包和发布作业可见。

      我还尝试使用 include、extend 和 yaml 锚点,但我想将项目合并到现有的 yaml 映射(script: {}before_script: {})中,而不是用模板替换映射中的所有项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-10
        • 2022-08-11
        相关资源
        最近更新 更多