Help us learn about your current experience with the documentation. Take the survey.
Token 撤销 API
Token 撤销 API 是一个外部部署的 HTTP API,与 GitLab 交互以接收和撤销 GitLab 秘密检测检测到的 API 令牌和其他秘密。 有关秘密检测后处理和撤销流程的更多信息,请参阅高级架构。
GitLab.com 使用内部维护的秘密撤销服务 (仅限团队成员)作为其 Token 撤销 API。对于 GitLab 自托管,您可以创建自己的 API 并配置 GitLab 使用它。
为自托管实现 Token 撤销 API
想要使用撤销功能的 GitLab 自托管实例必须:
- 实现并部署您自己的 Token 撤销 API。
- 配置 GitLab 实例以使用 Token 撤销 API。
您的服务必须:
- 符合下面的 API 规范。
- 提供两个端点:
- 获取可撤销的令牌类型。
- 撤销泄露的令牌。
- 具有速率限制和幂等性。
对文档化端点的请求使用在 Authorization 头部中传递的 API 令牌进行身份验证。如果存在请求和响应体,则预期其内容类型为 application/json。
所有端点都可能返回以下响应:
401 未授权405 方法不允许500 内部服务器错误
GET /v1/revocable_token_types
返回用于 revoke_tokens 端点的有效 type 值。
这些值通过连接secrets 分析器
的主要标识符来匹配,即连接 primary_identifier.type 和 primary_identifier.value。
例如,值 gitleaks_rule_id_gitlab_personal_access_token 匹配以下发现标识符:
{"type": "gitleaks_rule_id", "name": "Gitleaks rule ID GitLab Personal Access Token", "value": "GitLab Personal Access Token"}| 状态码 | 描述 |
|---|---|
200 |
响应体包含有效的令牌 type 值。 |
示例响应体:
{
"types": ["gitleaks_rule_id_gitlab_personal_access_token"]
}POST /v1/revoke_tokens
接受要由相应提供商撤销的令牌列表。您的服务负责与每个提供商通信以撤销令牌。
| 状态码 | 描述 |
|---|---|
204 |
所有提交的令牌已被接受用于最终撤销。 |
400 |
请求体无效或提交的令牌类型之一不受支持。不应重试请求。 |
429 |
提供商收到了太多请求。应稍后重试请求。 |
示例请求体(在 token 值中添加空格字符以防止秘密检测警告):
[{
"type": "gitleaks_rule_id_gitlab_personal_access_token",
"token": "glpat - 8GMtG8Mf4EnMJzmAWDU",
"location": "https://example.com/some-repo/blob/abcdefghijklmnop/compromisedfile1.java"
},
{
"type": "gitleaks_rule_id_gitlab_personal_access_token",
"token": "glpat - tG84EGK33nMLLDE70zU",
"location": "https://example.com/some-repo/blob/abcdefghijklmnop/compromisedfile2.java"
}]配置 GitLab 与 Token 撤销 API 交互
您必须在 GitLab 实例中配置以下数据库设置:
| 设置 | 类型 | 描述 |
|---|---|---|
secret_detection_token_revocation_enabled |
Boolean | 是否启用自动令牌撤销 |
secret_detection_token_revocation_url |
String | Token 撤销 API 的 /v1/revoke_tokens 端点的完全限定 URL |
secret_detection_revocation_token_types_url |
String | Token 撤销 API 的 /v1/revocable_token_types 端点的完全限定 URL |
secret_detection_token_revocation_token |
String | 用于对 Token 撤销 API 请求进行身份验证的预共享令牌 |
例如,在Rails 控制台中配置这些值:
::Gitlab::CurrentSettings.update!(secret_detection_token_revocation_token: 'MYSECRETTOKEN')
::Gitlab::CurrentSettings.update!(secret_detection_token_revocation_url: 'https://gitlab.example.com/revocation_service/v1/revoke_tokens')
::Gitlab::CurrentSettings.update!(secret_detection_revocation_token_types_url: 'https://gitlab.example.com/revocation_service/v1/revocable_token_types')
::Gitlab::CurrentSettings.update!(secret_detection_token_revocation_enabled: true)配置这些值后,将根据高级架构 图调用 Token 撤销 API。