我们在构建器映像中嵌入了一个帮助脚本,它可以将指向机密的 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: {})中,而不是用模板替换映射中的所有项目。