summaryrefslogtreecommitdiff
path: root/lib/gitlab_shell.rb
blob: 0223deda7b4251709d10f8154215a69bb8210d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'open3'
require 'net/http'

require_relative 'gitlab_config'

class GitlabShell
  attr_accessor :username, :repo_name, :git_cmd, :repos_path, :repo_name

  def initialize
    @username = ARGV.shift
    @origin_cmd = ENV['SSH_ORIGINAL_COMMAND']
    @repos_path = GitlabConfig.new.repos_path
  end

  def exec
    if @origin_cmd
      parse_cmd

      if git_cmds.include?(@git_cmd)
        ENV['GL_USER'] = @username

        if validate_access
          process_cmd
        end
      else
        puts 'Not allowed command'
      end
    else
      puts "Welcome #{@username}!"
    end
  end

  protected

  def parse_cmd
    args = @origin_cmd.split(' ')
    @git_cmd = args.shift
    @repo_name = args.shift
  end

  def git_cmds
    %w(git-upload-pack git-receive-pack git-upload-archive)
  end

  def process_cmd
    repo_full_path = File.join(repos_path, repo_name)
    system("#{@git_cmd} #{repo_full_path}")
  end

  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'
  end
end