| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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_intrusion_prevention |
| 19 |
short_description: Manage intrustion prevention in the Meraki cloud |
| 20 |
description: |
| 21 |
- Allows for management of intrusion prevention rules networks within Meraki MX networks. |
| 22 |
|
| 23 |
deprecated: |
| 24 |
removed_in: '3.0.0' |
| 25 |
why: Updated modules released with increased functionality |
| 26 |
alternative: cisco.meraki.organizations_appliance_security_intrusion and cisco.meraki.networks_appliance_security_intrusion |
| 27 |
options: |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Create or modify an organization. |
| 31 |
choices: [ absent, present, query ] |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
net_name: |
| 35 |
description: |
| 36 |
- Name of a network. |
| 37 |
aliases: [ name, network ] |
| 38 |
type: str |
| 39 |
net_id: |
| 40 |
description: |
| 41 |
- ID number of a network. |
| 42 |
type: str |
| 43 |
mode: |
| 44 |
description: |
| 45 |
- Operational mode of Intrusion Prevention system. |
| 46 |
choices: [ detection, disabled, prevention ] |
| 47 |
type: str |
| 48 |
ids_rulesets: |
| 49 |
description: |
| 50 |
- Ruleset complexity setting. |
| 51 |
choices: [ connectivity, balanced, security ] |
| 52 |
type: str |
| 53 |
allowed_rules: |
| 54 |
description: |
| 55 |
- List of IDs related to rules which are allowed for the organization. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
suboptions: |
| 59 |
rule_id: |
| 60 |
description: |
| 61 |
- ID of rule as defined by Snort. |
| 62 |
type: str |
| 63 |
rule_message: |
| 64 |
description: |
| 65 |
- Description of rule. |
| 66 |
- This is overwritten by the API. |
| 67 |
- Formerly C(message_rule) which was deprecated but still maintained as an alias. |
| 68 |
type: str |
| 69 |
aliases: [ message_rule ] |
| 70 |
version_added: "2.3.0" |
| 71 |
protected_networks: |
| 72 |
description: |
| 73 |
- Set included/excluded networks for Intrusion Prevention. |
| 74 |
type: dict |
| 75 |
suboptions: |
| 76 |
use_default: |
| 77 |
description: |
| 78 |
- Whether to use special IPv4 addresses per RFC 5735. |
| 79 |
type: bool |
| 80 |
included_cidr: |
| 81 |
description: |
| 82 |
- List of network IP ranges to include in scanning. |
| 83 |
type: list |
| 84 |
elements: str |
| 85 |
excluded_cidr: |
| 86 |
description: |
| 87 |
- List of network IP ranges to exclude from scanning. |
| 88 |
type: list |
| 89 |
elements: str |
| 90 |
|
| 91 |
author: |
| 92 |
- Kevin Breit (@kbreit) |
| 93 |
extends_documentation_fragment: cisco.meraki.meraki |
| 94 |
''' |
| 95 |
|
| 96 |
EXAMPLES = r''' |
| 97 |
- name: Set whitelist for organization |
| 98 |
meraki_intrusion_prevention: |
| 99 |
auth_key: '{{ auth_key }}' |
| 100 |
state: present |
| 101 |
org_id: '{{ test_org_id }}' |
| 102 |
allowed_rules: |
| 103 |
- rule_id: "meraki:intrusion/snort/GID/01/SID/5805" |
| 104 |
rule_message: Test rule |
| 105 |
delegate_to: localhost |
| 106 |
|
| 107 |
- name: Query IPS info for organization |
| 108 |
meraki_intrusion_prevention: |
| 109 |
auth_key: '{{ auth_key }}' |
| 110 |
state: query |
| 111 |
org_name: '{{ test_org_name }}' |
| 112 |
delegate_to: localhost |
| 113 |
register: query_org |
| 114 |
|
| 115 |
- name: Set full ruleset with check mode |
| 116 |
meraki_intrusion_prevention: |
| 117 |
auth_key: '{{ auth_key }}' |
| 118 |
state: present |
| 119 |
org_name: '{{ test_org_name }}' |
| 120 |
net_name: '{{ test_net_name }} - IPS' |
| 121 |
mode: prevention |
| 122 |
ids_rulesets: security |
| 123 |
protected_networks: |
| 124 |
use_default: true |
| 125 |
included_cidr: |
| 126 |
- 192.0.1.0/24 |
| 127 |
excluded_cidr: |
| 128 |
- 10.0.1.0/24 |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Clear rules from organization |
| 132 |
meraki_intrusion_prevention: |
| 133 |
auth_key: '{{ auth_key }}' |
| 134 |
state: absent |
| 135 |
org_name: '{{ test_org_name }}' |
| 136 |
allowed_rules: [] |
| 137 |
delegate_to: localhost |
| 138 |
''' |
| 139 |
|
| 140 |
RETURN = r''' |
| 141 |
data: |
| 142 |
description: Information about the Threat Protection settings. |
| 143 |
returned: success |
| 144 |
type: complex |
| 145 |
contains: |
| 146 |
whitelistedRules: |
| 147 |
description: List of whitelisted IPS rules. |
| 148 |
returned: success, when organization is queried or modified |
| 149 |
type: complex |
| 150 |
contains: |
| 151 |
ruleId: |
| 152 |
description: A rule identifier for an IPS rule. |
| 153 |
returned: success, when organization is queried or modified |
| 154 |
type: str |
| 155 |
sample: "meraki:intrusion/snort/GID/01/SID/5805" |
| 156 |
rule_message: |
| 157 |
description: Description of rule. |
| 158 |
returned: success, when organization is queried or modified |
| 159 |
type: str |
| 160 |
sample: "MALWARE-OTHER Trackware myway speedbar runtime detection - switch engines" |
| 161 |
mode: |
| 162 |
description: Enabled setting of intrusion prevention. |
| 163 |
returned: success, when network is queried or modified |
| 164 |
type: str |
| 165 |
sample: enabled |
| 166 |
idsRulesets: |
| 167 |
description: Setting of selected ruleset. |
| 168 |
returned: success, when network is queried or modified |
| 169 |
type: str |
| 170 |
sample: balanced |
| 171 |
protectedNetworks: |
| 172 |
description: Networks protected by IPS. |
| 173 |
returned: success, when network is queried or modified |
| 174 |
type: complex |
| 175 |
contains: |
| 176 |
useDefault: |
| 177 |
description: Whether to use special IPv4 addresses. |
| 178 |
returned: success, when network is queried or modified |
| 179 |
type: bool |
| 180 |
sample: true |
| 181 |
includedCidr: |
| 182 |
description: List of CIDR notiation networks to protect. |
| 183 |
returned: success, when network is queried or modified |
| 184 |
type: str |
| 185 |
sample: 192.0.1.0/24 |
| 186 |
excludedCidr: |
| 187 |
description: List of CIDR notiation networks to exclude from protection. |
| 188 |
returned: success, when network is queried or modified |
| 189 |
type: str |
| 190 |
sample: 192.0.1.0/24 |
| 191 |
|
| 192 |
''' |
| 193 |
|
| 194 |
from ansible.module_utils.basic import AnsibleModule, json |
| 195 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 196 |
|
| 197 |
param_map = {'allowed_rules': 'allowedrules', |
| 198 |
'rule_id': 'ruleId', |
| 199 |
'rule_message': 'message_rule', |
| 200 |
'mode': 'mode', |
| 201 |
'protected_networks': 'protectedNetworks', |
| 202 |
'use_default': 'useDefault', |
| 203 |
'included_cidr': 'includedCidr', |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
allowedrules_arg_spec = dict(rule_id=dict(type='str'), |
| 213 |
rule_message=dict(type='str', |
| 214 |
aliases=['message_rule'], |
| 215 |
deprecated_aliases=[dict(name='message_rule', version='3.0.0', collection_name='cisco.meraki')]), |
| 216 |
) |
| 217 |
|
| 218 |
protected_nets_arg_spec = dict(use_default=dict(type='bool'), |
| 219 |
included_cidr=dict(type='list', elements='str'), |
| 220 |
excluded_cidr=dict(type='list', elements='str'), |
| 221 |
) |
| 222 |
|
| 223 |
argument_spec = meraki_argument_spec() |
| 224 |
argument_spec.update( |
| 225 |
net_id=dict(type='str'), |
| 226 |
net_name=dict(type='str', aliases=['name', 'network']), |
| 227 |
state=dict(type='str', choices=['absent', 'present', 'query'], default='present'), |
| 228 |
allowed_rules=dict(type='list', default=None, elements='dict', options=allowedrules_arg_spec), |
| 229 |
mode=dict(type='str', choices=['detection', 'disabled', 'prevention']), |
| 230 |
ids_rulesets=dict(type='str', choices=['connectivity', 'balanced', 'security']), |
| 231 |
protected_networks=dict(type='dict', default=None, options=protected_nets_arg_spec), |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
module = AnsibleModule(argument_spec=argument_spec, |
| 239 |
supports_check_mode=True, |
| 240 |
) |
| 241 |
|
| 242 |
meraki = MerakiModule(module, function='intrusion_prevention') |
| 243 |
module.params['follow_redirects'] = 'all' |
| 244 |
payload = None |
| 245 |
|
| 246 |
query_org_urls = {'intrusion_prevention': '/organizations/{org_id}/appliance/security/intrusion'} |
| 247 |
query_net_urls = {'intrusion_prevention': '/networks/{net_id}/appliance/security/intrusion'} |
| 248 |
set_org_urls = {'intrusion_prevention': '/organizations/{org_id}/appliance/security/intrusion'} |
| 249 |
set_net_urls = {'intrusion_prevention': '/networks/{net_id}/appliance/security/intrusion'} |
| 250 |
meraki.url_catalog['query_org'] = query_org_urls |
| 251 |
meraki.url_catalog['query_net'] = query_net_urls |
| 252 |
meraki.url_catalog['set_org'] = set_org_urls |
| 253 |
meraki.url_catalog['set_net'] = set_net_urls |
| 254 |
|
| 255 |
if not meraki.params['org_name'] and not meraki.params['org_id']: |
| 256 |
meraki.fail_json(msg='org_name or org_id parameters are required') |
| 257 |
if meraki.params['net_name'] and meraki.params['net_id']: |
| 258 |
meraki.fail_json(msg='net_name and net_id are mutually exclusive') |
| 259 |
if meraki.params['net_name'] is None and meraki.params['net_id'] is None: |
| 260 |
if meraki.params['state'] == 'present': |
| 261 |
if meraki.params['allowed_rules'] is None: |
| 262 |
meraki.fail_json(msg='allowed_rules is required when state is present and no network is specified.') |
| 263 |
if meraki.params['net_name'] or meraki.params['net_id']: |
| 264 |
if meraki.params['state'] == 'present': |
| 265 |
if meraki.params['protected_networks'] is not None: |
| 266 |
if meraki.params['protected_networks']['use_default'] is False and meraki.params['protected_networks']['included_cidr'] is None: |
| 267 |
meraki.fail_json(msg="included_cidr is required when use_default is False.") |
| 268 |
if meraki.params['protected_networks']['use_default'] is False and meraki.params['protected_networks']['excluded_cidr'] is None: |
| 269 |
meraki.fail_json(msg="excluded_cidr is required when use_default is False.") |
| 270 |
|
| 271 |
org_id = meraki.params['org_id'] |
| 272 |
if not org_id: |
| 273 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 274 |
net_id = meraki.params['net_id'] |
| 275 |
if net_id is None and meraki.params['net_name']: |
| 276 |
nets = meraki.get_nets(org_id=org_id) |
| 277 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) |
| 278 |
|
| 279 |
|
| 280 |
if meraki.params['state'] == 'present': |
| 281 |
if net_id is None: |
| 282 |
rules = [] |
| 283 |
for rule in meraki.params['allowed_rules']: |
| 284 |
rules.append({'ruleId': rule['rule_id'], |
| 285 |
'message_rule': rule['rule_message'], |
| 286 |
}) |
| 287 |
payload = {'allowedRules': rules} |
| 288 |
else: |
| 289 |
payload = dict() |
| 290 |
if meraki.params['mode']: |
| 291 |
payload['mode'] = meraki.params['mode'] |
| 292 |
if meraki.params['ids_rulesets']: |
| 293 |
payload['idsRulesets'] = meraki.params['ids_rulesets'] |
| 294 |
if meraki.params['protected_networks']: |
| 295 |
payload['protectedNetworks'] = {} |
| 296 |
if meraki.params['protected_networks']['use_default']: |
| 297 |
payload['protectedNetworks'].update({'useDefault': meraki.params['protected_networks']['use_default']}) |
| 298 |
if meraki.params['protected_networks']['included_cidr']: |
| 299 |
payload['protectedNetworks'].update({'includedCidr': meraki.params['protected_networks']['included_cidr']}) |
| 300 |
if meraki.params['protected_networks']['excluded_cidr']: |
| 301 |
payload['protectedNetworks'].update({'excludedCidr': meraki.params['protected_networks']['excluded_cidr']}) |
| 302 |
elif meraki.params['state'] == 'absent': |
| 303 |
if net_id is None: |
| 304 |
payload = {'allowedRules': []} |
| 305 |
|
| 306 |
if meraki.params['state'] == 'query': |
| 307 |
if net_id is None: |
| 308 |
path = meraki.construct_path('query_org', org_id=org_id) |
| 309 |
data = meraki.request(path, method='GET') |
| 310 |
if meraki.status == 200: |
| 311 |
meraki.result['data'] = data |
| 312 |
else: |
| 313 |
path = meraki.construct_path('query_net', net_id=net_id) |
| 314 |
data = meraki.request(path, method='GET') |
| 315 |
elif meraki.params['state'] == 'present': |
| 316 |
path = meraki.construct_path('query_org', org_id=org_id) |
| 317 |
original = meraki.request(path, method='GET') |
| 318 |
if net_id is None: |
| 319 |
if meraki.is_update_required(original, payload, optional_ignore=['message_rule']): |
| 320 |
if meraki.module.check_mode is True: |
| 321 |
original.update(payload) |
| 322 |
meraki.result['data'] = original |
| 323 |
meraki.result['changed'] = True |
| 324 |
meraki.exit_json(**meraki.result) |
| 325 |
path = meraki.construct_path('set_org', org_id=org_id) |
| 326 |
data = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 327 |
if meraki.status == 200: |
| 328 |
meraki.result['data'] = data |
| 329 |
meraki.result['changed'] = True |
| 330 |
else: |
| 331 |
meraki.result['data'] = original |
| 332 |
meraki.result['changed'] = False |
| 333 |
else: |
| 334 |
path = meraki.construct_path('query_net', net_id=net_id) |
| 335 |
original = meraki.request(path, method='GET') |
| 336 |
if meraki.is_update_required(original, payload): |
| 337 |
if meraki.module.check_mode is True: |
| 338 |
payload.update(original) |
| 339 |
meraki.result['data'] = payload |
| 340 |
meraki.result['changed'] = True |
| 341 |
meraki.exit_json(**meraki.result) |
| 342 |
path = meraki.construct_path('set_net', net_id=net_id) |
| 343 |
data = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 344 |
if meraki.status == 200: |
| 345 |
meraki.result['data'] = data |
| 346 |
meraki.result['changed'] = True |
| 347 |
else: |
| 348 |
meraki.result['data'] = original |
| 349 |
meraki.result['changed'] = False |
| 350 |
elif meraki.params['state'] == 'absent': |
| 351 |
if net_id is None: |
| 352 |
path = meraki.construct_path('query_org', org_id=org_id) |
| 353 |
original = meraki.request(path, method='GET') |
| 354 |
if meraki.is_update_required(original, payload): |
| 355 |
if meraki.module.check_mode is True: |
| 356 |
payload.update(original) |
| 357 |
meraki.result['data'] = payload |
| 358 |
meraki.result['changed'] = True |
| 359 |
meraki.exit_json(**meraki.result) |
| 360 |
path = meraki.construct_path('set_org', org_id=org_id) |
| 361 |
data = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 362 |
if meraki.status == 200: |
| 363 |
meraki.result['data'] = data |
| 364 |
meraki.result['changed'] = True |
| 365 |
else: |
| 366 |
meraki.result['data'] = original |
| 367 |
meraki.result['changed'] = False |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
meraki.exit_json(**meraki.result) |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == '__main__': |
| 375 |
main() |