Linuxセキュリティ設定テスト
spec/server-01/security_spec.rb
04 | it { should be_enforcing } |
07 | describe command('getsebool httpd_can_network_connect') do |
08 | it { should return_stdout /on$/ } |
11 | ### ポイント: 複数繰り返し処理する際のパラメータ定義 ### |
12 | accept_ports = [{:protocol => 'tcp', :port => 22 }, |
13 | {:protocol => 'tcp', :port => 80}, |
14 | {:protocol => 'tcp', :port => 10051}] |
16 | accept_ports.each do |p| # ポイント: 同じ処理をループ処理化 |
18 | it { should have_rule("-A INPUT -p #{p[:protocol]} -m state --state NEW -m #{p[:protocol]} --dport #{p[:port]} -j ACCEPT") } |
【ポイント】
同じSELinux関連の設定の確認であっても、Matcherとして対応していない部分(SELinuxのルール設定)の確認をしたいケースもあります。そういった場合には別途commandというResourceTypeを活用して、任意のコマンドの処理結果をテストする形の記述方法を採ることができます。また、よく利用するResourceTypeやMatcherは独自にカスタム定義して利用することも可能です。Serverspecのカスタマイズについては下記の記事を参照して下さい。
Tech-Sketch「serverspecを環境にあわせてカスタマイズ」
また、ServerspecのテストコードはRubyのコードとして記述します。そのため、iptablesの設定確認など異なるポート番号毎に同じような確認処理を繰り返し行う場合は、Rubyのループ文で回して記述できます。同じ内容の記述をできる限り減らし、メンテナンスしやすい形でテストコードを管理することをお勧めします。このようにRubyの基本的な書き方を知ることでServerspecのコードもより管理しやすくなります。
DNS設定テスト
spec/server-01/dns_spec.rb
03 | dns_server = 'dns-server01' |
05 | ### ポイント: 名前解決に関する設定の確認 ### |
06 | describe file('/etc/resolv.conf') do |
07 | its(:content) { should match /^nameserver #{dns_server}/ } |
10 | describe file('/etc/nsswitch.conf') do |
11 | its(:content) { should match /^hosts:\s*files\s+dns/ } |
14 | ### ポイント: 本当に名前解決できるかの確認 ### |
15 | describe host('server-01') do |
16 | it { should be_resolvable } |
【ポイント】
ServerspecにはfileというResourceTypeがあり、ファイルの中にどういった設定が行われているかのテストを書くことができます。設定としてどういった記述が行われているかをテストすることも必要ですが、その設定が正しく反映されてその通りに稼働しているかをテストすることも重要です。そのため、ここではDNSサーバの設定ファイルの中身の確認だけでなく、実際に名前解決ができるかどうかを確認しています。
Webサーバ(Apache HTTP server)部分のテストコード例
ZabbixのWebGUIを動かすために、Webサーバ部分の正常な稼働状態としては例えば以下のような状態をテストします。
正常な稼働状態
- Apacheのパッケージが導入されている
- Apacheのサービスが正常に稼働している
- ApacheのサービスがTCPの80番ポートでListenしている
- ApacheのプロセスがPHPのモジュールを読み込んでいる
- Apacheの設定ファイルにてZabbixのWebGUI用のPHPファイルを参照できる設定がされている
テストコード
spec/server-01/httpd_spec.rb
03 | ### ポイント: OSの種類毎に異なる処理の分岐 ### |
05 | if os[:family] == 'RedHat' |
06 | describe package('httpd') do |
07 | it { should be_installed } |
09 | describe service('httpd') do |
10 | it { should be_enabled } |
11 | it { should be_running } |
13 | describe file('/etc/httpd/conf/httpd.conf') do |
15 | its(:content) { should match /Include conf.d\/\*\.conf/ } |
17 | describe file('/etc/httpd/conf.d/zabbix.conf') do |
19 | its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ } |
21 | elsif ['Debian', 'Ubuntu'].include?(os[:family]) |
22 | describe package('apache2') do |
23 | it { should be_installed } |
25 | describe service('apache2') do |
26 | it { should be_enabled } |
27 | it { should be_running } |
29 | describe file('/etc/apache2/apache2.conf') do |
31 | its(:content) { should match /IncludeOptional conf-enabled \/\*\.conf/ } |
33 | describe file('/etc/apache2/conf-enabled/zabbix.conf') do |
34 | it { should be_linked_to '/etc/apache2/conf-available/zabbix.conf' } |
35 | its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ } |
44 | it { should be_listening } |
47 | describe command('apachectl -M |grep php5_module') do |
48 | it { should return_stdout /php5_module/ } |
【ポイント】
上記テストコードの例ではApacheのパッケージの導入確認部分において、OSの種別毎に分岐処理を行っています。第1回の記事でも紹介した通り、Serverspecではある程度OSの違いを吸収してテスト処理が実行できるようになっていますが、OS毎にパッケージ名が異なっていたり、特定のOSでのみ実施したい処理がある場合などは、テスト実行対象サーバのOS情報をもとに条件分岐させて処理を記述することが必要となります。