2015-10-07 21:47:57 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'utils/convert'
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class NetworkInterface < Inspec.resource(1)
|
|
|
|
name 'interface'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
|
|
|
supports platform: 'windows'
|
2017-05-18 19:25:16 +00:00
|
|
|
desc 'Use the interface InSpec audit resource to test basic network adapter properties, such as name, status, and link speed (in MB/sec).'
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2016-03-08 18:06:55 +00:00
|
|
|
describe interface('eth0') do
|
|
|
|
it { should exist }
|
|
|
|
it { should be_up }
|
2016-05-03 22:14:33 +00:00
|
|
|
its('speed') { should eq 1000 }
|
2019-03-18 22:53:51 +00:00
|
|
|
its('ipv4_addresses') { should include '127.0.0.1' }
|
|
|
|
its('ipv6_cidrs') { should include '::1/128' }
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2019-03-19 14:17:32 +00:00
|
|
|
EXAMPLE
|
2016-03-08 18:06:55 +00:00
|
|
|
def initialize(iface)
|
|
|
|
@iface = iface
|
|
|
|
|
|
|
|
@interface_provider = nil
|
|
|
|
if inspec.os.linux?
|
|
|
|
@interface_provider = LinuxInterface.new(inspec)
|
|
|
|
elsif inspec.os.windows?
|
|
|
|
@interface_provider = WindowsInterface.new(inspec)
|
|
|
|
else
|
|
|
|
return skip_resource 'The `interface` resource is not supported on your OS yet.'
|
|
|
|
end
|
2015-11-27 13:02:38 +00:00
|
|
|
end
|
2015-10-08 08:06:49 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def exists?
|
|
|
|
!interface_info.nil? && !interface_info[:name].nil?
|
|
|
|
end
|
2015-10-08 10:11:55 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def up?
|
|
|
|
interface_info.nil? ? false : interface_info[:up]
|
2015-10-08 10:11:55 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# returns link speed in Mbits/sec
|
|
|
|
def speed
|
|
|
|
interface_info.nil? ? nil : interface_info[:speed]
|
2015-10-08 10:11:55 +00:00
|
|
|
end
|
|
|
|
|
2019-03-18 22:53:51 +00:00
|
|
|
def ipv4_address?
|
|
|
|
!ipv4_addresses.nil? && !ipv4_addresses.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv6_address?
|
|
|
|
!ipv6_addresses.nil? && !ipv6_addresses.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv4_addresses
|
|
|
|
ipv4_cidrs.map { |i| i.split('/')[0] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv6_addresses
|
|
|
|
ipv6_cidrs.map { |i| i.split('/')[0] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv4_addresses_netmask
|
|
|
|
ipv4_cidrs.map { |i| i.split('/') }.map do |addr, netlen|
|
|
|
|
binmask = "#{'1' * netlen.to_i}#{'0' * (32 - netlen.to_i)}".to_i(2)
|
|
|
|
netmask = []
|
|
|
|
(1..4).each do |_byte|
|
|
|
|
netmask.unshift(binmask & 255)
|
|
|
|
binmask = binmask >> 8
|
|
|
|
end
|
|
|
|
"#{addr}/#{netmask.join('.')}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv4_cidrs
|
|
|
|
interface_info.nil? ? [] : interface_info[:ipv4_addresses]
|
|
|
|
end
|
|
|
|
|
|
|
|
def ipv6_cidrs
|
|
|
|
interface_info.nil? ? [] : interface_info[:ipv6_addresses]
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"Interface #{@iface}"
|
|
|
|
end
|
2015-10-08 11:01:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
private
|
2015-10-08 11:01:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def interface_info
|
|
|
|
return @cache if defined?(@cache)
|
|
|
|
@cache = @interface_provider.interface_info(@iface) if !@interface_provider.nil?
|
2015-10-08 11:01:09 +00:00
|
|
|
end
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-10-08 11:01:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
class InterfaceInfo
|
|
|
|
include Converter
|
|
|
|
attr_reader :inspec
|
|
|
|
def initialize(inspec)
|
|
|
|
@inspec = inspec
|
|
|
|
end
|
|
|
|
end
|
2015-10-08 11:01:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
class LinuxInterface < InterfaceInfo
|
|
|
|
def interface_info(iface)
|
|
|
|
# will return "[mtu]\n1500\n[type]\n1"
|
2017-05-28 14:44:42 +00:00
|
|
|
cmd = inspec.command("find /sys/class/net/#{iface}/ -maxdepth 1 -type f -exec sh -c 'echo \"[$(basename {})]\"; cat {} || echo -n' \\;")
|
2016-03-08 18:06:55 +00:00
|
|
|
return nil if cmd.exit_status.to_i != 0
|
|
|
|
|
|
|
|
# parse values, we only recieve values, therefore we threat them as keys
|
|
|
|
params = SimpleConfig.new(cmd.stdout.chomp).params
|
|
|
|
|
|
|
|
# abort if we got an empty result-set
|
|
|
|
return nil if params.empty?
|
|
|
|
|
|
|
|
# parse state
|
|
|
|
state = false
|
|
|
|
if params.key?('operstate')
|
|
|
|
operstate, _value = params['operstate'].first
|
|
|
|
state = operstate == 'up'
|
|
|
|
end
|
|
|
|
|
|
|
|
# parse speed
|
|
|
|
speed = nil
|
|
|
|
if params.key?('speed')
|
|
|
|
speed, _value = params['speed'].first
|
|
|
|
speed = convert_to_i(speed)
|
|
|
|
end
|
|
|
|
|
2019-03-18 22:53:51 +00:00
|
|
|
family_addresses = addresses(iface)
|
2016-03-08 18:06:55 +00:00
|
|
|
{
|
|
|
|
name: iface,
|
|
|
|
up: state,
|
|
|
|
speed: speed,
|
2019-03-18 22:53:51 +00:00
|
|
|
ipv4_addresses: family_addresses['inet'],
|
|
|
|
ipv6_addresses: family_addresses['inet6'],
|
2015-10-08 11:01:09 +00:00
|
|
|
}
|
|
|
|
end
|
2019-03-18 22:53:51 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def addresses(iface)
|
|
|
|
addrs_by_family = { 'inet6' => [], 'inet' => [] }
|
|
|
|
[4, 6].each do |v|
|
|
|
|
cmd = inspec.command("/sbin/ip -br -#{v} address show dev #{iface}")
|
|
|
|
next unless cmd.exit_status.to_i == 0
|
|
|
|
family = v == 6 ? 'inet6' : 'inet'
|
|
|
|
|
|
|
|
cmd.stdout.each_line do |line|
|
|
|
|
_dev, _state, *addrs = line.split(/\s+/)
|
|
|
|
addrs_by_family[family] = addrs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
addrs_by_family
|
|
|
|
end
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-10-08 11:01:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
class WindowsInterface < InterfaceInfo
|
|
|
|
def interface_info(iface)
|
|
|
|
# gather all network interfaces
|
2019-03-18 22:53:51 +00:00
|
|
|
cmd = inspec.command('Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, Status, State, ' \
|
|
|
|
'MacAddress, LinkSpeed, ReceiveLinkSpeed, TransmitLinkSpeed, Virtual | ConvertTo-Json')
|
|
|
|
|
|
|
|
addr_cmd = inspec.command('Get-NetIPAddress | Select-Object -Property IPv6Address, IPv4Address, InterfaceAlias,' \
|
|
|
|
' PrefixLength | ConvertTo-Json')
|
2016-03-08 18:06:55 +00:00
|
|
|
|
|
|
|
# filter network interface
|
|
|
|
begin
|
|
|
|
net_adapter = JSON.parse(cmd.stdout)
|
2019-03-18 22:53:51 +00:00
|
|
|
addresses = JSON.parse(addr_cmd.stdout)
|
2016-03-08 18:06:55 +00:00
|
|
|
rescue JSON::ParserError => _e
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# ensure we have an array of groups
|
|
|
|
net_adapter = [net_adapter] if !net_adapter.is_a?(Array)
|
2019-03-18 22:53:51 +00:00
|
|
|
addresses = [addresses] if !addresses.is_a?(Array)
|
2016-03-08 18:06:55 +00:00
|
|
|
|
|
|
|
# select the requested interface
|
|
|
|
adapters = net_adapter.each_with_object([]) do |adapter, adapter_collection|
|
|
|
|
# map object
|
|
|
|
info = {
|
|
|
|
name: adapter['Name'],
|
|
|
|
up: adapter['State'] == 2,
|
|
|
|
speed: adapter['ReceiveLinkSpeed'] / 1000,
|
2019-03-18 22:53:51 +00:00
|
|
|
ipv4_addresses: addresses_for_proto(addresses, adapter['Name'], 'IPv4'),
|
|
|
|
ipv6_addresses: addresses_for_proto(addresses, adapter['Name'], 'IPv6'),
|
2016-03-08 18:06:55 +00:00
|
|
|
}
|
|
|
|
adapter_collection.push(info) if info[:name].casecmp(iface) == 0
|
|
|
|
end
|
|
|
|
|
2017-02-08 22:49:16 +00:00
|
|
|
return nil if adapters.empty?
|
2016-03-08 18:06:55 +00:00
|
|
|
warn "[Possible Error] detected multiple network interfaces with the name #{iface}" if adapters.size > 1
|
|
|
|
adapters[0]
|
|
|
|
end
|
2019-03-18 22:53:51 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def addresses_for_proto(all_addresses, iface, proto)
|
|
|
|
all_addresses.select { |i| i['InterfaceAlias'] == iface }
|
|
|
|
.map { |i| "#{i["#{proto}Address"]}/#{i['PrefixLength']}" unless i["#{proto}Address"].nil? }
|
|
|
|
.compact
|
|
|
|
end
|
2015-10-08 11:01:09 +00:00
|
|
|
end
|
|
|
|
end
|