| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, 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_mx_l7_firewall |
| 19 |
short_description: Manage MX appliance layer 7 firewalls in the Meraki cloud |
| 20 |
description: |
| 21 |
- Allows for creation, management, and visibility into layer 7 firewalls implemented on Meraki MX firewalls. |
| 22 |
notes: |
| 23 |
- Module assumes a complete list of firewall rules are passed as a parameter. |
| 24 |
- If there is interest in this module allowing manipulation of a single firewall rule, please submit an issue against this module. |
| 25 |
deprecated: |
| 26 |
removed_in: '3.0.0' |
| 27 |
why: Updated modules released with increased functionality |
| 28 |
alternative: cisco.meraki.networks_appliance_firewall_l7_firewall_rules |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Query or modify a firewall rule. |
| 33 |
choices: ['present', 'query'] |
| 34 |
default: present |
| 35 |
type: str |
| 36 |
net_name: |
| 37 |
description: |
| 38 |
- Name of network which MX firewall is in. |
| 39 |
type: str |
| 40 |
net_id: |
| 41 |
description: |
| 42 |
- ID of network which MX firewall is in. |
| 43 |
type: str |
| 44 |
rules: |
| 45 |
description: |
| 46 |
- List of layer 7 firewall rules. |
| 47 |
type: list |
| 48 |
elements: dict |
| 49 |
suboptions: |
| 50 |
policy: |
| 51 |
description: |
| 52 |
- Policy to apply if rule is hit. |
| 53 |
choices: [deny] |
| 54 |
default: deny |
| 55 |
type: str |
| 56 |
type: |
| 57 |
description: |
| 58 |
- Type of policy to apply. |
| 59 |
choices: [application, |
| 60 |
application_category, |
| 61 |
blocked_countries, |
| 62 |
host, |
| 63 |
ip_range, |
| 64 |
port, |
| 65 |
allowed_countries] |
| 66 |
type: str |
| 67 |
application: |
| 68 |
description: |
| 69 |
- Application to filter. |
| 70 |
type: dict |
| 71 |
suboptions: |
| 72 |
name: |
| 73 |
description: |
| 74 |
- Name of application to filter as defined by Meraki. |
| 75 |
type: str |
| 76 |
id: |
| 77 |
description: |
| 78 |
- URI of application as defined by Meraki. |
| 79 |
type: str |
| 80 |
host: |
| 81 |
description: |
| 82 |
- FQDN of host to filter. |
| 83 |
type: str |
| 84 |
ip_range: |
| 85 |
description: |
| 86 |
- CIDR notation range of IP addresses to apply rule to. |
| 87 |
- Port can be appended to range with a C(":"). |
| 88 |
type: str |
| 89 |
port: |
| 90 |
description: |
| 91 |
- TCP or UDP based port to filter. |
| 92 |
type: str |
| 93 |
countries: |
| 94 |
description: |
| 95 |
- List of countries to whitelist or blacklist. |
| 96 |
- The countries follow the two-letter ISO 3166-1 alpha-2 format. |
| 97 |
type: list |
| 98 |
elements: str |
| 99 |
categories: |
| 100 |
description: |
| 101 |
- When C(True), specifies that applications and application categories should be queried instead of firewall rules. |
| 102 |
type: bool |
| 103 |
author: |
| 104 |
- Kevin Breit (@kbreit) |
| 105 |
extends_documentation_fragment: cisco.meraki.meraki |
| 106 |
''' |
| 107 |
|
| 108 |
EXAMPLES = r''' |
| 109 |
- name: Query firewall rules |
| 110 |
meraki_mx_l7_firewall: |
| 111 |
auth_key: abc123 |
| 112 |
org_name: YourOrg |
| 113 |
net_name: YourNet |
| 114 |
state: query |
| 115 |
delegate_to: localhost |
| 116 |
|
| 117 |
- name: Query applications and application categories |
| 118 |
meraki_mx_l7_firewall: |
| 119 |
auth_key: abc123 |
| 120 |
org_name: YourOrg |
| 121 |
net_name: YourNet |
| 122 |
categories: true |
| 123 |
state: query |
| 124 |
delegate_to: localhost |
| 125 |
|
| 126 |
- name: Set firewall rules |
| 127 |
meraki_mx_l7_firewall: |
| 128 |
auth_key: abc123 |
| 129 |
org_name: YourOrg |
| 130 |
net_name: YourNet |
| 131 |
state: present |
| 132 |
rules: |
| 133 |
- type: allowed_countries |
| 134 |
countries: |
| 135 |
- US |
| 136 |
- FR |
| 137 |
- type: blocked_countries |
| 138 |
countries: |
| 139 |
- CN |
| 140 |
- policy: deny |
| 141 |
type: port |
| 142 |
port: 8080 |
| 143 |
- type: port |
| 144 |
port: 1234 |
| 145 |
- type: host |
| 146 |
host: asdf.com |
| 147 |
- type: application |
| 148 |
application: |
| 149 |
id: meraki:layer7/application/205 |
| 150 |
- type: application_category |
| 151 |
application: |
| 152 |
id: meraki:layer7/category/24 |
| 153 |
delegate_to: localhost |
| 154 |
''' |
| 155 |
|
| 156 |
RETURN = r''' |
| 157 |
data: |
| 158 |
description: Firewall rules associated to network. |
| 159 |
returned: success |
| 160 |
type: complex |
| 161 |
contains: |
| 162 |
rules: |
| 163 |
description: Ordered list of firewall rules. |
| 164 |
returned: success, when not querying applications |
| 165 |
type: list |
| 166 |
contains: |
| 167 |
policy: |
| 168 |
description: Action to apply when rule is hit. |
| 169 |
returned: success |
| 170 |
type: str |
| 171 |
sample: deny |
| 172 |
type: |
| 173 |
description: Type of rule category. |
| 174 |
returned: success |
| 175 |
type: str |
| 176 |
sample: applications |
| 177 |
applications: |
| 178 |
description: List of applications within a category. |
| 179 |
type: list |
| 180 |
contains: |
| 181 |
id: |
| 182 |
description: URI of application. |
| 183 |
returned: success |
| 184 |
type: str |
| 185 |
sample: Gmail |
| 186 |
name: |
| 187 |
description: Descriptive name of application. |
| 188 |
returned: success |
| 189 |
type: str |
| 190 |
sample: meraki:layer7/application/4 |
| 191 |
applicationCategory: |
| 192 |
description: List of application categories within a category. |
| 193 |
type: list |
| 194 |
contains: |
| 195 |
id: |
| 196 |
description: URI of application. |
| 197 |
returned: success |
| 198 |
type: str |
| 199 |
sample: Gmail |
| 200 |
name: |
| 201 |
description: Descriptive name of application. |
| 202 |
returned: success |
| 203 |
type: str |
| 204 |
sample: meraki:layer7/application/4 |
| 205 |
port: |
| 206 |
description: Port number in rule. |
| 207 |
returned: success |
| 208 |
type: str |
| 209 |
sample: 23 |
| 210 |
ipRange: |
| 211 |
description: Range of IP addresses in rule. |
| 212 |
returned: success |
| 213 |
type: str |
| 214 |
sample: 1.1.1.0/23 |
| 215 |
allowedCountries: |
| 216 |
description: Countries to be allowed. |
| 217 |
returned: success |
| 218 |
type: str |
| 219 |
sample: CA |
| 220 |
blockedCountries: |
| 221 |
description: Countries to be blacklisted. |
| 222 |
returned: success |
| 223 |
type: str |
| 224 |
sample: RU |
| 225 |
application_categories: |
| 226 |
description: List of application categories and applications. |
| 227 |
type: list |
| 228 |
returned: success, when querying applications |
| 229 |
contains: |
| 230 |
applications: |
| 231 |
description: List of applications within a category. |
| 232 |
type: list |
| 233 |
contains: |
| 234 |
id: |
| 235 |
description: URI of application. |
| 236 |
returned: success |
| 237 |
type: str |
| 238 |
sample: Gmail |
| 239 |
name: |
| 240 |
description: Descriptive name of application. |
| 241 |
returned: success |
| 242 |
type: str |
| 243 |
sample: meraki:layer7/application/4 |
| 244 |
id: |
| 245 |
description: URI of application category. |
| 246 |
returned: success |
| 247 |
type: str |
| 248 |
sample: Email |
| 249 |
name: |
| 250 |
description: Descriptive name of application category. |
| 251 |
returned: success |
| 252 |
type: str |
| 253 |
sample: layer7/category/1 |
| 254 |
''' |
| 255 |
|
| 256 |
import copy |
| 257 |
from ansible.module_utils.basic import AnsibleModule, json |
| 258 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 259 |
|
| 260 |
|
| 261 |
def get_applications(meraki, net_id): |
| 262 |
path = meraki.construct_path('get_categories', net_id=net_id) |
| 263 |
return meraki.request(path, method='GET') |
| 264 |
|
| 265 |
|
| 266 |
def lookup_application(meraki, net_id, application): |
| 267 |
response = get_applications(meraki, net_id) |
| 268 |
for category in response['applicationCategories']: |
| 269 |
if category['name'].lower() == application.lower(): |
| 270 |
return category['id'] |
| 271 |
for app in category['applications']: |
| 272 |
if app['name'].lower() == application.lower(): |
| 273 |
return app['id'] |
| 274 |
meraki.fail_json(msg="No application or category named {0} found".format(application)) |
| 275 |
|
| 276 |
|
| 277 |
def assemble_payload(meraki, net_id, rule): |
| 278 |
if rule['type'] == 'application': |
| 279 |
new_rule = {'policy': rule['policy'], |
| 280 |
'type': 'application', |
| 281 |
} |
| 282 |
if rule['application']['id']: |
| 283 |
new_rule['value'] = {'id': rule['application']['id']} |
| 284 |
elif rule['application']['name']: |
| 285 |
new_rule['value'] = {'id': lookup_application(meraki, net_id, rule['application']['name'])} |
| 286 |
elif rule['type'] == 'application_category': |
| 287 |
new_rule = {'policy': rule['policy'], |
| 288 |
'type': 'applicationCategory', |
| 289 |
} |
| 290 |
if rule['application']['id']: |
| 291 |
new_rule['value'] = {'id': rule['application']['id']} |
| 292 |
elif rule['application']['name']: |
| 293 |
new_rule['value'] = {'id': lookup_application(meraki, net_id, rule['application']['name'])} |
| 294 |
elif rule['type'] == 'ip_range': |
| 295 |
new_rule = {'policy': rule['policy'], |
| 296 |
'type': 'ipRange', |
| 297 |
'value': rule['ip_range']} |
| 298 |
elif rule['type'] == 'host': |
| 299 |
new_rule = {'policy': rule['policy'], |
| 300 |
'type': rule['type'], |
| 301 |
'value': rule['host']} |
| 302 |
elif rule['type'] == 'port': |
| 303 |
new_rule = {'policy': rule['policy'], |
| 304 |
'type': rule['type'], |
| 305 |
'value': rule['port']} |
| 306 |
elif rule['type'] == 'blocked_countries': |
| 307 |
new_rule = {'policy': rule['policy'], |
| 308 |
'type': 'blockedCountries', |
| 309 |
'value': rule['countries'] |
| 310 |
} |
| 311 |
elif rule['type'] == 'allowed_countries': |
| 312 |
new_rule = {'policy': rule['policy'], |
| 313 |
'type': 'allowedCountries', |
| 314 |
'value': rule['countries'] |
| 315 |
} |
| 316 |
return new_rule |
| 317 |
|
| 318 |
|
| 319 |
def restructure_response(rules): |
| 320 |
for rule in rules['rules']: |
| 321 |
type = rule['type'] |
| 322 |
rule[type] = copy.deepcopy(rule['value']) |
| 323 |
del rule['value'] |
| 324 |
return rules |
| 325 |
|
| 326 |
|
| 327 |
def get_rules(meraki, net_id): |
| 328 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 329 |
response = meraki.request(path, method='GET') |
| 330 |
if meraki.status == 200: |
| 331 |
return response |
| 332 |
|
| 333 |
|
| 334 |
def main(): |
| 335 |
# define the available arguments/parameters that a user can pass to |
| 336 |
# the module |
| 337 |
|
| 338 |
application_arg_spec = dict(id=dict(type='str'), |
| 339 |
name=dict(type='str'), |
| 340 |
) |
| 341 |
|
| 342 |
rule_arg_spec = dict(policy=dict(type='str', choices=['deny'], default='deny'), |
| 343 |
type=dict(type='str', choices=['application', |
| 344 |
'application_category', |
| 345 |
'blocked_countries', |
| 346 |
'host', |
| 347 |
'ip_range', |
| 348 |
'port', |
| 349 |
'allowed_countries']), |
| 350 |
ip_range=dict(type='str'), |
| 351 |
application=dict(type='dict', default=None, options=application_arg_spec), |
| 352 |
host=dict(type='str'), |
| 353 |
port=dict(type='str'), |
| 354 |
countries=dict(type='list', elements='str'), |
| 355 |
) |
| 356 |
|
| 357 |
argument_spec = meraki_argument_spec() |
| 358 |
argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='present'), |
| 359 |
net_name=dict(type='str'), |
| 360 |
net_id=dict(type='str'), |
| 361 |
rules=dict(type='list', default=None, elements='dict', options=rule_arg_spec), |
| 362 |
categories=dict(type='bool'), |
| 363 |
) |
| 364 |
|
| 365 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 366 |
# this includes instantiation, a couple of common attr would be the |
| 367 |
# args/params passed to the execution, as well as if the module |
| 368 |
# supports check mode |
| 369 |
module = AnsibleModule(argument_spec=argument_spec, |
| 370 |
supports_check_mode=True, |
| 371 |
) |
| 372 |
meraki = MerakiModule(module, function='mx_l7_firewall') |
| 373 |
|
| 374 |
# check for argument completeness |
| 375 |
if meraki.params['rules']: |
| 376 |
for rule in meraki.params['rules']: |
| 377 |
if rule['type'] == 'application' and rule['application'] is None: |
| 378 |
meraki.fail_json(msg="application argument is required when type is application.") |
| 379 |
elif rule['type'] == 'application_category' and rule['application'] is None: |
| 380 |
meraki.fail_json(msg="application argument is required when type is application_category.") |
| 381 |
elif rule['type'] == 'blocked_countries' and rule['countries'] is None: |
| 382 |
meraki.fail_json(msg="countries argument is required when type is blocked_countries.") |
| 383 |
elif rule['type'] == 'host' and rule['host'] is None: |
| 384 |
meraki.fail_json(msg="host argument is required when type is host.") |
| 385 |
elif rule['type'] == 'port' and rule['port'] is None: |
| 386 |
meraki.fail_json(msg="port argument is required when type is port.") |
| 387 |
elif rule['type'] == 'allowed_countries' and rule['countries'] is None: |
| 388 |
meraki.fail_json(msg="countries argument is required when type is allowed_countries.") |
| 389 |
|
| 390 |
meraki.params['follow_redirects'] = 'all' |
| 391 |
|
| 392 |
query_urls = {'mx_l7_firewall': '/networks/{net_id}/appliance/firewall/l7FirewallRules/'} |
| 393 |
query_category_urls = {'mx_l7_firewall': '/networks/{net_id}/appliance/firewall/l7FirewallRules/applicationCategories'} |
| 394 |
update_urls = {'mx_l7_firewall': '/networks/{net_id}/appliance/firewall/l7FirewallRules/'} |
| 395 |
|
| 396 |
meraki.url_catalog['get_all'].update(query_urls) |
| 397 |
meraki.url_catalog['get_categories'] = (query_category_urls) |
| 398 |
meraki.url_catalog['update'] = update_urls |
| 399 |
|
| 400 |
payload = None |
| 401 |
|
| 402 |
# manipulate or modify the state as needed (this is going to be the |
| 403 |
# part where your module will do what it needs to do) |
| 404 |
org_id = meraki.params['org_id'] |
| 405 |
orgs = None |
| 406 |
if org_id is None: |
| 407 |
orgs = meraki.get_orgs() |
| 408 |
for org in orgs: |
| 409 |
if org['name'] == meraki.params['org_name']: |
| 410 |
org_id = org['id'] |
| 411 |
net_id = meraki.params['net_id'] |
| 412 |
if net_id is None: |
| 413 |
if orgs is None: |
| 414 |
orgs = meraki.get_orgs() |
| 415 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], |
| 416 |
data=meraki.get_nets(org_id=org_id)) |
| 417 |
|
| 418 |
if meraki.params['state'] == 'query': |
| 419 |
if meraki.params['categories'] is True: # Output only applications |
| 420 |
meraki.result['data'] = get_applications(meraki, net_id) |
| 421 |
else: |
| 422 |
meraki.result['data'] = restructure_response(get_rules(meraki, net_id)) |
| 423 |
elif meraki.params['state'] == 'present': |
| 424 |
rules = get_rules(meraki, net_id) |
| 425 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 426 |
|
| 427 |
# Detect if no rules are given, special case |
| 428 |
if len(meraki.params['rules']) == 0: |
| 429 |
# Conditionally wrap parameters in rules makes it comparable |
| 430 |
if isinstance(meraki.params['rules'], list): |
| 431 |
param_rules = {'rules': meraki.params['rules']} |
| 432 |
else: |
| 433 |
param_rules = meraki.params['rules'] |
| 434 |
if meraki.is_update_required(rules, param_rules): |
| 435 |
if meraki.module.check_mode is True: |
| 436 |
meraki.result['data'] = meraki.params['rules'] |
| 437 |
meraki.result['changed'] = True |
| 438 |
meraki.exit_json(**meraki.result) |
| 439 |
payload = {'rules': []} |
| 440 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 441 |
meraki.result['data'] = response |
| 442 |
meraki.result['changed'] = True |
| 443 |
meraki.exit_json(**meraki.result) |
| 444 |
else: |
| 445 |
meraki.result['data'] = param_rules |
| 446 |
meraki.exit_json(**meraki.result) |
| 447 |
if meraki.params['rules']: |
| 448 |
payload = {'rules': []} |
| 449 |
for rule in meraki.params['rules']: |
| 450 |
payload['rules'].append(assemble_payload(meraki, net_id, rule)) |
| 451 |
else: |
| 452 |
payload = dict() |
| 453 |
if meraki.is_update_required(rules, payload, force_include='id'): |
| 454 |
if meraki.module.check_mode is True: |
| 455 |
response = restructure_response(payload) |
| 456 |
meraki.generate_diff(restructure_response(rules), response) |
| 457 |
meraki.result['data'] = response |
| 458 |
meraki.result['changed'] = True |
| 459 |
meraki.exit_json(**meraki.result) |
| 460 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 461 |
response = restructure_response(response) |
| 462 |
if meraki.status == 200: |
| 463 |
meraki.generate_diff(restructure_response(rules), response) |
| 464 |
meraki.result['data'] = response |
| 465 |
meraki.result['changed'] = True |
| 466 |
else: |
| 467 |
if meraki.module.check_mode is True: |
| 468 |
meraki.result['data'] = rules |
| 469 |
meraki.result['changed'] = False |
| 470 |
meraki.exit_json(**meraki.result) |
| 471 |
meraki.result['data'] = payload |
| 472 |
|
| 473 |
# in the event of a successful module execution, you will want to |
| 474 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 475 |
meraki.exit_json(**meraki.result) |
| 476 |
|
| 477 |
|
| 478 |
if __name__ == '__main__': |
| 479 |
main() |