Table of contents
1.
Introduction 
2.
Core Concepts
2.1.
Create a profile
2.2.
Add the tests
2.3.
Target the system
3.
Installing Chef InSpec
3.1.
macOS
3.1.1.
Homebrew
3.1.2.
CLI
3.2.
Windows
3.2.1.
Installer
3.2.2.
Powershell
4.
Uninstalling Chef InSpec
4.1.
macOS
4.1.1.
Homebrew
4.1.2.
CLI
4.2.
Windows
4.2.1.
Installer
5.
Using Chef InSpec on Cloud Platforms
5.1.
AWS Platform Support
5.1.1.
Setting Up
5.1.2.
Using Environment Variables to provide credentials
5.1.3.
Using Chef InSpec target option to provide credentials on AWS
5.2.
Azure Platform Support in InSpec
5.2.1.
Setting Up Azure Credentials
5.2.2.
Setting up the Azure Credentials File
5.2.3.
Using Environment variables to provide credentials
6.
Chef InSpec and Friends
6.1.
RSpec
6.2.
Serverspec
6.2.1.
How is Chef InSpec different from Serverspec?
7.
Frequently Asked Questions
7.1.
Which authentication mechanism is used by Chef InSpec?
7.2.
Which version supported the use of resources like AWS, Azure, and GCP?
7.3.
Which format is used when providing credentials on AWS from the Chef InSpec target option?
8.
Conclusion
Last Updated: Mar 27, 2024

Overview of Chef Inspec

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

It is an open-source framework that is used for testing and auditing applications and infrastructure. It works by comparing the actual state of the system with the desired state, which is expressed in easy-to-read and easy-to-write Ched InSpec code. It is also capable of detecting violations and displays the findings in the form of a report. It gives users the control of remediation of the detected violations.

Overview of Chef Inspec

Core Concepts

Some of the core concepts that make up Chef InSpec are mentioned below:

Create a profile

They are the core of the testing experience. The user can use the Chef InSpec profiles for managing everything they need to run a security or compliance scan-attributes, metadata, as well as the tests themselves.

Add the tests

There are three ways in which the user can create tests: by comparing their own tests, by including tests from the Chef Supermarket, or by simply adding tests from the Dev-Sec project in the form of dependencies. The user has the ability to customize the tests- pulling in the tests from the Supermarket and making the required changes in them to suit their unique needs with the help of the easy-to-read and easy-to-write Chef InSpec language.

Target the system

The user can run the tests on the infrastructure, irrespective of whether it's locally or on the cloud. It is designed for platforms and treats operating systems as a special case. It helps the user whether the user is using a Windows server on their own hardware or Linux in the docker containers in the cloud. In the case of the cloud, the user can use the Chef InSpec to target applications as well as services that are running on AWS and Azure.

Installing Chef InSpec

macOS

Homebrew

It is available as a standalone Homebrew package. The below-mentioned command can be used in the terminal to install Chef InSpec

brew install chef/chef/inspec


The user may be prompted to enter the password for the system while this command is running.

CLI

The user can use the below-mentioned command to download the Chef InSpec via curl script

curl https://omnitruck.chef.io/install.sh | sudo bash -s -- -P inspec

Windows

Installer

The user can download the latest version of the Chef InSpec package as per their Microsoft version from the website directly, double-click the .msi file in order to launch the installer, and follow the prompts displayed.

Powershell

The below-mentioned command can be used to install Chef InSpec via Powershell script

. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -project inspec


Once the installation is complete, the user can run the inspec version command to verify the status of the installation.

Uninstalling Chef InSpec

macOS

Homebrew

The below-mentioned destructive command can be used to remove the Chef InSpec standalone homebrew package

brew cask uninstall inspec

CLI

The below-mentioned destructive command can be used in the terminal to remove the Chef InSpec package:

sudo rm -rf /opt/inspec

Windows

Installer

By using the Add/Remove Programs, the user can remove the Chef InSpec.

Using Chef InSpec on Cloud Platforms

Since Chef InSpec 2.0, the platform support has been expanded to AWS, Azure, GCP, and AliCloud resources. By using InSpec, the user can use several Chef InSpec resources for auditing properties of the cloud infrastructure.

AWS Platform Support

AWS Platform Support

Setting Up

The standard AWS authentication mechanism is used by the Chef InSpec. For this, the user needs to create an IAM user specifically for auditing activities:

  • From the AWS console, create an IAM user with a username of choice. Make sure that the box marked as 'Programmatic Access' is checked.
  • Select Direct Attach from the Permissions screen. Choose the IAM Profile that is named 'ReadOnlyAccess', which AWS manages. If the user further wants to restrict the user, the user can check the individual Chef InSpec resources to identify what permissions are required.
  • Once the key is generated, record both: the Access Key ID and Secret Key.


Using Environment Variables to provide credentials

The user can provide the credentials to the Chef InSpec by setting the following variables: AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY. Once the variables have been set, the credentials can be verified by running the below command

$ inspec detect -t aws://

== Platform Details
Name: aws
Families: cloud, api
Release: aws-sdk-v2.10.125


Using Chef InSpec target option to provide credentials on AWS

Locate a file in the home directory labeled as ~/.aws/credentials. If in case the file does not exist, then create it. Choose a name for the profile, for reference, 'auditing' is used. Using the below-mentioned INI format, add your credentials:

