Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfixes for json dummy module #153

Merged
merged 13 commits into from
Oct 26, 2015
12 changes: 6 additions & 6 deletions docs/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/inspec/profile_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def create_outer_dsl(dsl)
__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
Expand Down
4 changes: 4 additions & 0 deletions lib/resources/etc_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def to_s

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)
Expand Down
4 changes: 4 additions & 0 deletions lib/resources/group_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def gpo(policy_path, policy_name)
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']}')"
Expand Down
2 changes: 1 addition & 1 deletion lib/resources/inetd_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# end

class InetdConf < Inspec.resource(1)
name 'inetd_config'
name 'inetd_conf'

def initialize(path = nil)
@conf_path = path || '/etc/inetd.conf'
Expand Down
4 changes: 2 additions & 2 deletions lib/resources/mysql_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/resources/mysql_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
8 changes: 4 additions & 4 deletions lib/resources/parse_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -64,7 +64,7 @@ def to_s
class PConfigFile < PConfig
name 'parse_config_file'

def initialize(path, opts)
def initialize(path, opts = nil)
super(nil, opts)
parse_file(path)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/resources/postgres_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/resources/postgres_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ def to_s
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
Expand Down
1 change: 1 addition & 0 deletions lib/resources/processes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def ps_aux
# 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
Expand Down
32 changes: 17 additions & 15 deletions lib/utils/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,11 @@
module ContentParser
# parse etc/passwd file
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
def parse_passwd_line(line)
x = line.split(':')
{
'name' => x.at(0),
'password' => x.at(1),
'uid' => x.at(2),
'gid' => x.at(3),
'desc' => x.at(4),
'home' => x.at(5),
'shell' => x.at(6),
}
end

def parse_comment_line(raw, opts)
idx_nl = raw.index("\n")
idx_comment = raw.index(opts[:comment_char])
Expand All @@ -46,4 +32,20 @@ def parse_comment_line(raw, opts)
end
[line, idx_nl]
end

private

# parse a etc/passwd line
def parse_passwd_line(line)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this public again. it is directly used in user resource https://github.com/chef/inspec/blob/master/lib/resources/user.rb#L222

x = line.split(':')
{
'name' => x.at(0),
'password' => x.at(1),
'uid' => x.at(2),
'gid' => x.at(3),
'desc' => x.at(4),
'home' => x.at(5),
'shell' => x.at(6),
}
end
end
3 changes: 2 additions & 1 deletion lib/utils/simpleconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ def initialize(raw_data, opts = {})
# 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?
Expand Down
2 changes: 1 addition & 1 deletion test/unit/resources/inetd_conf_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/resources/processes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
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 [{
Expand Down
30 changes: 30 additions & 0 deletions test/unit/utils/content_parser_test.rb
Original file line number Diff line number Diff line change
@@ -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