git_handler 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/git_handler.rb +7 -1
- data/lib/git_handler/core_ext/hash.rb +2 -2
- data/lib/git_handler/git_command.rb +2 -2
- data/lib/git_handler/session.rb +15 -18
- data/lib/git_handler/version.rb +1 -1
- data/spec/command_spec.rb +20 -20
- data/spec/core_ext_spec.rb +42 -0
- metadata +4 -2
data/lib/git_handler.rb
CHANGED
@@ -7,4 +7,10 @@ require 'git_handler/request'
|
|
7
7
|
require 'git_handler/git_command'
|
8
8
|
require 'git_handler/session'
|
9
9
|
|
10
|
-
module GitHandler
|
10
|
+
module GitHandler
|
11
|
+
# Shorthand for GitHandler::Session.new
|
12
|
+
# @return [Session] new session instance
|
13
|
+
def self.new(config)
|
14
|
+
GitHandler::Session.new(config)
|
15
|
+
end
|
16
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module GitHandler
|
2
2
|
module GitCommand
|
3
|
-
GIT_COMMAND =
|
3
|
+
GIT_COMMAND = /\A(git-upload-pack|git upload-pack|git-upload-archive|git upload-archive|git-receive-pack|git receive-pack) '(.*)'\z/
|
4
4
|
|
5
5
|
COMMANDS_READONLY = [
|
6
6
|
'git-upload-pack',
|
@@ -43,4 +43,4 @@ module GitHandler
|
|
43
43
|
COMMANDS_WRITE.include?(cmd)
|
44
44
|
end
|
45
45
|
end
|
46
|
-
end
|
46
|
+
end
|
data/lib/git_handler/session.rb
CHANGED
@@ -8,9 +8,7 @@ module GitHandler
|
|
8
8
|
attr_reader :log
|
9
9
|
|
10
10
|
# Initialize a new Session
|
11
|
-
#
|
12
|
-
# config - GitHandler::Configuration instance
|
13
|
-
#
|
11
|
+
# @param [Configuration] config an existing configuration instance
|
14
12
|
def initialize(config=nil)
|
15
13
|
unless config.kind_of?(GitHandler::Configuration)
|
16
14
|
raise SessionError, 'Configuration required!'
|
@@ -28,12 +26,11 @@ module GitHandler
|
|
28
26
|
@log = Logger.new(@config.log_path)
|
29
27
|
end
|
30
28
|
|
31
|
-
# Execute session
|
32
|
-
#
|
33
|
-
# args
|
34
|
-
# env
|
35
|
-
# run_git
|
36
|
-
#
|
29
|
+
# Execute session
|
30
|
+
#
|
31
|
+
# @param [Array] args session arguments
|
32
|
+
# @param [Hash] env hash with environment variables, use ENV.to_hash.dup
|
33
|
+
# @param [Boolean] run_git execute git command if set to true
|
37
34
|
def execute(args, env, run_git=true)
|
38
35
|
@args = args
|
39
36
|
@env = env
|
@@ -77,9 +74,12 @@ module GitHandler
|
|
77
74
|
# exec("ssh", "git@TARGET", "#{args.join(' ')}")
|
78
75
|
end
|
79
76
|
|
80
|
-
# Execute session
|
81
|
-
#
|
82
|
-
#
|
77
|
+
# Execute session with catch-all-exceptions wrapper
|
78
|
+
# terminates session on SessionError or Exception
|
79
|
+
#
|
80
|
+
# @param [Array] args session arguments
|
81
|
+
# @param [Hash] env hash with environment variables, use ENV.to_hash.dup
|
82
|
+
# @param [Boolean] run_git execute git command if set to true
|
83
83
|
def execute_safe(args, env, run_git=true)
|
84
84
|
begin
|
85
85
|
execute(args, env, run_git)
|
@@ -94,9 +94,8 @@ module GitHandler
|
|
94
94
|
|
95
95
|
# Terminate session execution
|
96
96
|
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
97
|
+
# @param [String] reason
|
98
|
+
# @param [Fixnum] exit_status
|
100
99
|
def terminate(reason='', exit_status=1)
|
101
100
|
logger.error("Session terminated. Reason: #{reason}")
|
102
101
|
$stderr.puts("Request failed: #{reason}")
|
@@ -104,15 +103,13 @@ module GitHandler
|
|
104
103
|
end
|
105
104
|
|
106
105
|
# Check if session environment is valid
|
107
|
-
#
|
108
106
|
def valid_environment?
|
109
107
|
env['USER'] == config.user && env['HOME'] == config.home_path
|
110
108
|
end
|
111
109
|
|
112
110
|
# Check if session request is valid
|
113
|
-
#
|
114
111
|
def valid_request?
|
115
|
-
if env.
|
112
|
+
if env.include_all?(['SSH_CLIENT', 'SSH_CONNECTION', 'SSH_ORIGINAL_COMMAND'])
|
116
113
|
if valid_command?(env['SSH_ORIGINAL_COMMAND'])
|
117
114
|
return true
|
118
115
|
end
|
data/lib/git_handler/version.rb
CHANGED
data/spec/command_spec.rb
CHANGED
@@ -5,29 +5,29 @@ class TestInstance
|
|
5
5
|
end
|
6
6
|
|
7
7
|
describe GitHandler::GitCommand do
|
8
|
-
|
9
|
-
@obj = TestInstance.new
|
10
|
-
end
|
8
|
+
let(:command) { TestInstance.new }
|
11
9
|
|
12
10
|
it 'detects a valid git command' do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
command.valid_command?("invalid command").should be_false
|
12
|
+
command.valid_command?("line1\nline2").should be_false
|
13
|
+
command.valid_command?("git-receive-pack").should be_false
|
14
|
+
command.valid_command?("git-receive-pack repo.git").should be_false
|
15
|
+
command.valid_command?("git-receive-pack 'repo'").should be_true
|
16
|
+
command.valid_command?("git-receive-pack 'repo.git'").should be_true
|
17
|
+
command.valid_command?("git-receive-pack 'repo'\ngit-upload-pack 'repo'").should be_false
|
18
18
|
end
|
19
19
|
|
20
20
|
context '.parse_command' do
|
21
21
|
it 'raises error on invalid git command' do
|
22
|
-
proc {
|
22
|
+
proc { command.parse_command("invalid command") }.
|
23
23
|
should raise_error GitHandler::ParseError
|
24
24
|
|
25
|
-
proc {
|
25
|
+
proc { command.parse_command("git-receive-pack 'repo.git'") }.
|
26
26
|
should_not raise_error GitHandler::ParseError
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'returns a proper action and repo' do
|
30
|
-
result =
|
30
|
+
result = command.parse_command("git-receive-pack 'repo.git'")
|
31
31
|
result.should be_a Hash
|
32
32
|
result.should eql(
|
33
33
|
:action => 'git-receive-pack',
|
@@ -39,17 +39,17 @@ describe GitHandler::GitCommand do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'detects read command' do
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
command.read_command?('git-receive-pack').should be_false
|
43
|
+
command.read_command?('git-upload-pack').should be_true
|
44
|
+
command.read_command?('git upload-pack').should be_true
|
45
|
+
command.read_command?('git-upload-archive').should be_true
|
46
|
+
command.read_command?('git upload-archive').should be_true
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'detects write command' do
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
command.write_command?("git-upload-pack").should be_false
|
51
|
+
command.write_command?("git-upload-archive").should be_false
|
52
|
+
command.write_command?("git receive-pack").should be_true
|
53
|
+
command.write_command?("git-receive-pack").should be_true
|
54
54
|
end
|
55
55
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
it { should respond_to :include_all? }
|
5
|
+
it { should respond_to :include_any? }
|
6
|
+
|
7
|
+
it 'should include all elements' do
|
8
|
+
[1,2,3].include_all?([1,2,3]).should be_true
|
9
|
+
[1,2,3].include_all?([1,2]).should be_true
|
10
|
+
[1,2].include_all?([1,2,3]).should be_false
|
11
|
+
[1,2].include_all?([4,5,6]).should be_false
|
12
|
+
[].include_all?([4,5,6]).should be_false
|
13
|
+
[].include_all?([]).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should include any elements' do
|
17
|
+
[1,2,3].include_any?([1,2,3]).should be_true
|
18
|
+
[1,2,3].include_any?([1,2]).should be_true
|
19
|
+
[1,2,3].include_any?([1,4,5]).should be_true
|
20
|
+
[1,2,3].include_any?([4,5,6]).should be_false
|
21
|
+
[].include_any?([]).should be_false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Hash do
|
26
|
+
it { should respond_to :include_all? }
|
27
|
+
it { should respond_to :include_any? }
|
28
|
+
|
29
|
+
it 'should include all keys' do
|
30
|
+
({'a' => 1, 'b' => 2, 'c' => 3}.include_all?(['a','b','c'])).should be_true
|
31
|
+
({'a' => 1, 'b' => 2, 'c' => 3}.include_all?(['a','b'])).should be_true
|
32
|
+
({'a' => 1, :b => 2}).include_all?(['a', 'b']).should be_false
|
33
|
+
({'a' => 1, 'b' => 2}.include_all?(['c'])).should be_false
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should include any keys' do
|
37
|
+
({'a' => 1, 'b' => 2}.include_any?(['a', 'b', 'c'])).should be_true
|
38
|
+
({'a' => 1, :b => 2}.include_any?(['a', 'b', 'c'])).should be_true
|
39
|
+
({'a' => 1, 'b' => 2}.include_any?([:a, :b])).should be_false
|
40
|
+
({'a' => 1, 'b' => 2}.include_any?(['d', 'e'])).should be_false
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: git_handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Dan Sosedoff
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-06-22 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- spec/authorized_keys_spec.rb
|
88
88
|
- spec/command_spec.rb
|
89
89
|
- spec/configuration_spec.rb
|
90
|
+
- spec/core_ext_spec.rb
|
90
91
|
- spec/fixtures/.gitkeep
|
91
92
|
- spec/public_key_spec.rb
|
92
93
|
- spec/session_spec.rb
|
@@ -122,6 +123,7 @@ test_files:
|
|
122
123
|
- spec/authorized_keys_spec.rb
|
123
124
|
- spec/command_spec.rb
|
124
125
|
- spec/configuration_spec.rb
|
126
|
+
- spec/core_ext_spec.rb
|
125
127
|
- spec/fixtures/.gitkeep
|
126
128
|
- spec/public_key_spec.rb
|
127
129
|
- spec/session_spec.rb
|