Class: Inspec::Resources::AzureVirtualMachine

Inherits:
AzureResourceBase show all
Defined in:
lib/resources/azure/azure_virtual_machine.rb

Instance Attribute Summary

Attributes inherited from AzureResourceBase

#azure, #client, #opts

Instance Method Summary collapse

Methods inherited from AzureResourceBase

#catch_azure_errors, #create_tag_methods, #failed_resource?, #has_tags?, #resource_group, #resources, #tag_count

Constructor Details

#initialize(opts = {}) ⇒ AzureVirtualMachine

Constructor for the resource. This calls the parent constructor to get the generic resource for the specified machine. This will provide static methods that are documented

Author:

  • Russell Seymour



20
21
22
23
24
25
26
27
28
29
# File 'lib/resources/azure/azure_virtual_machine.rb', line 20

def initialize(opts = {})
  # The generic resource needs to pass back a Microsoft.Compute/virtualMachines object so force it
  opts[:type] = 'Microsoft.Compute/virtualMachines'
  super(opts)

  # Find the virtual machines
  resources

  create_tag_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id) ⇒ Object

Method to catch calls that are not explicitly defined. This allows the simple attributes of the virtual machine to be read without having to define each one in turn.

rubocop:disable Metrics/AbcSize

Parameters:

  • symobl

    method_id The symbol of the method that has been called

Returns:

  • Value of attribute that has been called



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/resources/azure/azure_virtual_machine.rb', line 40

def method_missing(method_id)
  # Depending on the method that has been called, determine what value should be returned
  # These are set as camel case methods to comply with rubocop
  image_reference_attrs = %w{sku publisher offer}
  osdisk_attrs = %w{os_type caching create_option disk_size_gb}
  hardware_profile_attrs = %w{vm_size}
  os_profile_attrs = %w{computer_name admin_username}
  osdisk_managed_disk_attrs = %w{storage_account_type}

  # determine the method name to call by converting the snake_case to camelCase
  # method_name = self.camel_case(method_id.to_s)
  method_name = method_id.to_s.split('_').inject([]) { |buffer, e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
  method_name.end_with?('Gb') ? method_name.gsub!(/Gb/, &:upcase) : false

  if image_reference_attrs.include?(method_id.to_s)
    properties.storageProfile.imageReference.send(method_name)
  elsif osdisk_attrs.include?(method_id.to_s)
    properties.storageProfile.osDisk.send(method_name)
  elsif hardware_profile_attrs.include?(method_id.to_s)
    properties.hardwareProfile.send(method_name)
  elsif os_profile_attrs.include?(method_id.to_s)
    properties.osProfile.send(method_name)
  elsif osdisk_managed_disk_attrs.include?(method_id.to_s)
    properties.storageProfile.osDisk.managedDisk.send(method_name)
  end
end

Instance Method Details

#boot_diagnostics_storage_uriObject

Return the URI that has been set for the boot diagnostics storage

Returns:

  • string



226
227
228
# File 'lib/resources/azure/azure_virtual_machine.rb', line 226

def boot_diagnostics_storage_uri
  properties.diagnosticsProfile.bootDiagnostics.storageUri
end

#connected_nicsObject

Return an array of the connected NICs so that it can be tested to ensure the machine is connected properly

Returns:

  • array Array of NIC names connected to the machine



99
100
101
102
103
104
105
# File 'lib/resources/azure/azure_virtual_machine.rb', line 99

def connected_nics
  nic_names = []
  properties.networkProfile.networkInterfaces.each do |nic|
    nic_names << nic.id.split(%r{/}).last
  end
  nic_names
end

#custom_data?Boolean

Determine if custom data has been set

Returns:

  • (Boolean)

    boolean



159
160
161
162
163
164
165
# File 'lib/resources/azure/azure_virtual_machine.rb', line 159

def custom_data?
  if defined?(properties.osProfile.CustomData)
    true
  else
    false
  end
end

#data_disk_countObject

How many data disks are connected

Returns:

  • integer



117
118
119
# File 'lib/resources/azure/azure_virtual_machine.rb', line 117

def data_disk_count
  properties.storageProfile.dataDisks.count
end

#has_automatic_agent_update?Boolean

If a windows machine see if automatic updates for the agent are enabled

Returns:

  • (Boolean)

    boolean



244
245
246
247
248
249
250
# File 'lib/resources/azure/azure_virtual_machine.rb', line 244

def has_automatic_agent_update?
  if defined?(properties.osProfile.windowsConfiguration)
    properties.osProfile.windowsConfiguration.enableAutomaticUpdates
  else
    false
  end
end

#has_boot_diagnostics?Boolean

Does the machine have boot diagnostics enabled

Returns:

  • (Boolean)

    boolean



215
216
217
218
219
220
221
# File 'lib/resources/azure/azure_virtual_machine.rb', line 215

def has_boot_diagnostics?
  if defined?(properties.diagnosticsProfile)
    properties.diagnosticsProfile.bootDiagnostics.enabled
  else
    false
  end
end

#has_custom_data?Boolean

Has the machine been given Custom Data at creation

This allows the use of

it { should have_custom_data }

within the Inspec Profile

Returns:

  • (Boolean)

    boolean



152
153
154
# File 'lib/resources/azure/azure_virtual_machine.rb', line 152

def has_custom_data?
  custom_data?
end

#has_data_disks?Boolean

Whether the machine has data disks or not

Returns:

  • (Boolean)

    boolean



110
111
112
# File 'lib/resources/azure/azure_virtual_machine.rb', line 110

def has_data_disks?
  properties.storageProfile.dataDisks.count != 0
end

#has_managed_osdisk?Boolean

Determine if the OS disk is a managed disk

Returns:

  • (Boolean)

    boolean



77
78
79
# File 'lib/resources/azure/azure_virtual_machine.rb', line 77

def has_managed_osdisk?
  defined?(properties.storageProfile.osDisk.managedDisk)
end

#has_nics?Boolean

Does the machine have any NICs connected

Returns:

  • (Boolean)

    boolean



84
85
86
# File 'lib/resources/azure/azure_virtual_machine.rb', line 84

def has_nics?
  properties.networkProfile.networkInterfaces.count != 0
end

#has_password_authentication?Boolean

Does the machine allow password authentication

This allows the use of

it { should have_password_authentication }

within the Inspec profile

Returns:

  • (Boolean)

    boolean



128
129
130
# File 'lib/resources/azure/azure_virtual_machine.rb', line 128

def has_password_authentication?
  password_authentication?
end

#has_provision_vmagent?Boolean

If this is a windows machine, returns whether the agent was provisioned or not

Returns:

  • (Boolean)

    boolean



233
234
235
236
237
238
239
# File 'lib/resources/azure/azure_virtual_machine.rb', line 233

def has_provision_vmagent?
  if defined?(properties.osProfile.windowsConfiguration)
    properties.osProfile.windowsConfiguration.provisionVMAgent
  else
    false
  end
end

#has_ssh_keys?Boolean

Are any SSH Keys assigned to the machine

This allows the use of

it { should have_ssh_keys }

within the Inspec Profile

Returns:

  • (Boolean)

    boolean



174
175
176
# File 'lib/resources/azure/azure_virtual_machine.rb', line 174

def has_ssh_keys?
  ssh_keys?
end

#has_winrm_options?Boolean

If this is a windows machine return a boolean to state of the WinRM options have been set

Returns:

  • (Boolean)

    boolean



256
257
258
259
260
261
262
# File 'lib/resources/azure/azure_virtual_machine.rb', line 256

def has_winrm_options?
  if defined?(properties.osProfile.windowsConfiguration) && defined?(properties.osProfile.windowsConfiguration.winrm)
    properties.osProfile.windowsConfiguration.winrm.protocol
  else
    false
  end
end

#nic_countObject

How many NICs are connected to the machine

Returns:

  • integer



91
92
93
# File 'lib/resources/azure/azure_virtual_machine.rb', line 91

def nic_count
  properties.networkProfile.networkInterfaces.count
end

#os_disk_nameObject

Return the name of the os disk

Returns:

  • string Name of the OS disk



70
71
72
# File 'lib/resources/azure/azure_virtual_machine.rb', line 70

def os_disk_name
  properties.storageProfile.osDisk.name
end

#password_authentication?Boolean

Deteremine if the machine allows password authentication

Returns:

  • (Boolean)

    boolean



135
136
137
138
139
140
141
142
143
# File 'lib/resources/azure/azure_virtual_machine.rb', line 135

def password_authentication?
  # if the osProfile property has a linuxConfiguration section then interrogate that
  # otherwise it is a Windows machine and that always has password auth
  if defined?(properties.osProfile.linuxConfiguration)
    !properties.osProfile.linuxConfiguration.disablePasswordAuthentication
  else
    true
  end
end

#ssh_key_countObject

Return the number of ssh keys that have been assigned to the machine

Returns:

  • integer



192
193
194
195
196
197
198
# File 'lib/resources/azure/azure_virtual_machine.rb', line 192

def ssh_key_count
  if defined?(properties.osProfile.linuxConfiguration.ssh)
    properties.osProfile.linuxConfiguration.ssh.publicKeys.count
  else
    0
  end
end

#ssh_keysObject

Determine is the specified key is in the ssh_keys list

Returns:

  • array Array of the public keys that are assigned to allow for testing of that key



203
204
205
206
207
208
209
210
# File 'lib/resources/azure/azure_virtual_machine.rb', line 203

def ssh_keys
  # iterate around the keys
  keys = []
  properties.osProfile.linuxConfiguration.ssh.publicKeys.each do |key|
    keys << key.keyData
  end
  keys
end

#ssh_keys?Boolean

Determine if any ssh keys have been asigned to the machine

Returns:

  • (Boolean)

    boolean



181
182
183
184
185
186
187
# File 'lib/resources/azure/azure_virtual_machine.rb', line 181

def ssh_keys?
  if defined?(properties.osProfile.linuxConfiguration.ssh)
    properties.osProfile.linuxConfiguration.ssh.publicKeys != 0
  else
    false
  end
end