访问令牌 Rake 任务
- Tier: 基础版, 专业版, 旗舰版
- Offering: 私有化部署
History
- 引入于极狐GitLab 17.2。
分析令牌过期日期
在极狐GitLab 16.0 中,一次后台迁移给所有不设过期的个人、项目和群组访问令牌设置了一个过期日期,该日期为令牌创建后的一年。
要识别哪些令牌可能受此迁移影响,您可以运行一个 Rake 任务,该任务会分析所有访问令牌并显示最常见的前十个过期日期:
shellgitlab-rake gitlab:tokens:analyze
此任务分析所有访问令牌并按过期日期对其进行分组。左侧列显示过期日期,右侧列显示有多少令牌具有该过期日期。示例输出:
plaintext1======= Personal/Project/Group Access Token Expiration Migration ======= 2Started at: 2023-06-15 10:20:35 +0000 3Finished : 2023-06-15 10:23:01 +0000 4===== Top 10 Personal/Project/Group Access Token Expiration Dates ===== 5| Expiration Date | Count | 6|-----------------|-------| 7| 2024-06-15 | 1565353 | 8| 2017-12-31 | 2508 | 9| 2018-01-01 | 1008 | 10| 2016-12-31 | 833 | 11| 2017-08-31 | 705 | 12| 2017-06-30 | 596 | 13| 2018-12-31 | 548 | 14| 2017-05-31 | 523 | 15| 2017-09-30 | 520 | 16| 2017-07-31 | 494 | 17========================================================================
在此示例中,您可以看到超过 150 万个访问令牌的过期日期为 2024-06-15,即迁移运行后的一年,迁移运行于 2023-06-15。这表明这些令牌大多数是由迁移分配的。但是,没有办法确定是否有其他令牌是手动创建的并具有相同的日期。
批量更新过期日期
先决条件:
您必须:
- 是管理员。
- 拥有一个交互式终端。
运行以下 Rake 任务以批量扩展或删除令牌的过期日期:
-
运行工具:
shellgitlab-rake gitlab:tokens:edit
shell# Find the toolbox pod kubectl --namespace <namespace> get pods -lapp=toolbox kubectl exec -it <toolbox-pod-name> -- sh -c 'cd /srv/gitlab && bin/rake gitlab:tokens:edit'
shellsudo docker exec -it <container_name> /bin/bash gitlab-rake gitlab:tokens:edit
shellsudo RAILS_ENV=production -u git -H bundle exec rake gitlab:tokens:edit
工具启动后,它会显示来自分析步骤的输出以及有关修改过期日期的额外提示:
plaintext1======= Personal/Project/Group Access Token Expiration Migration ======= 2Started at: 2023-06-15 10:20:35 +0000 3Finished : 2023-06-15 10:23:01 +0000 4===== Top 10 Personal/Project/Group Access Token Expiration Dates ===== 5| Expiration Date | Count | 6|-----------------|-------| 7| 2024-05-14 | 1565353 | 8| 2017-12-31 | 2508 | 9| 2018-01-01 | 1008 | 10| 2016-12-31 | 833 | 11| 2017-08-31 | 705 | 12| 2017-06-30 | 596 | 13| 2018-12-31 | 548 | 14| 2017-05-31 | 523 | 15| 2017-09-30 | 520 | 16| 2017-07-31 | 494 | 17======================================================================== 18What do you want to do? (Press ↑/↓ arrow or 1-3 number to move and Enter to select) 19‣ 1. Extend expiration date 20 2. Remove expiration date 21 3. Quit
扩展过期日期
要扩展所有与给定过期日期匹配的令牌的过期日期:
-
选择选项 1,Extend expiration date:
plaintextWhat do you want to do? ‣ 1. Extend expiration date 2. Remove expiration date 3. Quit
-
工具要求您选择列出的过期日期之一。例如:
plaintext1Select an expiration date (Press ↑/↓/←/→ arrow to move and Enter to select) 2‣ 2024-05-14 3 2017-12-31 4 2018-01-01 5 2016-12-31 6 2017-08-31 7 2017-06-30
使用键盘上的箭头键选择一个日期。要中止,向下滚动并选择--> Abort。按 Enter 确认您的选择:
plaintext1Select an expiration date 2 2017-06-30 3 2018-12-31 4 2017-05-31 5 2017-09-30 6 2017-07-31 7‣ --> Abort
如果您选择了一个日期,工具会提示您输入新的过期日期:
plaintextWhat would you like the new expiration date to be? (2025-05-14) 2024-05-14
默认日期是从选定日期起一年。按 Enter 使用默认值,或手动输入 YYYY-MM-DD 格式的日期。
-
输入有效日期后,工具会再次要求确认:
plaintextOld expiration date: 2024-05-14 New expiration date: 2025-05-14 WARNING: This will now update 1565353 token(s). Are you sure? (y/N)
如果您输入 y,工具将为所有具有选定过期日期的令牌扩展过期日期。
如果您输入 N,工具将中止更新任务并返回到原始分析输出。
移除过期日期
要移除所有与给定过期日期匹配的令牌的过期日期:
-
选择选项 2,Remove expiration date:
plaintextWhat do you want to do? 1. Extend expiration date ‣ 2. Remove expiration date 3. Quit
-
工具要求您从表中选择过期日期。例如:
plaintext1Select an expiration date (Press ↑/↓/←/→ arrow to move and Enter to select) 2‣ 2024-05-14 3 2017-12-31 4 2018-01-01 5 2016-12-31 6 2017-08-31 7 2017-06-30
使用键盘上的箭头键选择一个日期。要中止,向下滚动并选择--> Abort。按 Enter 确认您的选择:
plaintext1Select an expiration date 2 2017-06-30 3 2018-12-31 4 2017-05-31 5 2017-09-30 6 2017-07-31 7‣ --> Abort
-
选择日期后,工具会提示您确认选择:
plaintextWARNING: This will remove the expiration for tokens that expire on 2024-05-14. This will affect 1565353 tokens. Are you sure? (y/N)
如果您输入 y,工具将移除所有具有选定过期日期的令牌的过期日期。
如果您输入 N,工具将中止更新任务并返回到第一个菜单。