Commit f78947624133f06712d60197809a269338053c3a
Exists in
master
and in
4 other branches
Merge branch 'master' into preselected_milestone_while_create_new_issue
Showing
7 changed files
with
243 additions
and
186 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -14,10 +14,6 @@ class ApplicationController < ActionController::Base |
| 14 | 14 | render "errors/gitolite", layout: "error" |
| 15 | 15 | end |
| 16 | 16 | |
| 17 | - rescue_from Gitlab::Gitolite::InvalidKey do |exception| | |
| 18 | - render "errors/invalid_ssh_key", layout: "error" | |
| 19 | - end | |
| 20 | - | |
| 21 | 17 | rescue_from Encoding::CompatibilityError do |exception| |
| 22 | 18 | render "errors/encoding", layout: "error", status: 404 |
| 23 | 19 | end | ... | ... |
app/views/errors/invalid_ssh_key.html.haml
lib/gitlab/backend/gitolite.rb
| 1 | -require 'gitolite' | |
| 2 | -require 'timeout' | |
| 3 | -require 'fileutils' | |
| 1 | +require_relative 'gitolite_config' | |
| 4 | 2 | |
| 5 | -# TODO: refactor & cleanup | |
| 6 | 3 | module Gitlab |
| 7 | 4 | class Gitolite |
| 8 | 5 | class AccessDenied < StandardError; end |
| 9 | - class InvalidKey < StandardError; end | |
| 6 | + | |
| 7 | + def config | |
| 8 | + Gitlab::GitoliteConfig.new | |
| 9 | + end | |
| 10 | 10 | |
| 11 | 11 | def set_key key_id, key_content, projects |
| 12 | - configure do |c| | |
| 13 | - c.update_keys(key_id, key_content) | |
| 14 | - c.update_projects(projects) | |
| 12 | + config.apply do |config| | |
| 13 | + config.write_key(key_id, key_content) | |
| 14 | + config.update_projects(projects) | |
| 15 | 15 | end |
| 16 | 16 | end |
| 17 | 17 | |
| 18 | 18 | def remove_key key_id, projects |
| 19 | - configure do |c| | |
| 20 | - c.delete_key(key_id) | |
| 21 | - c.update_projects(projects) | |
| 19 | + config.apply do |config| | |
| 20 | + config.rm_key(key_id) | |
| 21 | + config.update_projects(projects) | |
| 22 | 22 | end |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | 25 | def update_repository project |
| 26 | - configure do |c| | |
| 27 | - c.update_project(project.path, project) | |
| 28 | - end | |
| 26 | + config.update_project!(project.path, project) | |
| 29 | 27 | end |
| 30 | 28 | |
| 31 | - alias_method :create_repository, :update_repository | |
| 32 | - | |
| 33 | 29 | def remove_repository project |
| 34 | - configure do |c| | |
| 35 | - c.destroy_project(project) | |
| 36 | - end | |
| 30 | + config.destroy_project!(project) | |
| 37 | 31 | end |
| 38 | 32 | |
| 39 | 33 | def url_to_repo path |
| 40 | 34 | Gitlab.config.ssh_path + "#{path}.git" |
| 41 | 35 | end |
| 42 | 36 | |
| 43 | - def initialize | |
| 44 | - # create tmp dir | |
| 45 | - @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 46 | - end | |
| 47 | - | |
| 48 | 37 | def enable_automerge |
| 49 | - configure do |git| | |
| 50 | - git.admin_all_repo | |
| 51 | - end | |
| 52 | - end | |
| 53 | - | |
| 54 | - protected | |
| 55 | - | |
| 56 | - def destroy_project(project) | |
| 57 | - FileUtils.rm_rf(project.path_to_repo) | |
| 58 | - | |
| 59 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 60 | - conf = ga_repo.config | |
| 61 | - conf.rm_repo(project.path) | |
| 62 | - ga_repo.save | |
| 63 | - end | |
| 64 | - | |
| 65 | - #update or create | |
| 66 | - def update_keys(user, key) | |
| 67 | - File.open(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } | |
| 68 | - end | |
| 69 | - | |
| 70 | - def delete_key(user) | |
| 71 | - File.unlink(File.join(@local_dir, 'gitolite/keydir',"#{user}.pub")) | |
| 72 | - `cd #{File.join(@local_dir,'gitolite')} ; git rm keydir/#{user}.pub` | |
| 73 | - end | |
| 74 | - | |
| 75 | - # update or create | |
| 76 | - def update_project(repo_name, project) | |
| 77 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 78 | - conf = ga_repo.config | |
| 79 | - repo = update_project_config(project, conf) | |
| 80 | - conf.add_repo(repo, true) | |
| 81 | - | |
| 82 | - ga_repo.save | |
| 83 | - end | |
| 84 | - | |
| 85 | - # Updates many projects and uses project.path as the repo path | |
| 86 | - # An order of magnitude faster than update_project | |
| 87 | - def update_projects(projects) | |
| 88 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 89 | - conf = ga_repo.config | |
| 90 | - | |
| 91 | - projects.each do |project| | |
| 92 | - repo = update_project_config(project, conf) | |
| 93 | - conf.add_repo(repo, true) | |
| 94 | - end | |
| 95 | - | |
| 96 | - ga_repo.save | |
| 97 | - end | |
| 98 | - | |
| 99 | - def update_project_config(project, conf) | |
| 100 | - repo_name = project.path | |
| 101 | - | |
| 102 | - repo = if conf.has_repo?(repo_name) | |
| 103 | - conf.get_repo(repo_name) | |
| 104 | - else | |
| 105 | - ::Gitolite::Config::Repo.new(repo_name) | |
| 106 | - end | |
| 107 | - | |
| 108 | - name_readers = project.repository_readers | |
| 109 | - name_writers = project.repository_writers | |
| 110 | - name_masters = project.repository_masters | |
| 111 | - | |
| 112 | - pr_br = project.protected_branches.map(&:name).join("$ ") | |
| 113 | - | |
| 114 | - repo.clean_permissions | |
| 115 | - | |
| 116 | - # Deny access to protected branches for writers | |
| 117 | - unless name_writers.blank? || pr_br.blank? | |
| 118 | - repo.add_permission("-", pr_br.strip + "$ ", name_writers) | |
| 119 | - end | |
| 120 | - | |
| 121 | - # Add read permissions | |
| 122 | - repo.add_permission("R", "", name_readers) unless name_readers.blank? | |
| 123 | - | |
| 124 | - # Add write permissions | |
| 125 | - repo.add_permission("RW+", "", name_writers) unless name_writers.blank? | |
| 126 | - repo.add_permission("RW+", "", name_masters) unless name_masters.blank? | |
| 127 | - | |
| 128 | - repo | |
| 129 | - end | |
| 130 | - | |
| 131 | - def admin_all_repo | |
| 132 | - ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(@local_dir,'gitolite')) | |
| 133 | - conf = ga_repo.config | |
| 134 | - owner_name = "" | |
| 135 | - | |
| 136 | - # Read gitolite-admin user | |
| 137 | - # | |
| 138 | - begin | |
| 139 | - repo = conf.get_repo("gitolite-admin") | |
| 140 | - owner_name = repo.permissions[0]["RW+"][""][0] | |
| 141 | - raise StandardError if owner_name.blank? | |
| 142 | - rescue => ex | |
| 143 | - puts "Can't determine gitolite-admin owner".red | |
| 144 | - raise StandardError | |
| 145 | - end | |
| 146 | - | |
| 147 | - # @ALL repos premission for gitolite owner | |
| 148 | - repo_name = "@all" | |
| 149 | - repo = if conf.has_repo?(repo_name) | |
| 150 | - conf.get_repo(repo_name) | |
| 151 | - else | |
| 152 | - ::Gitolite::Config::Repo.new(repo_name) | |
| 153 | - end | |
| 154 | - | |
| 155 | - repo.add_permission("RW+", "", owner_name) | |
| 156 | - conf.add_repo(repo, true) | |
| 157 | - ga_repo.save | |
| 38 | + config.admin_all_repo!(project) | |
| 158 | 39 | end |
| 159 | 40 | |
| 160 | - private | |
| 161 | - | |
| 162 | - def pull | |
| 163 | - # create tmp dir | |
| 164 | - @local_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 165 | - Dir.mkdir @local_dir | |
| 166 | - | |
| 167 | - `git clone #{Gitlab.config.gitolite_admin_uri} #{@local_dir}/gitolite` | |
| 168 | - end | |
| 169 | - | |
| 170 | - def push | |
| 171 | - Dir.chdir(File.join(@local_dir, "gitolite")) | |
| 172 | - `git add -A` | |
| 173 | - `git commit -am "GitLab"` | |
| 174 | - `git push` | |
| 175 | - Dir.chdir(Rails.root) | |
| 176 | - | |
| 177 | - FileUtils.rm_rf(@local_dir) | |
| 178 | - end | |
| 179 | - | |
| 180 | - def configure | |
| 181 | - Timeout::timeout(30) do | |
| 182 | - File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| | |
| 183 | - begin | |
| 184 | - f.flock(File::LOCK_EX) | |
| 185 | - pull | |
| 186 | - yield(self) | |
| 187 | - push | |
| 188 | - ensure | |
| 189 | - f.flock(File::LOCK_UN) | |
| 190 | - end | |
| 191 | - end | |
| 192 | - end | |
| 193 | - rescue Exception => ex | |
| 194 | - if ex.message =~ /is not a valid SSH key string/ | |
| 195 | - raise Gitolite::InvalidKey.new("ssh key is not valid") | |
| 196 | - else | |
| 197 | - Gitlab::Logger.error(ex.message) | |
| 198 | - raise Gitolite::AccessDenied.new("gitolite timeout") | |
| 199 | - end | |
| 200 | - end | |
| 41 | + alias_method :create_repository, :update_repository | |
| 201 | 42 | end |
| 202 | 43 | end | ... | ... |
| ... | ... | @@ -0,0 +1,170 @@ |
| 1 | +require 'gitolite' | |
| 2 | +require 'timeout' | |
| 3 | +require 'fileutils' | |
| 4 | + | |
| 5 | +module Gitlab | |
| 6 | + class GitoliteConfig | |
| 7 | + attr_reader :config_tmp_dir | |
| 8 | + | |
| 9 | + def reset_config_tmp_dir | |
| 10 | + @config_tmp_dir = File.join(Rails.root, 'tmp',"gitlabhq-gitolite-#{Time.now.to_i}") | |
| 11 | + end | |
| 12 | + | |
| 13 | + def apply | |
| 14 | + Timeout::timeout(30) do | |
| 15 | + File.open(File.join(Rails.root, 'tmp', "gitlabhq-gitolite.lock"), "w+") do |f| | |
| 16 | + begin | |
| 17 | + f.flock(File::LOCK_EX) | |
| 18 | + reset_config_tmp_dir | |
| 19 | + pull | |
| 20 | + yield(self) | |
| 21 | + push | |
| 22 | + FileUtils.rm_rf(config_tmp_dir) | |
| 23 | + ensure | |
| 24 | + f.flock(File::LOCK_UN) | |
| 25 | + end | |
| 26 | + end | |
| 27 | + end | |
| 28 | + rescue Exception => ex | |
| 29 | + Gitlab::Logger.error(ex.message) | |
| 30 | + raise Gitolite::AccessDenied.new("gitolite timeout") | |
| 31 | + end | |
| 32 | + | |
| 33 | + def destroy_project(project) | |
| 34 | + FileUtils.rm_rf(project.path_to_repo) | |
| 35 | + | |
| 36 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
| 37 | + conf = ga_repo.config | |
| 38 | + conf.rm_repo(project.path) | |
| 39 | + ga_repo.save | |
| 40 | + end | |
| 41 | + | |
| 42 | + def destroy_project!(project) | |
| 43 | + apply do |config| | |
| 44 | + config.destroy_project(project) | |
| 45 | + end | |
| 46 | + end | |
| 47 | + | |
| 48 | + def write_key(id, key) | |
| 49 | + File.open(File.join(config_tmp_dir, 'gitolite/keydir',"#{id}.pub"), 'w') do |f| | |
| 50 | + f.write(key.gsub(/\n/,'')) | |
| 51 | + end | |
| 52 | + end | |
| 53 | + | |
| 54 | + def rm_key(user) | |
| 55 | + File.unlink(File.join(config_tmp_dir, 'gitolite/keydir',"#{user}.pub")) | |
| 56 | + `cd #{File.join(config_tmp_dir,'gitolite')} ; git rm keydir/#{user}.pub` | |
| 57 | + end | |
| 58 | + | |
| 59 | + # update or create | |
| 60 | + def update_project(repo_name, project) | |
| 61 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
| 62 | + conf = ga_repo.config | |
| 63 | + repo = update_project_config(project, conf) | |
| 64 | + conf.add_repo(repo, true) | |
| 65 | + | |
| 66 | + ga_repo.save | |
| 67 | + end | |
| 68 | + | |
| 69 | + def update_project!(repo_name, project) | |
| 70 | + apply do |config| | |
| 71 | + config.update_project(repo_name, project) | |
| 72 | + end | |
| 73 | + end | |
| 74 | + | |
| 75 | + # Updates many projects and uses project.path as the repo path | |
| 76 | + # An order of magnitude faster than update_project | |
| 77 | + def update_projects(projects) | |
| 78 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
| 79 | + conf = ga_repo.config | |
| 80 | + | |
| 81 | + projects.each do |project| | |
| 82 | + repo = update_project_config(project, conf) | |
| 83 | + conf.add_repo(repo, true) | |
| 84 | + end | |
| 85 | + | |
| 86 | + ga_repo.save | |
| 87 | + end | |
| 88 | + | |
| 89 | + def update_project_config(project, conf) | |
| 90 | + repo_name = project.path | |
| 91 | + | |
| 92 | + repo = if conf.has_repo?(repo_name) | |
| 93 | + conf.get_repo(repo_name) | |
| 94 | + else | |
| 95 | + ::Gitolite::Config::Repo.new(repo_name) | |
| 96 | + end | |
| 97 | + | |
| 98 | + name_readers = project.repository_readers | |
| 99 | + name_writers = project.repository_writers | |
| 100 | + name_masters = project.repository_masters | |
| 101 | + | |
| 102 | + pr_br = project.protected_branches.map(&:name).join("$ ") | |
| 103 | + | |
| 104 | + repo.clean_permissions | |
| 105 | + | |
| 106 | + # Deny access to protected branches for writers | |
| 107 | + unless name_writers.blank? || pr_br.blank? | |
| 108 | + repo.add_permission("-", pr_br.strip + "$ ", name_writers) | |
| 109 | + end | |
| 110 | + | |
| 111 | + # Add read permissions | |
| 112 | + repo.add_permission("R", "", name_readers) unless name_readers.blank? | |
| 113 | + | |
| 114 | + # Add write permissions | |
| 115 | + repo.add_permission("RW+", "", name_writers) unless name_writers.blank? | |
| 116 | + repo.add_permission("RW+", "", name_masters) unless name_masters.blank? | |
| 117 | + | |
| 118 | + repo | |
| 119 | + end | |
| 120 | + | |
| 121 | + def admin_all_repo | |
| 122 | + ga_repo = ::Gitolite::GitoliteAdmin.new(File.join(config_tmp_dir,'gitolite')) | |
| 123 | + conf = ga_repo.config | |
| 124 | + owner_name = "" | |
| 125 | + | |
| 126 | + # Read gitolite-admin user | |
| 127 | + # | |
| 128 | + begin | |
| 129 | + repo = conf.get_repo("gitolite-admin") | |
| 130 | + owner_name = repo.permissions[0]["RW+"][""][0] | |
| 131 | + raise StandardError if owner_name.blank? | |
| 132 | + rescue => ex | |
| 133 | + puts "Can't determine gitolite-admin owner".red | |
| 134 | + raise StandardError | |
| 135 | + end | |
| 136 | + | |
| 137 | + # @ALL repos premission for gitolite owner | |
| 138 | + repo_name = "@all" | |
| 139 | + repo = if conf.has_repo?(repo_name) | |
| 140 | + conf.get_repo(repo_name) | |
| 141 | + else | |
| 142 | + ::Gitolite::Config::Repo.new(repo_name) | |
| 143 | + end | |
| 144 | + | |
| 145 | + repo.add_permission("RW+", "", owner_name) | |
| 146 | + conf.add_repo(repo, true) | |
| 147 | + ga_repo.save | |
| 148 | + end | |
| 149 | + | |
| 150 | + def admin_all_repo! | |
| 151 | + apply { |config| config.admin_all_repo } | |
| 152 | + end | |
| 153 | + | |
| 154 | + private | |
| 155 | + | |
| 156 | + def pull | |
| 157 | + Dir.mkdir config_tmp_dir | |
| 158 | + `git clone #{Gitlab.config.gitolite_admin_uri} #{config_tmp_dir}/gitolite` | |
| 159 | + end | |
| 160 | + | |
| 161 | + def push | |
| 162 | + Dir.chdir(File.join(config_tmp_dir, "gitolite")) | |
| 163 | + `git add -A` | |
| 164 | + `git commit -am "GitLab"` | |
| 165 | + `git push` | |
| 166 | + Dir.chdir(Rails.root) | |
| 167 | + end | |
| 168 | + end | |
| 169 | +end | |
| 170 | + | ... | ... |
| ... | ... | @@ -0,0 +1,16 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Gitlab::GitoliteConfig do | |
| 4 | + let(:gitolite) { Gitlab::GitoliteConfig.new } | |
| 5 | + | |
| 6 | + it { should respond_to :write_key } | |
| 7 | + it { should respond_to :rm_key } | |
| 8 | + it { should respond_to :update_project } | |
| 9 | + it { should respond_to :update_project! } | |
| 10 | + it { should respond_to :update_projects } | |
| 11 | + it { should respond_to :destroy_project } | |
| 12 | + it { should respond_to :destroy_project! } | |
| 13 | + it { should respond_to :apply } | |
| 14 | + it { should respond_to :admin_all_repo } | |
| 15 | + it { should respond_to :admin_all_repo! } | |
| 16 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,25 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Gitlab::Gitolite do | |
| 4 | + let(:project) { double('Project', path: 'diaspora') } | |
| 5 | + let(:gitolite_config) { double('Gitlab::GitoliteConfig') } | |
| 6 | + let(:gitolite) { Gitlab::Gitolite.new } | |
| 7 | + | |
| 8 | + before do | |
| 9 | + gitolite.stub(config: gitolite_config) | |
| 10 | + end | |
| 11 | + | |
| 12 | + it { should respond_to :set_key } | |
| 13 | + it { should respond_to :remove_key } | |
| 14 | + | |
| 15 | + it { should respond_to :update_repository } | |
| 16 | + it { should respond_to :create_repository } | |
| 17 | + it { should respond_to :remove_repository } | |
| 18 | + | |
| 19 | + it { gitolite.url_to_repo('diaspora').should == Gitlab.config.ssh_path + "diaspora.git" } | |
| 20 | + | |
| 21 | + it "should call config update" do | |
| 22 | + gitolite_config.should_receive(:update_project!) | |
| 23 | + gitolite.update_repository project | |
| 24 | + end | |
| 25 | +end | ... | ... |
spec/support/gitolite_stub.rb
| ... | ... | @@ -17,7 +17,7 @@ module GitoliteStub |
| 17 | 17 | ) |
| 18 | 18 | |
| 19 | 19 | gitolite_admin = double( |
| 20 | - 'Gitolite::GitoliteAdmin', | |
| 20 | + 'Gitolite::GitoliteAdmin', | |
| 21 | 21 | config: gitolite_config, |
| 22 | 22 | save: true, |
| 23 | 23 | ) |
| ... | ... | @@ -27,9 +27,21 @@ module GitoliteStub |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | 29 | def stub_gitlab_gitolite |
| 30 | - gitlab_gitolite = Gitlab::Gitolite.new | |
| 31 | - Gitlab::Gitolite.stub(new: gitlab_gitolite) | |
| 32 | - gitlab_gitolite.stub(configure: ->() { yield(self) }) | |
| 33 | - gitlab_gitolite.stub(update_keys: true) | |
| 30 | + gitolite_config = double('Gitlab::GitoliteConfig') | |
| 31 | + gitolite_config.stub( | |
| 32 | + apply: ->() { yield(self) }, | |
| 33 | + write_key: true, | |
| 34 | + rm_key: true, | |
| 35 | + update_projects: true, | |
| 36 | + update_project: true, | |
| 37 | + update_project!: true, | |
| 38 | + destroy_project: true, | |
| 39 | + destroy_project!: true, | |
| 40 | + admin_all_repo: true, | |
| 41 | + admin_all_repo!: true, | |
| 42 | + | |
| 43 | + ) | |
| 44 | + | |
| 45 | + Gitlab::GitoliteConfig.stub(new: gitolite_config) | |
| 34 | 46 | end |
| 35 | 47 | end | ... | ... |