Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 175ef16

Browse files
committed
Merge pull request #35 from Kasen/master
The mechanism of weights added
2 parents 34f9345 + eb6bcda commit 175ef16

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ By default rules are added to the filter table but the nat and mangle tables are
100100
rule "-p tcp -o eth0 -d 10/8 --jump REJECT --reject-with tcp-reset"
101101
end
102102

103+
By default rules are added to the chain, in the order in which its occur in the recipes.
104+
You may use the weight parameter for control the order of the rules in chains. For example:
105+
106+
simple_iptables_rule "reject" do
107+
chain "INPUT"
108+
rule ""
109+
jump "REJECT --reject-with icmp-host-prohibited"
110+
weight 90
111+
end
112+
113+
simple_iptables_rule "established" do
114+
chain "INPUT"
115+
rule "-m conntrack --ctstate ESTABLISHED,RELATED"
116+
jump "ACCEPT"
117+
weight 1
118+
end
119+
120+
simple_iptables_rule "icmp" do
121+
chain "INPUT"
122+
rule "--proto icmp"
123+
jump "ACCEPT"
124+
weight 2
125+
end
126+
127+
This would generate the rules:
128+
-A INPUT --jump ACCEPT -m conntrack --ctstate ESTABLISHED,RELATED
129+
-A INPUT --jump ACCEPT --proto icmp
130+
-A INPUT --jump REJECT --reject-with icmp-host-prohibited
131+
132+
103133
`simple_iptables_policy` Resource
104134
---------------------------------
105135

@@ -265,6 +295,8 @@ Which results in the following iptables configuration:
265295
Changes
266296
=======
267297

298+
* 0.6.1 (April 14, 2014)
299+
* Add support mechanism weights.
268300
* 0.6.0 (March 19, 2014)
269301
* Add support for the raw table (#33 - Ray Ruvinskiy)
270302
* Add :delete semantics to iptables rules (#34 - Michael Parrott)

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
},
3030
"recipes": {
3131
},
32-
"version": "0.6.0"
32+
"version": "0.6.1"
3333
}

metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
license "BSD"
44
description "Simple LWRP and recipe for managing iptables rules"
55
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
6-
version "0.6.0"
6+
version "0.6.1"
77
name "simple_iptables"
88

99
supports "debian", ">= 6.0"

providers/rule.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
else
88
rules = new_resource.rule
99
end
10-
1110
# Ensure that the rules are actually valid iptable rules by testing with a temporary chain
1211
test_rules(new_resource, rules)
1312

1413
if not node["simple_iptables"]["chains"][new_resource.table].include?(new_resource.chain)
1514
node.set["simple_iptables"]["chains"][new_resource.table] = node["simple_iptables"]["chains"][new_resource.table].dup << new_resource.chain unless ["PREROUTING", "INPUT", "FORWARD", "OUTPUT", "POSTROUTING"].include?(new_resource.chain)
1615
unless new_resource.chain == new_resource.direction
17-
node.set["simple_iptables"]["rules"][new_resource.table] = node["simple_iptables"]["rules"][new_resource.table].dup << "-A #{new_resource.direction} --jump #{new_resource.chain}"
16+
node.set["simple_iptables"]["rules"][new_resource.table] << ["-A #{new_resource.direction} --jump #{new_resource.chain}", new_resource.weight]
1817
end
1918
end
2019

2120
# Then apply the rules to the node
2221
rules.each do |rule|
2322
new_rule = rule_string(new_resource, rule, false)
24-
if not node["simple_iptables"]["rules"][new_resource.table].include?(new_rule)
25-
node.set["simple_iptables"]["rules"][new_resource.table] = node["simple_iptables"]["rules"][new_resource.table].dup << new_rule
23+
if not node["simple_iptables"]["rules"][new_resource.table].include?([new_rule, new_resource.weight])
24+
node.set["simple_iptables"]["rules"][new_resource.table] << [new_rule, new_resource.weight]
25+
node.set["simple_iptables"]["rules"][new_resource.table].sort! {|a,b| a[1] <=> b[1]}
2626
new_resource.updated_by_last_action(true)
2727
Chef::Log.debug("added rule '#{new_rule}'")
2828
else
@@ -68,3 +68,4 @@ def rule_string(new_resource, rule, include_table)
6868
rule = "#{table}-A #{new_resource.chain} #{jump}#{rule}"
6969
rule
7070
end
71+

resources/rule.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
attribute :rule, :kind_of => [String, Array], :required => true
66
attribute :jump, :kind_of => [String, FalseClass], :default => "ACCEPT"
77
attribute :direction, :equal_to => ["INPUT", "FORWARD", "OUTPUT", "PREROUTING", "POSTROUTING"], :default => "INPUT"
8-
8+
attribute :weight, :kind_of => Integer, :default => 50
99

1010
def initialize(*args)
1111
super

templates/default/iptables-rules.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:<%= chain %> - [0:0]
99
<% end -%>
1010
<% node["simple_iptables"]["rules"]["nat"].each do |rule| -%>
11-
<%= rule %>
11+
<%= rule[0] %>
1212
<% end -%>
1313
COMMIT
1414
# Completed
@@ -23,7 +23,7 @@ COMMIT
2323
:<%= chain %> - [0:0]
2424
<% end -%>
2525
<% node["simple_iptables"]["rules"]["mangle"].each do |rule| -%>
26-
<%= rule %>
26+
<%= rule[0] %>
2727
<% end -%>
2828
COMMIT
2929
# Completed
@@ -36,7 +36,7 @@ COMMIT
3636
:<%= chain %> - [0:0]
3737
<% end -%>
3838
<% node["simple_iptables"]["rules"]["filter"].each do |rule| -%>
39-
<%= rule %>
39+
<%= rule[0] %>
4040
<% end -%>
4141
COMMIT
4242
# Completed
@@ -48,7 +48,7 @@ COMMIT
4848
:<%= chain %> - [0:0]
4949
<% end -%>
5050
<% node["simple_iptables"]["rules"]["raw"].each do |rule| -%>
51-
<%= rule %>
51+
<%= rule[0] %>
5252
<% end -%>
5353
COMMIT
5454
# Completed

0 commit comments

Comments
 (0)