mirror of
https://github.com/inspec/inspec
synced 2024-11-14 08:57:11 +00:00
Merge pull request #153 from chef/dummy-tests
Merged change 6ce18d67-c6d7-4488-89a3-3b48ed93be37 From review branch dummy-tests into master Signed-off-by: chartmann <chartmann@chef.io>
This commit is contained in:
commit
91c898e5c4
16 changed files with 87 additions and 23 deletions
|
@ -41,7 +41,7 @@ In addition to the open source resources, Chef Compliance ships with additional
|
|||
* ``csv``
|
||||
* ``etc_group``
|
||||
* ``group_policy``
|
||||
* ``inetd_config``
|
||||
* ``inetd_conf``
|
||||
* ``json``
|
||||
* ``limits_conf``
|
||||
* ``login_defs``
|
||||
|
@ -1710,17 +1710,17 @@ The following examples show how to use this InSpec resource.
|
|||
|
||||
|
||||
|
||||
inetd_config -- DONE
|
||||
inetd_conf -- DONE
|
||||
=====================================================
|
||||
Use the ``inetd_config`` InSpec resource to test if a service is enabled in the ``inetd.conf`` file on |linux| and |unix| platforms. |inetd|---the Internet service daemon---listens on dedicated ports, and then loads the appropriate program based on a request. The ``inetd.conf`` file is typically located at ``/etc/inetd.conf`` and contains a list of Internet services associated to the ports on which that service will listen. Only enabled services may handle a request; only services that are required by the system should be enabled.
|
||||
Use the ``inetd_conf`` InSpec resource to test if a service is enabled in the ``inetd.conf`` file on |linux| and |unix| platforms. |inetd|---the Internet service daemon---listens on dedicated ports, and then loads the appropriate program based on a request. The ``inetd.conf`` file is typically located at ``/etc/inetd.conf`` and contains a list of Internet services associated to the ports on which that service will listen. Only enabled services may handle a request; only services that are required by the system should be enabled.
|
||||
|
||||
Syntax -- DONE
|
||||
-----------------------------------------------------
|
||||
A ``inetd_config`` InSpec resource block declares the list of services that should be disabled in the ``inetd.conf`` file:
|
||||
A ``inetd_conf`` InSpec resource block declares the list of services that should be disabled in the ``inetd.conf`` file:
|
||||
|
||||
.. code-block:: ruby
|
||||
|
||||
describe inetd_config('path') do
|
||||
describe inetd_conf('path') do
|
||||
its('service_name') { should eq 'value' }
|
||||
end
|
||||
|
||||
|
@ -1777,7 +1777,7 @@ and the following test is defined:
|
|||
|
||||
.. code-block:: ruby
|
||||
|
||||
describe inetd_config do
|
||||
describe inetd_conf do
|
||||
its('ftp') { should eq nil }
|
||||
its('telnet') { should eq nil }
|
||||
end
|
||||
|
|
|
@ -102,6 +102,11 @@ module Inspec
|
|||
__register_rule rule, &block
|
||||
end
|
||||
|
||||
# TODO: mock method for attributes; import attribute handling
|
||||
define_method :attributes do |_name, _options|
|
||||
nil
|
||||
end
|
||||
|
||||
def skip_rule(id)
|
||||
__unregister_rule id
|
||||
end
|
||||
|
|
|
@ -91,6 +91,10 @@ class EtcGroup < Inspec.resource(1)
|
|||
|
||||
def parse_group(path)
|
||||
@content = inspec.file(path).content
|
||||
if @content.nil?
|
||||
skip_resource "Can't access group file in #{path}"
|
||||
return []
|
||||
end
|
||||
# iterate over each line and filter comments
|
||||
@content.split("\n").each_with_object([]) do |line, lines|
|
||||
grp_info = parse_group_line(line)
|
||||
|
|
|
@ -18,6 +18,10 @@ end
|
|||
class GroupPolicy < Inspec.resource(1)
|
||||
name 'group_policy'
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def get_registry_value(entry)
|
||||
keys = entry['registry_information'][0]
|
||||
cmd = "(Get-Item 'Registry::#{keys['path']}').GetValue('#{keys['key']}')"
|
||||
|
|
|
@ -15,7 +15,7 @@ require 'utils/simpleconfig'
|
|||
# end
|
||||
|
||||
class InetdConf < Inspec.resource(1)
|
||||
name 'inetd_config'
|
||||
name 'inetd_conf'
|
||||
|
||||
def initialize(path = nil)
|
||||
@conf_path = path || '/etc/inetd.conf'
|
||||
|
|
|
@ -31,8 +31,8 @@ class MysqlConf < Inspec.resource(1)
|
|||
|
||||
include FindFiles
|
||||
|
||||
def initialize(conf_path)
|
||||
@conf_path = conf_path
|
||||
def initialize(conf_path = nil)
|
||||
@conf_path = conf_path || inspec.mysql.conf_path
|
||||
@files_contents = {}
|
||||
@content = nil
|
||||
@params = nil
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
class MysqlSession < Inspec.resource(1)
|
||||
name 'mysql_session'
|
||||
|
||||
def initialize(user, pass)
|
||||
def initialize(user = nil, pass = nil)
|
||||
@user = user
|
||||
@pass = pass
|
||||
init_fallback if user.nil? or pass.nil?
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
class PConfig < Inspec.resource(1)
|
||||
name 'parse_config'
|
||||
|
||||
def initialize(content = nil, useropts = {})
|
||||
default_options = {}
|
||||
@opts = default_options.merge(useropts)
|
||||
def initialize(content = nil, useropts = nil)
|
||||
@opts = {}
|
||||
@opts = useropts.dup unless useropts.nil?
|
||||
@files_contents = {}
|
||||
@params = nil
|
||||
|
||||
|
@ -64,7 +64,7 @@ end
|
|||
class PConfigFile < PConfig
|
||||
name 'parse_config_file'
|
||||
|
||||
def initialize(path, opts)
|
||||
def initialize(path, opts = nil)
|
||||
super(nil, opts)
|
||||
parse_file(path)
|
||||
end
|
||||
|
|
|
@ -13,8 +13,8 @@ class PostgresConf < Inspec.resource(1)
|
|||
|
||||
include FindFiles
|
||||
|
||||
def initialize(conf_path)
|
||||
@conf_path = conf_path
|
||||
def initialize(conf_path = nil)
|
||||
@conf_path = conf_path || inspec.postgres.conf_path
|
||||
@conf_dir = File.expand_path(File.dirname @conf_path)
|
||||
@files_contents = {}
|
||||
@content = nil
|
||||
|
|
|
@ -23,13 +23,15 @@ class Lines
|
|||
end
|
||||
end
|
||||
|
||||
class PostgresSession
|
||||
class PostgresSession < Inspec.resource(1)
|
||||
name 'postgres_session'
|
||||
|
||||
def initialize(user, pass)
|
||||
@user = user || 'postgres'
|
||||
@pass = pass
|
||||
end
|
||||
|
||||
def describe(query, db = [], &block)
|
||||
def query(query, db = [], &block)
|
||||
dbs = db.map { |x| "-d #{x}" }.join(' ')
|
||||
# TODO: simple escape, must be handled by a library
|
||||
# that does this securely
|
||||
|
|
|
@ -31,6 +31,7 @@ class Processes < Inspec.resource(1)
|
|||
# get all running processes
|
||||
cmd = inspec.command('ps aux')
|
||||
all = cmd.stdout.split("\n")[1..-1]
|
||||
return [] if all.nil?
|
||||
|
||||
lines = all.map do |line|
|
||||
# user 32296 0.0 0.0 42592 7972 pts/15 Ss+ Apr06 0:00 zsh
|
||||
|
|
|
@ -3,14 +3,20 @@
|
|||
# author: Dominik Richter
|
||||
|
||||
module ContentParser
|
||||
# parse etc/passwd file
|
||||
# Parse /etc/passwd files.
|
||||
#
|
||||
# @param [String] content the raw content of /etc/passwd
|
||||
# @return [Array] Collection of passwd entries
|
||||
def parse_passwd(content)
|
||||
content.split("\n").map do |line|
|
||||
content.to_s.split("\n").map do |line|
|
||||
parse_passwd_line(line)
|
||||
end
|
||||
end
|
||||
|
||||
# parse a etc/passwd line
|
||||
# Parse a line of /etc/passwd
|
||||
#
|
||||
# @param [String] line a line of /etc/passwd
|
||||
# @return [Hash] Map of entries in this line
|
||||
def parse_passwd_line(line)
|
||||
x = line.split(':')
|
||||
{
|
||||
|
@ -24,6 +30,12 @@ module ContentParser
|
|||
}
|
||||
end
|
||||
|
||||
# Parse a line with a command. For example: `a = b # comment`.
|
||||
# Retrieves the actual content.
|
||||
#
|
||||
# @param [String] raw the content lines you want to be parsed
|
||||
# @param [Hash] opts optional configuration
|
||||
# @return [Array] contains the actual line and the position of the line end
|
||||
def parse_comment_line(raw, opts)
|
||||
idx_nl = raw.index("\n")
|
||||
idx_comment = raw.index(opts[:comment_char])
|
||||
|
|
|
@ -21,11 +21,12 @@ class SimpleConfig
|
|||
# comment_char: char which identifies comments
|
||||
# standalone_comments: comments must appear alone in a line; if set to true,
|
||||
# no comments can be added to the end of an assignment/statement line
|
||||
def parse(raw_data, opts = {})
|
||||
def parse(raw_data, opts = nil)
|
||||
@params = {}
|
||||
@groups = []
|
||||
@vals = @params
|
||||
options = default_options.merge(opts || {})
|
||||
return if raw_data.nil?
|
||||
|
||||
# prepare raw data if required
|
||||
if !options[:line_separator].nil?
|
||||
|
|
|
@ -7,7 +7,7 @@ require 'inspec/resource'
|
|||
|
||||
describe 'Inspec::Resources::InetdConf' do
|
||||
it 'verify limits.conf config parsing' do
|
||||
resource = load_resource('inetd_config')
|
||||
resource = load_resource('inetd_conf')
|
||||
_(resource.send('shell')).must_equal nil
|
||||
_(resource.send('login')).must_equal nil
|
||||
_(resource.send('ftp')).must_equal %w{stream tcp nowait root /usr/sbin/in.ftpd in.ftpd}
|
||||
|
|
|
@ -6,6 +6,11 @@ require 'helper'
|
|||
require 'inspec/resource'
|
||||
|
||||
describe 'Inspec::Resources::Processes' do
|
||||
it 'handles empty process results' do
|
||||
resource = load_resource('processes', 'nothing')
|
||||
_(resource.list).must_equal []
|
||||
end
|
||||
|
||||
it 'verify processes resource' do
|
||||
resource = load_resource('processes', '/bin/bash')
|
||||
_(resource.list).must_equal [{
|
||||
|
|
30
test/unit/utils/content_parser_test.rb
Normal file
30
test/unit/utils/content_parser_test.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
describe ContentParser do
|
||||
let (:parser) { Class.new() { include ContentParser }.new }
|
||||
|
||||
describe '#parse_passwd' do
|
||||
it 'parses nil content' do
|
||||
parser.parse_passwd(nil).must_equal([])
|
||||
end
|
||||
|
||||
it 'parses an empty passwd line' do
|
||||
parser.parse_passwd('').must_equal([])
|
||||
end
|
||||
|
||||
it 'parses a valid passwd line' do
|
||||
info = [{
|
||||
"name"=>"root",
|
||||
"password"=>"x",
|
||||
"uid"=>"0",
|
||||
"gid"=>"0",
|
||||
"desc"=>"root",
|
||||
"home"=>"/root",
|
||||
"shell"=>"/bin/sh"
|
||||
}]
|
||||
parser.parse_passwd('root:x:0:0:root:/root:/bin/sh').must_equal(info)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue