こんにちは!beardです!
今回は、CentOS6.5のインスタンスでServerspecによる確認を行います。
EC2でCentOSを使ってWEBサーバにアクセスできるようにするためには、SELinuxとiptablesを無効にする必要があります。
この2つが無効になっているか、Serverspecで確認する方法をご紹介します。
少し気になった点にも言及しておきます。
1. SELinuxを無効にする。
以下のファイルを編集します。
| 1 2 3 4 5 6 7 8 9 10 11 | vi /etc/sysconfig/selinux This file controls the state of SELinux on the system. SELINUX= can take one of these three values:     enforcing - SELinux security policy is enforced.     permissive - SELinux prints warnings instead of enforcing.     disabled - No SELinux policy is loaded. SELINUX=disabled ←「disabledに変更する」 SELINUXTYPE= can take one of these two values:     targeted - Targeted processes are protected,     mls - Multi Level Security protection. SELINUXTYPE=targeted | 
2. iptablesを無効にします。
| 1 2 3 4 | service iptables stop ←サービス停止 chkconfig iptables off ←サービス自動起動無効化 chkconfig iptables –list iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off | 
3. server-initコマンドを実行してテストの準備をします。
できあがったServerspecの設定ファイル「httpd_spec.rb」を編集します。
| 1 2 3 4 5 6 7 8 9 10 11 | require 'spec_helper' # SELinux should be disabled describe selinux do     it { should be_disabled } end #iptables無効化 describe service('iptables') do     it { should_not be_enabled }     it { should_not be_running } end | 
4. 編集したらテストを実行します。
| 1 2 3 4 5 6 7 | rake spec (in /usr/local/spec/i-26b8133f) /opt/ruby-2.0.0/bin/ruby -S rspec spec/localhost/httpd_spec.rb ... Finished in 0.16944 seconds 3 examples, 0 failures | 
5. 0failuresとなっているので成功です。
簡単な解説
SELinuxはServerspecにおいてResouceTypeとして規定されているので「無効な状態」か確認したい場合は「should be_disabled」が使えます。
しかし、iptablesはResouceTypeとして既定されているのが、IPフィルタリングと変換のルールだけのようです。
従ってserviceとして判断しています。最初は以下のようにshould be_disabledと書いてしまってエラーになりました。
| 1 2 3 4 | #ダメな例 describe iptables do     it { should be_disabled } end | 
いかがでしたか?
今回はサービスが無効になっているか確認を行いました。
ServerspecはOSによって記述の内容を変える必要はありますが、文法は変わりません。
次回もお楽しみに!!

