Sha256: 81b856e17947edcf026fbe142cc3fdf73ae04924ff7af684466257edb2868098

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Gisha
  class Commands::Receive
    class DisallowedCommandError < StandardError; end

    attr_accessor :key_id, :git_cmd, :repos_path, :repo_name

    def initialize(key_id)
      @key_id = key_id
    end

    def exec
      if origin_cmd
        parse_cmd
        if git_cmds.include?(git_cmd)
          process_cmd
        else
          raise DisallowedCommandError
        end
      end
    end

    def repos_path
      '/store/repositories'
    end

    private

    def process_cmd
      repo_full_path = File.join(repos_path, repo_name)
      exec_cmd(@git_cmd, repo_full_path)
    end

    def exec_cmd(*args)
      Kernel::exec(
      {
        'PATH' => ENV['PATH'],
        'LD_LIBRARY_PATH' => ENV['LD_LIBRARY_PATH'],
        'KEY_ID' => key_id
      }, *args, unsetenv_others: true)
    end

    def parse_cmd
      args = Shellwords.shellwords(origin_cmd)
      raise DisallowedCommandError unless args.count == 2
      @git_cmd = args[0]
      @repo_name = escape_path(args[1])
    end

    def origin_cmd
      @origin_cmd ||= ENV['SSH_ORIGINAL_COMMAND']
    end

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

    def escape_path(path)
      full_repo_path = File.join(repos_path, path)

      if File.absolute_path(full_repo_path) == full_repo_path
        path
      else
        abort "Wrong repository path"
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gisha-0.1.1 lib/gisha/commands/receive.rb
gisha-0.1.0 lib/gisha/commands/receive.rb