inspec/lib/resources/bond.rb
eramoto c7e87ca3e3 Unify method in which file content is read across all resources (#2359)
* Create file-check functionality into utility file

There are the similar issues as PR #2302. Almost resources return false
positives when a file does not exist or is not read.

* Replace to file-check functionality
* Fix dh_params and x509_certificate resources

If a file is empty, OpenSSL::PKey::DH and OpenSSL::X509::Certificate have
raised an exception and have skipped the inspection. Thus x509_certificate
and dh_params resources are not allowed to read a empty file.

* to_s of shadow expects filters is not nil
* Remove workaround of sshd_config

Removes the workaround of sshd_config since Travis CI fails due to a bug
of dev-sec/ssh-baseline and the PR #100 will fix it.

* Use init block variable in methods

Signed-off-by: ERAMOTO Masaya <eramoto.masaya@jp.fujitsu.com>
2018-03-22 08:25:45 -04:00

69 lines
1.5 KiB
Ruby

# encoding: utf-8
require 'resources/file'
require 'utils/file_reader'
module Inspec::Resources
class Bond < FileResource
name 'bond'
supports platform: 'unix'
desc 'Use the bond InSpec audit resource to test a logical, bonded network interface (i.e. "two or more network interfaces aggregated into a single, logical network interface"). On Linux platforms, any value in the /proc/net/bonding directory may be tested.'
example "
describe bond('bond0') do
it { should exist }
end
"
include FileReader
def initialize(bond)
@bond = bond
@path = "/proc/net/bonding/#{bond}"
@file = inspec.file(@path)
@content = read_file_content(@path, allow_empty: true)
@params = {}
@loaded = false
end
def read_content
@params = SimpleConfig.new(
@content,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true,
).params if @file.exist?
@loaded = true
@content
end
# ensures the content is loaded before we return the params
def params
read_content if @loaded == false
@params
end
def content
read_content if @loaded == false
@content
end
def exist?
@file.exist?
end
def has_interface?(interface)
params['Slave Interface'].include?(interface)
end
def interfaces
params['Slave Interface']
end
def mode
params['Bonding Mode'].first
end
def to_s
"Bond #{@bond}"
end
end
end