{{< details >}}
- Tier: 专业版, 基础版
- Offering: JihuLab.com, 私有化部署
{{< /details >}}
{{< history >}}
- 在极狐GitLab 和 Runner 16.3 中引入。
- 在极狐GitLab Runner 16.6 中 GA。
{{< /history >}}
在极狐GitLab CI/CD 流水线中使用 Azure 密钥保管库中的密钥
您可以在极狐GitLab CI/CD 流水线中使用存储在 Azure Key Vault 中的密钥。
前提条件:
- 在 Azure 上拥有一个 Key Vault。
- 您的 IAM 用户必须被 授予 Key Vault 管理员 角色分配给分配给 Key Vault 的 资源群组。否则,您无法在 Key Vault 中创建密钥。
- 在 Azure 中配置 OpenID Connect 以检索临时凭证。这些步骤包括有关如何为 Key Vault 访问创建 Azure AD 应用程序的说明。
- 向您的项目添加 CI/CD 变量,以提供有关您的 Vault 服务器的详细信息:
-
AZURE_KEY_VAULT_SERVER_URL
:您的 Azure Key Vault 服务器的 URL,例如https://vault.example.com
。 -
AZURE_CLIENT_ID
:Azure 应用程序的客户端 ID。 -
AZURE_TENANT_ID
:Azure 应用程序的租户 ID。
-
在 CI/CD 作业中使用 Azure Key Vault 密钥
您可以通过使用 azure_key_vault
关键字在作业中定义存储在 Azure Key Vault 中的密钥:
job:
id_tokens:
AZURE_JWT:
aud: 'https://gitlab.com'
secrets:
DATABASE_PASSWORD:
token: $AZURE_JWT
azure_key_vault:
name: 'test'
version: '00000000000000000000000000000000'
在这个例子中:
-
aud
是受众,必须与 创建联合身份凭证 时使用的受众匹配。 -
name
是 Azure Key Vault 中密钥的名称。 -
version
是 Azure Key Vault 中密钥的版本。版本是一个生成的 GUID,没有连字符,可以在 Azure Key Vault 密钥页面上找到。 - 极狐GitLab 从 Azure Key Vault 中获取密钥,并将值存储在临时文件中。该文件的路径存储在一个
DATABASE_PASSWORD
CI/CD 变量中,类似于 文件类型 CI/CD 变量。
故障排除
有关在 Azure 中设置 OIDC 的常规问题,请参考 Azure 的 OIDC 故障排除。
JWT token is invalid or malformed
消息
从 Azure Key Vault 获取密钥时,您可能会收到此错误:
RESPONSE 400 Bad Request
AADSTS50027: JWT token is invalid or malformed.
这由于极狐GitLab Runner 中的一个已知问题导致 JWT token 没有被正确解析。要解决此问题,请升级到极狐GitLab Runner 16.6 或更高版本。
Caller is not authorized to perform action on resource
消息
从 Azure Key Vault 获取密钥时,您可能会收到此错误:
RESPONSE 403: 403 Forbidden
ERROR CODE: Forbidden
Caller is not authorized to perform action on resource.\r\nIf role assignments, deny assignments or role definitions were changed recently, please observe propagation time.
ForbiddenByRbac
如果您的 Azure Key Vault 使用 RBAC,您必须向您的 Azure AD 应用程序添加 Key Vault Secrets User 角色分配。
例如:
appId=$(az ad app list --display-name gitlab-oidc --query '[0].appId' -otsv)
az role assignment create --assignee $appId --role "Key Vault Secrets User" --scope /subscriptions/<subscription-id>
您可以在以下位置找到您的订阅 ID:
The secrets provider can not be found. Check your CI/CD variables and try again.
消息
尝试启动配置为访问 Azure Key Vault 的作业时,您可能会收到此错误:
The secrets provider can not be found. Check your CI/CD variables and try again.
无法创建作业,因为一个或多个必需的变量未定义:
AZURE_KEY_VAULT_SERVER_URL
AZURE_CLIENT_ID
AZURE_TENANT_ID