- Project State: Maintained
For more information on project states and SLAs, see this documentation.
This InSpec resource pack uses the Azure REST API and provides the required resources to write tests for resources in Azure.
- Ruby
- Bundler installed
- Azure Service Principal Account
- Azure Service Principal may read the Azure Active Directory
Your Azure Service Principal Account must have contributor
role to any subscription that you'd like to use this resource pack against. You should have the following pieces of information:
- TENANT_ID
- CLIENT_ID
- CLIENT_SECRET
- SUBSCRIPTION_ID
To create your account Service Principal Account:
- Login to the Azure portal.
- Click on
Azure Active Directory
. - Click on
APP registrations
. - Click on
New application registration
. - Fill in a name and a Sign-on URL. Select
Web app / API
from theApplication Type
drop down. Save your application. - Note your Application ID. This is your
client_id
above. - Click on
Settings
- Click on
Keys
- Create a new password. This value is your
client_secret
above. - Go to your subscription (click on
All Services
then subscriptions). Choose your subscription from that list. - Note your Subscription ID can be found here.
- Click
Access Control (IAM)
- Click Add
- Select the
contributor
role. - Select the application you just created and save.
These must be stored in a environment variables prefaced with AZURE_
. If you use Dotenv then you may save these values in your own .envrc
file. Either source it or run direnv allow
. If you don't use Dotenv then you may just create environment variables in the way that your prefer.
Since this is an InSpec resource pack, it only defines InSpec resources. To use these resources in your own controls you should create your own profile:
$ inspec init profile my-profile
Example inspec.yml:
name: my-profile
title: My own Azure profile
version: 0.1.0
inspec_version: '>= 4.6.9'
depends:
- name: inspec-azure
url: https://github.com/inspec/inspec-azure/archive/x.tar.gz
supports:
- platform: azure
(For available inspec-azure versions, see this list of inspec-azure versions.)
Verify properties of an Azure VM
control 'azurerm_virtual_machine' do
describe azurerm_virtual_machine(resource_group: 'MyResourceGroup', name: 'prod-web-01') do
it { should exist }
it { should have_monitoring_agent_installed }
it { should_not have_endpoint_protection_installed([]) }
it { should have_only_approved_extensions(['MicrosoftMonitoringAgent']) }
its('type') { should eq 'Microsoft.Compute/virtualMachines' }
its('installed_extensions_types') { should include('MicrosoftMonitoringAgent') }
its('installed_extensions_names') { should include('LogAnalytics') }
end
end
Verify properties of a security group
control 'azure_network_security_group' do
describe azure_network_security_group(resource_group: 'ProductionResourceGroup', name: 'ProdServers') do
it { should exist }
its('type') { should eq 'Microsoft.Network/networkSecurityGroups' }
its('security_rules') { should_not be_empty }
its('default_security_rules') { should_not be_empty }
it { should_not allow_rdp_from_internet }
it { should_not allow_ssh_from_internet }
end
end
The following resources are available in the InSpec Azure Resource Pack
- azurerm_ad_user
- azurerm_ad_users
- azurerm_aks_cluster
- azurerm_aks_clusters
- azurerm_key_vault
- azurerm_key_vault_key
- azurerm_key_vault_keys
- azurerm_key_vault_secret
- azurerm_key_vault_secrets
- azurerm_key_vaults
- azurerm_management_group
- azurerm_management_groups
- azurerm_monitor_activity_log_alert
- azurerm_monitor_activity_log_alerts
- azurerm_monitor_log_profile
- azurerm_monitor_log_profiles
- azurerm_network_security_group
- azurerm_network_security_groups
- azurerm_network_watcher
- azurerm_network_watchers
- azurerm_resource_groups
- azurerm_security_center_policies
- azurerm_security_center_policy
- azurerm_sql_database
- azurerm_sql_databases
- azurerm_sql_server
- azurerm_sql_servers
- azurerm_storage_account_blob_container
- azurerm_storage_account_blob_containers
- azurerm_subnet
- azurerm_subnets
- azurerm_virtual_machine
- azurerm_virtual_machine_disk
- azurerm_virtual_machines
- azurerm_virtual_network
- azurerm_virtual_networks
See Connectors for more information on the different connection strategies we support.
If you'd like to contribute to this project please see Contributing Rules. The following instructions will help you get your development environment setup to run integration tests.
Copy .envrc-example
to .envrc
and fill in the fields with the values from your account.
export AZURE_SUBSCRIPTION_ID=<subscription id>
export AZURE_CLIENT_ID=<client id>
export AZURE_TENANT_ID=<tenant id>
export AZURE_CLIENT_SECRET=<client secret>
For PowerShell, set the following environment variables
$env:AZURE_SUBSCRIPTION_ID="<subscription id>"
$env:AZURE_CLIENT_ID="<client id>"
$env:AZURE_CLIENT_SECRET="<tenant id>"
$env:AZURE_TENANT_ID="<client secret>"
Setup Azure CLI
- Follow the instructions for your platform here
- macOS:
brew update && brew install azure-cli
- macOS:
- Login with the azure-cli
rake azure:login
- Verify azure-cli is logged in:
az account show
First ensure your system has Terraform (Version 0.11.7) installed.
This environment may be used to run your profile against or to run integration tests on it. We are using Terraform workspaces to allow for teams to have completely unique environments without affecting each other.
Direnv is used to initialize an environment variable WORKSPACE
to your username. We recommend using direnv
and allowing it to run in your environment. However, if you prefer to not use direnv
you may also source .envrc
.
Remote state has been removed. The first time you run Terraform after having remote state removed you will be presented with a message like:
Do you want to migrate all workspaces to "local"?
Both the existing "azurerm" backend and the newly configured "local" backend support
workspaces. When migrating between backends, Terraform will copy all
workspaces (with the same names). THIS WILL OVERWRITE any conflicting
states in the destination.
Terraform initialization doesn't currently migrate only select workspaces.
If you want to migrate a select number of workspaces, you must manually
pull and push those states.
If you answer "yes", Terraform will migrate all states. If you answer
"no", Terraform will abort.
Enter a value: yes
Enter yes or press enter.
Creating a new environment:
rake azure:login
rake tf:apply
Creating a new environment with a Network Watcher:
rake azure:login
rake network_watcher tf:apply
You may only have a single Network Watcher per a subscription. Use this carefully if you are working with other team members.
Updating a running environment (e.g. when you change the .tf file):
rake azure:login
rake tf:apply
Checking if your state has diverged from your plan:
rake azure:login
rake tf:plan
Destroying your environment:
rake azure:login
rake tf:destroy
To start up an environment and run all tests:
bundle
rake azure:login
rake azure
To run all tests:
bundle
rake
To run integration tests:
bundle
rake test:integration
To run integration tests including a Network Watcher:
bundle
rake network_watcher test:integration
To run a control called azurerm_virtual_machine
:
bundle
rake test:integration[azurerm_virtual_machine]
You may run multiple controls:
bundle
rake test:integration[azure_resource_group,azurerm_virtual_machine]
By default, rake tasks will only use core components. Optional components have associated integrations that will be skipped unless you enable these. We have the following optional pieces that may be managed with Terraform.
Network Watcher may be enabled to run integration tests related to the Network Watcher. We recommend leaving this disabled unless you are specifically working on related resources. You may only have one Network Watcher enabled per an Azure subscription at a time. To enable Network Watcher:
rake options[network_watcher]
direnv allow # or source .envrc
rake tf:apply
Graph API support may be enabled to test with azure_ad
related resources.
Graph requires granting "Active Directory Read" to the Service Principal. If
your account does not have access leave this disabled.
Please refer to the Microsoft Documentation for information on how to grant these permissions to your application.
Note: An Azure Administrator must grant your application these permissions.
rake options[graph]
direnv allow # or source .envrc
rake tf:apply
Managed Service Identity (MSI) is another way to connect to the Azure APIs.
This option starts an additonal virtual machine with MSI enabled and a public
ip address. You will need to put a hole in your firewall to connect to the
virtual machine. You will also need to grant the contributor
role to this
identity for your subscription.
rake options[msi]
direnv allow # or source .envrc
rake tf:apply
Optional Components may be combined when running tasks:
rake options[option_1,option_2,option3]
direnv allow # or source .envrc
rake tf:apply
To disable optional components run rake options[]
including only the optional components you wish to enable. Any omitted component will be disabled.
rake options[] # disable all optional components
rake options[option_1] # enables option_1 disabling all other optional components