2013-07-01 13:07:06 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ec2_group
|
2013-11-19 00:55:49 +01:00
|
|
|
version_added: "1.3"
|
2013-07-01 13:07:06 +02:00
|
|
|
short_description: maintain an ec2 VPC security group.
|
|
|
|
description:
|
|
|
|
- maintains ec2 security groups. This module has a dependency on python-boto >= 2.5
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the security group.
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Description of the security group.
|
|
|
|
required: true
|
|
|
|
vpc_id:
|
|
|
|
description:
|
|
|
|
- ID of the VPC to create the group in.
|
2013-08-19 18:35:43 +02:00
|
|
|
required: false
|
2013-07-01 13:07:06 +02:00
|
|
|
rules:
|
|
|
|
description:
|
2014-11-14 00:03:59 +01:00
|
|
|
- List of firewall inbound rules to enforce in this group (see'''
|
|
|
|
''' example). If none are supplied, a default all-out rule is assumed.'''
|
|
|
|
''' If an empty list is supplied, no inbound rules will be enabled.
|
2014-03-20 17:23:53 +01:00
|
|
|
required: false
|
|
|
|
rules_egress:
|
|
|
|
description:
|
2014-11-14 00:03:59 +01:00
|
|
|
- List of firewall outbound rules to enforce in this group (see'''
|
|
|
|
''' example). If none are supplied, a default all-out rule is assumed.'''
|
|
|
|
''' If an empty list is supplied, no outbound rules will be enabled.
|
2014-03-20 17:22:56 +01:00
|
|
|
required: false
|
2014-03-28 18:51:22 +01:00
|
|
|
version_added: "1.6"
|
2013-07-01 13:07:06 +02:00
|
|
|
region:
|
|
|
|
description:
|
|
|
|
- the EC2 region to use
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-10-02 17:09:23 +02:00
|
|
|
state:
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
2014-08-08 15:35:15 +02:00
|
|
|
- Create or delete a security group
|
2013-10-02 17:09:23 +02:00
|
|
|
required: false
|
|
|
|
default: 'present'
|
2014-08-08 15:35:15 +02:00
|
|
|
choices: [ "present", "absent" ]
|
2013-10-02 17:09:23 +02:00
|
|
|
aliases: []
|
2014-05-28 20:19:29 +02:00
|
|
|
purge_rules:
|
2014-08-08 18:36:04 +02:00
|
|
|
version_added: "1.8"
|
2014-05-28 20:19:29 +02:00
|
|
|
description:
|
|
|
|
- Purge existing rules on security group that are not found in rules
|
|
|
|
required: false
|
|
|
|
default: 'true'
|
|
|
|
aliases: []
|
|
|
|
purge_rules_egress:
|
2014-08-08 18:36:04 +02:00
|
|
|
version_added: "1.8"
|
2014-05-28 20:19:29 +02:00
|
|
|
description:
|
2014-11-29 06:05:22 +01:00
|
|
|
- Purge existing rules_egress on security group that are not found in rules_egress
|
2014-05-28 20:19:29 +02:00
|
|
|
required: false
|
|
|
|
default: 'true'
|
|
|
|
aliases: []
|
2013-10-02 17:09:23 +02:00
|
|
|
|
2014-04-09 08:43:55 +02:00
|
|
|
extends_documentation_fragment: aws
|
2014-03-21 08:35:25 +01:00
|
|
|
|
|
|
|
notes:
|
|
|
|
- If a rule declares a group_name and that group doesn't exist, it will be
|
|
|
|
automatically created. In that case, group_desc should be provided as well.
|
|
|
|
The module will refuse to create a depended-on group without a description.
|
2013-07-01 13:07:06 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: example ec2 group
|
2014-12-01 21:14:57 +01:00
|
|
|
ec2_group:
|
2013-07-01 13:07:06 +02:00
|
|
|
name: example
|
|
|
|
description: an example EC2 group
|
|
|
|
vpc_id: 12345
|
|
|
|
region: eu-west-1a
|
2014-05-30 23:57:55 +02:00
|
|
|
aws_secret_key: SECRET
|
|
|
|
aws_access_key: ACCESS
|
2013-07-01 13:07:06 +02:00
|
|
|
rules:
|
|
|
|
- proto: tcp
|
|
|
|
from_port: 80
|
|
|
|
to_port: 80
|
|
|
|
cidr_ip: 0.0.0.0/0
|
|
|
|
- proto: tcp
|
|
|
|
from_port: 22
|
|
|
|
to_port: 22
|
|
|
|
cidr_ip: 10.0.0.0/8
|
|
|
|
- proto: udp
|
|
|
|
from_port: 10050
|
|
|
|
to_port: 10050
|
|
|
|
cidr_ip: 10.0.0.0/8
|
|
|
|
- proto: udp
|
|
|
|
from_port: 10051
|
|
|
|
to_port: 10051
|
2013-12-20 22:06:55 +01:00
|
|
|
group_id: sg-12345678
|
|
|
|
- proto: all
|
|
|
|
# the containing group name may be specified here
|
|
|
|
group_name: example
|
2014-03-20 17:23:53 +01:00
|
|
|
rules_egress:
|
|
|
|
- proto: tcp
|
|
|
|
from_port: 80
|
|
|
|
to_port: 80
|
2014-10-23 14:19:23 +02:00
|
|
|
cidr_ip: 0.0.0.0/0
|
2014-03-21 08:35:25 +01:00
|
|
|
group_name: example-other
|
|
|
|
# description to use if example-other needs to be created
|
|
|
|
group_desc: other example EC2 group
|
2013-07-01 13:07:06 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2013-07-01 13:07:06 +02:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
2013-07-01 13:07:06 +02:00
|
|
|
|
|
|
|
|
2014-12-17 12:06:05 +01:00
|
|
|
def make_rule_key(prefix, rule, group_id, cidr_ip):
|
|
|
|
"""Creates a unique key for an individual group rule"""
|
|
|
|
if isinstance(rule, dict):
|
2014-12-17 18:01:50 +01:00
|
|
|
proto, from_port, to_port = [rule.get(x, None) for x in ('proto', 'from_port', 'to_port')]
|
2014-12-17 12:06:05 +01:00
|
|
|
else: # isinstance boto.ec2.securitygroup.IPPermissions
|
2014-12-17 18:01:50 +01:00
|
|
|
proto, from_port, to_port = [getattr(rule, x, None) for x in ('ip_protocol', 'from_port', 'to_port')]
|
2014-12-17 12:06:05 +01:00
|
|
|
|
|
|
|
key = "%s-%s-%s-%s-%s-%s" % (prefix, proto, from_port, to_port, group_id, cidr_ip)
|
|
|
|
return key.lower().replace('-none', '-None')
|
|
|
|
|
|
|
|
|
2013-07-01 13:07:06 +02:00
|
|
|
def addRulesToLookup(rules, prefix, dict):
|
|
|
|
for rule in rules:
|
|
|
|
for grant in rule.grants:
|
2015-01-24 06:52:37 +01:00
|
|
|
dict[make_rule_key(prefix, rule, grant.group_id, grant.cidr_ip)] = (rule, grant)
|
2013-07-01 13:07:06 +02:00
|
|
|
|
2014-03-20 16:19:35 +01:00
|
|
|
|
2014-07-17 17:03:38 +02:00
|
|
|
def get_target_from_rule(module, ec2, rule, name, group, groups, vpc_id):
|
2014-03-20 16:19:35 +01:00
|
|
|
"""
|
|
|
|
Returns tuple of (group_id, ip) after validating rule params.
|
|
|
|
|
|
|
|
rule: Dict describing a rule.
|
|
|
|
name: Name of the security group being managed.
|
|
|
|
groups: Dict of all available security groups.
|
|
|
|
|
|
|
|
AWS accepts an ip range or a security group as target of a rule. This
|
|
|
|
function validate the rule specification and return either a non-None
|
|
|
|
group_id or a non-None ip range.
|
|
|
|
"""
|
|
|
|
|
|
|
|
group_id = None
|
|
|
|
group_name = None
|
|
|
|
ip = None
|
2014-03-20 16:38:54 +01:00
|
|
|
target_group_created = False
|
2014-03-20 16:19:35 +01:00
|
|
|
if 'group_id' in rule and 'cidr_ip' in rule:
|
|
|
|
module.fail_json(msg="Specify group_id OR cidr_ip, not both")
|
|
|
|
elif 'group_name' in rule and 'cidr_ip' in rule:
|
|
|
|
module.fail_json(msg="Specify group_name OR cidr_ip, not both")
|
|
|
|
elif 'group_id' in rule and 'group_name' in rule:
|
|
|
|
module.fail_json(msg="Specify group_id OR group_name, not both")
|
|
|
|
elif 'group_id' in rule:
|
|
|
|
group_id = rule['group_id']
|
|
|
|
elif 'group_name' in rule:
|
|
|
|
group_name = rule['group_name']
|
|
|
|
if group_name in groups:
|
|
|
|
group_id = groups[group_name].id
|
|
|
|
elif group_name == name:
|
|
|
|
group_id = group.id
|
|
|
|
groups[group_id] = group
|
|
|
|
groups[group_name] = group
|
2014-03-20 16:38:54 +01:00
|
|
|
else:
|
|
|
|
if not rule.get('group_desc', '').strip():
|
|
|
|
module.fail_json(msg="group %s will be automatically created by rule %s and no description was provided" % (group_name, rule))
|
|
|
|
if not module.check_mode:
|
|
|
|
auto_group = ec2.create_security_group(group_name, rule['group_desc'], vpc_id=vpc_id)
|
|
|
|
group_id = auto_group.id
|
|
|
|
groups[group_id] = auto_group
|
|
|
|
groups[group_name] = auto_group
|
|
|
|
target_group_created = True
|
2014-03-20 16:19:35 +01:00
|
|
|
elif 'cidr_ip' in rule:
|
|
|
|
ip = rule['cidr_ip']
|
|
|
|
|
2014-03-20 16:38:54 +01:00
|
|
|
return group_id, ip, target_group_created
|
2014-03-20 16:19:35 +01:00
|
|
|
|
|
|
|
|
2013-07-01 13:07:06 +02:00
|
|
|
def main():
|
2014-02-05 12:11:06 +01:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2013-07-01 13:07:06 +02:00
|
|
|
name=dict(required=True),
|
|
|
|
description=dict(required=True),
|
|
|
|
vpc_id=dict(),
|
|
|
|
rules=dict(),
|
2014-03-20 15:50:08 +01:00
|
|
|
rules_egress=dict(),
|
2013-10-02 17:09:23 +02:00
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
2014-05-28 20:19:29 +02:00
|
|
|
purge_rules=dict(default=True, required=False, type='bool'),
|
|
|
|
purge_rules_egress=dict(default=True, required=False, type='bool'),
|
|
|
|
|
2014-02-05 12:11:06 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2013-07-01 13:07:06 +02:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2013-11-01 16:59:24 +01:00
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2013-07-01 13:07:06 +02:00
|
|
|
name = module.params['name']
|
|
|
|
description = module.params['description']
|
|
|
|
vpc_id = module.params['vpc_id']
|
|
|
|
rules = module.params['rules']
|
2014-03-20 15:50:08 +01:00
|
|
|
rules_egress = module.params['rules_egress']
|
2013-10-02 17:09:23 +02:00
|
|
|
state = module.params.get('state')
|
2014-05-28 20:19:29 +02:00
|
|
|
purge_rules = module.params['purge_rules']
|
|
|
|
purge_rules_egress = module.params['purge_rules_egress']
|
2013-07-01 13:07:06 +02:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2013-12-17 03:04:12 +01:00
|
|
|
ec2 = ec2_connect(module)
|
2013-07-01 13:07:06 +02:00
|
|
|
|
|
|
|
# find the group if present
|
|
|
|
group = None
|
|
|
|
groups = {}
|
|
|
|
for curGroup in ec2.get_all_security_groups():
|
|
|
|
groups[curGroup.id] = curGroup
|
2013-12-20 22:06:55 +01:00
|
|
|
groups[curGroup.name] = curGroup
|
2013-07-01 13:07:06 +02:00
|
|
|
|
2014-02-26 13:39:03 +01:00
|
|
|
if curGroup.name == name and (vpc_id is None or curGroup.vpc_id == vpc_id):
|
2013-07-01 13:07:06 +02:00
|
|
|
group = curGroup
|
|
|
|
|
2013-10-02 17:09:23 +02:00
|
|
|
# Ensure requested group is absent
|
|
|
|
if state == 'absent':
|
|
|
|
if group:
|
|
|
|
'''found a match, delete it'''
|
|
|
|
try:
|
|
|
|
group.delete()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="Unable to delete security group '%s' - %s" % (group, e))
|
|
|
|
else:
|
|
|
|
group = None
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
'''no match found, no changes required'''
|
|
|
|
|
|
|
|
# Ensure requested group is present
|
|
|
|
elif state == 'present':
|
|
|
|
if group:
|
|
|
|
'''existing group found'''
|
|
|
|
# check the group parameters are correct
|
|
|
|
group_in_use = False
|
|
|
|
rs = ec2.get_all_instances()
|
|
|
|
for r in rs:
|
|
|
|
for i in r.instances:
|
|
|
|
group_in_use |= reduce(lambda x, y: x | (y.name == 'public-ssh'), i.groups, False)
|
|
|
|
|
|
|
|
if group.description != description:
|
|
|
|
if group_in_use:
|
|
|
|
module.fail_json(msg="Group description does not match, but it is in use so cannot be changed.")
|
|
|
|
|
|
|
|
# if the group doesn't exist, create it now
|
|
|
|
else:
|
|
|
|
'''no match found, create it'''
|
|
|
|
if not module.check_mode:
|
|
|
|
group = ec2.create_security_group(name, description, vpc_id=vpc_id)
|
2014-03-20 17:20:21 +01:00
|
|
|
|
|
|
|
# When a group is created, an egress_rule ALLOW ALL
|
|
|
|
# to 0.0.0.0/0 is added automatically but it's not
|
|
|
|
# reflected in the object returned by the AWS API
|
|
|
|
# call. We re-read the group for getting an updated object
|
2014-06-03 20:25:48 +02:00
|
|
|
# amazon sometimes takes a couple seconds to update the security group so wait till it exists
|
|
|
|
while len(ec2.get_all_security_groups(filters={ 'group_id': group.id, })) == 0:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2014-03-20 17:20:21 +01:00
|
|
|
group = ec2.get_all_security_groups(group_ids=(group.id,))[0]
|
2013-10-02 17:09:23 +02:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unsupported state requested: %s" % state)
|
2013-07-01 13:07:06 +02:00
|
|
|
|
|
|
|
# create a lookup for all existing rules on the group
|
|
|
|
if group:
|
2014-03-20 15:50:08 +01:00
|
|
|
|
|
|
|
# Manage ingress rules
|
2013-10-02 17:09:23 +02:00
|
|
|
groupRules = {}
|
2013-07-01 13:07:06 +02:00
|
|
|
addRulesToLookup(group.rules, 'in', groupRules)
|
|
|
|
|
2013-10-02 17:09:23 +02:00
|
|
|
# Now, go through all provided rules and ensure they are there.
|
2014-11-14 00:03:59 +01:00
|
|
|
if rules is not None:
|
2013-10-02 17:09:23 +02:00
|
|
|
for rule in rules:
|
2014-07-17 17:03:38 +02:00
|
|
|
group_id, ip, target_group_created = get_target_from_rule(module, ec2, rule, name, group, groups, vpc_id)
|
2014-03-20 16:38:54 +01:00
|
|
|
if target_group_created:
|
|
|
|
changed = True
|
2013-07-01 13:07:06 +02:00
|
|
|
|
2014-07-31 21:01:44 +02:00
|
|
|
if rule['proto'] in ('all', '-1', -1):
|
2013-10-02 17:09:23 +02:00
|
|
|
rule['proto'] = -1
|
|
|
|
rule['from_port'] = None
|
|
|
|
rule['to_port'] = None
|
2013-07-01 13:07:06 +02:00
|
|
|
|
2013-10-02 17:09:23 +02:00
|
|
|
# If rule already exists, don't later delete it
|
2014-12-17 12:06:05 +01:00
|
|
|
ruleId = make_rule_key('in', rule, group_id, ip)
|
2013-10-02 17:09:23 +02:00
|
|
|
if ruleId in groupRules:
|
|
|
|
del groupRules[ruleId]
|
|
|
|
# Otherwise, add new rule
|
|
|
|
else:
|
|
|
|
grantGroup = None
|
|
|
|
if group_id:
|
|
|
|
grantGroup = groups[group_id]
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
group.authorize(rule['proto'], rule['from_port'], rule['to_port'], ip, grantGroup)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# Finally, remove anything left in the groupRules -- these will be defunct rules
|
2014-05-28 20:19:29 +02:00
|
|
|
if purge_rules:
|
2015-01-24 06:52:37 +01:00
|
|
|
for (rule, grant) in groupRules.itervalues() :
|
|
|
|
grantGroup = None
|
|
|
|
if grant.group_id:
|
|
|
|
grantGroup = groups[grant.group_id]
|
|
|
|
if not module.check_mode:
|
|
|
|
group.revoke(rule.ip_protocol, rule.from_port, rule.to_port, grant.cidr_ip, grantGroup)
|
|
|
|
changed = True
|
2013-07-01 13:07:06 +02:00
|
|
|
|
2014-03-20 15:50:08 +01:00
|
|
|
# Manage egress rules
|
|
|
|
groupRules = {}
|
|
|
|
addRulesToLookup(group.rules_egress, 'out', groupRules)
|
|
|
|
|
|
|
|
# Now, go through all provided rules and ensure they are there.
|
2014-11-14 00:03:59 +01:00
|
|
|
if rules_egress is not None:
|
2014-03-20 15:50:08 +01:00
|
|
|
for rule in rules_egress:
|
2014-07-17 17:03:38 +02:00
|
|
|
group_id, ip, target_group_created = get_target_from_rule(module, ec2, rule, name, group, groups, vpc_id)
|
2014-03-20 16:38:54 +01:00
|
|
|
if target_group_created:
|
|
|
|
changed = True
|
2014-03-20 15:50:08 +01:00
|
|
|
|
2014-07-31 21:01:44 +02:00
|
|
|
if rule['proto'] in ('all', '-1', -1):
|
2014-03-20 15:50:08 +01:00
|
|
|
rule['proto'] = -1
|
|
|
|
rule['from_port'] = None
|
|
|
|
rule['to_port'] = None
|
|
|
|
|
|
|
|
# If rule already exists, don't later delete it
|
2014-12-17 12:06:05 +01:00
|
|
|
ruleId = make_rule_key('out', rule, group_id, ip)
|
2014-03-20 15:50:08 +01:00
|
|
|
if ruleId in groupRules:
|
|
|
|
del groupRules[ruleId]
|
|
|
|
# Otherwise, add new rule
|
|
|
|
else:
|
|
|
|
grantGroup = None
|
|
|
|
if group_id:
|
|
|
|
grantGroup = groups[group_id].id
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
ec2.authorize_security_group_egress(
|
|
|
|
group_id=group.id,
|
|
|
|
ip_protocol=rule['proto'],
|
|
|
|
from_port=rule['from_port'],
|
|
|
|
to_port=rule['to_port'],
|
|
|
|
src_group_id=grantGroup,
|
|
|
|
cidr_ip=ip)
|
|
|
|
changed = True
|
2014-04-17 22:24:18 +02:00
|
|
|
elif vpc_id and not module.check_mode:
|
2014-10-23 14:19:23 +02:00
|
|
|
# when using a vpc, but no egress rules are specified,
|
2014-04-17 22:24:18 +02:00
|
|
|
# we add in a default allow all out rule, which was the
|
|
|
|
# default behavior before egress rules were added
|
2014-05-08 03:52:03 +02:00
|
|
|
default_egress_rule = 'out--1-None-None-None-0.0.0.0/0'
|
|
|
|
if default_egress_rule not in groupRules:
|
2014-04-17 22:24:18 +02:00
|
|
|
ec2.authorize_security_group_egress(
|
|
|
|
group_id=group.id,
|
|
|
|
ip_protocol=-1,
|
|
|
|
from_port=None,
|
|
|
|
to_port=None,
|
|
|
|
src_group_id=None,
|
|
|
|
cidr_ip='0.0.0.0/0'
|
|
|
|
)
|
|
|
|
changed = True
|
2014-05-08 03:52:03 +02:00
|
|
|
else:
|
|
|
|
# make sure the default egress rule is not removed
|
|
|
|
del groupRules[default_egress_rule]
|
2014-03-20 15:50:08 +01:00
|
|
|
|
|
|
|
# Finally, remove anything left in the groupRules -- these will be defunct rules
|
2014-05-28 20:19:29 +02:00
|
|
|
if purge_rules_egress:
|
2015-01-24 06:52:37 +01:00
|
|
|
for (rule, grant) in groupRules.itervalues():
|
|
|
|
grantGroup = None
|
|
|
|
if grant.group_id:
|
|
|
|
grantGroup = groups[grant.group_id].id
|
|
|
|
if not module.check_mode:
|
|
|
|
ec2.revoke_security_group_egress(
|
|
|
|
group_id=group.id,
|
|
|
|
ip_protocol=rule.ip_protocol,
|
|
|
|
from_port=rule.from_port,
|
|
|
|
to_port=rule.to_port,
|
|
|
|
src_group_id=grantGroup,
|
|
|
|
cidr_ip=grant.cidr_ip)
|
|
|
|
changed = True
|
2014-03-20 15:50:08 +01:00
|
|
|
|
2013-10-02 17:09:23 +02:00
|
|
|
if group:
|
|
|
|
module.exit_json(changed=changed, group_id=group.id)
|
|
|
|
else:
|
2013-07-01 13:07:06 +02:00
|
|
|
module.exit_json(changed=changed, group_id=None)
|
|
|
|
|
2013-11-01 16:59:24 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
2013-07-01 13:07:06 +02:00
|
|
|
main()
|