下面我用实战视角给你讲清楚:如何用 Chef 做“合规运维”(Compliance as Code)。不堆术语,重点在“能落地”。
目标:
用代码定义“服务器应该长什么样”,自动检查 + 自动修复,确保始终符合安全/运维规范。
典型合规场景:
| 组件 | 作用 |
|---|---|
| Chef Cookbook | 定义“应该怎么做” |
| InSpec | 定义“应该怎么检查”(合规即代码) |
| Chef Automate | 合规报告、审计、可视化 |
| chef-client | 执行 + 修复 |
✅ 最佳实践组合:
Cookbook + InSpec + Chef Automate
不符合就改
# cookbook/recipes/sshd.rb
service 'sshd' do
action [:enable, :start]
end
file '/etc/ssh/sshd_config' do
content 'PermitRootLogin no'
notifies :restart, 'service[sshd]'
end
✅ 适合:配置管理、基线加固
只报告,不修复(审计合规)
# inspec/controls/sshd.rb
control 'ssh-1' do
title 'Root login must be disabled'
describe ssh_config do
its('PermitRootLogin') { should eq 'no' }
end
end
运行:
inspec exec sshd.rb -t ssh://server
✅ 适合:等保、ISO、PCI-DSS
Cookbook 修复,InSpec 验证
Cookbook → 改配置
InSpec → 证明你改对了
Automate → 出报告
# hardening/recipes/default.rb
execute 'disable_empty_passwords' do
command "sed -i 's/PermitEmptyPasswords yes/PermitEmptyPasswords no/' /etc/ssh/sshd_config"
not_if "grep -q '^PermitEmptyPasswords no' /etc/ssh/sshd_config"
end
package 'auditd' do
action :install
end
service 'auditd' do
action [:enable, :start]
end
# hardening/controls/baseline.rb
control 'CIS-1.1.1' do
title 'Empty passwords must be disabled'
describe ssh_config do
its('PermitEmptyPasswords') { should eq 'no' }
end
end
control 'CIS-2.2.1' do
title 'auditd must be running'
describe service('auditd') do
it { should be_running }
end
end
# 修复
chef-client -o recipe[hardening]
# 检查
inspec exec hardening/controls -t ssh://host
Chef Server
↓
Chef Client(执行 + 扫描)
↓
Chef Automate(合规报告)
能力:
✅ 不要手写脚本
✅ 用 Cookbook + InSpec
✅ 合规规则版本化
✅ 先审计,再强制
✅ CI 里跑 InSpec
如果你愿意,我可以:
你现在是 Linux / Windows / 云上 / 等保场景?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。