Commit 02f4cb52 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

gitlab net

parent 43bdc5da
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
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
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment