Skip to content

Latest commit

 

History

History
127 lines (104 loc) · 6.18 KB

azure_virtual_machine.md

File metadata and controls

127 lines (104 loc) · 6.18 KB
title platform
About the azure_virtual_machine Resource
azure

azure_virtual_machine

Use the azure_virtual_machine InSpec audit resource to test properties related to a virtual machine.

Azure REST API version, endpoint and http client parameters

This resource interacts with api versions supported by the resource provider. The api_version can be defined as a resource parameter. If not provided, the latest version will be used. For more information, refer to azure_generic_resource.

Unless defined, azure_cloud global endpoint, and default values for the http client will be used. For more information, refer to the resource pack README.

Availability

Installation

This resource is available in the InSpec Azure resource pack. For an example inspec.yml file and how to set up your Azure credentials, refer to resource pack README.

Syntax

resource_group and virtual machine name or the resource_id must be given as a parameter.

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should exist }
end
describe azure_virtual_machine(resource_id: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vmName}') do
  it { should exist }
end

Parameters

Name Description
resource_group Azure resource group that the targeted resource resides in. MyResourceGroup
name Name of the Azure resource to test. MyVM
resource_id The unique resource ID. /subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vmName}

Either one of the parameter sets can be provided for a valid query:

  • resource_id
  • resource_group and name

Properties

Property Description
admin_username The admin user name.
resources The virtual machine child extension resources.
zones The virtual machine's availability zones. its('zones') should include('zone1', 'zone2')
installed_extensions_types List of all installed extensions' types for the virtual machine. its('installed_extensions_types') { should include('ExtensionType') }
installed_extensions_names List of all installed extensions' names for the virtual machine. its('installed_extensions_names') { should include('ExtensionName') }
has_monitoring_agent_installed? Indicates whether a monitoring agent is installed.
has_endpoint_protection_installed? Indicates whether a list of endpoint protection extension types are installed. it { should have_endpoint_protection_installed(%w{ep_type_1 ep_type_2}) }
has_only_approved_extensions? Indicates whether only provided extension types are installed. it { should have_only_approved_extensions(%w{extension_type_1 extension_type_2}) }
os_disk_name The virtual machine's operating system disk name. its('os_disk_name') { should cmp 'OsDiskName' }
data_disk_names The virtual machine's data disk names. its('data_disk_names') { should include('DataDisk1') }

For properties applicable to all resources, such as type, name, id, properties, refer to azure_generic_resource.

Also, refer to Azure documentation for other properties available. Any attribute in the response may be accessed with the key names separated by dots (.).

Examples

Ensure that the Virtual Machine has the Expected Data Disks

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  its('data_disk_names') { should include('DataDisk1') }
end

Ensure that the Virtual Machine has the Expected Monitoring Agent Installed

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_monitoring_agent_installed }
end

Matchers

This InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our Universal Matchers page.

exists

# If a virtual machine is found it will exist
describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should exist }
end

# virtual machines that aren't found will not exist
describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'DoesNotExist') do
  it { should_not exist }
end

have_only_approved_extensions

# Check if a virtual machine has only approved extensions. If an extension
# is used that's not in the list then the check will fail.
describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_only_approved_extensions(['ApprovedExtension', 'OtherApprovedExtensions']) }
end

have_monitoring_agent_installed

# Will be true if the MicrosoftMonitoringAgent is installed (Windows only)
describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_monitoring_agent_installed }
end

have_endpoint_protection_installed

# Will be true if any of the given extensions are installed.
describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_endpoint_protection_installed(['Extension1', 'Extension2']) }
end

Azure Permissions

Your Service Principal must be setup with a contributor role on the subscription you wish to test.