serverspec 0.2.18 → 0.2.19

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.
@@ -92,11 +92,16 @@ module Serverspec
92
92
  end
93
93
 
94
94
  def check_login_shell user, path_to_shell
95
- "grep -w ^#{user} /etc/passwd | cut -f 7 -d ':' | grep -w #{path_to_shell}"
95
+ "getent passwd #{user} | cut -f 7 -d ':' | grep -w #{path_to_shell}"
96
96
  end
97
97
 
98
98
  def check_home_directory user, path_to_home
99
- "grep -w ^#{user} /etc/passwd | cut -f 6 -d ':' | grep -w #{path_to_home}"
99
+ "getent passwd #{user} | cut -f 6 -d ':' | grep -w #{path_to_home}"
100
+ end
101
+
102
+ def check_authorized_key user, key
103
+ key.sub!(/\s+\S*$/, '') if key.match(/^\S+\s+\S+\s+\S*$/)
104
+ "grep -w '#{key}' ~#{user}/.ssh/authorized_keys"
100
105
  end
101
106
 
102
107
  def check_iptables_rule rule, table=nil, chain=nil
@@ -8,6 +8,10 @@ module Serverspec
8
8
  def check_installed package
9
9
  "/usr/bin/eix #{package} --installed"
10
10
  end
11
+
12
+ def check_running service
13
+ "/etc/init.d/#{service} status"
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -64,6 +64,18 @@ module Serverspec
64
64
  def check_belonging_group user, group
65
65
  "id -Gn #{user} | grep #{group}"
66
66
  end
67
+
68
+ def check_gid group, gid
69
+ "getent group | grep ^#{group}: | cut -f 3 -d ':' | grep -w #{gid}"
70
+ end
71
+
72
+ def check_home_directory user, path_to_home
73
+ "getent passwd #{user} | cut -f 6 -d ':' | grep -w #{path_to_home}"
74
+ end
75
+
76
+ def check_login_shell user, path_to_shell
77
+ "getent passwd #{user} | cut -f 7 -d ':' | grep -w #{path_to_shell}"
78
+ end
67
79
  end
68
80
  end
69
81
  end
@@ -6,7 +6,7 @@ module Serverspec
6
6
  # Linux kernel parameters
7
7
  %w( abi crypto debug dev fs kernel net sunrpc vm ).each do |param|
