rename SimpleConfig / parse_config / parse_config_file options

See https://github.com/chef/inspec/issues/1709
Fixes https://github.com/chef/inspec/issues/1709

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2017-04-26 23:18:14 +02:00
parent 71a4f0d8f0
commit 1dafe50bd9
20 changed files with 95 additions and 49 deletions

View file

@ -20,7 +20,7 @@ or:
audit = command('/sbin/auditctl -l').stdout
options = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true
}
@ -39,15 +39,15 @@ where each test
This InSpec audit resource has the following matchers:
### assignment_re
### assignment_regex
Use `assignment_re` to test a key value using a regular expression:
Use `assignment_regex` to test a key value using a regular expression:
'key = value'
may be tested using the following regular expression, which determines assignment from key to value:
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
### be
@ -71,15 +71,15 @@ Use `comment_char` to test for comments in a configuration file:
<%= partial "/shared/matcher_include" %>
### key_vals
### key_values
Use `key_vals` to test how many values a key contains:
Use `key_values` to test how many values a key contains:
key = a b c
contains three values. To test that value to ensure it only contains one, use:
key_vals: 1
key_values: 1
### match

View file

@ -17,7 +17,7 @@ A `parse_config_file` InSpec audit resource block declares the location of the c
or:
options = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true
}
@ -37,7 +37,7 @@ where each test
This resource supports the following options for parsing configuration data. Use them in an `options` block stated outside of (and immediately before) the actual test:
options = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true
}
describe parse_config_file('path/to/file', options) do
@ -48,15 +48,15 @@ This resource supports the following options for parsing configuration data. Use
This InSpec audit resource has the following matchers:
### assignment_re
### assignment_regex
Use `assignment_re` to test a key value using a regular expression:
Use `assignment_regex` to test a key value using a regular expression:
'key = value'
may be tested using the following regular expression, which determines assignment from key to value:
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
### be
@ -80,15 +80,15 @@ Use `comment_char` to test for comments in a configuration file:
<%= partial "/shared/matcher_include" %>
### key_vals
### key_values
Use `key_vals` to test how many values a key contains:
Use `key_values` to test how many values a key contains:
key = a b c
contains three values. To test that value to ensure it only contains one, use:
key_vals: 1
key_values: 1
### match

View file

@ -82,7 +82,7 @@ module Inspec::Resources
# parse include file parameters
params = SimpleConfig.new(
raw_conf,
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
assignment_regex: /^\s*(\S+)\s+(.*)\s*$/,
multiple_values: true,
).params
@params.merge!(params)

View file

@ -12,7 +12,7 @@ module Inspec::Resources
def initialize(content)
@content = content
@opts = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true,
}
end
@ -27,7 +27,7 @@ module Inspec::Resources
def status(name)
@status_opts = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
}
@status_content ||= inspec.command('/sbin/auditctl -s').stdout.chomp

View file

@ -28,7 +28,7 @@ module Inspec::Resources
@content = @file.content
@params = SimpleConfig.new(
@file.content,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: true,
).params if @file.exist?
@loaded = true

View file

@ -50,8 +50,8 @@ module Inspec::Resources
# parse the file
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_vals: 6,
assignment_regex: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_values: 6,
multiple_values: false,
)
@params = conf.params

View file

@ -43,8 +43,8 @@ module Inspec::Resources
# parse the file
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_vals: 3,
assignment_regex: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_values: 3,
multiple_values: true,
)
@params = conf.params

View file

@ -55,7 +55,7 @@ module Inspec::Resources
# parse the file
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+)\s+(\S*)\s*$/,
assignment_regex: /^\s*(\S+)\s+(\S*)\s*$/,
multiple_values: false,
)
@params = conf.params

View file

@ -51,7 +51,7 @@ module Inspec::Resources
# parse the file
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
assignment_regex: /^\s*(\S+)\s+(.*)\s*$/,
multiple_values: true,
)
@params = conf.params

View file

@ -88,7 +88,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
# If the package is removed and not purged, Status is "deinstall ok config-files" with exit_status 0
@ -111,7 +111,7 @@ module Inspec::Resources
return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
# On some (all?) systems, the linebreak before the vendor line is missing
@ -161,7 +161,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
@ -261,7 +261,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
@ -282,7 +282,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params

View file

@ -8,7 +8,7 @@
#
# audit = command('/sbin/auditctl -l').stdout
# options = {
# assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
# assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
# multiple_values: true
# }
# describe parse_config(audit, options ) do
@ -26,7 +26,7 @@ module Inspec::Resources
output2 = command('curl http://127.0.0.1/php_status').stdout
# php status is in format 'key : value', and we do not allow for multiple values
options2 = {
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false
}

View file

@ -31,7 +31,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
@info[:name] = params['Name']

View file

@ -75,7 +75,7 @@ module Inspec::Resources
@content += raw_conf
opts = {
assignment_re: /^\s*([^=]*?)\s*=\s*[']?\s*(.*?)\s*[']?\s*$/,
assignment_regex: /^\s*([^=]*?)\s*=\s*[']?\s*(.*?)\s*[']?\s*$/,
}
params = SimpleConfig.new(raw_conf, opts).params
@params.merge!(params)

