| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Kevin Breit (@kbreit) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = { |
| 11 |
'metadata_version': '1.1', |
| 12 |
"status": ['deprecated'], |
| 13 |
'supported_by': 'community' |
| 14 |
} |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: meraki_mr_l3_firewall |
| 19 |
short_description: Manage MR access point layer 3 firewalls in the Meraki cloud |
| 20 |
description: |
| 21 |
- Allows for creation, management, and visibility into layer 3 firewalls implemented on Meraki MR access points. |
| 22 |
- Module is not idempotent as of current release. |
| 23 |
deprecated: |
| 24 |
removed_in: '3.0.0' |
| 25 |
why: Updated modules released with increased functionality |
| 26 |
alternative: cisco.meraki.networks_appliance_firewall_l3_firewall_rules |
| 27 |
options: |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Create or modify an organization. |
| 31 |
type: str |
| 32 |
choices: [ present, query ] |
| 33 |
default: present |
| 34 |
net_name: |
| 35 |
description: |
| 36 |
- Name of network containing access points. |
| 37 |
type: str |
| 38 |
net_id: |
| 39 |
description: |
| 40 |
- ID of network containing access points. |
| 41 |
type: str |
| 42 |
number: |
| 43 |
description: |
| 44 |
- Number of SSID to apply firewall rule to. |
| 45 |
type: str |
| 46 |
aliases: [ ssid_number ] |
| 47 |
ssid_name: |
| 48 |
description: |
| 49 |
- Name of SSID to apply firewall rule to. |
| 50 |
type: str |
| 51 |
aliases: [ ssid ] |
| 52 |
allow_lan_access: |
| 53 |
description: |
| 54 |
- Sets whether devices can talk to other devices on the same LAN. |
| 55 |
type: bool |
| 56 |
default: true |
| 57 |
rules: |
| 58 |
description: |
| 59 |
- List of firewall rules. |
| 60 |
type: list |
| 61 |
elements: dict |
| 62 |
suboptions: |
| 63 |
policy: |
| 64 |
description: |
| 65 |
- Specifies the action that should be taken when rule is hit. |
| 66 |
type: str |
| 67 |
choices: [ allow, deny ] |
| 68 |
protocol: |
| 69 |
description: |
| 70 |
- Specifies protocol to match against. |
| 71 |
type: str |
| 72 |
choices: [ any, icmp, tcp, udp ] |
| 73 |
dest_port: |
| 74 |
description: |
| 75 |
- Comma-seperated list of destination ports to match. |
| 76 |
type: str |
| 77 |
dest_cidr: |
| 78 |
description: |
| 79 |
- Comma-separated list of CIDR notation networks to match. |
| 80 |
type: str |
| 81 |
comment: |
| 82 |
description: |
| 83 |
- Optional comment describing the firewall rule. |
| 84 |
type: str |
| 85 |
author: |
| 86 |
- Kevin Breit (@kbreit) |
| 87 |
extends_documentation_fragment: cisco.meraki.meraki |
| 88 |
''' |
| 89 |
|
| 90 |
EXAMPLES = r''' |
| 91 |
- name: Create single firewall rule |
| 92 |
meraki_mr_l3_firewall: |
| 93 |
auth_key: abc123 |
| 94 |
state: present |
| 95 |
org_name: YourOrg |
| 96 |
net_id: 12345 |
| 97 |
number: 1 |
| 98 |
rules: |
| 99 |
- comment: Integration test rule |
| 100 |
policy: allow |
| 101 |
protocol: tcp |
| 102 |
dest_port: 80 |
| 103 |
dest_cidr: 192.0.2.0/24 |
| 104 |
allow_lan_access: false |
| 105 |
delegate_to: localhost |
| 106 |
|
| 107 |
- name: Enable local LAN access |
| 108 |
meraki_mr_l3_firewall: |
| 109 |
auth_key: abc123 |
| 110 |
state: present |
| 111 |
org_name: YourOrg |
| 112 |
net_id: 123 |
| 113 |
number: 1 |
| 114 |
rules: |
| 115 |
allow_lan_access: true |
| 116 |
delegate_to: localhost |
| 117 |
|
| 118 |
- name: Query firewall rules |
| 119 |
meraki_mr_l3_firewall: |
| 120 |
auth_key: abc123 |
| 121 |
state: query |
| 122 |
org_name: YourOrg |
| 123 |
net_name: YourNet |
| 124 |
number: 1 |
| 125 |
delegate_to: localhost |
| 126 |
''' |
| 127 |
|
| 128 |
RETURN = r''' |
| 129 |
|
| 130 |
''' |
| 131 |
|
| 132 |
from ansible.module_utils.basic import AnsibleModule, json |
| 133 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 134 |
|
| 135 |
|
| 136 |
def assemble_payload(meraki): |
| 137 |
params_map = {'policy': 'policy', |
| 138 |
'protocol': 'protocol', |
| 139 |
'dest_port': 'destPort', |
| 140 |
'dest_cidr': 'destCidr', |
| 141 |
'comment': 'comment', |
| 142 |
} |
| 143 |
rules = [] |
| 144 |
for rule in meraki.params['rules']: |
| 145 |
proposed_rule = dict() |
| 146 |
for k, v in rule.items(): |
| 147 |
proposed_rule[params_map[k]] = v |
| 148 |
rules.append(proposed_rule) |
| 149 |
payload = {'rules': rules} |
| 150 |
return payload |
| 151 |
|
| 152 |
|
| 153 |
def get_rules(meraki, net_id, number): |
| 154 |
path = meraki.construct_path('get_all', net_id=net_id, custom={'number': number}) |
| 155 |
response = meraki.request(path, method='GET') |
| 156 |
if meraki.status == 200: |
| 157 |
return normalize_rule_case(response) |
| 158 |
|
| 159 |
|
| 160 |
def normalize_rule_case(rules): |
| 161 |
excluded = ['comment'] |
| 162 |
try: |
| 163 |
for r in rules['rules']: |
| 164 |
for k in r: |
| 165 |
if k not in excluded: |
| 166 |
r[k] = r[k].lower() |
| 167 |
except KeyError: |
| 168 |
return rules |
| 169 |
return rules |
| 170 |
|
| 171 |
|
| 172 |
def get_ssid_number(name, data): |
| 173 |
for ssid in data: |
| 174 |
if name == ssid['name']: |
| 175 |
return ssid['number'] |
| 176 |
return False |
| 177 |
|
| 178 |
|
| 179 |
def get_ssids(meraki, net_id): |
| 180 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 181 |
return meraki.request(path, method='GET') |
| 182 |
|
| 183 |
|
| 184 |
def main(): |
| 185 |
# define the available arguments/parameters that a user can pass to |
| 186 |
# the module |
| 187 |
|
| 188 |
fw_rules = dict(policy=dict(type='str', choices=['allow', 'deny']), |
| 189 |
protocol=dict(type='str', choices=['tcp', 'udp', 'icmp', 'any']), |
| 190 |
dest_port=dict(type='str'), |
| 191 |
dest_cidr=dict(type='str'), |
| 192 |
comment=dict(type='str'), |
| 193 |
) |
| 194 |
|
| 195 |
argument_spec = meraki_argument_spec() |
| 196 |
argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='present'), |
| 197 |
net_name=dict(type='str'), |
| 198 |
net_id=dict(type='str'), |
| 199 |
number=dict(type='str', aliases=['ssid_number']), |
| 200 |
ssid_name=dict(type='str', aliases=['ssid']), |
| 201 |
rules=dict(type='list', default=None, elements='dict', options=fw_rules), |
| 202 |
allow_lan_access=dict(type='bool', default=True), |
| 203 |
) |
| 204 |
|
| 205 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 206 |
# this includes instantiation, a couple of common attr would be the |
| 207 |
# args/params passed to the execution, as well as if the module |
| 208 |
# supports check mode |
| 209 |
module = AnsibleModule(argument_spec=argument_spec, |
| 210 |
supports_check_mode=True, |
| 211 |
) |
| 212 |
meraki = MerakiModule(module, function='mr_l3_firewall') |
| 213 |
|
| 214 |
meraki.params['follow_redirects'] = 'all' |
| 215 |
|
| 216 |
query_urls = {'mr_l3_firewall': '/networks/{net_id}/wireless/ssids/{number}/firewall/l3FirewallRules'} |
| 217 |
update_urls = {'mr_l3_firewall': '/networks/{net_id}/wireless/ssids/{number}/firewall/l3FirewallRules'} |
| 218 |
|
| 219 |
meraki.url_catalog['get_all'].update(query_urls) |
| 220 |
meraki.url_catalog['update'] = update_urls |
| 221 |
|
| 222 |
payload = None |
| 223 |
|
| 224 |
# execute checks for argument completeness |
| 225 |
|
| 226 |
# manipulate or modify the state as needed (this is going to be the |
| 227 |
# part where your module will do what it needs to do) |
| 228 |
org_id = meraki.params['org_id'] |
| 229 |
orgs = None |
| 230 |
if org_id is None: |
| 231 |
orgs = meraki.get_orgs() |
| 232 |
for org in orgs: |
| 233 |
if org['name'] == meraki.params['org_name']: |
| 234 |
org_id = org['id'] |
| 235 |
net_id = meraki.params['net_id'] |
| 236 |
if net_id is None: |
| 237 |
if orgs is None: |
| 238 |
orgs = meraki.get_orgs() |
| 239 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], |
| 240 |
data=meraki.get_nets(org_id=org_id)) |
| 241 |
number = meraki.params['number'] |
| 242 |
if meraki.params['ssid_name']: |
| 243 |
number = get_ssid_number(meraki.params['ssid_name'], get_ssids(meraki, net_id)) |
| 244 |
|
| 245 |
if meraki.params['state'] == 'query': |
| 246 |
meraki.result['data'] = get_rules(meraki, net_id, number) |
| 247 |
elif meraki.params['state'] == 'present': |
| 248 |
rules = get_rules(meraki, net_id, number) |
| 249 |
path = meraki.construct_path('get_all', net_id=net_id, custom={'number': number}) |
| 250 |
if meraki.params['rules']: |
| 251 |
payload = assemble_payload(meraki) |
| 252 |
else: |
| 253 |
payload = dict() |
| 254 |
update = False |
| 255 |
try: |
| 256 |
if len(rules) != len(payload['rules']): # Quick and simple check to avoid more processing |
| 257 |
update = True |
| 258 |
if update is False: |
| 259 |
for r in range(len(rules) - 2): |
| 260 |
if meraki.is_update_required(rules[r], payload[r]) is True: |
| 261 |
update = True |
| 262 |
except KeyError: |
| 263 |
pass |
| 264 |
# meraki.fail_json(msg=rules) |
| 265 |
if rules['rules'][len(rules['rules']) - 2] != meraki.params['allow_lan_access']: |
| 266 |
update = True |
| 267 |
if update is True: |
| 268 |
payload['allowLanAccess'] = meraki.params['allow_lan_access'] |
| 269 |
if meraki.check_mode is True: |
| 270 |
# This code is disgusting, rework it at some point |
| 271 |
if 'rules' in payload: |
| 272 |
cleansed_payload = payload['rules'] |
| 273 |
cleansed_payload.append(rules['rules'][len(rules['rules']) - 1]) |
| 274 |
cleansed_payload.append(rules['rules'][len(rules['rules']) - 2]) |
| 275 |
if meraki.params['allow_lan_access'] is None: |
| 276 |
cleansed_payload[len(cleansed_payload) - 2]['policy'] = rules['rules'][len(rules['rules']) - 2]['policy'] |
| 277 |
else: |
| 278 |
if meraki.params['allow_lan_access'] is True: |
| 279 |
cleansed_payload[len(cleansed_payload) - 2]['policy'] = 'allow' |
| 280 |
else: |
| 281 |
cleansed_payload[len(cleansed_payload) - 2]['policy'] = 'deny' |
| 282 |
else: |
| 283 |
if meraki.params['allow_lan_access'] is True: |
| 284 |
rules['rules'][len(rules['rules']) - 2]['policy'] = 'allow' |
| 285 |
else: |
| 286 |
rules['rules'][len(rules['rules']) - 2]['policy'] = 'deny' |
| 287 |
cleansed_payload = rules |
| 288 |
meraki.result['data'] = cleansed_payload |
| 289 |
meraki.result['changed'] = True |
| 290 |
meraki.exit_json(**meraki.result) |
| 291 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 292 |
if meraki.status == 200: |
| 293 |
meraki.result['data'] = response |
| 294 |
meraki.result['changed'] = True |
| 295 |
else: |
| 296 |
meraki.result['data'] = rules |
| 297 |
|
| 298 |
# in the event of a successful module execution, you will want to |
| 299 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 300 |
meraki.exit_json(**meraki.result) |
| 301 |
|
| 302 |
|
| 303 |
if __name__ == '__main__': |
| 304 |
main() |