fbpx

[和訳] Chef Audit Mode イントロダクション #getchef #serverspec

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

本稿は Chef Audit Mode Introduction (2015/05/06) の和訳です。

先にアナウンスしたaudit-cis Cookbook (訳注:和訳)など、Chefバージョン12.1.0で導入されたAudit Mode機能を取り上げてきました。Audit Modeは新しいDSLヘルパーを使って、ChefのRecipe中にCustom Rule (Control)を書くことができます。James CaseyはChefConf 2015 talk, “Compliance At Velocity,”において、背景と理由について詳しく説明しました。ここでは、この機能を試してみたいユーザについていくつかチップスを共有したいと思います。

まず、ChefDKをバージョン0.5.0に更新する必要があります。これはchef-clientにAudit Modeを設定できるようにするtest-kitchenのバージョンを含んでいます。


curl -L https://chef.io/chef/install.sh | sudo bash -s -- -P chefdk

次に、Audit Modeのテストのための新しいCookbookを作成します。


chef generate cookbook audit-test
cd audit-test

そして、audit-test Cookbookの.kitchen.ymlを変更します。


driver:
name: vagrant
provisioner:
name: chef_zero
client_rb:
audit_mode: :audit_only
platforms:
- name: ubuntu-12.04
- name: centos-6.5
suites:
- name: default
run_list:
- recipe[audit-test::default]
attributes:

生成された.kitchen.ymlに、Audit Modeを有効にするclient_rbをprovisioner設定に追加します。:audit_onlyはRubyのSymbol型の文法を使わなければいけないことに注意してください。audit_modeの有効な値は:enabled:audit_only:disabledです。これは生成される設定ファイル(/tmp/kitchen/client.rb)実際のRubyのSymbol型に変換されます。


audit_mode :audit_only

では、テスト用のControl Ruleを書いてみましょう。Ubuntu 12.04にSSHで接続するデフォルトの.kitchen.ymlを使うので、SSHが起動していて、22番ポートで待ち受けしていると仮定します。次のControlはそれが真であると仮定します。


control_group 'Blog Post Examples' do
control 'SSH' do
it 'should be listening on port 22' do
expect(port(22)).to be_listening
end
end
end

kitchen converge ubuntuを実行してChefを実行しましょう。ただしその後にVMを削除はしません。他の例に使うからです。次はChefの実行によるAuditフェイズの出力です。


% kitchen converge ubuntu
Synchronizing Cookbooks:
- audit-test
Compiling Cookbooks...
Starting audit phase

Blog Post Examples
SSH
should be listening on port 22

Finished in 0.10453 seconds (files took 0.37536 seconds to load)
1 example, 0 failures
Auditing complete

やった! デフォルトではNodeがControlに適合すると仮定していたからです。Controlが失敗するのはどんなときでしょうか? やってみましょう。SSHが既に動作しているので、sshdの設定を使いましょう。デフォルトのVagrant base boxではrootログインを許可するようになっています。


PermitRootLogin yes

しかし、セキュリティポリシーからこれをnoとする決まりなので、次のように監査します。


control_group 'Blog Post Examples' do
control 'SSH' do
it 'should be listening on port 22' do
expect(port(22)).to be_listening
end

it 'disables root logins over ssh' do
expect(file('/etc/ssh/sshd_config').content).to contain('PermitRootLogin no')
end
end
end

kitchen converge ubuntuを再実行すると、検証が失敗します。


Starting audit phase

Blog Post Examples
SSH
should be listening on port 22
disables root logins over ssh (FAILED - 1)

Failures:

1) Blog Post Examples SSH disables root logins over ssh
Failure/Error: expect(file('/etc/ssh/sshd_config').content).to contain('PermitRootLogin no')
expected File "/etc/ssh/sshd_config" to contain "PermitRootLogin no"
# /tmp/kitchen/cache/cookbooks/audit-test/recipes/default.rb:8:in `block (3 levels) in from_file'

Finished in 0.13067 seconds (files took 0.32089 seconds to load)
2 examples, 1 failure

Failed examples:

rspec # Blog Post Examples SSH disables root logins over ssh
[2015-04-04T03:29:41+00:00] ERROR: Audit phase failed with error message: Audit phase found failures - 1/2 controls failed

Audit phase exception:
Audit phase found failures - 1/2 controls failed

失敗したら、失敗に関する前後関係の情報が得られます。失敗したRecipeの行番号や、(ここでは省略されていますが)スタックトレースといった、デバッグに必要な情報です。このテストを修正するには、あるべき状態に設定ファイルを単純に修正するか、しかるべき値に設定するようChefでファイルを管理します。いずれかの方法によってファイルを更新した後、検証をパスし、すべてがよくなるでしょう。

ポリシーを監査するために必要なit検証ルールに多くのcontrol_groupcontrolブロックを追加できます。もし多数の検証項目があると、失敗した場合にすべての出力を追うことが難しくなるでしょう。ChefのAudit ModeはRSpecをベースとしたServerspecをベースとしています。デバッグに関係するcontrolブロックやit文のみを実行するためのRSpecのfilter_tag機能を設定できます。これを使うには、control_group内にRSpec.configurationブロックが必要になります。Audit Modeの実装方式により、control_groupの外では設定できません。

例えば次のようにすると、rootログインの設定をデバッグできるでしょう。


control_group 'Blog Post Examples' do
::RSpec.configure do |c|
c.filter_run focus: true
end

control 'SSH' do
it 'should be listening on port 22' do
expect(port(22)).to be_listening
end

it 'disables root logins over ssh', focus: true do
expect(file('/etc/ssh/sshd_config').content).to contain('PermitRootLogin no')
end
end
end

これのカギはitブロックにてfocus: true (もしハッシュが好みなら :focus => true)を引数に渡していることです。これはcontrolブロックでも次のように使えます。


control 'SSH', focus: true do
it 'does stuff...'
end

これでkitchen converge ubuntuを実行すると、この検証のみを見られます。


Starting audit phase

Blog Post Examples
SSH
disables root logins over ssh (FAILED - 1)

Failures:

1) Blog Post Examples SSH disables root logins over ssh
Failure/Error: expect(file('/etc/ssh/sshd_config')content).to contain('PermitRootLogin no')

この例は単純すぎてfocusを使うまでもないですが、完全なセキュリティポリシーを確認するAudit Modeを実装したら、数十から数百のControlとなっているでしょう。

Audit Modeはまだ開発中の機能ですが、システムをさまざまな監査要求に適合させる必要のある組織にとって大変価値があると考えます。また、Audit ModeはChefのRecipeによるResourceの収束なしでも実行できるので、Chefを実装する前の既存のシステムの状態を評価する用途にもとても有効でしょう。

新規CTA