Edit2:此更改目前有一个副作用,因为部署密钥上没有用户。所以你会发现一些难看的消息,比如ERROR -> POST-RECEIVE: Triggered hook for non-existing user
。因此,通过部署密钥进行写入推送时不会处理缓存失效(可能还有其他事情),这有点难看。bundle exec rake cache:clear RAILS_ENV=production
是你的朋友,直到我找到解决方法(请参阅下面的 GitHub 链接) - 请记住,清除 Redis 缓存也会注销所有用户。
以下是一个简短的解决方法,可以按每个密钥存档以下内容:
这是通过在密钥标题前添加一些特殊字符来存档的:
-
*
做一个My SSH keys
只读
-
!
使 Deploy-Keys 可读可写
-
!!
使 Deploy-Keys 成为主控(写入受保护的分支)
因此,您可以将密钥命名为“*我的全局只读密钥”等,而不是将其命名为“我的全局密钥”。
diff --git a/lib/api/internal.rb b/lib/api/internal.rb
index ed6b50c..ce350b1 100644
--- a/lib/api/internal.rb
+++ b/lib/api/internal.rb
@@ -30,7 +30,11 @@ module API
if key.is_a? DeployKey
- key.projects.include?(project) && DOWNLOAD_COMMANDS.include?(git_cmd)
+return false unless key.projects.include?(project)
+return true if DOWNLOAD_COMMANDS.include?(git_cmd)
+return true if key.title.start_with? '!!'
+return false if project.protected_branch?(params[:ref])
+key.title.start_with? '!'
else
user = key.user
@@ -42,6 +46,7 @@ module API
then :download_code
when *PUSH_COMMANDS
then
+return false if key.title.start_with? '*' # VAHI 2014-02-09
if project.protected_branch?(params[:ref])
:push_code_to_protected_branches
else
Edit1:该补丁现已提供cherry-pick
在 GitHub 上,请参阅https://github.com/hilbix/gitlabhq/wiki https://github.com/hilbix/gitlabhq/wiki
Edit3:如果您推送部署密钥,您可能还需要以下解决方法。这并不完美,因为它不会触发所有这些钩子,但它会使缓存失效,以便网页不再显示陈旧的数据:
diff --git a/app/workers/post_receive.rb b/app/workers/post_receive.rb
index 6416aa6..2fe98d4 100644
--- a/app/workers/post_receive.rb
+++ b/app/workers/post_receive.rb
@@ -26,6 +26,8 @@ class PostReceive
unless user
log("Triggered hook for non-existing user \"#{identifier} \"")
+ project.ensure_satellite_exists
+ project.repository.expire_cache
return false
end
仍然存在一些问题:
一个小问题是,您不能同时在帐户级别和部署级别使用相同的密钥。因此,对于在全局基础上仅具有只读访问权限的密钥(可能是使用的默认密钥),您需要第二个特殊的“仅推送”密钥,它允许对存储库进行推送访问。
Edit3:最重要的是部署密钥没有附加用户,因此所有便利功能都不起作用。如果这对您来说是个问题,唯一的方法是,为每个 SSH 密钥创建一个虚拟用户,并将其添加到组/项目中,并为该虚拟用户提供正确的权限。
Linux 下测试存储库的完整示例位于[email protected] /cdn-cgi/l/email-protection:root/test.git
将以上补丁应用到 GitLab
重新启动 GitLab 以读入新代码
添加您的~/.ssh/id_rsa.pub
到 GitLab 管理员下My SSH keys
并命名它* my readonly key
(或者不同的东西,从*
).
验证以下内容是否有效:git clone [email protected] /cdn-cgi/l/email-protection:root/test.git
-
验证以下操作失败git push
step:
cd test
date > DATE.tmp
git add DATE.tmp
git commit -m testing
git push
创建第二个 SSH 密钥~/.ssh/id_push
: ssh-keygen -b 4096 -f ~/.ssh/id_push
Add ~/.ssh/id_push.pub
作为存储库的部署密钥root/test.git
。命名它! my push key
(或者不同的东西,从!
)
-
添加以下内容到~/.ssh/config
Host gitlab-push
Hostname gitlab.example.com
User git
IdentityFile ~/.ssh/id_push
将此目标添加到您的克隆存储库中:git remote add to gitlab-push:root/test.git
验证以下工作:git push to master
Notes:
我使用复制+粘贴来插入补丁。它很可能不会应用干净。对不起。
是的,这确实是一个非常粗糙的黑客攻击,不应该进入主线。正确的实现是在数据库中有一个执行此操作的标志,以便您可以通过 GUI 对其进行编辑。
对于部署密钥,此“密钥级别”标志应位于密钥和项目之间的交集表中。在非部署密钥变体中,它应该位于密钥本身上。也许这个领域可以充当default
将部署密钥添加到另一个项目时的值并记录该密钥的最后使用情况。
不幸的是,我自己无法正确实现这一点,因为我缺乏如何向 GUI 添加必要元素的知识,抱歉。 ;(