2015-09-03 15:33:19 +00:00
|
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
# author: Christoph Hartmann
|
2015-09-03 15:33:19 +00:00
|
|
|
|
|
2015-09-02 02:13:59 +00:00
|
|
|
|
require 'minitest/autorun'
|
|
|
|
|
require 'minitest/spec'
|
2015-12-07 19:41:05 +00:00
|
|
|
|
require 'mocha/setup'
|
2015-09-04 18:24:38 +00:00
|
|
|
|
|
|
|
|
|
require 'simplecov'
|
|
|
|
|
SimpleCov.start do
|
|
|
|
|
add_filter '/test/'
|
|
|
|
|
add_group 'Resources', 'lib/resources'
|
|
|
|
|
add_group 'Matchers', 'lib/matchers'
|
2015-10-26 03:04:18 +00:00
|
|
|
|
add_group 'Backends', 'lib/inspec/backend'
|
2015-09-04 18:24:38 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
|
require 'inspec/resource'
|
|
|
|
|
require 'inspec/backend'
|
2015-10-26 21:57:34 +00:00
|
|
|
|
require 'inspec/profile'
|
2016-01-13 14:28:11 +00:00
|
|
|
|
require 'inspec/targets'
|
|
|
|
|
require 'inspec/targets/zip'
|
2015-09-03 15:33:19 +00:00
|
|
|
|
|
2015-09-22 16:31:21 +00:00
|
|
|
|
class MockLoader
|
|
|
|
|
# pass the os identifier to emulate a specific operating system
|
|
|
|
|
def initialize(os = nil)
|
|
|
|
|
# collects emulation operating systems
|
|
|
|
|
@operating_systems = {
|
2015-09-23 13:49:19 +00:00
|
|
|
|
arch: { family: 'arch', release: nil, arch: nil },
|
2015-09-22 16:31:21 +00:00
|
|
|
|
centos5: { family: 'redhat', release: '5.11', arch: 'x86_64' },
|
|
|
|
|
centos6: { family: 'redhat', release: '6.6', arch: 'x86_64' },
|
|
|
|
|
centos7: { family: 'redhat', release: '7.1.1503', arch: 'x86_64' },
|
2015-09-23 13:49:19 +00:00
|
|
|
|
debian6: { family: 'debian', release: '6', arch: 'x86_64' },
|
|
|
|
|
debian7: { family: 'debian', release: '7', arch: 'x86_64' },
|
|
|
|
|
debian8: { family: 'debian', release: '8', arch: 'x86_64' },
|
2015-09-22 16:31:21 +00:00
|
|
|
|
freebsd9: { family: 'freebsd', release: '9', arch: 'amd64' },
|
|
|
|
|
freebsd10: { family: 'freebsd', release: '10', arch: 'amd64' },
|
2015-09-23 13:49:19 +00:00
|
|
|
|
osx104: { family: 'darwin', release: '10.10.4', arch: nil, name: 'mac_os_x' },
|
|
|
|
|
ubuntu1204: { family: 'ubuntu', release: '12.04', arch: 'x86_64' },
|
|
|
|
|
ubuntu1404: { family: 'ubuntu', release: '14.04', arch: 'x86_64' },
|
2015-09-23 14:44:50 +00:00
|
|
|
|
ubuntu1504: { family: 'ubuntu', release: '15.04', arch: 'x86_64' },
|
2015-09-23 13:49:19 +00:00
|
|
|
|
windows: { family: 'windows', release: nil, arch: nil },
|
2015-12-03 03:08:49 +00:00
|
|
|
|
wrlinux: { family: 'wrlinux', release: '7.0(3)I2(2)', arch: 'x86_64' },
|
2015-09-23 12:54:43 +00:00
|
|
|
|
undefined: { family: nil, release: nil, arch: nil },
|
2015-09-22 16:31:21 +00:00
|
|
|
|
}
|
2015-09-05 15:40:02 +00:00
|
|
|
|
|
2015-09-22 16:31:21 +00:00
|
|
|
|
# selects operating system
|
2015-09-22 17:35:38 +00:00
|
|
|
|
@os = @operating_systems[os || :ubuntu1404]
|
2015-09-22 16:31:21 +00:00
|
|
|
|
end
|
2015-09-06 18:24:25 +00:00
|
|
|
|
|
2015-10-05 21:22:13 +00:00
|
|
|
|
def backend
|
|
|
|
|
return @backend if defined?(@backend)
|
2015-09-22 16:31:21 +00:00
|
|
|
|
scriptpath = ::File.realpath(::File.dirname(__FILE__))
|
2015-09-06 18:24:25 +00:00
|
|
|
|
|
2015-09-22 16:31:21 +00:00
|
|
|
|
# create mock backend
|
2015-10-26 03:04:18 +00:00
|
|
|
|
@backend = Inspec::Backend.create({ backend: :mock })
|
2015-10-14 21:44:15 +00:00
|
|
|
|
mock = @backend.backend
|
|
|
|
|
|
|
|
|
|
# set os emulation
|
|
|
|
|
mock.mock_os(@os)
|
2015-09-03 15:33:19 +00:00
|
|
|
|
|
2015-09-22 16:31:21 +00:00
|
|
|
|
# create all mock files
|
2015-10-14 21:13:49 +00:00
|
|
|
|
local = Train.create('local').connection
|
2015-09-22 16:31:21 +00:00
|
|
|
|
mockfile = lambda { |x|
|
|
|
|
|
path = ::File.join(scriptpath, '/unit/mock/files', x)
|
|
|
|
|
local.file(path)
|
|
|
|
|
}
|
2015-10-09 13:07:58 +00:00
|
|
|
|
mockdir = lambda { |x|
|
|
|
|
|
md = Object.new
|
2015-10-09 17:55:16 +00:00
|
|
|
|
|
|
|
|
|
class << md
|
|
|
|
|
attr_accessor :isdir
|
|
|
|
|
end
|
|
|
|
|
md.isdir = x
|
|
|
|
|
|
2015-10-09 13:07:58 +00:00
|
|
|
|
def md.directory?
|
2015-10-09 17:55:16 +00:00
|
|
|
|
isdir
|
2015-10-09 13:07:58 +00:00
|
|
|
|
end
|
|
|
|
|
md
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 21:22:13 +00:00
|
|
|
|
mock.files = {
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'/proc/net/bonding/bond0' => mockfile.call('bond0'),
|
|
|
|
|
'/etc/ssh/ssh_config' => mockfile.call('ssh_config'),
|
|
|
|
|
'/etc/ssh/sshd_config' => mockfile.call('sshd_config'),
|
|
|
|
|
'/etc/passwd' => mockfile.call('passwd'),
|
|
|
|
|
'/etc/ntp.conf' => mockfile.call('ntp.conf'),
|
|
|
|
|
'/etc/login.defs' => mockfile.call('login.defs'),
|
|
|
|
|
'/etc/security/limits.conf' => mockfile.call('limits.conf'),
|
|
|
|
|
'/etc/inetd.conf' => mockfile.call('inetd.conf'),
|
2015-10-06 11:47:34 +00:00
|
|
|
|
'/etc/group' => mockfile.call('etcgroup'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'/etc/audit/auditd.conf' => mockfile.call('auditd.conf'),
|
|
|
|
|
'/etc/mysql/my.cnf' => mockfile.call('mysql.conf'),
|
|
|
|
|
'/etc/mysql/mysql2.conf' => mockfile.call('mysql2.conf'),
|
|
|
|
|
'kitchen.yml' => mockfile.call('kitchen.yml'),
|
|
|
|
|
'example.csv' => mockfile.call('example.csv'),
|
|
|
|
|
'policyfile.lock.json' => mockfile.call('policyfile.lock.json'),
|
2015-10-09 13:07:58 +00:00
|
|
|
|
'/sys/class/net/br0/bridge' => mockdir.call(true),
|
2015-11-24 12:06:31 +00:00
|
|
|
|
'rootwrap.conf' => mockfile.call('rootwrap.conf'),
|
2015-12-04 07:59:44 +00:00
|
|
|
|
'/etc/apache2/apache2.conf' => mockfile.call('apache2.conf'),
|
|
|
|
|
'/etc/apache2/ports.conf' => mockfile.call('ports.conf'),
|
|
|
|
|
'/etc/apache2/conf-enabled/serve-cgi-bin.conf' => mockfile.call('serve-cgi-bin.conf'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
}
|
2015-09-03 15:33:19 +00:00
|
|
|
|
|
2015-09-22 16:31:21 +00:00
|
|
|
|
# create all mock commands
|
|
|
|
|
cmd = lambda {|x|
|
|
|
|
|
stdout = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x))
|
2015-10-14 21:44:15 +00:00
|
|
|
|
mock.mock_command('', stdout, '', 0)
|
2015-09-22 16:31:21 +00:00
|
|
|
|
}
|
2015-10-08 10:11:55 +00:00
|
|
|
|
|
|
|
|
|
empty = lambda {
|
2015-10-14 21:44:15 +00:00
|
|
|
|
mock.mock_command('', '', '', 0)
|
2015-10-08 10:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 21:22:13 +00:00
|
|
|
|
mock.commands = {
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'ps aux' => cmd.call('ps-aux'),
|
2015-12-17 15:08:28 +00:00
|
|
|
|
'Get-Content win_secpol.cfg' => cmd.call('secedit-export'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'secedit /export /cfg win_secpol.cfg' => cmd.call('success'),
|
2015-12-17 15:08:28 +00:00
|
|
|
|
'Remove-Item win_secpol.cfg' => cmd.call('success'),
|
2015-11-13 10:53:21 +00:00
|
|
|
|
'env' => cmd.call('env'),
|
2015-11-19 02:50:38 +00:00
|
|
|
|
'$Env:PATH' => cmd.call('$env-PATH'),
|
2015-11-17 21:10:59 +00:00
|
|
|
|
# registry key test
|
|
|
|
|
'2790db1e88204a073ed7fd3493f5445e5ce531afd0d2724a0e36c17110c535e6' => cmd.call('reg_schedule'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'),
|
|
|
|
|
'/sbin/auditctl -l' => cmd.call('auditctl'),
|
|
|
|
|
'yum -v repolist all' => cmd.call('yum-repolist-all'),
|
|
|
|
|
'dpkg -s curl' => cmd.call('dpkg-s-curl'),
|
|
|
|
|
'rpm -qia curl' => cmd.call('rpm-qia-curl'),
|
2015-09-23 13:49:19 +00:00
|
|
|
|
'pacman -Qi curl' => cmd.call('pacman-qi-curl'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'),
|
|
|
|
|
'npm ls -g --json bower' => cmd.call('npm-ls-g--json-bower'),
|
|
|
|
|
'pip show jinja2' => cmd.call('pip-show-jinja2'),
|
2015-10-02 09:09:47 +00:00
|
|
|
|
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package-firefox'),
|
|
|
|
|
"Get-Package -Name 'Ruby 2.1.6-p336-x64' | ConvertTo-Json" => cmd.call('get-package-ruby'),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
|
|
|
|
|
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
|
|
|
|
|
'lsmod' => cmd.call('lsmod'),
|
|
|
|
|
'/sbin/sysctl -q -n net.ipv4.conf.all.forwarding' => cmd.call('sbin_sysctl'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# ports on windows
|
2015-09-23 13:23:33 +00:00
|
|
|
|
'Get-NetTCPConnection | Select-Object -Property State, Caption, Description, LocalAddress, LocalPort, RemoteAddress, RemotePort, DisplayName, Status | ConvertTo-Json' => cmd.call('get-net-tcpconnection'),
|
2016-01-06 19:14:36 +00:00
|
|
|
|
# lsof formatted list of ports (should be quite cross platform)
|
2015-12-23 21:45:41 +00:00
|
|
|
|
'lsof -nP -i -FpctPn' => cmd.call('lsof-nP-i-FpctPn'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# ports on linux
|
2015-09-23 13:19:48 +00:00
|
|
|
|
'netstat -tulpen' => cmd.call('netstat-tulpen'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# ports on freebsd
|
2015-09-23 13:24:46 +00:00
|
|
|
|
'sockstat -46l' => cmd.call('sockstat'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# packages on windows
|
2015-09-23 14:11:48 +00:00
|
|
|
|
"Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq 'Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.6161'} | Select-Object -Property Name,Version,Vendor,PackageCode,Caption,Description | ConvertTo-Json" => cmd.call('win32_product'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# service status upstart on ubuntu
|
2015-09-23 20:32:31 +00:00
|
|
|
|
'initctl status ssh' => cmd.call('initctl-status-ssh'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# service config for upstart on ubuntu
|
2015-09-23 20:32:31 +00:00
|
|
|
|
'initctl show-config ssh' => cmd.call('initctl-show-config-ssh'),
|
2015-09-25 09:51:21 +00:00
|
|
|
|
# show ssh service Centos 7
|
|
|
|
|
'systemctl show --all sshd' => cmd.call('systemctl-show-all-sshd'),
|
2015-09-25 10:51:12 +00:00
|
|
|
|
# services on macos
|
|
|
|
|
'launchctl list' => cmd.call('launchctl-list'),
|
2015-09-25 10:53:40 +00:00
|
|
|
|
# services on freebsd 10
|
|
|
|
|
'service -e' => cmd.call('service-e'),
|
|
|
|
|
'service sendmail onestatus' => cmd.call('service-sendmail-onestatus'),
|
2015-09-25 10:58:03 +00:00
|
|
|
|
# services for system 5 e.g. centos6, debian 6
|
|
|
|
|
'service sshd status' => cmd.call('service-sshd-status'),
|
2015-10-08 09:44:56 +00:00
|
|
|
|
'find /etc/rc*.d -name S*' => cmd.call('find-etc-rc-d-name-S'),
|
2015-09-25 10:58:03 +00:00
|
|
|
|
'ls -1 /etc/init.d/' => cmd.call('ls-1-etc-init.d'),
|
2015-10-05 09:21:25 +00:00
|
|
|
|
# user information for linux
|
|
|
|
|
'id root' => cmd.call('id-root'),
|
|
|
|
|
'getent passwd root' => cmd.call('getent-passwd-root'),
|
2015-10-08 09:44:56 +00:00
|
|
|
|
'chage -l root' => cmd.call('chage-l-root'),
|
2015-11-24 15:18:55 +00:00
|
|
|
|
# user information for ldap test
|
|
|
|
|
'id jfolmer' => cmd.call('id-jfolmer'),
|
|
|
|
|
'getent passwd jfolmer' => cmd.call('getent-passwd-jfolmer'),
|
|
|
|
|
'chage -l jfolmer' => cmd.call('chage-l-root'),
|
2015-10-05 09:21:25 +00:00
|
|
|
|
# user info for mac
|
|
|
|
|
'id chartmann' => cmd.call('id-chartmann'),
|
|
|
|
|
'dscl -q . -read /Users/chartmann NFSHomeDirectory PrimaryGroupID RecordName UniqueID UserShell' => cmd.call('dscl'),
|
|
|
|
|
# user info for freebsd
|
|
|
|
|
'pw usershow root -7' => cmd.call('pw-usershow-root-7'),
|
|
|
|
|
# user info for windows
|
|
|
|
|
'650b6b72a66316418b25421a54afe21a230704558082914c54711904bb10e370' => cmd.call('GetUserAccount'),
|
2015-10-07 10:04:48 +00:00
|
|
|
|
# group info for windows
|
2015-10-08 09:44:56 +00:00
|
|
|
|
'Get-WmiObject Win32_Group | Select-Object -Property Caption, Domain, Name, SID, LocalAccount | ConvertTo-Json' => cmd.call('GetWin32Group'),
|
2015-10-08 10:11:55 +00:00
|
|
|
|
# network interface
|
|
|
|
|
'9e80f048a1af5a0f6ab8a465e46ea5ed5ba6587e9b5e54a7a0c0a1a02bb6f663' => cmd.call('find-net-interface'),
|
|
|
|
|
'c33821dece09c8b334e03a5bb9daefdf622007f73af4932605e758506584ec3f' => empty.call,
|
2015-10-08 11:01:09 +00:00
|
|
|
|
'Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, Status, State, MacAddress, LinkSpeed, ReceiveLinkSpeed, TransmitLinkSpeed, Virtual | ConvertTo-Json' => cmd.call('Get-NetAdapter'),
|
2015-10-09 13:26:59 +00:00
|
|
|
|
# bridge on linux
|
2015-10-09 13:07:58 +00:00
|
|
|
|
'ls -1 /sys/class/net/br0/brif/' => cmd.call('ls-sys-class-net-br'),
|
2015-10-09 13:26:59 +00:00
|
|
|
|
# bridge on Windows
|
|
|
|
|
'Get-NetAdapterBinding -ComponentID ms_bridge | Get-NetAdapter | Select-Object -Property Name, InterfaceDescription | ConvertTo-Json' => cmd.call('get-netadapter-binding-bridge'),
|
2015-10-09 17:21:55 +00:00
|
|
|
|
# host for Windows
|
|
|
|
|
'Resolve-DnsName –Type A microsoft.com | ConvertTo-Json' => cmd.call('Resolve-DnsName'),
|
|
|
|
|
'Test-NetConnection -ComputerName microsoft.com | Select-Object -Property ComputerName, PingSucceeded | ConvertTo-Json' => cmd.call('Test-NetConnection'),
|
2015-10-09 17:55:06 +00:00
|
|
|
|
# host for Linux
|
|
|
|
|
'getent hosts example.com' => cmd.call('getent-hosts-example.com'),
|
|
|
|
|
'ping -w 1 -c 1 example.com' => cmd.call('ping-example.com'),
|
2015-10-10 17:54:00 +00:00
|
|
|
|
# apt
|
|
|
|
|
"find /etc/apt/ -name *.list -exec sh -c 'cat {} || echo -n' \\;" => cmd.call('etc-apt'),
|
2015-10-12 08:32:14 +00:00
|
|
|
|
# iptables
|
|
|
|
|
'iptables -S' => cmd.call('iptables-s'),
|
2015-12-04 07:59:44 +00:00
|
|
|
|
# apache_conf
|
|
|
|
|
'find /etc/apache2/ports.conf -maxdepth 1 -type f' => cmd.call('find-apache2-ports-conf'),
|
|
|
|
|
'find /etc/apache2/conf-enabled/*.conf -maxdepth 1 -type f' => cmd.call('find-apache2-conf-enabled'),
|
2015-12-31 00:10:06 +00:00
|
|
|
|
# mount
|
|
|
|
|
"mount | grep -- ' on /'" => cmd.call("mount"),
|
2016-01-02 23:01:26 +00:00
|
|
|
|
"mount | grep -- ' on /mnt/iso-disk'" => cmd.call("mount-multiple"),
|
2015-09-22 16:31:21 +00:00
|
|
|
|
}
|
2015-09-03 15:33:19 +00:00
|
|
|
|
|
2015-10-05 21:22:13 +00:00
|
|
|
|
@backend
|
|
|
|
|
end
|
2015-09-22 16:31:21 +00:00
|
|
|
|
|
2015-10-05 21:22:13 +00:00
|
|
|
|
# loads a resource class and instantiates the class with the given arguments
|
|
|
|
|
def load_resource(resource, *args)
|
2015-09-22 16:31:21 +00:00
|
|
|
|
# initialize resource with backend and parameters
|
2015-10-26 03:04:18 +00:00
|
|
|
|
@resource_class = Inspec::Resource.registry[resource]
|
2015-10-07 21:59:26 +00:00
|
|
|
|
@resource = @resource_class.new(backend, resource, *args)
|
2015-09-22 16:31:21 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def load_resource(*args)
|
|
|
|
|
m = MockLoader.new(:ubuntu1404)
|
|
|
|
|
m.send('load_resource', *args)
|
2015-09-03 15:33:19 +00:00
|
|
|
|
end
|