Skip to content

Commit 02f4cb5

Browse files
committed
gitlab net
1 parent 43bdc5d commit 02f4cb5

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

lib/gitlab_net.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'net/http'
2+
require 'json'
3+
4+
class GitlabNet
5+
def allowed?(cmd, repo, key, ref)
6+
project_name = repo.gsub("'", "")
7+
project_name = project_name.gsub(/\.git$/, "")
8+
key_id = key.gsub("key-", "")
9+
10+
url = "#{host}/allowed?project=#{project_name}&key_id=#{key_id}&action=#{cmd}&ref=#{ref}"
11+
12+
resp = get(url)
13+
14+
!!(resp.code == '200' && resp.body == 'true')
15+
end
16+
17+
def discover(key)
18+
key_id = key.gsub("key-", "")
19+
resp = get("#{host}/discover?key_id=#{key_id}")
20+
JSON.parse(resp.body)
21+
end
22+
23+
protected
24+
25+
def host
26+
"http://127.0.0.1:3000/api/v3/internal"
27+
end
28+
29+
def get(url)
30+
Net::HTTP.get_response(URI.parse(url))
31+
end
32+
end

lib/gitlab_shell.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require 'open3'
2-
require 'net/http'
32

43
require_relative 'gitlab_config'
4+
require_relative 'gitlab_net'
55

66
class GitlabShell
7-
attr_accessor :username, :repo_name, :git_cmd, :repos_path, :repo_name
7+
attr_accessor :key_id, :repo_name, :git_cmd, :repos_path, :repo_name
88

99
def initialize
10-
@username = ARGV.shift
10+
@key_id = ARGV.shift
1111
@origin_cmd = ENV['SSH_ORIGINAL_COMMAND']
1212
@repos_path = GitlabConfig.new.repos_path
1313
end
@@ -17,7 +17,7 @@ def exec
1717
parse_cmd
1818

1919
if git_cmds.include?(@git_cmd)
20-
ENV['GL_USER'] = @username
20+
ENV['GL_USER'] = @key_id
2121

2222
if validate_access
2323
process_cmd
@@ -26,7 +26,8 @@ def exec
2626
puts 'Not allowed command'
2727
end
2828
else
29-
puts "Welcome #{@username}!"
29+
user = api.discover(@key_id)
30+
puts "Welcome to GitLab, #{user['name']}!"
3031
end
3132
end
3233

@@ -49,10 +50,11 @@ def process_cmd
4950

5051
def validate_access
5152
@ref_name = 'master' # just hardcode it cause we dont know ref
52-
project_name = @repo_name.gsub("'", "")
53-
project_name = project_name.gsub(/\.git$/, "")
54-
url = "http://127.0.0.1:3000/api/v3/allowed?project=#{project_name}&username=#{@username}&action=#{@git_cmd}&ref=#{@ref_name}"
55-
resp = Net::HTTP.get_response(URI.parse(url))
56-
resp.code == '200' && resp.body == 'true'
53+
54+
api.allowed?(@git_cmd, @repo_name, @key_id, @ref_name)
55+
end
56+
57+
def api
58+
GitlabNet.new
5759
end
5860
end

spec/gitlab_shell_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
describe :initialize do
66
before do
77
ssh_cmd 'git-receive-pack'
8-
ARGV[0] = 'dzaporozhets'
8+
ARGV[0] = 'key-56'
99
@shell = GitlabShell.new
1010
end
1111

12-
it { @shell.username.should == 'dzaporozhets' }
12+
it { @shell.key_id.should == 'key-56' }
1313
it { @shell.repos_path.should == "/home/git/repositories" }
1414
end
1515

@@ -62,7 +62,7 @@ def ssh_cmd(cmd)
6262
end
6363

6464
def stubbed_shell
65-
ARGV[0] = 'dzaporozhets'
65+
ARGV[0] = 'key-56'
6666
@shell = GitlabShell.new
6767
@shell.stub(validate_access: true)
6868
@shell.stub(process_cmd: true)

0 commit comments

Comments
 (0)