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

(MODULES-4200) Add simple sanity check for the rule to hash parser #666

Merged
merged 5 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/puppet/provider/firewall/iptables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,27 @@ def self.rule_to_hash(line, table, counter)
end

# Manually remove chain
values.slice!('-A')
keys << :chain
if values =~ /(\s|^)-A\s/
values = values.sub(/(\s|^)-A\s/, '\1')
keys << :chain
end

# Manually remove table (used in some tests)
if values =~ /^-t\s/
values = values.sub(/^-t\s/, '')
keys << :table
end

valrev = values.scan(/("([^"\\]|\\.)*"|\S+)/).transpose[0].reverse

if keys.length != valrev.length then
raise "Parser error: keys (#{keys.length}) and values (#{valrev.length}) count mismatch on line: #{line}"
end

# Here we generate the main hash by scanning arguments off the values
# string, handling any quoted characters present in the value, and then
# zipping the values with the array of keys.
keys.zip(values.scan(/("([^"\\]|\\.)*"|\S+)/).transpose[0].reverse) do |f, v|
keys.zip(valrev) do |f, v|
if v =~ /^".*"$/ then
hash[f] = v.sub(/^"(.*)"$/, '\1').gsub(/\\(\\|'|")/, '\1')
else
Expand Down
8 changes: 7 additions & 1 deletion spec/fixtures/iptables/conversion_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@
:proto => "tcp",
},
},
'parser_sanity_check' => {
:line => '-A INPUT -s 1.2.3.4/32 -p tcp -m tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -m comment --comment "004 parser sanity check" -j ACCEPT',
:table => 'filter',
:raise_error => true,
:params => {},
},
}

# This hash is for testing converting a hash to an argument line.
Expand Down Expand Up @@ -1275,4 +1281,4 @@
},
:args => ["-t", :filter, "-s", "1.2.3.4/32", "-d", "4.3.2.1/32", "-p", :tcp, "-m", "comment", "--comment", "003 nfqueue dont specify queue_num or queue_bypass", "-j", "NFQUEUE"]
}
}
}
7 changes: 7 additions & 0 deletions spec/unit/puppet/provider/iptables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@
ARGS_TO_HASH.each do |test_name,data|
describe "for test data '#{test_name}'" do
let(:resource) { provider.rule_to_hash(data[:line], data[:table], 0) }
# If this option is enabled, make sure the error was raised
if data[:raise_error] then
it "the input rules should raise an error by rules_to_hash" do
expect{ resource }.to raise_error
end
end

# If this option is enabled, make sure the parameters exactly match
if data[:compare_all] then
it "the parameter hash keys should be the same as returned by rules_to_hash" do
Expand Down