April 4th 2019
InSpec is an open source audit and automated testing framework tool that can be used to check cloud infrastructure compliancy.
In order to run inspec locally, you need the following:
inspec.ymlfile to include a reference to the InSpec Azure Resource Pack
Readerrights on your Azure subscription
~/.azure/credentialsfile defining your Azure service principal credentials
Note that the InSpec Azure Resource Pack suggests your service principal has
Contributorrights on your subscription. However, given the principal of least privilege, it is better to start with
Readerrights and increase if necessary.
To get started with InSpec, you can either clone an existing sample repository:
git clone https://github.com/learn-chef/auditd.git
or use the InSpec cli to create a new profile:
inspec init profile foo
In order to test against Azure, you can use the InSpec Azure Resource Pack. This requires both an update to your
inspec.yml file, and your Azure service principal credentials within a file at
inspec.yml to add a reference to the InSpec Azure Resource Pack:
1name: inspec-azure-demo2title: InSpec Azure Demo3maintainer: foo4copyright: foo5copyright_email: email@example.com: Apache-2.07summary: An InSpec Azure Profile Demo8version: 0.1.09depends:10- name: inspec-azure11url: https://github.com/inspec/inspec-azure/archive/master.tar.gz12supports:13platform: azure
To create the
~/.azure/credentials file, you can edit that file, for example if you use Visual Studio Code:
Within the file, enter the service principal credentials:
[<subscription_id>]client_id = "<client_id>"client_secret = "<client_secret>"tenant_id = "<tenant_id>"
To login to Azure within PowerShell you can run:
az login --service-principal --username <client_id> --password <client_secret> --tenant <tenant_id>
To execute the InSpec tests within PowerShell run the following from your root InSpec project folder (that contains
inspec exec . -t azure://
To execute the InSpec tests within Azure DevOps, you can define the following build steps within your
1# The following variables need defining in Azure DevOps:2# inspec.clientId3# inspec.clientSecret4# inspec.subscriptionId5# inspec.tenantId67pool:8vmImage: 'Ubuntu-16.04'910steps:11- task: UseRubyVersion@012displayName: 'Install Ruby'13inputs:14versionSpec: '>= 2.5'15addToPath: true1617- script: gem install inspec18displayName: 'Install inspec'1920- task: Bash@321displayName: 'Run inspec tests'22inputs:23targetType: inline24script: |25export AZURE_SUBSCRIPTION_ID="$(inspec.subscriptionId)"26export AZURE_CLIENT_ID="$(inspec.clientId)"27export AZURE_CLIENT_SECRET="$(inspec.clientSecret)"28export AZURE_TENANT_ID="$(inspec.tenantId)"29inspec exec . -t azure:// --reporter cli junit:testresults.xml html:report.html3031- task: PublishTestResults@232displayName: Publish inspec test results33condition: succeededOrFailed()34inputs:35testResultsFiles: '**/testresults.xml'36mergeTestResults: true3738- task: PublishBuildArtifacts@139displayName: Publish inspec HTML report40condition: succeededOrFailed()41inputs:42pathtoPublish: 'report.html'43artifactName: 'report'
These steps perform the following:
export VAR=VALUE), these are required by the InSpec Azure Resource Pack
inspec exec, here we pass in reporter parameters to output the test results in the CLI, as a JUnit XML file, and as an HTML file
The test result publish and build artefact tasks are configured to run on a successful or failed build. This means that if an InSpec test fails, the build will fail, but the test results and report will still be generated.
To run the Azure DevOps build, you will need to define the following build variables for your service principal credentials:
This build process can be scheduled (currently not configurable within YAML) to run e.g. nightly, so that every night your Azure environment is tested for compliance.