8
8
  if description_args.match(/^#{param}\./)
9
- ret = backend(Serverspec::Commands::Base).do_check("sysctl -q -n #{description_args}")
9
+ ret = backend(Serverspec::Commands::Base).do_check("/sbin/sysctl -q -n #{description_args}")
10
10
  val = ret[:stdout].strip
11
11
  val = val.to_i if val.match(/^\d+$/)
12
12
  subject = Serverspec::Subject.new
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_authorized_key do |key|
2
+ match do |user|
3
+ backend.check_authorized_key(example, user, key)
4
+ end
5
+ end
@@ -18,6 +18,7 @@ require 'serverspec/matchers/have_gid'
18
18
  require 'serverspec/matchers/have_uid'
19
19
  require 'serverspec/matchers/have_login_shell'
20
20
  require 'serverspec/matchers/have_home_directory'
21
+ require 'serverspec/matchers/have_authorized_key'
21
22
  require 'serverspec/matchers/have_iptables_rule'
22
23
  require 'serverspec/matchers/get_stdout'
23
24
  require 'serverspec/matchers/be_zfs'
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.2.18"
2
+ VERSION = "0.2.19"
3
3
  end
@@ -125,12 +125,29 @@ end
125
125
 
126
126
  describe 'have_login_shell', :os => :debian do
127
127
  subject { commands.check_login_shell('root', '/bin/bash') }
128
- it { should eq "grep -w ^root /etc/passwd | cut -f 7 -d ':' | grep -w /bin/bash" }
128
+ it { should eq "getent passwd root | cut -f 7 -d ':' | grep -w /bin/bash" }
129
129
  end
130
130
 
131
131
  describe 'have_home_directory', :os => :debian do
132
132
  subject { commands.check_home_directory('root', '/root') }
133
- it { should eq "grep -w ^root /etc/passwd | cut -f 6 -d ':' | grep -w /root" }
133
+ it { should eq "getent passwd root | cut -f 6 -d ':' | grep -w /root" }
134
+ end
135
+
136
+ describe 'have_authorized_key', :os => :debian do
137
+ key = "ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH"
138
+
139
+ context 'with commented publickey' do
140
+ commented_key = key + " foo@bar.local"
141
+ subject { commands.check_authorized_key('root', commented_key) }
142
+ describe 'when command insert publickey is removed comment' do
143
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
144
+ end
145
+ end
146
+
147
+ context 'with uncomented publickey' do
148
+ subject { commands.check_authorized_key('root', key) }
149
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
150
+ end
134
151
  end
135
152
 
136
153
  describe 'check_ipatbles', :os => :debian do
@@ -35,6 +35,7 @@ describe 'Serverspec matchers of Debian family', :os => :debian do
35
35
  it_behaves_like 'support have_uid matcher', 'root', 0
36
36
  it_behaves_like 'support have_login_shell matcher', 'root', '/bin/bash'
37
37
  it_behaves_like 'support have_home_directory matcher', 'root', '/root'
38
+ it_behaves_like 'support have_authorized_key matcher', 'root', 'ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH foo@bar.local'
38
39
 
39
40
  it_behaves_like 'support have_iptables_rule matcher', '-P INPUT ACCEPT'
40
41
  it_behaves_like 'support have_iptables_rule.with_table.with_chain matcher', '-P INPUT ACCEPT', 'mangle', 'INPUT'
@@ -37,7 +37,7 @@ end
37
37
 
38
38
  describe 'check_running', :os => :gentoo do
39
39
  subject { commands.check_running('httpd') }
40
- it { should eq 'service httpd status' }
40
+ it { should eq '/etc/init.d/httpd status' }
41
41
  end
42
42
 
43
43
  describe 'check_running_under_supervisor', :os => :gentoo do
@@ -124,12 +124,29 @@ end
124
124
 
125
125
  describe 'have_login_shell', :os => :gentoo do
126
126
  subject { commands.check_login_shell('root', '/bin/bash') }
127
- it { should eq "grep -w ^root /etc/passwd | cut -f 7 -d ':' | grep -w /bin/bash" }
127
+ it { should eq "getent passwd root | cut -f 7 -d ':' | grep -w /bin/bash" }
128
128
  end
129
129
 
130
130
  describe 'have_home_directory', :os => :gentoo do
131
131
  subject { commands.check_home_directory('root', '/root') }
132
- it { should eq "grep -w ^root /etc/passwd | cut -f 6 -d ':' | grep -w /root" }
132
+ it { should eq "getent passwd root | cut -f 6 -d ':' | grep -w /root" }
133
+ end
134
+
135
+ describe 'have_authorized_key', :os => :gentoo do
136
+ key = "ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH"
137
+
138
+ context 'with commented publickey' do
139
+ commented_key = key + " foo@bar.local"
140
+ subject { commands.check_authorized_key('root', commented_key) }
141
+ describe 'when command insert publickey is removed comment' do
142
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
143
+ end
144
+ end
145
+
146
+ context 'with uncomented publickey' do
147
+ subject { commands.check_authorized_key('root', key) }
148
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
149
+ end
133
150
  end
134
151
 
135
152
  describe 'check_ipatbles', :os => :gentoo do
@@ -36,6 +36,7 @@ describe 'Serverspec matchers of Gentoo family', :os => :gentoo do
36
36
  it_behaves_like 'support have_uid matcher', 'root', 0
37
37
  it_behaves_like 'support have_login_shell matcher', 'root', '/bin/bash'
38
38
  it_behaves_like 'support have_home_directory matcher', 'root', '/root'
39
+ it_behaves_like 'support have_authorized_key matcher', 'root', 'ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH foo@bar.local'
39
40
 
40
41
  it_behaves_like 'support have_iptables_rule matcher', '-P INPUT ACCEPT'
41
42
  it_behaves_like 'support have_iptables_rule.with_table.with_chain matcher', '-P INPUT ACCEPT', 'mangle', 'INPUT'
@@ -124,12 +124,29 @@ end
124
124
 
125
125
  describe 'have_login_shell', :os => :redhat do
126
126
  subject { commands.check_login_shell('root', '/bin/bash') }
127
- it { should eq "grep -w ^root /etc/passwd | cut -f 7 -d ':' | grep -w /bin/bash" }
127
+ it { should eq "getent passwd root | cut -f 7 -d ':' | grep -w /bin/bash" }
128
128
  end
129
129
 
130
130
  describe 'have_home_directory', :os => :redhat do
131
131
  subject { commands.check_home_directory('root', '/root') }
132
- it { should eq "grep -w ^root /etc/passwd | cut -f 6 -d ':' | grep -w /root" }
132
+ it { should eq "getent passwd root | cut -f 6 -d ':' | grep -w /root" }
133
+ end
134
+
135
+ describe 'have_authorized_key', :os => :redhat do
136
+ key = "ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH"
137
+
138
+ context 'with commented publickey' do
139
+ commented_key = key + " foo@bar.local"
140
+ subject { commands.check_authorized_key('root', commented_key) }
141
+ describe 'when command insert publickey is removed comment' do
142
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
143
+ end
144
+ end
145
+
146
+ context 'with uncomented publickey' do
147
+ subject { commands.check_authorized_key('root', key) }
148
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
149
+ end
133
150
  end
134
151
 
135
152
  describe 'check_ipatbles', :os => :redhat do
@@ -37,6 +37,7 @@ describe 'Serverspec matchers of Red Hat family', :os => :redhat do
37
37
  it_behaves_like 'support have_uid matcher', 'root', 0
38
38
  it_behaves_like 'support have_login_shell matcher', 'root', '/bin/bash'
39
39
  it_behaves_like 'support have_home_directory matcher', 'root', '/root'
40
+ it_behaves_like 'support have_authorized_key matcher', 'root', 'ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH foo@bar.local'
40
41
 
41
42
  it_behaves_like 'support have_iptables_rule matcher', '-P INPUT ACCEPT'
42
43
  it_behaves_like 'support have_iptables_rule.with_table.with_chain matcher', '-P INPUT ACCEPT', 'mangle', 'INPUT'
@@ -114,7 +114,7 @@ end
114
114
 
115
115
  describe 'have_gid', :os => :solaris do
116
116
  subject { commands.check_gid('root', 0) }
117
- it { should eq "getent group | grep -w ^root | cut -f 3 -d ':' | grep -w 0" }
117
+ it { should eq "getent group | grep ^root: | cut -f 3 -d ':' | grep -w 0" }
118
118
  end
119
119
 
120
120
  describe 'have_uid', :os => :solaris do
@@ -124,12 +124,29 @@ end
124
124
 
125
125
  describe 'have_login_shell', :os => :solaris do
126
126
  subject { commands.check_login_shell('root', '/bin/bash') }
127
- it { should eq "grep -w ^root /etc/passwd | cut -f 7 -d ':' | grep -w /bin/bash" }
127
+ it { should eq "getent passwd root | cut -f 7 -d ':' | grep -w /bin/bash" }
128
128
  end
129
129
 
130
130
  describe 'have_home_directory', :os => :solaris do
131
131
  subject { commands.check_home_directory('root', '/root') }
132
- it { should eq "grep -w ^root /etc/passwd | cut -f 6 -d ':' | grep -w /root" }
132
+ it { should eq "getent passwd root | cut -f 6 -d ':' | grep -w /root" }
133
+ end
134
+
135
+ describe 'have_authorized_key', :os => :solaris do
136
+ key = "ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH"
137
+
138
+ context 'with commented publickey' do
139
+ commented_key = key + " foo@bar.local"
140
+ subject { commands.check_authorized_key('root', commented_key) }
141
+ describe 'when command insert publickey is removed comment' do
142
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
143
+ end
144
+ end
145
+
146
+ context 'with uncomented publickey' do
147
+ subject { commands.check_authorized_key('root', key) }
148
+ it { should eq "grep -w '#{key}' ~root/.ssh/authorized_keys" }
149
+ end
133
150
  end
134
151
 
135
152
  describe 'check_zfs', :os => :solaris do
@@ -36,6 +36,7 @@ describe 'Serverspec matchers of Solaris family', :os => :solaris do
36
36
  it_behaves_like 'support have_uid matcher', 'root', 0
37
37
  it_behaves_like 'support have_login_shell matcher', 'root', '/bin/bash'
38
38
  it_behaves_like 'support have_home_directory matcher', 'root', '/root'
39
+ it_behaves_like 'support have_authorized_key matcher', 'root', 'ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH foo@bar.local'
39
40
 
40
41
  it_behaves_like 'support be_zfs matcher', 'rpool'
41
42
  it_behaves_like 'support be_zfs.property matcher', 'rpool', { 'mountpoint' => '/rpool' }
@@ -370,6 +370,22 @@ shared_examples_for 'support have_home_directory matcher' do |user, path_to_home
370
370
  end
371
371
  end
372
372
 
373
+ shared_examples_for 'support have_authorized_key matcher' do |user, key|
374
+ describe 'have_authorized_key' do
375
+ describe user do
376
+ it { should have_authorized_key key }
377
+ end
378
+
379
+ describe user do
380
+ it { should_not have_authorized_key 'invalid-publickey' }
381
+ end
382
+
383
+ describe 'dummyuser' do
384
+ it { should_not have_authorized_key 'invalid-publickey' }
385
+ end
386
+ end
387
+ end
388
+
373
389
  shared_examples_for 'support have_iptables_rule matcher' do |rule|
374
390
  describe 'have_iptables_rule' do
375
391
  describe 'iptables' do
@@ -891,4 +907,4 @@ shared_examples_for 'support linux kernel parameter checking with regexp' do |pa
891
907
  its(:value) { should_not match /invalid-string/ }
892
908
  end
893
909
  end
894
- end
910
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 18
10
- version: 0.2.18
9
+ - 19
10
+ version: 0.2.19
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gosuke Miyashita
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-05-01 00:00:00 +09:00
18
+ date: 2013-05-02 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -151,6 +151,7 @@ files:
151
151
  - lib/serverspec/matchers/belong_to_group.rb
152
152
  - lib/serverspec/matchers/contain.rb
153
153
  - lib/serverspec/matchers/get_stdout.rb
154
+ - lib/serverspec/matchers/have_authorized_key.rb
154
155
  - lib/serverspec/matchers/have_cron_entry.rb
155
156
  - lib/serverspec/matchers/have_gid.rb
156
157
  - lib/serverspec/matchers/have_home_directory.rb