Chef InSpec is a framework for testing and auditing applications and infrastructure. Chef InSpec compares the actual state of the system with the desired state defined in a chef recepe written in ruby. Chef InSpec detects violations and displays findings in the form of a report, but puts you in control of remediation.
Inspec Profiles: contain a whole inspec definition.
Controls contain tests (like in ansible tasks contain the atomic tasks)
Inspect does not need to be installed on remote machines.
Alternatives to inspect are: puppet, chef, ansible, OpenSCA.
On the executing machine, run echo "StrictHostKeyChecking no" >> ~/.ssh/configto prevent the ssh agent from prompting YES or NO question. Then run a baseline profile:
inspec exec
https://github.com/dev-sec/linux-baseline # The chosen profile, some linux baseline tests
-t ssh://root@prod-LQc4TsDQ # target machine
-i ~/.ssh/id_rsa # specify the ssh-key since we are using login in via ssh
--chef-license accept # prevent the inspec from prompting YES or NO question
Integration into CD/CI
We want to run Inspec on the prod machine $DEPLOYMENT_SERVER. On the executing machine (here our gitlab server) we need to configure a variable $DEPLOYMENT_SERVER_SSH_KEY and store the ssh key of the prod machine to it.
Create Custom Inspec Profile
Create a folder, enter and initialize a new profile for a ubuntu machine:
cat >> ubuntu/controls/example.rb <<EOL
describe file('/etc/shadow') do
it { should exist }
it { should be_file }
it { should be_owned_by 'root' }
end
EOL
control "Firewall-1.0" do
impact 1.0
title "Ensure default deny firewall policy."
%w[INPUT OUTPUT FORWARD].each do |chain|
describe.one do
describe iptables do
it { should have_rule("-P #{chain} DROP") }
end
describe iptables do
it { should have_rule("-P #{chain} REJECT") }
end
end
end
end
control "SSH-1.0" do
impact 0.8
title "Ensure SSH uses RSA authentication"
describe sshd_config do
its('RSAAuthentication') { should_not eq 'no' }
its('Protocol') { should eq '2' }
end
end