Effective Testing with Ansible and InSpec
Nathen Harvey
Hello!
Nathen Harvey
VP, Community
Development
Chef
@nathenharvey
@nathenharvey
@nathenharvey
@nathenharvey
Why Testing?
• Move fast with a safety net
• Decrease feedback cycles
• Increase confidence
• Prevent regressions
• Scale
@nathenharvey
Types of Testing
• Static analysis - https://github.com/willthames/ansible-lint
• Unit testing
• Integration testing
• Compliance testing
@nathenharvey
Testing Playbooks
• Write the playbook
• Execute the playbook on a system
• Verify results
@nathenharvey
The Playbook
- name: Updates apt cache
- name: Installs necessary packages
- name: Push future default virtual host configuration
- name: Deploy the configuration management camp website
- name: Activates the virtualhost
- name: Check that the config is valid
- name: Deactivates the default virtualhost
- name: Deactivates the default ssl virtualhost
https://github.com/turkenh/ansible-interactive-tutorial
@nathenharvey
Test Kitchen
@nathenharvey
kitchen create
Test Kitchen
@nathenharvey
kitchen create
kitchen destroy
Test Kitchen
@nathenharvey
kitchen create
kitchen destroy
Test Kitchen
@nathenharvey
kitchen create
kitchen destroy
driver
Test Kitchen
@nathenharvey
kitchen create
kitchen destroy
driver
Test Kitchen
@nathenharvey
The Test Kitchen
driver:
name: vagrant
platforms:
- name: ubuntu-16.04
suites:
- name: default
kitchen create driver
@nathenharvey
https://youtu.be/Warn6p28spA
List Kitchen(s)
@nathenharvey
https://youtu.be/d14Ma-S78QE
Create Kitchen(s)
@nathenharvey
https://youtu.be/CeJ0egGdQe8
Run the Playbook
@nathenharvey
https://youtu.be/pIbLYLp4z68
Verify the Application
@nathenharvey
Workflow
• kitchen create
• ansible-playbook -i hosts sites.yml
• Manually verify the application
kitchen create
kitchen destroy
driver
ansible-playbook
@nathenharvey
kitchen create
kitchen converge
kitchen destroy
driver
provisioner
Test Kitchen
@nathenharvey
Kitchen Provisioner
provisioner:
hosts: test-kitchen
name: ansible_playbook
roles_path: roles
require_ansible_repo: true
ansible_verbose: true
ansible_version: latest
require_chef_for_busser: false
playbook: site.yml
@nathenharvey
https://youtu.be/PRJi4uR2UJI
Kitchen Converge
@nathenharvey
https://youtu.be/pIbLYLp4z68
Manually Verify
@nathenharvey
Workflow
• kitchen converge
• Manually verify the application
kitchen create
kitchen destroy
driver
kitchen converge provisioner
@nathenharvey
InSpec
• Open-source framework
• Infrastructure testing
• Make assertions about state of resources in the infrastructure
@nathenharvey
Verify the Site with InSpec
describe service('apache2') do
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
describe http('http://localhost', enable_remote_worker: true) do
its('status') { should cmp 200 }
its('body') { should match /Configuration Management Camp/ }
end
Test Locally
$ inspec exec /path/to/profile
Test Remotely
$ inspec exec /path/to/profile -i ssh.key -t ssh://me@myhost
@nathenharvey
https://youtu.be/TKQTEhhwS4c
Execute InSpec on Remote Target
Test Remotely
$ inspec exec /path/to/profile -t winrm://me@myhost --password secret
Test Remotely
$ inspec exec /path/to/profile -t docker://3cc8837bb6a8
@nathenharvey
https://youtu.be/6u4j57ksmSU
Using the InSpec Shell
@nathenharvey
InSpec in Test Kitchen
@nathenharvey
https://youtu.be/v8_e9l4p1K8
Kitchen Verify
@nathenharvey
Workflow
• kitchen verify kitchen create
kitchen destroy
driver
kitchen converge provisioner
kitchen verify verifier
@nathenharvey
Compliance Testing
@nathenharvey
@nathenharvey
InSpec to Detect Policy Violations
• InSpec is great for integration testing
• But it can also be used for security or compliance checks
@nathenharvey
https://youtu.be/udomS3XyxQg
Assert telnetd is not installed
Map Documentation to Controls
control 'sox-404.3.5' do
title 'Network Device to Central Auth Encryption'
impact 1.0
desc "
All communication between network devices and
central auth must be encrypted. Our TACACS+ servers
encrypt all the time and the presence of a
pre-shared key proves it."
describe ini('/etc/tac_plus/tac_plus.conf') do
its('key') { should_not be_nil }
end
end
404.3.5:
Communication
between network
devices and central
authentication systems
must be encrypted at
all times.
Share Context
control 'sox-404.3.5' do
title 'Network Device to Central Auth Encryption'
impact 1.0
desc "
All communication between network devices and
central auth must be encrypted. Our TACACS+ servers
encrypt all the time and the presence of a
pre-shared key proves it."
describe ini('/etc/tac_plus/tac_plus.conf') do
its('key') { should_not be_nil }
end
end
404.3.5:
Communication
between network
devices and central
authentication systems
must be encrypted at
all times.
Automate Test Execution
control 'sox-404.3.5' do
title 'Network Device to Central Auth Encryption'
impact 1.0
desc "
All communication between network devices and
central auth must be encrypted. Our TACACS+ servers
encrypt all the time and the presence of a
pre-shared key proves it."
describe ini('/etc/tac_plus/tac_plus.conf') do
its('key') { should_not be_nil }
end
end
404.3.5:
Communication
between network
devices and central
authentication systems
must be encrypted at
all times.
@nathenharvey
dev-sec.io
@nathenharvey
Add Linux Baseline to Test Kitchen
suites:
- name: default
verifier:
inspec_tests:
- test/integration/default
- https://github.com/dev-sec/linux-baseline
@nathenharvey
https://youtu.be/jP704Io0ikU
Kitchen Verify
@nathenharvey
So many failures
• Stop when the build breaks
• We need to get to green
• What is the best way to get the build green?
@nathenharvey
Wrap it up
• Create a TODO list
• One measure of technical debt
• Get to green by commenting out tests?!
@nathenharvey
Wrapper Profile
name: my-linux-baseline
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
depends:
- name: linux-baseline
url: https://github.com/dev-sec/linux-baseline/archive/master.tar.gz
@nathenharvey
Wrapper Profile
include_controls 'linux-baseline' do
skip_control 'os-05'
skip_control 'package-08'
skip_control 'sysctl-05'
...
end
@nathenharvey
https://youtu.be/fp7y3Qj7vMk
Kitchen Verify
@nathenharvey
Hardening Playbook
@nathenharvey
https://youtu.be/eosqOmXde-8
Install the Role
@nathenharvey
Add to the Playbook
- hosts: all
become: true
become_user: root
become_method: sudo
roles:
- { role: apache }
- { role: dev-sec.os-hardening }
@nathenharvey
https://youtu.be/RU4T4wIt76g
Apply the new role
@nathenharvey
https://youtu.be/ZVn6i8Kqq_c
Verify
@nathenharvey
https://youtu.be/eMUuODXOymw
How about those skipped controls?
@nathenharvey
https://youtu.be/WDImV9oYQI4
Back to Green
@nathenharvey
One Commit
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
kitchen destroy
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
kitchen destroy
kitchen create
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
kitchen destroy
kitchen create
kitchen converge
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
kitchen destroy
kitchen create
kitchen converge
kitchen verify
@nathenharvey
kitchen create
kitchen converge
kitchen verify
kitchen destroy
driver
provisioner
Test Kitchen
kitchen test
kitchen destroy
kitchen create
kitchen converge
kitchen verify
kitchen destroy
@nathenharvey
https://youtu.be/BbB7lrihWtM
Kitchen Test
PART OF A PROCESS OF CONTINUOUS COMPLIANCE
Scan for
Compliance
Build & Test
Locally
Build & Test
CI/CD Remediate Verify
A SIMPLE EXAMPLE OF AN INSPEC CIS RULE
InSpec
▪ Translate compliance into Code
▪ Clearly express statements of policy
▪ Move risk to build/test from runtime
▪ Find issues early
▪ Write code quickly
▪ Run code anywhere
▪ Inspect machines, data, and APIs
Turn security and
compliance into code
control 'cis-1.4.1' do
title '1.4.1 Enable SELinux in /etc/grub.conf’
desc '
Do not disable SELinux and enforcing in your GRUB
configuration. These are important security features that
prevent attackers from escalating their access to your systems.
For reference see …
'
impact 1.0
expect(grub_conf.param 'selinux').to_not eq '0'
expect(grub_conf.param 'enforcing').to_not eq '0'
end
Continuous Compliance
@nathenharvey
Get Started with InSpec
• Install Chef Development Kit - https://downloads.chef.io/chefdk
Test Kitchen
InSpec
• Install Ansible Provisioner
chef gem install kitchen-ansible
• Install Driver Requirements
Vagrant – VirtualBox & Vagrant
Docker – Docker
EC2 – None, but you need an AWS account
@nathenharvey
Use, Share, Contribute!
• dev-sec.io - https://github.com/dev-sec/
• InSpec – https://github.com/chef/inspec
• Supermarket - https://supermarket.chef.io/tools?type=compliance_profile
• Test Kitchen - https://github.com/test-kitchen
• Test Kitchen Ansible Provisioner - https://github.com/neillturner/kitchen-ansible
• Code from this presentation - https://github.com/nathenharvey/testing-ansible-
with-inspec
Join us on Slack
• http://community-slack.chef.io
• #general (for Chef stuff)
• #inspec
• #test-kitchen
The Chef community
believes that diversity is one
of our biggest strengths!
Ansible users are more than
welcome here!
@nathenharvey
September 10 & 11
@nathenharvey
Post Script
@nathenharvey
@nathenharvey
kitchen-dokken
• The only Test Kitchen plugin that enables multi-host test suites.
• Ansible support?
@nathenharvey
kitchen-dokken
• The only Test Kitchen plugin that enables multi-host test suites.
• Ansible support?
@nathenharvey
Will it support InSpec?
@nathenharvey
Will it support InSpec?
@nathenharvey
Will it support InSpec?
@nathenharvey
Will it support InSpec?
@nathenharvey
TODO
• Rewrite this talk – Molecule now supports InSpec!
• https://github.com/metacloud/molecule/issues/1072
Thank You!
Nathen Harvey
VP, Community
Development
Chef
@nathenharvey

