diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-02-05 11:59:53 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-02-05 11:59:53 +0200 |
commit | 02f4cb520458ad336e67c259810359ebcdaedb59 (patch) | |
tree | 4780bb36ed4a500419406af7698bbce7947574d2 | |
parent | 43bdc5da612af265b3c06eec26c36f7dbd98329e (diff) | |
download | gitlab-shell-02f4cb520458ad336e67c259810359ebcdaedb59.tar.gz |
gitlab net
-rw-r--r-- | lib/gitlab_net.rb | 32 | ||||
-rw-r--r-- | lib/gitlab_shell.rb | 22 | ||||
-rw-r--r-- | spec/gitlab_shell_spec.rb | 6 |
3 files changed, 47 insertions, 13 deletions
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb new file mode 100644 index 0000000..d92d3ab --- /dev/null +++ b/lib/gitlab_net.rb @@ -0,0 +1,32 @@ +require 'net/http' +require 'json' + +class GitlabNet + def allowed?(cmd, repo, key, ref) + project_name = repo.gsub("'", "") + project_name = project_name.gsub(/\.git$/, "") + key_id = key.gsub("key-", "") + + url = "#{host}/allowed?project=#{project_name}&key_id=#{key_id}&action=#{cmd}&ref=#{ref}" + + resp = get(url) + + !!(resp.code == '200' && resp.body == 'true') + end + + def discover(key) + key_id = key.gsub("key-", "") + resp = get("#{host}/discover?key_id=#{key_id}") + JSON.parse(resp.body) + end + + protected + + def host + "http://127.0.0.1:3000/api/v3/internal" + end + + def get(url) + Net::HTTP.get_response(URI.parse(url)) + end +end diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index 0223ded..9167bac 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -1,13 +1,13 @@ require 'open3' -require 'net/http' require_relative 'gitlab_config' +require_relative 'gitlab_net' class GitlabShell - attr_accessor :username, :repo_name, :git_cmd, :repos_path, :repo_name + attr_accessor :key_id, :repo_name, :git_cmd, :repos_path, :repo_name def initialize - @username = ARGV.shift + @key_id = ARGV.shift @origin_cmd = ENV['SSH_ORIGINAL_COMMAND'] @repos_path = GitlabConfig.new.repos_path end @@ -17,7 +17,7 @@ class GitlabShell parse_cmd if git_cmds.include?(@git_cmd) - ENV['GL_USER'] = @username + ENV['GL_USER'] = @key_id if validate_access process_cmd @@ -26,7 +26,8 @@ class GitlabShell puts 'Not allowed command' end else - puts "Welcome #{@username}!" + user = api.discover(@key_id) + puts "Welcome to GitLab, #{user['name']}!" end end @@ -49,10 +50,11 @@ class GitlabShell def validate_access @ref_name = 'master' # just hardcode it cause we dont know ref - project_name = @repo_name.gsub("'", "") - project_name = project_name.gsub(/\.git$/, "") - url = "http://127.0.0.1:3000/api/v3/allowed?project=#{project_name}&username=#{@username}&action=#{@git_cmd}&ref=#{@ref_name}" - resp = Net::HTTP.get_response(URI.parse(url)) - resp.code == '200' && resp.body == 'true' + + api.allowed?(@git_cmd, @repo_name, @key_id, @ref_name) + end + + def api + GitlabNet.new end end diff --git a/spec/gitlab_shell_spec.rb b/spec/gitlab_shell_spec.rb index ada3e87..11815d7 100644 --- a/spec/gitlab_shell_spec.rb +++ b/spec/gitlab_shell_spec.rb @@ -5,11 +5,11 @@ describe GitlabShell do describe :initialize do before do ssh_cmd 'git-receive-pack' - ARGV[0] = 'dzaporozhets' + ARGV[0] = 'key-56' @shell = GitlabShell.new end - it { @shell.username.should == 'dzaporozhets' } + it { @shell.key_id.should == 'key-56' } it { @shell.repos_path.should == "/home/git/repositories" } end @@ -62,7 +62,7 @@ describe GitlabShell do end def stubbed_shell - ARGV[0] = 'dzaporozhets' + ARGV[0] = 'key-56' @shell = GitlabShell.new @shell.stub(validate_access: true) @shell.stub(process_cmd: true) |