View file

@ -132,7 +132,7 @@ module Inspec::Resources
conf = SimpleConfig.new(
@content,
assignment_re: /^\s*(.*)=\s*(\S*)\s*$/,
assignment_regex: /^\s*(.*)=\s*(\S*)\s*$/,
)
@params = convert_hash(conf.params)
end

View file

@ -262,7 +262,7 @@ module Inspec::Resources
# parse data
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
multiple_values: false,
).params
@ -637,7 +637,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^(\w+)\s*(.*)$/,
assignment_regex: /^(\w+)\s*(.*)$/,
multiple_values: false,
).params

View file

@ -76,7 +76,7 @@ module Inspec::Resources
return @params = {} if read_content.nil?
conf = SimpleConfig.new(
read_content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s*$/,
assignment_regex: /^\s*(\S+?)\s+(.*?)\s*$/,
multiple_values: true,
)
@params = convert_hash(conf.params)

View file

@ -358,7 +358,7 @@ module Inspec::Resources
SimpleConfig.new(
line,
line_separator: ',',
assignment_re: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
assignment_regex: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/,
group_re: nil,
multiple_values: false,
).params
@ -372,7 +372,7 @@ module Inspec::Resources
# parse words
params = SimpleConfig.new(
parse_id_entries(cmd.stdout.chomp),
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
group_re: nil,
multiple_values: false,
).params
@ -419,7 +419,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
group_re: nil,
multiple_values: false,
).params
@ -512,7 +512,7 @@ module Inspec::Resources
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
group_re: nil,
multiple_values: false,
).params

View file

@ -55,16 +55,28 @@ class SimpleConfig
end
def parse_params_line(line, opts)
# Deprecation handling
if opts.key?(:assignment_re)
warn '[DEPRECATION] `:assignment_re` is deprecated in favor of `:assignment_regex` '\
'and will be removed in the next major version. See: https://github.com/chef/inspec/issues/1709'
opts[:assignment_regex] = opts[:assignment_re]
end
if opts.key?(:key_vals)
warn '[DEPRECATION] `:key_vals` is deprecated in favor of `:key_values` '\
'and will be removed in the next major version. See: https://github.com/chef/inspec/issues/1709'
opts[:key_values] = opts[:key_vals]
end
# now line contains what we are interested in parsing
# check if it is an assignment
m = opts[:assignment_re].match(line)
m = opts[:assignment_regex].match(line)
return nil if m.nil?
if opts[:multiple_values]
@vals[m[1]] ||= []
@vals[m[1]].push(parse_values(m, opts[:key_vals]))
@vals[m[1]].push(parse_values(m, opts[:key_values]))
else
@vals[m[1]] = parse_values(m, opts[:key_vals])
@vals[m[1]] = parse_values(m, opts[:key_values])
end
end
@ -111,9 +123,9 @@ class SimpleConfig
multiline: false,
comment_char: '#',
line_separator: nil, # uses this char to seperate lines before parsing
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
group_re: /\[([^\]]+)\]\s*$/,
key_vals: 1, # default for key=value, may require for 'key val1 val2 val3'
key_values: 1, # default for key=value, may require for 'key val1 val2 val3'
standalone_comments: false,
multiple_values: false,
}

View file

@ -5,7 +5,7 @@ describe 'Inspec::Resources::ParseConfig' do
it 'verify parse_config resource' do
options = {
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
}
resource = MockLoader.new(:centos6).load_resource('parse_config', 'kernel.domainname = example.com', options)
result = {"kernel.domainname"=>"example.com"}
@ -16,7 +16,7 @@ describe 'Inspec::Resources::ParseConfig' do
it 'verify parse_config_file resource' do
options = {
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
}
resource = MockLoader.new(:centos6).load_resource('parse_config_file', '/etc/sysctl.conf', options)
result = {"kernel.domainname"=>"example.com"}

View file

@ -84,4 +84,38 @@ describe 'SimpleConfig Default Parser' do
cur.params['section2'].key2.must_equal('value2')
cur.params['section2'].missing_key.must_be_nil
end
it 'supports :assignment_regex for specifying the assignment' do
cur = SimpleConfig.new("key:::val", assignment_regex: /^(.*):::(.*)$/)
cur.params.must_equal({'key' => 'val'})
end
it 'supports :assignment_re for specifying the assignment with a deprecation warning' do
assert_output(nil, /DEPRECATION/) do
cur = SimpleConfig.new("key:::val", assignment_re: /^(.*):::(.*)$/)
cur.params.must_equal({'key' => 'val'})
end
end
it 'only reads the first assignment match group by default' do
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/)
cur.params.must_equal({'1' => '2'})
end
it 'supports :key_values for specifying the number of values' do
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/, key_values: 2)
cur.params.must_equal({'1' => ['2', '3']})
end
it 'supports :key_values with more values than match groups' do
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/, key_values: 4)
cur.params.must_equal({'1' => ['2', '3', nil, nil]})
end
it 'supports :key_vals for specifying the assignment with a deprecation warning' do
assert_output(nil, /DEPRECATION/) do
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/, key_vals: 2)
cur.params.must_equal({'1' => ['2', '3']})
end
end
end