specinfra-backend-extension-ssh_su 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52dffdc170754fa45004d759043258607a1a021e
4
+ data.tar.gz: 549874669dedd12abddb0bfd95cec8bbe0efb001
5
+ SHA512:
6
+ metadata.gz: 1d6fbd5879bb7b673636f049547dbb80282a462b15285c627b5557d6cea780388708ed4494441fc5314840e183076f4aba6d621d125df92536b4904134711d85
7
+ data.tar.gz: 92905e3611b85e1eb8e9b2535c179b72cde01e1bf477e62056beaa6cb46d4ece70f88a88f5c4146b8791f097daf4c22ee1ea1da600acafcf21ab44a4d18414df
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in specinfra-backend-extension-ssh_su.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Specinfra::Backend::Extension::SshSu
2
+
3
+ SSH + su backend for specinfra.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'specinfra-backend-extension-ssh_su'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install specinfra-backend-extension-ssh_su
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'specinfra'
25
+ require 'specinfra/helper/set'
26
+ require 'specinfra/backend/extension/ssh_su'
27
+ require 'highline/import'
28
+
29
+ include Specinfra::Helper::Set
30
+ include Specinfra::Helper::Os
31
+
32
+ host = ARGV[0]
33
+
34
+ set :host, host
35
+ set :backend, :ssh_su
36
+
37
+ options = Net::SSH::Config.for(host)
38
+ options[:user] = ask("Enter ssh user: ")
39
+ options[:password] = ask("Enter ssh password: ") { |q| q.echo = false }
40
+ set :ssh_options, options
41
+
42
+ set :su_password, ask("Enter su password: ") { |q| q.echo = false }
43
+
44
+ puts Specinfra::Runner::run_command('whoami').stdout
45
+ puts Specinfra::Runner::run_command('cat /etc/passwd | grep `whoami`').stdout
46
+
47
+ set :os, os
48
+ if Specinfra.configuration.os[:family] == 'redhat'
49
+ puts Specinfra::Runner::install_package('epel-release').stdout
50
+ end
51
+
52
+ puts Specinfra::Runner::install_package('nginx').stdout
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/specinfra-backend-extension-ssh_su/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,117 @@
1
+ require "specinfra/backend/extension/ssh_su/version"
2
+ require "specinfra/backend/ssh"
3
+
4
+ module Specinfra
5
+ module Backend
6
+ module Extension
7
+ module SshSu
8
+ end
9
+ end
10
+
11
+ class SshSu < Ssh
12
+ def initialize
13
+ Specinfra.configuration.disable_sudo = true
14
+ end
15
+
16
+ def run_command(cmd, opt={})
17
+ cmd = build_command(cmd)
18
+ res = super(cmd)
19
+
20
+ if su?
21
+ stdout = res.instance_variable_get(:@stdout).gsub!(/\A\n/, "")
22
+
23
+ if @example
24
+ @example.metadata[:stdout] = stdout
25
+ end
26
+ end
27
+
28
+ res
29
+ end
30
+
31
+ def build_command(cmd)
32
+ cmd = super(cmd)
33
+ if su?
34
+ su_user = Specinfra.configuration.su_user || 'root'
35
+ cmd = "#{su} - #{su_user} -c #{cmd.shellescape}"
36
+ end
37
+ cmd
38
+ end
39
+
40
+ private
41
+ def prompt
42
+ Specinfra.configuration.su_prompt || 'Password: '
43
+ end
44
+
45
+ def ssh_exec!(command)
46
+ stdout_data = ''
47
+ stderr_data = ''
48
+ exit_status = nil
49
+ exit_signal = nil
50
+
51
+ if Specinfra.configuration.ssh.nil?
52
+ Specinfra.configuration.ssh = create_ssh
53
+ end
54
+
55
+ ssh = Specinfra.configuration.ssh
56
+ ssh.open_channel do |channel|
57
+ if Specinfra.configuration.su_password or Specinfra.configuration.request_pty
58
+ channel.request_pty do |ch, success|
59
+ abort "Could not obtain pty " if !success
60
+ end
61
+ end
62
+ channel.exec("#{command}") do |ch, success|
63
+ abort "FAILED: couldn't execute command (ssh.channel.exec)" if !success
64
+ channel.on_data do |ch, data|
65
+ if data.match /^#{prompt}/
66
+ channel.send_data "#{Specinfra.configuration.su_password}\n"
67
+ else
68
+ stdout_data += data
69
+ end
70
+ end
71
+
72
+ channel.on_extended_data do |ch, type, data|
73
+ if data.match /(standard in must be a tty|must be run from a terminal)/
74
+ abort 'Please write "set :request_pty, true" in your spec_helper.rb or other appropriate file.'
75
+ end
76
+
77
+ stderr_data += data
78
+ end
79
+
80
+ channel.on_request("exit-status") do |ch, data|
81
+ exit_status = data.read_long
82
+ end
83
+
84
+ channel.on_request("exit-signal") do |ch, data|
85
+ exit_signal = data.read_long
86
+ end
87
+ end
88
+ end
89
+ ssh.loop
90
+ { :stdout => stdout_data, :stderr => stderr_data, :exit_status => exit_status, :exit_signal => exit_signal }
91
+ end
92
+
93
+ def su
94
+ if su_path = Specinfra.configuration.su_path
95
+ su_path += '/su'
96
+ else
97
+ su_path = 'su'
98
+ end
99
+
100
+ su_options = Specinfra.configuration.su_options
101
+ if su_options
102
+ su_options = su_options.shelljoin if su_options.is_a?(Array)
103
+ su_options = ' ' + su_options
104
+ end
105
+
106
+ "#{su_path.shellescape}#{su_options}"
107
+ end
108
+
109
+ def su?
110
+ user = Specinfra.configuration.ssh_options[:user]
111
+ disable_su = Specinfra.configuration.disable_su
112
+ user != 'root' && !disable_su
113
+ end
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,9 @@
1
+ module Specinfra
2
+ module Backend
3
+ module Extension
4
+ module SshSu
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ set :backend, :ssh_su
4
+
5
+ describe Specinfra::Backend::Ssh do
6
+ describe '#build_command' do
7
+ context 'with root user' do
8
+ before do
9
+ RSpec.configure do |c|
10
+ set :ssh_options, :user => 'root'
11
+ c.ssh = double(:ssh, Specinfra.configuration.ssh_options)
12
+ end
13
+ end
14
+
15
+ it 'should not prepend sudo' do
16
+ expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/bin/sh -c test\ -f\ /etc/passwd'
17
+ end
18
+
19
+ it 'should escape special characters' do
20
+ expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq '/bin/sh -c test\ \!\ -f\ /etc/selinux/config\ \|\|\ \(getenforce\ \|\ grep\ -i\ --\ disabled\ \&\&\ grep\ -i\ --\ \^SELINUX\=disabled\$\ /etc/selinux/config\)'
21
+ end
22
+ end
23
+
24
+ context 'with non-root user' do
25
+ before do
26
+ RSpec.configure do |c|
27
+ set :ssh_options, :user => 'foo'
28
+ c.ssh = double(:ssh, Specinfra.configuration.ssh_options)
29
+ end
30
+ end
31
+
32
+ it 'should prepend su' do
33
+ expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq 'su - root -c /bin/sh\\ -c\\ test\\\\\\ -f\\\\\\ /etc/passwd'
34
+ end
35
+
36
+ it 'should escape special characters' do
37
+ expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq 'su - root -c /bin/sh\\ -c\\ test\\\\\\ \\\\\\!\\\\\\ -f\\\\\\ /etc/selinux/config\\\\\\ \\\\\\|\\\\\\|\\\\\\ \\\\\\(getenforce\\\\\\ \\\\\\|\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ disabled\\\\\\ \\\\\\&\\\\\\&\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ \\\\\\^SELINUX\\\\\\=disabled\\\\\\$\\\\\\ /etc/selinux/config\\\\\\)'
38
+ end
39
+ end
40
+
41
+ context 'with custom su user' do before do
42
+ RSpec.configure do |c|
43
+ set :ssh_options, :user => 'foo'
44
+ c.ssh = double(:ssh, Specinfra.configuration.ssh_options)
45
+ c.su_user = 'bar'
46
+ end
47
+ end
48
+
49
+ after do
50
+ RSpec.configure do |c|
51
+ c.su_user = nil
52
+ end
53
+ end
54
+
55
+ it 'command pattern 1a' do
56
+ expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq 'su - bar -c /bin/sh\\ -c\\ test\\\\\\ -f\\\\\\ /etc/passwd'
57
+ end
58
+
59
+ it 'command pattern 2a' do
60
+ expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq 'su - bar -c /bin/sh\\ -c\\ test\\\\\\ \\\\\\!\\\\\\ -f\\\\\\ /etc/selinux/config\\\\\\ \\\\\\|\\\\\\|\\\\\\ \\\\\\(getenforce\\\\\\ \\\\\\|\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ disabled\\\\\\ \\\\\\&\\\\\\&\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ \\\\\\^SELINUX\\\\\\=disabled\\\\\\$\\\\\\ /etc/selinux/config\\\\\\)'
61
+ end
62
+ end
63
+
64
+ context 'with custom su path' do before do
65
+ RSpec.configure do |c|
66
+ set :ssh_options, :user => 'foo'
67
+ c.ssh = double(:ssh, Specinfra.configuration.ssh_options)
68
+ c.su_path = '/usr/local/bin'
69
+ end
70
+ end
71
+
72
+ after do
73
+ RSpec.configure do |c|
74
+ c.sudo_path = nil
75
+ end
76
+ end
77
+
78
+ it 'command pattern 1a' do
79
+ expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/usr/local/bin/su - root -c /bin/sh\\ -c\\ test\\\\\\ -f\\\\\\ /etc/passwd'
80
+ end
81
+
82
+ it 'command pattern 2a' do
83
+ expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq '/usr/local/bin/su - root -c /bin/sh\\ -c\\ test\\\\\\ \\\\\\!\\\\\\ -f\\\\\\ /etc/selinux/config\\\\\\ \\\\\\|\\\\\\|\\\\\\ \\\\\\(getenforce\\\\\\ \\\\\\|\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ disabled\\\\\\ \\\\\\&\\\\\\&\\\\\\ grep\\\\\\ -i\\\\\\ --\\\\\\ \\\\\\^SELINUX\\\\\\=disabled\\\\\\$\\\\\\ /etc/selinux/config\\\\\\)'
84
+ end
85
+ end
86
+
87
+ context 'without su' do
88
+ before do
89
+ RSpec.configure do |c|
90
+ set :ssh_options, :user => 'foo'
91
+ c.ssh = double(:ssh, Specinfra.configuration.ssh_options)
92
+ c.disable_su = true
93
+ end
94
+ end
95
+
96
+ after do
97
+ RSpec.configure do |c|
98
+ c.disable_su = false
99
+ end
100
+ end
101
+
102
+ it 'command pattern 1b' do
103
+ expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/bin/sh -c test\ -f\ /etc/passwd'
104
+ end
105
+
106
+ it 'command pattern 2b' do
107
+ expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq '/bin/sh -c test\ \!\ -f\ /etc/selinux/config\ \|\|\ \(getenforce\ \|\ grep\ -i\ --\ disabled\ \&\&\ grep\ -i\ --\ \^SELINUX\=disabled\$\ /etc/selinux/config\)'
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,24 @@
1
+ require 'specinfra'
2
+ require 'specinfra/backend/extension/ssh_su'
3
+ require 'rspec/mocks/standalone'
4
+ require 'rspec/its'
5
+ require 'specinfra/helper/set'
6
+ include Specinfra::Helper::Set
7
+
8
+ set :backend, :ssh_su
9
+
10
+ module Specinfra
11
+ module Backend
12
+ class Ssh
13
+ def run_command(cmd, opts={})
14
+ CommandResult.new :stdout => nil, :exit_status => 0
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ RSpec.configure do |c|
21
+ c.add_setting :su_user, :deafult => nil
22
+ c.add_setting :su_path, :deafult => nil
23
+ c.add_setting :disable_su, :deafult => false
24
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'specinfra/backend/extension/ssh_su/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "specinfra-backend-extension-ssh_su"
8
+ spec.version = Specinfra::Backend::Extension::SshSu::VERSION
9
+ spec.authors = ["hayajo"]
10
+ spec.email = ["hayajo@cpan.org"]
11
+ spec.summary = %q{SSH + su backend for specinfra.}
12
+ spec.description = %q{SSH + su backend for specinfra.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "specinfra", "~> 2.11.5"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-its"
27
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: specinfra-backend-extension-ssh_su
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hayajo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: specinfra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.11.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.11.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: SSH + su backend for specinfra.
84
+ email:
85
+ - hayajo@cpan.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/specinfra/backend/extension/ssh_su.rb
96
+ - lib/specinfra/backend/extension/ssh_su/version.rb
97
+ - spec/build_command_spec.rb
98
+ - spec/spec_helper.rb
99
+ - specinfra-backend-extension-ssh_su.gemspec
100
+ homepage: ''
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.5
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: SSH + su backend for specinfra.
124
+ test_files:
125
+ - spec/build_command_spec.rb
126
+ - spec/spec_helper.rb