Effective Testing with Ansible and InSpec

Editor's Notes

  • #17 https://youtu.be/Warn6p28spA
  • #18 https://youtu.be/d14Ma-S78QE
  • #19 https://youtu.be/CeJ0egGdQe8 ansible-playbook -i hosts site.yml
  • #20 https://youtu.be/pIbLYLp4z68
  • #24 https://youtu.be/PRJi4uR2UJI
  • #25 https://youtu.be/pIbLYLp4z68
  • #32 https://youtu.be/TKQTEhhwS4c
  • #35 https://youtu.be/6u4j57ksmSU
  • #37 https://youtu.be/v8_e9l4p1K8
  • #42 https://youtu.be/udomS3XyxQg
  • #48 https://youtu.be/jP704Io0ikU
  • #53 https://youtu.be/fp7y3Qj7vMk
  • #55 https://youtu.be/eosqOmXde-8
  • #57 https://youtu.be/RU4T4wIt76g
  • #58 https://youtu.be/ZVn6i8Kqq_c
  • #59 https://youtu.be/eMUuODXOymw
  • #60 https://youtu.be/WDImV9oYQI4
  • #68 https://youtu.be/BbB7lrihWtM
  • #70 Let’s look at all of this in practice… This is a screen shot from a customer. We cannot say who the customer is. Never mention their name even if you think you know who they are. I’m not telling you who they are.