[auditing]
aws_access_key_id = AKIA....
aws_secret_access_key = 1234....abcd


The user can now run the Chef InSpec using the -t option. If the user wants to verify the credentials, run the following command:

$ inspec detect -t aws://

== Platform Details
Name: aws
Families: cloud, api
Release: aws-sdk-v2.10.125

Azure Platform Support in InSpec

Setting Up Azure Credentials

If the user wishes to use the Chef InSpec Azure resources, then the user needs to create an SPN (Service Principal Name) for the purpose of auditing the Azure subscription.

This can be done either through the command line or from the Azure Portal:

  • Azure CLI
  • PowerShell
  • Azure Portal

Information from SPN can be specified in the file ~/.azure/credentials in the form of environment variables or by using the Chef InSpec target URIs.

Setting up the Azure Credentials File

By default, the Chef InSpec is configured to look at the ~/.azure/credentials, and it must contain:

[<SUBSCRIPTION_ID>]
client_id = "<CLIENT_ID>"
client_secret = "<CLIENT_SECRET>"
tenant_id = "<TENANT_ID>"

If the credentials are in place, then the user may execute InSpec

inspec exec my-inspec-profile -t azure://


Note

Some of the values are labeled differently in the Azure web portal, which are:

  • The client_id is the other name for 'Application ID'.
  • The client_secret is the other name for 'Key (Password Type)'
  • The tenant_id is the other name for Directory ID'.

Using Environment variables to provide credentials

The user can also use environment variables to set the Azure credentials:

  • AZURE_SUBSCRIPTION_ID
  • AZURE_CLIENT_ID
  • AZURE_CLIENT_SECRET
  • AZURE_TENANT_ID

Chef InSpec and Friends

RSpec

It's an amazing framework widely used to test the Ruby code. It allows test-driven development and helps the developers write better and more efficient code every day. RSpec is used as a base for building Chef InSpec, it uses it as the underlying foundation for executing tests. The key strengths of RSpec are used: easy execution of tests and DSL for writing tests as well as extends the functionality for use as a compliance audits. As the Chef InSpec comes with custom audit resources, making it easier to write audit checks along with the ability to run these checks on remote servers. The audit resources which are provided know the difference between operating systems and thereby help the user to be abstract from the local operating system, similar to other resources that the user might use in the Chef recipes.

The complete Chef InSpec rule looks like this:

control "sshd-11" do
  impact 1.0
  title "Server: Set protocol version to SSHv2"
  desc "Set the SSH protocol version to 2. Don't use legacy
        insecure SSHv1 connections anymore."
  tag security: "level-1"
  tag "openssh-server"
  ref "Server Security Guide v.1.0", url: "http://..."

  describe sshd_config do
    its('Protocol') { should eq('2') }
  end
end


The key differences between Chef InSpec and RSpec are mentioned below:

  • In the case of InSpec, the describe blocks shouldn't be nested. Instead, control blocks should be used to describe the higher-level grouping of tests.
  • The shared_example construct of the RSpec is not supported. Instead of this, create a simple custom resource that executes repetitious tasks.
  • Since Chef InSpec is aimed at compliance practitioners and infrastructure testers, the focus is to provide well-supported, easy-to-use universal matchers like cmp. Whereas RSpec is a tool which is designed for software engineers. Therefore it supports a large range of matchers, that enables testing of software engineering constructs such as exceptions and so on.
  • Though Chef InSpec uses parts of the RSpec project and codebase, it is a separate project from InSpec. 

Serverspec

It is the first extension of RSpec that enabled the users to run RSpec tests onto the servers to verify the deployed artifacts. It is one of the core test frameworks within the test-kitchen and is being widely used within the Chef ecosystem. The Chef InSpec takes the lessons learned by implementing as well as using Serverspec and builds on them to make auditing and compliance easier.

Some of the lessons learned from the Serverspec include:

  • IT, compliance, and security professionals require metadata which is beyond what Serverspec offers to completely describe controls.
  • It must be easy to set up and run the same tests across multiple machines
  • To locate, debug as well as extend the operating system-dependent code must be easy.
  • The process of extending the language and creating custom resources must be easy. 
  • It must be capable of running multiple tests simultaneously.
  • A CLI is needed for faster iteration of the test code.

 

How is Chef InSpec different from Serverspec?

The major difference between Chef InSpec and Serverspec is that it targets more user groups. Moreover, it is optimized for DevOps, Security, and Compliance professionals. Metadata like impact, title, and description makes the process of describing the controls easier, which in turn makes it easier to share the controls with other departments. 

Frequently Asked Questions

Which authentication mechanism is used by Chef InSpec?

The standard AWS authentication mechanism is used by the Chef InSpec

Which version supported the use of resources like AWS, Azure, and GCP?

Chef InSpec 2.0 started the support for AWS, Azure, and GCP.

Which format is used when providing credentials on AWS from the Chef InSpec target option?

The INI format is used when providing credentials on AWS from the Chef InSpec target option.

Conclusion

In this article, we have extensively discussed the Overview of Chef InSpec

After reading about the Chef InSpec, are you not feeling excited to read/explore more articles on Configuration Management? Don't worry; Coding Ninjas has you covered. To learn about what ansible iswhat is ansible YAML syntax and how errors are handled in the ansible playbook

If you wish to enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, etc., you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company. 

Do upvote if you find the blogs helpful.

Happy Learning!

Thank you image

Live masterclass