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

Do not join flag parameters in iptables module #36658

Merged
merged 4 commits into from
May 17, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions lib/ansible/modules/system/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@
description:
- TCP flags specification.
- C(tcp_flags) expects a dict with the two keys C(flags) and C(flags_set).
- The C(flags) list is the mask, a list of flags you want to examine.
- The C(flags_set) list tells which one(s) should be set.
If one of the two values is missing, the --tcp-flags option will be ignored.
default: {}
version_added: "2.4"
suboptions:
flags:
description:
- List of flags you want to examine.
flags_set:
description:
- Flags to be set.
match:
description:
- Specifies a match to use, that is, an extension module that tests for
Expand Down Expand Up @@ -340,6 +344,19 @@
protocol: tcp
reject_with: tcp-reset
ip_version: ipv4

# Set tcp flags
- iptables:
chain: OUTPUT
jump: DROP
protocol: tcp
tcp_flags:
flags: ALL
flags_set:
- ACK
- RST
- SYN
- FIN
'''

import re
Expand Down Expand Up @@ -518,7 +535,11 @@ def main():
destination=dict(type='str'),
to_destination=dict(type='str'),
match=dict(type='list', default=[]),
tcp_flags=dict(type='dict', default={}),
tcp_flags=dict(type='dict',
options=dict(
flags=dict(type='list'),
flags_set=dict(type='list'))
),
jump=dict(type='str'),
log_prefix=dict(type='str'),
goto=dict(type='str'),
Expand Down Expand Up @@ -605,5 +626,6 @@ def main():

module.exit_json(**args)


if __name__ == '__main__':
main()
60 changes: 59 additions & 1 deletion test/units/modules/system/test_iptables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
from ansible.module_utils import basic
from ansible.modules.system import iptables
Expand Down Expand Up @@ -578,3 +577,62 @@ def test_insert_jump_reject_with_reject(self):
'--reject-with',
'tcp-reset',
])

def test_tcp_flags(self):
""" Test various ways of inputting tcp_flags """
args = [
{
'chain': 'OUTPUT',
'protocol': 'tcp',
'jump': 'DROP',
'tcp_flags': 'flags=ALL flags_set="ACK,RST,SYN,FIN"'
},
{
'chain': 'OUTPUT',
'protocol': 'tcp',
'jump': 'DROP',
'tcp_flags': {
'flags': 'ALL',
'flags_set': 'ACK,RST,SYN,FIN'
}
},
{
'chain': 'OUTPUT',
'protocol': 'tcp',
'jump': 'DROP',
'tcp_flags': {
'flags': ['ALL'],
'flags_set': ['ACK', 'RST', 'SYN', 'FIN']
}
},

]

for item in args:
set_module_args(item)

commands_results = [
(0, '', ''),
]

with patch.object(basic.AnsibleModule, 'run_command') as run_command:
run_command.side_effect = commands_results
with self.assertRaises(AnsibleExitJson) as result:
iptables.main()
self.assertTrue(result.exception.args[0]['changed'])

self.assertEqual(run_command.call_count, 1)
self.assertEqual(run_command.call_args_list[0][0][0], [
'/sbin/iptables',
'-t',
'filter',
'-C',
'OUTPUT',
'-p',
'tcp',
'--tcp-flags',
'ALL',
'ACK,RST,SYN,FIN',
'-j',
'DROP'
])