diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_access.rb | 34 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 12 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 31 | ||||
-rw-r--r-- | lib/gitlab_projects.rb | 8 | ||||
-rw-r--r-- | lib/gitlab_update.rb | 58 |
5 files changed, 74 insertions, 69 deletions
diff --git a/lib/gitlab_access.rb b/lib/gitlab_access.rb new file mode 100644 index 0000000..1f328b7 --- /dev/null +++ b/lib/gitlab_access.rb @@ -0,0 +1,34 @@ +require_relative 'gitlab_init' +require_relative 'gitlab_net' +require_relative 'names_helper' +require 'json' + +class GitlabAccess + include NamesHelper + + attr_reader :config, :repo_path, :repo_name, :changes + + def initialize(repo_path, actor, changes) + @config = GitlabConfig.new + @repo_path, @actor = repo_path.strip, actor + @repo_name = extract_repo_name(@repo_path.dup, config.repos_path.to_s) + @changes = changes.lines + end + + def exec + if api.allowed?('git-receive-pack', @repo_name, @actor, @changes) + exit 0 + else + # reset GL_ID env since we stop git push here + ENV['GL_ID'] = nil + puts "GitLab: You are not allowed to access #{@ref_name}!" + exit 1 + end + end + + protected + + def api + GitlabNet.new + end +end diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 6397106..65c2828 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -6,21 +6,17 @@ require_relative 'gitlab_config' require_relative 'gitlab_logger' class GitlabNet - def allowed?(cmd, repo, actor, ref, oldrev = nil, newrev = nil, forced_push = false) + def allowed?(cmd, repo, actor, changes) project_name = repo.gsub("'", "") project_name = project_name.gsub(/\.git\Z/, "") project_name = project_name.gsub(/\A\//, "") params = { action: cmd, - ref: ref, + changes: changes, project: project_name, - forced_push: forced_push, } - params.merge!(oldrev: oldrev) if oldrev - params.merge!(newrev: newrev) if newrev - if actor =~ /\Akey\-\d+\Z/ params.merge!(key_id: actor.gsub("key-", "")) elsif actor =~ /\Auser\-\d+\Z/ @@ -86,7 +82,7 @@ class GitlabNet end def cert_store - @cert_store ||= OpenSSL::X509::Store.new.tap { |store| + @cert_store ||= OpenSSL::X509::Store.new.tap do |store| store.set_default_paths if ca_file = config.http_settings['ca_file'] @@ -96,6 +92,6 @@ class GitlabNet if ca_path = config.http_settings['ca_path'] store.add_path(ca_path) end - } + end end end diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb new file mode 100644 index 0000000..3f9f384 --- /dev/null +++ b/lib/gitlab_post_receive.rb @@ -0,0 +1,31 @@ +require_relative 'gitlab_init' +require 'json' + +class GitlabPostReceive + attr_reader :config, :repo_path, :changes + + def initialize(repo_path, actor, changes) + @config = GitlabConfig.new + @repo_path, @actor = repo_path.strip, actor + @changes = changes.lines + end + + def exec + # reset GL_ID env since we already + # get value from it + ENV['GL_ID'] = nil + + update_redis + end + + protected + + def update_redis + queue = "#{config.redis_namespace}:queue:post_receive" + msg = JSON.dump({'class' => 'PostReceive', 'args' => [@repo_path, @actor, @changes]}) + unless system(*config.redis_command, 'rpush', queue, msg, err: '/dev/null', out: '/dev/null') + puts "GitLab: An unexpected error occurred (redis-cli returned #{$?.exitstatus})." + exit 1 + end + end +end diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index a6fa1b5..a27036b 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -18,9 +18,11 @@ class GitlabProjects attr_reader :full_path def self.create_hooks(path) - hook = File.join(path, 'hooks', 'update') - File.delete(hook) if File.exists?(hook) - File.symlink(File.join(ROOT_PATH, 'hooks', 'update'), hook) + %w(pre-receive post-receive).each do |hook_name| + hook = File.join(path, 'hooks', hook_name) + File.delete(hook) if File.exists?(hook) + File.symlink(File.join(ROOT_PATH, 'hooks', hook_name), hook) + end end def initialize diff --git a/lib/gitlab_update.rb b/lib/gitlab_update.rb deleted file mode 100644 index cd7a1e5..0000000 --- a/lib/gitlab_update.rb +++ /dev/null @@ -1,58 +0,0 @@ -require_relative 'gitlab_init' -require_relative 'gitlab_net' -require_relative 'names_helper' -require 'json' - -class GitlabUpdate - include NamesHelper - - attr_reader :config, :repo_path, :repo_name, - :ref, :ref_name, :oldrev, :newrev - - def initialize(repo_path, actor, ref) - @config = GitlabConfig.new - @repo_path, @actor, @ref = repo_path.strip, actor, ref - @repo_name = extract_repo_name(@repo_path.dup, config.repos_path.to_s) - @ref_name = extract_ref_name(ref) - @oldrev = ARGV[1] - @newrev = ARGV[2] - end - - def forced_push? - if @oldrev !~ /00000000/ && @newrev !~ /00000000/ - missed_refs = IO.popen(%W(git rev-list #{@oldrev} ^#{@newrev})).read - missed_refs.split("\n").size > 0 - else - false - end - end - - def exec - # reset GL_ID env since we already - # get value from it - ENV['GL_ID'] = nil - - if api.allowed?('git-receive-pack', @repo_name, @actor, @ref_name, @oldrev, @newrev, forced_push?) - update_redis - exit 0 - else - puts "GitLab: You are not allowed to access #{@ref_name}!" - exit 1 - end - end - - protected - - def api - GitlabNet.new - end - - def update_redis - queue = "#{config.redis_namespace}:queue:post_receive" - msg = JSON.dump({'class' => 'PostReceive', 'args' => [@repo_path, @oldrev, @newrev, @ref, @actor]}) - unless system(*config.redis_command, 'rpush', queue, msg, err: '/dev/null', out: '/dev/null') - puts "GitLab: An unexpected error occurred (redis-cli returned #{$?.exitstatus})." - exit 1 - end - end -end |