return rule
# iptables-save outputs the comment before the jump so we need to match
# that order so _find_last_entry works
+ comment = '-m comment --comment "%s"' % comment
+ if rule.startswith('-j'):
+ # this is a jump only rule so we just put the comment first
+ return '%s %s' % (comment, rule)
try:
- start_of_jump = rule.index(' -j ')
+ jpos = rule.index(' -j ')
+ return ' '.join((rule[:jpos], comment, rule[jpos + 1:]))
except ValueError:
- return '%s -m comment --comment "%s"' % (rule, comment)
- return ' '.join([rule[0:start_of_jump],
- '-m comment --comment "%s"' % comment,
- rule[start_of_jump + 1:]])
+ return '%s %s' % (rule, comment)
def get_chain_name(chain_name, wrap=True):
self.fail("Iptables comment %s is longer than 255 characters."
% attr)
+ def test_reordering_of_jump_rule_comments(self):
+ # jump at the start
+ self.assertEqual(
+ '-m comment --comment "aloha" -j sg-chain',
+ iptables_manager.comment_rule('-j sg-chain', 'aloha'))
+ # jump in the middle
+ self.assertEqual(
+ '-s source -m comment --comment "aloha" -j sg-chain',
+ iptables_manager.comment_rule('-s source -j sg-chain', 'aloha'))
+ # no jump rule
+ self.assertEqual(
+ '-s source -m comment --comment "aloha"',
+ iptables_manager.comment_rule('-s source', 'aloha'))
+
def test_add_filter_rule(self):
iptables_args = {}
iptables_args.update(IPTABLES_ARG)