2015-03-31 22:37:07 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
|
|
|
|
#
|
|
|
|
# This module is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import shade
|
2015-06-30 22:51:18 +02:00
|
|
|
HAS_SHADE = True
|
2015-03-31 22:37:07 +02:00
|
|
|
except ImportError:
|
2015-06-30 22:51:18 +02:00
|
|
|
HAS_SHADE = False
|
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_security_group_rule
|
|
|
|
short_description: Add/Delete rule from an existing security group
|
|
|
|
extends_documentation_fragment: openstack
|
2015-06-09 03:27:40 +02:00
|
|
|
version_added: "2.0"
|
2015-03-31 22:37:07 +02:00
|
|
|
description:
|
|
|
|
- Add or Remove rule from an existing security group
|
|
|
|
options:
|
|
|
|
security_group:
|
|
|
|
description:
|
2016-04-14 20:03:13 +02:00
|
|
|
- Name or ID of the security group
|
2015-03-31 22:37:07 +02:00
|
|
|
required: true
|
|
|
|
protocol:
|
|
|
|
description:
|
2016-09-07 18:30:03 +02:00
|
|
|
- IP protocols TCP UDP ICMP 112 (VRRP)
|
2016-08-30 16:55:16 +02:00
|
|
|
choices: ['tcp', 'udp', 'icmp', 112, None]
|
2015-07-07 00:52:11 +02:00
|
|
|
default: None
|
2015-03-31 22:37:07 +02:00
|
|
|
port_range_min:
|
|
|
|
description:
|
|
|
|
- Starting port
|
2015-07-07 00:52:11 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
2015-03-31 22:37:07 +02:00
|
|
|
port_range_max:
|
|
|
|
description:
|
|
|
|
- Ending port
|
2015-07-07 00:52:11 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
2015-03-31 22:37:07 +02:00
|
|
|
remote_ip_prefix:
|
|
|
|
description:
|
|
|
|
- Source IP address(es) in CIDR notation (exclusive with remote_group)
|
2015-06-16 20:56:04 +02:00
|
|
|
required: false
|
2015-03-31 22:37:07 +02:00
|
|
|
remote_group:
|
|
|
|
description:
|
2016-04-14 20:03:13 +02:00
|
|
|
- Name or ID of the Security group to link (exclusive with
|
|
|
|
remote_ip_prefix)
|
2015-06-16 20:56:04 +02:00
|
|
|
required: false
|
|
|
|
ethertype:
|
|
|
|
description:
|
|
|
|
- Must be IPv4 or IPv6, and addresses represented in CIDR must
|
|
|
|
match the ingress or egress rules. Not all providers support IPv6.
|
|
|
|
choices: ['IPv4', 'IPv6']
|
|
|
|
default: IPv4
|
|
|
|
direction:
|
|
|
|
description:
|
|
|
|
- The direction in which the security group rule is applied. Not
|
|
|
|
all providers support egress.
|
|
|
|
choices: ['egress', 'ingress']
|
|
|
|
default: ingress
|
2015-03-31 22:37:07 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
|
|
|
requirements: ["shade"]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a security group rule
|
2015-06-04 18:03:05 +02:00
|
|
|
- os_security_group_rule:
|
|
|
|
cloud: mordred
|
|
|
|
security_group: foo
|
|
|
|
protocol: tcp
|
|
|
|
port_range_min: 80
|
|
|
|
port_range_max: 80
|
|
|
|
remote_ip_prefix: 0.0.0.0/0
|
2015-07-06 18:16:29 +02:00
|
|
|
|
|
|
|
# Create a security group rule for ping
|
|
|
|
- os_security_group_rule:
|
|
|
|
cloud: mordred
|
|
|
|
security_group: foo
|
|
|
|
protocol: icmp
|
|
|
|
remote_ip_prefix: 0.0.0.0/0
|
|
|
|
|
|
|
|
# Another way to create the ping rule
|
|
|
|
- os_security_group_rule:
|
|
|
|
cloud: mordred
|
|
|
|
security_group: foo
|
|
|
|
protocol: icmp
|
|
|
|
port_range_min: -1
|
|
|
|
port_range_max: -1
|
|
|
|
remote_ip_prefix: 0.0.0.0/0
|
|
|
|
|
|
|
|
# Create a TCP rule covering all ports
|
|
|
|
- os_security_group_rule:
|
|
|
|
cloud: mordred
|
|
|
|
security_group: foo
|
|
|
|
protocol: tcp
|
|
|
|
port_range_min: 1
|
|
|
|
port_range_max: 65535
|
|
|
|
remote_ip_prefix: 0.0.0.0/0
|
|
|
|
|
|
|
|
# Another way to create the TCP rule above (defaults to all ports)
|
|
|
|
- os_security_group_rule:
|
|
|
|
cloud: mordred
|
|
|
|
security_group: foo
|
|
|
|
protocol: tcp
|
|
|
|
remote_ip_prefix: 0.0.0.0/0
|
2016-08-30 16:55:16 +02:00
|
|
|
|
|
|
|
# Create a rule for VRRP with numbered protocol 112
|
|
|
|
- os_security_group_rule:
|
|
|
|
security_group: loadbalancer_sg
|
|
|
|
protocol: 112
|
|
|
|
remote_group: loadbalancer-node_sg
|
2015-03-31 22:37:07 +02:00
|
|
|
'''
|
|
|
|
|
2015-06-30 22:51:18 +02:00
|
|
|
RETURN = '''
|
|
|
|
id:
|
|
|
|
description: Unique rule UUID.
|
|
|
|
type: string
|
|
|
|
direction:
|
|
|
|
description: The direction in which the security group rule is applied.
|
|
|
|
type: string
|
|
|
|
sample: 'egress'
|
|
|
|
ethertype:
|
|
|
|
description: One of IPv4 or IPv6.
|
|
|
|
type: string
|
|
|
|
sample: 'IPv4'
|
|
|
|
port_range_min:
|
|
|
|
description: The minimum port number in the range that is matched by
|
|
|
|
the security group rule.
|
|
|
|
type: int
|
|
|
|
sample: 8000
|
|
|
|
port_range_max:
|
|
|
|
description: The maximum port number in the range that is matched by
|
|
|
|
the security group rule.
|
|
|
|
type: int
|
|
|
|
sample: 8000
|
|
|
|
protocol:
|
|
|
|
description: The protocol that is matched by the security group rule.
|
|
|
|
type: string
|
|
|
|
sample: 'tcp'
|
|
|
|
remote_ip_prefix:
|
|
|
|
description: The remote IP prefix to be associated with this security group rule.
|
|
|
|
type: string
|
|
|
|
sample: '0.0.0.0/0'
|
|
|
|
security_group_id:
|
|
|
|
description: The security group ID to associate with this security group rule.
|
|
|
|
type: string
|
|
|
|
'''
|
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-07-06 18:16:29 +02:00
|
|
|
def _ports_match(protocol, module_min, module_max, rule_min, rule_max):
|
|
|
|
"""
|
|
|
|
Capture the complex port matching logic.
|
|
|
|
|
|
|
|
The port values coming in for the module might be -1 (for ICMP),
|
|
|
|
which will work only for Nova, but this is handled by shade. Likewise,
|
|
|
|
they might be None, which works for Neutron, but not Nova. This too is
|
|
|
|
handled by shade. Since shade will consistently return these port
|
|
|
|
values as None, we need to convert any -1 values input to the module
|
|
|
|
to None here for comparison.
|
|
|
|
|
|
|
|
For TCP and UDP protocols, None values for both min and max are
|
|
|
|
represented as the range 1-65535 for Nova, but remain None for
|
|
|
|
Neutron. Shade returns the full range when Nova is the backend (since
|
|
|
|
that is how Nova stores them), and None values for Neutron. If None
|
|
|
|
values are input to the module for both values, then we need to adjust
|
|
|
|
for comparison.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check if the user is supplying -1 for ICMP.
|
|
|
|
if protocol == 'icmp':
|
|
|
|
if module_min and int(module_min) == -1:
|
|
|
|
module_min = None
|
|
|
|
if module_max and int(module_max) == -1:
|
|
|
|
module_max = None
|
|
|
|
|
|
|
|
# Check if user is supplying None values for full TCP/UDP port range.
|
|
|
|
if protocol in ['tcp', 'udp'] and module_min is None and module_max is None:
|
|
|
|
if (rule_min and int(rule_min) == 1
|
|
|
|
and rule_max and int(rule_max) == 65535):
|
|
|
|
# (None, None) == (1, 65535)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Sanity check to make sure we don't have type comparison issues.
|
|
|
|
if module_min:
|
|
|
|
module_min = int(module_min)
|
|
|
|
if module_max:
|
|
|
|
module_max = int(module_max)
|
|
|
|
if rule_min:
|
|
|
|
rule_min = int(rule_min)
|
|
|
|
if rule_max:
|
|
|
|
rule_max = int(rule_max)
|
|
|
|
|
|
|
|
return module_min == rule_min and module_max == rule_max
|
|
|
|
|
|
|
|
|
2016-04-14 20:03:13 +02:00
|
|
|
def _find_matching_rule(module, secgroup, remotegroup):
|
2015-06-16 20:56:04 +02:00
|
|
|
"""
|
|
|
|
Find a rule in the group that matches the module parameters.
|
|
|
|
:returns: The matching rule dict, or None if no matches.
|
|
|
|
"""
|
|
|
|
protocol = module.params['protocol']
|
|
|
|
remote_ip_prefix = module.params['remote_ip_prefix']
|
|
|
|
ethertype = module.params['ethertype']
|
|
|
|
direction = module.params['direction']
|
2016-04-14 20:03:13 +02:00
|
|
|
remote_group_id = remotegroup['id']
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-10 23:31:43 +02:00
|
|
|
for rule in secgroup['security_group_rules']:
|
2015-06-16 20:56:04 +02:00
|
|
|
if (protocol == rule['protocol']
|
|
|
|
and remote_ip_prefix == rule['remote_ip_prefix']
|
|
|
|
and ethertype == rule['ethertype']
|
2015-07-06 18:16:29 +02:00
|
|
|
and direction == rule['direction']
|
2015-08-04 09:22:56 +02:00
|
|
|
and remote_group_id == rule['remote_group_id']
|
2015-07-06 18:16:29 +02:00
|
|
|
and _ports_match(protocol,
|
|
|
|
module.params['port_range_min'],
|
|
|
|
module.params['port_range_max'],
|
|
|
|
rule['port_range_min'],
|
|
|
|
rule['port_range_max'])):
|
2015-03-31 22:37:07 +02:00
|
|
|
return rule
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-04-14 20:03:13 +02:00
|
|
|
def _system_state_change(module, secgroup, remotegroup):
|
2015-06-16 20:56:04 +02:00
|
|
|
state = module.params['state']
|
|
|
|
if secgroup:
|
2016-04-14 20:03:13 +02:00
|
|
|
rule_exists = _find_matching_rule(module, secgroup, remotegroup)
|
2015-06-16 20:56:04 +02:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if state == 'present' and not rule_exists:
|
|
|
|
return True
|
|
|
|
if state == 'absent' and rule_exists:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-06-09 03:27:40 +02:00
|
|
|
def main():
|
2015-03-31 22:37:07 +02:00
|
|
|
argument_spec = openstack_full_argument_spec(
|
2015-06-16 20:56:04 +02:00
|
|
|
security_group = dict(required=True),
|
2015-06-30 22:51:18 +02:00
|
|
|
# NOTE(Shrews): None is an acceptable protocol value for
|
|
|
|
# Neutron, but Nova will balk at this.
|
|
|
|
protocol = dict(default=None,
|
2016-08-30 16:55:16 +02:00
|
|
|
choices=[None, 'tcp', 'udp', 'icmp', 112]),
|
2015-06-30 22:51:18 +02:00
|
|
|
port_range_min = dict(required=False, type='int'),
|
|
|
|
port_range_max = dict(required=False, type='int'),
|
2015-06-16 20:56:04 +02:00
|
|
|
remote_ip_prefix = dict(required=False, default=None),
|
|
|
|
remote_group = dict(required=False, default=None),
|
|
|
|
ethertype = dict(default='IPv4',
|
|
|
|
choices=['IPv4', 'IPv6']),
|
|
|
|
direction = dict(default='ingress',
|
|
|
|
choices=['egress', 'ingress']),
|
|
|
|
state = dict(default='present',
|
|
|
|
choices=['absent', 'present']),
|
2015-03-31 22:37:07 +02:00
|
|
|
)
|
2015-06-16 20:56:04 +02:00
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
module_kwargs = openstack_module_kwargs(
|
|
|
|
mutually_exclusive=[
|
|
|
|
['remote_ip_prefix', 'remote_group'],
|
2015-06-04 18:34:21 +02:00
|
|
|
]
|
2015-03-31 22:37:07 +02:00
|
|
|
)
|
2015-06-16 20:56:04 +02:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
**module_kwargs)
|
|
|
|
|
2015-06-30 22:51:18 +02:00
|
|
|
if not HAS_SHADE:
|
|
|
|
module.fail_json(msg='shade is required for this module')
|
|
|
|
|
2015-06-16 20:56:04 +02:00
|
|
|
state = module.params['state']
|
|
|
|
security_group = module.params['security_group']
|
2016-04-14 20:03:13 +02:00
|
|
|
remote_group = module.params['remote_group']
|
2015-06-16 20:56:04 +02:00
|
|
|
changed = False
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
2015-06-16 20:56:04 +02:00
|
|
|
secgroup = cloud.get_security_group(security_group)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2016-04-14 20:03:13 +02:00
|
|
|
if remote_group:
|
|
|
|
remotegroup = cloud.get_security_group(remote_group)
|
|
|
|
else:
|
|
|
|
remotegroup = { 'id' : None }
|
|
|
|
|
2015-06-16 20:56:04 +02:00
|
|
|
if module.check_mode:
|
2016-04-14 20:03:13 +02:00
|
|
|
module.exit_json(changed=_system_state_change(module, secgroup, remotegroup))
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-16 20:56:04 +02:00
|
|
|
if state == 'present':
|
2015-03-31 22:37:07 +02:00
|
|
|
if not secgroup:
|
|
|
|
module.fail_json(msg='Could not find security group %s' %
|
2015-06-16 20:56:04 +02:00
|
|
|
security_group)
|
|
|
|
|
2016-04-14 20:03:13 +02:00
|
|
|
rule = _find_matching_rule(module, secgroup, remotegroup)
|
2015-06-17 13:39:27 +02:00
|
|
|
if not rule:
|
|
|
|
rule = cloud.create_security_group_rule(
|
2015-06-16 20:56:04 +02:00
|
|
|
secgroup['id'],
|
|
|
|
port_range_min=module.params['port_range_min'],
|
|
|
|
port_range_max=module.params['port_range_max'],
|
|
|
|
protocol=module.params['protocol'],
|
|
|
|
remote_ip_prefix=module.params['remote_ip_prefix'],
|
2016-04-14 20:03:13 +02:00
|
|
|
remote_group_id=remotegroup['id'],
|
2015-06-16 20:56:04 +02:00
|
|
|
direction=module.params['direction'],
|
|
|
|
ethertype=module.params['ethertype']
|
|
|
|
)
|
2015-03-31 22:37:07 +02:00
|
|
|
changed = True
|
2015-06-19 20:21:51 +02:00
|
|
|
module.exit_json(changed=changed, rule=rule, id=rule['id'])
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-16 20:56:04 +02:00
|
|
|
if state == 'absent' and secgroup:
|
2016-04-14 20:03:13 +02:00
|
|
|
rule = _find_matching_rule(module, secgroup, remotegroup)
|
2015-06-16 20:56:04 +02:00
|
|
|
if rule:
|
|
|
|
cloud.delete_security_group_rule(rule['id'])
|
2015-03-31 22:37:07 +02:00
|
|
|
changed = True
|
|
|
|
|
2015-06-17 13:39:27 +02:00
|
|
|
module.exit_json(changed=changed)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
except shade.OpenStackCloudException as e:
|
2016-01-13 17:00:16 +01:00
|
|
|
module.fail_json(msg=str(e))
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
2015-06-09 03:27:40 +02:00
|
|
|
|
2015-06-30 22:51:18 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|