sshkit-backends-netssh_global 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'turn'
5
+ require 'minitest/unit'
6
+ require 'mocha/setup'
7
+
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+
11
+ Dir[File.expand_path('test/support/*.rb')].each { |file| require file }
12
+
13
+ class UnitTest < MiniTest::Unit::TestCase
14
+ # def setup
15
+ # SSHKit.reset_configuration!
16
+ # end
17
+ end
18
+
19
+ class FunctionalTest < MiniTest::Unit::TestCase
20
+ def setup
21
+ unless VagrantWrapper.running?
22
+ warn "Vagrant VMs are not running. Please, start it manually with `vagrant up`"
23
+ end
24
+ end
25
+ end
26
+
27
+ #
28
+ # Force colours in Autotest
29
+ #
30
+ Turn.config.ansi = true
31
+ Turn.config.format = :pretty
32
+
33
+ MiniTest::Unit.autorun
@@ -0,0 +1,63 @@
1
+ # Copy of VagrantWrapper from SSHKit
2
+ require 'json'
3
+ require 'sshkit'
4
+
5
+ class VagrantWrapper
6
+ class << self
7
+ def hosts
8
+ @vm_hosts ||= begin
9
+ result = {}
10
+
11
+ boxes = boxes_list
12
+
13
+ unless running?
14
+ boxes.map! do |box|
15
+ box['user'] = ENV['USER']
16
+ box['port'] = '22'
17
+ box
18
+ end
19
+ end
20
+
21
+ boxes.each do |vm|
22
+ result[vm['name']] = vm_host(vm)
23
+ end
24
+
25
+ result
26
+ end
27
+ end
28
+
29
+ def reset!
30
+ @vm_hosts = nil
31
+ end
32
+
33
+ def running?
34
+ @running ||= begin
35
+ status = `#{vagrant_binary} status`
36
+ status.include?('running')
37
+ end
38
+ end
39
+
40
+ def boxes_list
41
+ json_config_path = File.join('test', 'boxes.json')
42
+ boxes = File.open(json_config_path).read
43
+ JSON.parse(boxes)
44
+ end
45
+
46
+ def vagrant_binary
47
+ 'vagrant'
48
+ end
49
+
50
+ private
51
+
52
+ def vm_host(vm)
53
+ host_options = {
54
+ user: vm['user'] || 'vagrant',
55
+ hostname: vm['hostname'] || 'localhost',
56
+ port: vm['port'] || '22',
57
+ password: vm['password'] || 'vagrant'
58
+ }
59
+
60
+ SSHKit::Host.new(host_options)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+ require 'sshkit/backends/netssh_global'
3
+
4
+ module SSHKit
5
+ module Backend
6
+ class TestNetsshGlobalUnit < UnitTest
7
+ def backend
8
+ @backend ||= NetsshGlobal
9
+ end
10
+
11
+ def test_net_ssh_configuration_options
12
+ backend.configure do |ssh|
13
+ ssh.pty = true
14
+ ssh.connection_timeout = 30
15
+ ssh.owner = 'fred'
16
+ ssh.ssh_options = {
17
+ keys: %w(/home/user/.ssh/id_rsa),
18
+ forward_agent: false,
19
+ auth_methods: %w(publickey password)
20
+ }
21
+ end
22
+
23
+ assert_equal 30, backend.config.connection_timeout
24
+ assert_equal true, backend.config.pty
25
+ assert_equal 'fred', backend.config.owner
26
+
27
+ assert_equal %w(/home/user/.ssh/id_rsa), backend.config.ssh_options[:keys]
28
+ assert_equal false, backend.config.ssh_options[:forward_agent]
29
+ assert_equal %w(publickey password), backend.config.ssh_options[:auth_methods]
30
+
31
+ end
32
+
33
+ def test_netssh_ext
34
+ assert_includes Net::SSH::Config.default_files, "#{Dir.pwd}/.ssh/config"
35
+ end
36
+
37
+ def test_transfer_summarizer
38
+ netssh_as_global = backend.new(Host.new('fake'))
39
+
40
+ summarizer = netssh_as_global.send(:transfer_summarizer,'Transferring')
41
+
42
+ [
43
+ [1, 1000, :debug, 'Transferring afile 0.1%'],
44
+ [1, 100, :debug, 'Transferring afile 1.0%'],
45
+ [99, 1000, :debug, 'Transferring afile 9.9%'],
46
+ [15, 100, :info, 'Transferring afile 15.0%'],
47
+ [1, 3, :info, 'Transferring afile 33.33%'],
48
+ [0, 1, :debug, 'Transferring afile 0.0%'],
49
+ [1, 2, :info, 'Transferring afile 50.0%'],
50
+ [0, 0, :warn, 'percentage 0/0'],
51
+ [1023, 343, :info, 'Transferring'],
52
+ ].each do |transferred,total,method,substring|
53
+ netssh_as_global.expects(method).with { |msg| msg.include?(substring) }
54
+ summarizer.call(nil,'afile',transferred,total)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,239 @@
1
+ require 'helper'
2
+ require 'sshkit/command_sudo_ssh_forward'
3
+
4
+ module SSHKit
5
+ class TestCommandSudoSshForward < UnitTest
6
+ def setup
7
+ SSHKit.reset_configuration!
8
+ end
9
+
10
+ def test_maps_a_command
11
+ c = CommandSudoSshForward.new('example')
12
+ assert_equal '/usr/bin/env example', c.to_command
13
+ end
14
+
15
+ def test_not_mapping_a_builtin
16
+ %w{if test time}.each do |builtin|
17
+ c = CommandSudoSshForward.new(builtin)
18
+ assert_equal builtin, c.to_command
19
+ end
20
+ end
21
+
22
+ def test_using_a_heredoc
23
+ c = CommandSudoSshForward.new <<-EOHEREDOC
24
+ if test ! -d /var/log; then
25
+ echo "Example"
26
+ fi
27
+ EOHEREDOC
28
+ assert_equal "if test ! -d /var/log; then; echo \"Example\"; fi", c.to_command
29
+ end
30
+
31
+ def test_including_the_env
32
+ SSHKit.config = nil
33
+ c = CommandSudoSshForward.new(:rails, 'server', env: {rails_env: :production})
34
+ assert_equal "( RAILS_ENV=production /usr/bin/env rails server )", c.to_command
35
+ end
36
+
37
+ def test_including_the_env_with_multiple_keys
38
+ SSHKit.config = nil
39
+ c = CommandSudoSshForward.new(:rails, 'server', env: {rails_env: :production, foo: 'bar'})
40
+ assert_equal "( RAILS_ENV=production FOO=bar /usr/bin/env rails server )", c.to_command
41
+ end
42
+
43
+ def test_including_the_env_with_string_keys
44
+ SSHKit.config = nil
45
+ c = CommandSudoSshForward.new(:rails, 'server', env: {'FACTER_env' => :production, foo: 'bar'})
46
+ assert_equal "( FACTER_env=production FOO=bar /usr/bin/env rails server )", c.to_command
47
+ end
48
+
49
+ def test_including_the_env_doesnt_addressively_escape
50
+ SSHKit.config = nil
51
+ c = CommandSudoSshForward.new(:rails, 'server', env: {path: '/example:$PATH'})
52
+ assert_equal "( PATH=/example:$PATH /usr/bin/env rails server )", c.to_command
53
+ end
54
+
55
+ def test_global_env
56
+ SSHKit.config = nil
57
+ SSHKit.config.default_env = { default: 'env' }
58
+ c = CommandSudoSshForward.new(:rails, 'server', env: {})
59
+ assert_equal "( DEFAULT=env /usr/bin/env rails server )", c.to_command
60
+ end
61
+
62
+ def test_default_env_is_overwritten_with_locally_defined
63
+ SSHKit.config.default_env = { foo: 'bar', over: 'under' }
64
+ c = CommandSudoSshForward.new(:rails, 'server', env: { over: 'write'})
65
+ assert_equal "( FOO=bar OVER=write /usr/bin/env rails server )", c.to_command
66
+ end
67
+
68
+ def test_working_in_a_given_directory
69
+ c = CommandSudoSshForward.new(:ls, '-l', in: "/opt/sites")
70
+ assert_equal "cd /opt/sites && /usr/bin/env ls -l", c.to_command
71
+ end
72
+
73
+ def test_working_in_a_given_directory_with_env
74
+ c = CommandSudoSshForward.new(:ls, '-l', in: "/opt/sites", env: {a: :b})
75
+ assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l )", c.to_command
76
+ end
77
+
78
+ def test_having_a_host_passed
79
+ refute CommandSudoSshForward.new(:date).host
80
+ assert CommandSudoSshForward.new(:date, host: :foo)
81
+ assert_equal :foo, CommandSudoSshForward.new(host: :foo).host
82
+ end
83
+
84
+ def test_working_as_a_given_user
85
+ c = CommandSudoSshForward.new(:whoami, user: :anotheruser)
86
+ assert_equal "sudo -u anotheruser -- sh -c '/usr/bin/env whoami'", c.to_command
87
+ end
88
+
89
+ def test_working_as_a_given_user_with_a_custom_shell
90
+ c = CommandSudoSshForward.new(:whoami, user: :anotheruser, shell: 'bash -l')
91
+ assert_equal "sudo -u anotheruser -- bash -l -c '/usr/bin/env whoami'", c.to_command
92
+ end
93
+
94
+ def test_working_as_a_given_group
95
+ c = CommandSudoSshForward.new(:whoami, group: :devvers)
96
+ assert_equal "sg devvers -c \\\"/usr/bin/env whoami\\\"", c.to_command
97
+ end
98
+
99
+ def test_working_as_a_given_user_and_group
100
+ c = CommandSudoSshForward.new(:whoami, user: :anotheruser, group: :devvers)
101
+ assert_equal "sudo -u anotheruser -- sh -c 'sg devvers -c \\\"/usr/bin/env whoami\\\"'", c.to_command
102
+ end
103
+
104
+ def test_umask
105
+ SSHKit.config.umask = '007'
106
+ c = CommandSudoSshForward.new(:touch, 'somefile')
107
+ assert_equal "umask 007 && /usr/bin/env touch somefile", c.to_command
108
+ end
109
+
110
+ def test_umask_with_working_directory
111
+ SSHKit.config.umask = '007'
112
+ c = CommandSudoSshForward.new(:touch, 'somefile', in: '/opt')
113
+ assert_equal "cd /opt && umask 007 && /usr/bin/env touch somefile", c.to_command
114
+ end
115
+
116
+ def test_umask_with_working_directory_and_user
117
+ SSHKit.config.umask = '007'
118
+ c = CommandSudoSshForward.new(:touch, 'somefile', in: '/var', user: 'alice')
119
+ assert_equal "cd /var && umask 007 && sudo -u alice -- sh -c '/usr/bin/env touch somefile'", c.to_command
120
+ end
121
+
122
+ def test_umask_with_env_and_working_directory_and_user
123
+ SSHKit.config.umask = '007'
124
+ c = CommandSudoSshForward.new(:touch, 'somefile', user: 'bob', env: {a: 'b'}, in: '/var')
125
+ assert_equal "cd /var && umask 007 && sudo -u bob A=b -- sh -c '/usr/bin/env touch somefile'", c.to_command
126
+ end
127
+
128
+ def test_verbosity_defaults_to_logger_info
129
+ assert_equal Logger::INFO, CommandSudoSshForward.new(:ls).verbosity
130
+ end
131
+
132
+ def test_overriding_verbosity_level_with_a_constant
133
+ assert_equal Logger::DEBUG, CommandSudoSshForward.new(:ls, verbosity: Logger::DEBUG).verbosity
134
+ end
135
+
136
+ def test_overriding_verbosity_level_with_a_symbol
137
+ assert_equal Logger::DEBUG, CommandSudoSshForward.new(:ls, verbosity: :debug).verbosity
138
+ end
139
+
140
+ def test_complete?
141
+ c = CommandSudoSshForward.new(:whoami, raise_on_non_zero_exit: false)
142
+ refute c.complete?
143
+ c.exit_status = 1
144
+ assert c.complete?
145
+ c.exit_status = 0
146
+ assert c.complete?
147
+ end
148
+
149
+ def test_successful?
150
+ c = CommandSudoSshForward.new(:whoami)
151
+ refute c.successful?
152
+ refute c.success?
153
+ c.exit_status = 0
154
+ assert c.successful?
155
+ assert c.success?
156
+ end
157
+
158
+ def test_failure?
159
+ c = CommandSudoSshForward.new(:whoami, raise_on_non_zero_exit: false)
160
+ refute c.failure?
161
+ refute c.failed?
162
+ c.exit_status = 1
163
+ assert c.failure?
164
+ assert c.failed?
165
+ c.exit_status = 127
166
+ assert c.failure?
167
+ assert c.failed?
168
+ end
169
+
170
+ def test_appending_stdout
171
+ c = CommandSudoSshForward.new(:whoami)
172
+ assert c.stdout += "test\n"
173
+ assert_equal "test\n", c.stdout
174
+ end
175
+
176
+ def test_appending_stderr
177
+ c = CommandSudoSshForward.new(:whoami)
178
+ assert c.stderr += "test\n"
179
+ assert_equal "test\n", c.stderr
180
+ end
181
+
182
+ def test_setting_exit_status
183
+ c = CommandSudoSshForward.new(:whoami, raise_on_non_zero_exit: false)
184
+ assert_equal nil, c.exit_status
185
+ assert c.exit_status = 1
186
+ assert_equal 1, c.exit_status
187
+ end
188
+
189
+ def test_command_has_a_guid
190
+ assert CommandSudoSshForward.new(:whoami).uuid
191
+ end
192
+
193
+ def test_wont_take_no_args
194
+ assert_raises ArgumentError do
195
+ CommandSudoSshForward.new
196
+ end
197
+ end
198
+
199
+ def test_command_raises_command_failed_error_when_non_zero_exit
200
+ error = assert_raises SSHKit::CommandSudoSshForward::Failed do
201
+ CommandSudoSshForward.new(:whoami).exit_status = 1
202
+ end
203
+ assert_equal "whoami exit status: 1\nwhoami stdout: Nothing written\nwhoami stderr: Nothing written\n", error.message
204
+ end
205
+
206
+ def test_does_not_forward_ssh_when_host_supports_it_but_sudo_not_required
207
+ host = Host.new('user@example.com')
208
+ host.ssh_options = {forward_agent: true}
209
+ c = CommandSudoSshForward.new(:whoami, host: host, ssh_commands: [:whoami])
210
+ assert_equal "/usr/bin/env whoami", c.to_command
211
+ end
212
+
213
+ def test_does_not_forward_ssh_when_host_does_not_forward_agent
214
+ host = Host.new('user@example.com')
215
+ host.ssh_options = {forward_agent: false, ssh_commands: [:whoami]}
216
+ c = CommandSudoSshForward.new(:whoami, host: host)
217
+ assert_equal "/usr/bin/env whoami", c.to_command
218
+ end
219
+
220
+ def test_forwards_ssh_when_host_supports_it_and_sudo_required
221
+ host = Host.new('user@example.com')
222
+ host.ssh_options = {forward_agent: true}
223
+ c = CommandSudoSshForward.new(:whoami, host: host, user: 'fred', ssh_commands: [:whoami])
224
+ assert_equal(
225
+ 'setfacl -m fred:x $(dirname $SSH_AUTH_SOCK) && '\
226
+ 'setfacl -m fred:rw $SSH_AUTH_SOCK && '\
227
+ "sudo -u fred SSH_AUTH_SOCK=$SSH_AUTH_SOCK -- sh -c '/usr/bin/env whoami'", c.to_command
228
+ )
229
+ end
230
+
231
+ def test_does_not_forward_ssh_when_command_is_not_ssh_command
232
+ host = Host.new('user@example.com')
233
+ host.ssh_options = {forward_agent: true}
234
+ c = CommandSudoSshForward.new(:whoami, host: host, user: 'fred', ssh_commands: [:something_else])
235
+
236
+ assert_equal "sudo -u fred -- sh -c '/usr/bin/env whoami'", c.to_command
237
+ end
238
+ end
239
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sshkit-backends-netssh_global
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Theo Cushion
8
+ - Dennis Ideler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-06-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sshkit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 1.7.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.7.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.11.3
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: 2.12.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.11.3
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.12.0
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: turn
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: mocha
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ description: |-
91
+ A backend to be used in conjunction with Capistrano 3
92
+ and SSHKit to allow deployment on setups where users login as one identity and
93
+ then need to sudo to a different identity for each command.
94
+ email:
95
+ - tcushion@pivotal.io
96
+ - dennis.ideler@fundingcircle.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - ".gitignore"
102
+ - Brewfile
103
+ - Gemfile
104
+ - LICENSE.md
105
+ - README.md
106
+ - Rakefile
107
+ - Vagrantfile
108
+ - lib/sshkit/backends/netssh_global.rb
109
+ - lib/sshkit/backends/version.rb
110
+ - lib/sshkit/command_sudo_ssh_forward.rb
111
+ - sshkit-backends-netssh_global.gemspec
112
+ - test/boxes.json
113
+ - test/functional/backends/test_netssh_global.rb
114
+ - test/helper.rb
115
+ - test/support/vagrant_wrapper.rb
116
+ - test/unit/backends/test_netssh_global.rb
117
+ - test/unit/test_command_sudo_ssh_forward.rb
118
+ homepage: http://github.com/fundingcircle/sshkit-backends-netssh_global
119
+ licenses: []
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.6
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: SSHKit backend for globally sudoing commands
141
+ test_files:
142
+ - test/boxes.json
143
+ - test/functional/backends/test_netssh_global.rb
144
+ - test/helper.rb
145
+ - test/support/vagrant_wrapper.rb
146
+ - test/unit/backends/test_netssh_global.rb
147
+ - test/unit/test_command_sudo_ssh_forward.rb