| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_firewall_log_profile |
| 13 |
short_description: Manages AFM logging profiles configured in the system |
| 14 |
description: |
| 15 |
- Manages AFM (Advanced Firewall Manager) logging profiles configured in the system along with basic information about each profile. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the log profile. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- Description of the log profile. |
| 26 |
type: str |
| 27 |
dos_protection: |
| 28 |
description: |
| 29 |
- Configures DoS related settings of the log profile. |
| 30 |
suboptions: |
| 31 |
dns_publisher: |
| 32 |
description: |
| 33 |
- Specifies the name of the log publisher used for DNS DoS events. |
| 34 |
- To specify the log_publisher on a different partition from the AFM log profile, specify the name in fullpath |
| 35 |
format, e.g. C(/Foobar/log-publisher), otherwise the partition for the log publisher |
| 36 |
is inferred from the C(partition) module parameter. |
| 37 |
type: str |
| 38 |
sip_publisher: |
| 39 |
description: |
| 40 |
- Specifies the name of the log publisher used for SIP DoS events. |
| 41 |
- To specify the log_publisher on a different partition from the AFM log profile, specify the name in fullpath |
| 42 |
format, e.g. C(/Foobar/log-publisher), otherwise the partition for the log publisher |
| 43 |
is inferred from the C(partition) module parameter. |
| 44 |
type: str |
| 45 |
network_publisher: |
| 46 |
description: |
| 47 |
- Specifies the name of the log publisher used for DoS Network events. |
| 48 |
- To specify the log_publisher on a different partition from the AFM log profile, specify the name in fullpath |
| 49 |
format, e.g. C(/Foobar/log-publisher), otherwise the partition for the log publisher |
| 50 |
is inferred from the C(partition) module parameter. |
| 51 |
type: str |
| 52 |
type: dict |
| 53 |
ip_intelligence: |
| 54 |
description: |
| 55 |
- Configures IP Intelligence related settings of the log profile. |
| 56 |
suboptions: |
| 57 |
log_publisher: |
| 58 |
description: |
| 59 |
- Specifies the name of the log publisher used for IP Intelligence events. |
| 60 |
- To specify the log_publisher on a different partition from the AFM log profile, specify the name in fullpath |
| 61 |
format, e.g. C(/Foobar/log-publisher), otherwise the partition for the log publisher |
| 62 |
is inferred the from C(partition) module parameter. |
| 63 |
type: str |
| 64 |
rate_limit: |
| 65 |
description: |
| 66 |
- Defines a rate limit for all combined IP intelligence log messages per second. Beyond this rate limit, |
| 67 |
log messages are not logged until the threshold drops below the specified rate. |
| 68 |
- To specify an indefinite rate, use the value C(indefinite). |
| 69 |
- If specifying a numeric rate, the value must be between C(1) and C(4294967295). |
| 70 |
type: str |
| 71 |
log_rtbh: |
| 72 |
description: |
| 73 |
- When C(true), specifies remotely triggered blackholing events are logged. |
| 74 |
type: bool |
| 75 |
log_shun: |
| 76 |
description: |
| 77 |
- When C(true), specifies IP Intelligence shun list events are logged. |
| 78 |
- This option can only be set on the C(global-network) built-in profile. |
| 79 |
type: bool |
| 80 |
log_translation_fields: |
| 81 |
description: |
| 82 |
- This option is used to enable or disable the logging of translated (i.e server side) fields in IP |
| 83 |
Intelligence log messages. |
| 84 |
- Translated fields include (but are not limited to) source address/port, destination address/port, |
| 85 |
IP protocol, route domain, and VLAN. |
| 86 |
type: bool |
| 87 |
type: dict |
| 88 |
port_misuse: |
| 89 |
description: |
| 90 |
- Port Misuse log configuration. |
| 91 |
suboptions: |
| 92 |
log_publisher: |
| 93 |
description: |
| 94 |
- Specifies the name of the log publisher used for Port Misuse events. |
| 95 |
- To specify the log_publisher on a different partition from the AFM log profile, specify the name in fullpath |
| 96 |
format, e.g. C(/Foobar/log-publisher), otherwise the partition for the log publisher |
| 97 |
is inferred from the C(partition) module parameter. |
| 98 |
type: str |
| 99 |
rate_limit: |
| 100 |
description: |
| 101 |
- Defines a rate limit for all combined port misuse log messages per second. Beyond this rate limit, |
| 102 |
log messages are not logged until the threshold drops below the specified rate. |
| 103 |
- To specify an indefinite rate, use the value C(indefinite). |
| 104 |
- If specifying a numeric rate, the value must be between C(1) and C(4294967295). |
| 105 |
type: str |
| 106 |
type: dict |
| 107 |
partition: |
| 108 |
description: |
| 109 |
- Device partition to create log profile on. |
| 110 |
- Parameter also used when specifying names for log publishers, unless log publisher names are in fullpath format. |
| 111 |
type: str |
| 112 |
default: Common |
| 113 |
state: |
| 114 |
description: |
| 115 |
- When C(state) is C(present), ensures the resource exists. |
| 116 |
- When C(state) is C(absent), ensures the resource is removed. Attempts to remove built-in system profiles are |
| 117 |
ignored and no change is returned. |
| 118 |
type: str |
| 119 |
choices: |
| 120 |
- present |
| 121 |
- absent |
| 122 |
default: present |
| 123 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 124 |
author: |
| 125 |
- Wojciech Wypior (@wojtek0806) |
| 126 |
''' |
| 127 |
|
| 128 |
EXAMPLES = r''' |
| 129 |
- name: Create a basic log profile with port misuse |
| 130 |
bigip_firewall_log_profile: |
| 131 |
name: barbaz |
| 132 |
port_misuse: |
| 133 |
rate_limit: 30000 |
| 134 |
log_publisher: local-db-pub |
| 135 |
provider: |
| 136 |
password: secret |
| 137 |
server: lb.mydomain.com |
| 138 |
user: admin |
| 139 |
delegate_to: localhost |
| 140 |
|
| 141 |
- name: Change ip_intelligence settings, publisher on different partition, remove port misuse |
| 142 |
bigip_firewall_log_profile: |
| 143 |
name: barbaz |
| 144 |
ip_intelligence: |
| 145 |
rate_limit: 400000 |
| 146 |
log_translation_fields: true |
| 147 |
log_rtbh: true |
| 148 |
log_publisher: "/foobar/non-local-db" |
| 149 |
port_misuse: |
| 150 |
log_publisher: "" |
| 151 |
provider: |
| 152 |
password: secret |
| 153 |
server: lb.mydomain.com |
| 154 |
user: admin |
| 155 |
delegate_to: localhost |
| 156 |
|
| 157 |
- name: Create a log profile with dos protection, different partition |
| 158 |
bigip_firewall_log_profile: |
| 159 |
name: foobar |
| 160 |
partition: foobar |
| 161 |
dos_protection: |
| 162 |
dns_publisher: "/Common/local-db-pub" |
| 163 |
sip_publisher: "non-local-db" |
| 164 |
network_publisher: "/Common/local-db-pub" |
| 165 |
provider: |
| 166 |
password: secret |
| 167 |
server: lb.mydomain.com |
| 168 |
user: admin |
| 169 |
delegate_to: localhost |
| 170 |
|
| 171 |
- name: Remove log profile |
| 172 |
bigip_firewall_log_profile: |
| 173 |
name: barbaz |
| 174 |
partition: Common |
| 175 |
state: absent |
| 176 |
provider: |
| 177 |
password: secret |
| 178 |
server: lb.mydomain.com |
| 179 |
user: admin |
| 180 |
delegate_to: localhost |
| 181 |
''' |
| 182 |
|
| 183 |
RETURN = r''' |
| 184 |
description: |
| 185 |
description: New description of the AFM log profile. |
| 186 |
returned: changed |
| 187 |
type: str |
| 188 |
sample: This is my description |
| 189 |
dos_protection: |
| 190 |
description: Log publishers used in DoS related settings of the log profile. |
| 191 |
type: complex |
| 192 |
returned: changed |
| 193 |
contains: |
| 194 |
dns_publisher: |
| 195 |
description: The name of the log publisher used for DNS DoS events. |
| 196 |
returned: changed |
| 197 |
type: str |
| 198 |
sample: "/Common/local-db-publisher" |
| 199 |
sip_publisher: |
| 200 |
description: The name of the log publisher used for SIP DoS events. |
| 201 |
returned: changed |
| 202 |
type: str |
| 203 |
sample: "/Common/local-db-publisher" |
| 204 |
network_publisher: |
| 205 |
description: The name of the log publisher used for DoS Network events. |
| 206 |
returned: changed |
| 207 |
type: str |
| 208 |
sample: "/Common/local-db-publisher" |
| 209 |
sample: hash/dictionary of values |
| 210 |
ip_intelligence: |
| 211 |
description: IP Intelligence related settings of the log profile. |
| 212 |
type: complex |
| 213 |
returned: changed |
| 214 |
contains: |
| 215 |
log_publisher: |
| 216 |
description: The name of the log publisher used for IP Intelligence events. |
| 217 |
returned: changed |
| 218 |
type: str |
| 219 |
sample: "/Common/local-db-publisher" |
| 220 |
rate_limit: |
| 221 |
description: The rate limit for all combined IP intelligence log messages per second. |
| 222 |
returned: changed |
| 223 |
type: str |
| 224 |
sample: "indefinite" |
| 225 |
log_rtbh: |
| 226 |
description: Logging of remotely triggered blackholing events. |
| 227 |
returned: changed |
| 228 |
type: bool |
| 229 |
sample: true |
| 230 |
log_shun: |
| 231 |
description: Logging of IP Intelligence shun list events. |
| 232 |
returned: changed |
| 233 |
type: bool |
| 234 |
sample: false |
| 235 |
log_translation_fields: |
| 236 |
description: Logging of translated fields in IP Intelligence log messages. |
| 237 |
returned: changed |
| 238 |
type: bool |
| 239 |
sample: false |
| 240 |
sample: hash/dictionary of values |
| 241 |
port_misuse: |
| 242 |
description: Port Misuse related settings of the log profile. |
| 243 |
type: complex |
| 244 |
returned: changed |
| 245 |
contains: |
| 246 |
log_publisher: |
| 247 |
description: The name of the log publisher used for Port Misuse events. |
| 248 |
returned: changed |
| 249 |
type: str |
| 250 |
sample: "/Common/local-db-publisher" |
| 251 |
rate_limit: |
| 252 |
description: The rate limit for all combined Port Misuse log messages per second. |
| 253 |
returned: changed |
| 254 |
type: str |
| 255 |
sample: "indefinite" |
| 256 |
sample: hash/dictionary of values |
| 257 |
''' |
| 258 |
from datetime import datetime |
| 259 |
from ansible.module_utils.basic import ( |
| 260 |
AnsibleModule, env_fallback |
| 261 |
) |
| 262 |
|
| 263 |
from ..module_utils.bigip import F5RestClient |
| 264 |
from ..module_utils.common import ( |
| 265 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 266 |
) |
| 267 |
from ..module_utils.compare import compare_dictionary |
| 268 |
from ..module_utils.icontrol import tmos_version |
| 269 |
from ..module_utils.teem import send_teem |
| 270 |
|
| 271 |
|
| 272 |
class Parameters(AnsibleF5Parameters): |
| 273 |
api_map = { |
| 274 |
'ipIntelligence': 'ip_intelligence', |
| 275 |
'portMisuse': 'port_misuse', |
| 276 |
'protocolDnsDosPublisher': 'dns_publisher', |
| 277 |
'protocolSipDosPublisher': 'sip_publisher', |
| 278 |
'dosNetworkPublisher': 'network_publisher', |
| 279 |
} |
| 280 |
|
| 281 |
api_attributes = [ |
| 282 |
'description', |
| 283 |
'ipIntelligence', |
| 284 |
'portMisuse', |
| 285 |
'dosNetworkPublisher', |
| 286 |
'protocolDnsDosPublisher', |
| 287 |
'protocolSipDosPublisher', |
| 288 |
] |
| 289 |
|
| 290 |
returnables = [ |
| 291 |
'ip_intelligence', |
| 292 |
'dns_publisher', |
| 293 |
'sip_publisher', |
| 294 |
'network_publisher', |
| 295 |
'port_misuse', |
| 296 |
'description', |
| 297 |
'ip_log_publisher', |
| 298 |
'ip_rate_limit', |
| 299 |
'ip_log_rthb', |
| 300 |
'ip_log_shun', |
| 301 |
'ip_log_translation_fields', |
| 302 |
'port_rate_limit', |
| 303 |
'port_log_publisher', |
| 304 |
] |
| 305 |
|
| 306 |
updatables = [ |
| 307 |
'dns_publisher', |
| 308 |
'sip_publisher', |
| 309 |
'network_publisher', |
| 310 |
'description', |
| 311 |
'ip_log_publisher', |
| 312 |
'ip_rate_limit', |
| 313 |
'ip_log_rthb', |
| 314 |
'ip_log_shun', |
| 315 |
'ip_log_translation_fields', |
| 316 |
'port_rate_limit', |
| 317 |
'port_log_publisher', |
| 318 |
] |
| 319 |
|
| 320 |
|
| 321 |
class ApiParameters(Parameters): |
| 322 |
@property |
| 323 |
def ip_log_publisher(self): |
| 324 |
result = self._values['ip_intelligence'].get('logPublisher', None) |
| 325 |
return result |
| 326 |
|
| 327 |
@property |
| 328 |
def ip_rate_limit(self): |
| 329 |
return self._values['ip_intelligence']['aggregateRate'] |
| 330 |
|
| 331 |
@property |
| 332 |
def port_rate_limit(self): |
| 333 |
return self._values['port_misuse']['aggregateRate'] |
| 334 |
|
| 335 |
@property |
| 336 |
def port_log_publisher(self): |
| 337 |
result = self._values['port_misuse'].get('logPublisher', None) |
| 338 |
return result |
| 339 |
|
| 340 |
@property |
| 341 |
def ip_log_rtbh(self): |
| 342 |
return self._values['ip_intelligence']['logRtbh'] |
| 343 |
|
| 344 |
@property |
| 345 |
def ip_log_shun(self): |
| 346 |
if self._values['name'] != 'global-network': |
| 347 |
return None |
| 348 |
return self._values['ip_intelligence']['logShun'] |
| 349 |
|
| 350 |
@property |
| 351 |
def ip_log_translation_fields(self): |
| 352 |
return self._values['ip_intelligence']['logTranslationFields'] |
| 353 |
|
| 354 |
|
| 355 |
class ModuleParameters(Parameters): |
| 356 |
def _transform_log_publisher(self, log_publisher): |
| 357 |
if log_publisher is None: |
| 358 |
return None |
| 359 |
if log_publisher in ['', 'none']: |
| 360 |
return {} |
| 361 |
return fq_name(self.partition, log_publisher) |
| 362 |
|
| 363 |
def _validate_rate_limit(self, rate_limit): |
| 364 |
if rate_limit is None: |
| 365 |
return None |
| 366 |
if rate_limit == 'indefinite': |
| 367 |
return 4294967295 |
| 368 |
if 0 <= int(rate_limit) <= 4294967295: |
| 369 |
return int(rate_limit) |
| 370 |
raise F5ModuleError( |
| 371 |
"Valid 'maximum_age' must be in range 0 - 4294967295, or 'indefinite'." |
| 372 |
) |
| 373 |
|
| 374 |
@property |
| 375 |
def ip_log_rtbh(self): |
| 376 |
if self._values['ip_intelligence'] is None: |
| 377 |
return None |
| 378 |
result = flatten_boolean(self._values['ip_intelligence']['log_rtbh']) |
| 379 |
if result == 'yes': |
| 380 |
return 'enabled' |
| 381 |
if result == 'no': |
| 382 |
return 'disabled' |
| 383 |
return result |
| 384 |
|
| 385 |
@property |
| 386 |
def ip_log_shun(self): |
| 387 |
if self._values['ip_intelligence'] is None: |
| 388 |
return None |
| 389 |
if 'global-network' not in self._values['name']: |
| 390 |
return None |
| 391 |
result = flatten_boolean(self._values['ip_intelligence']['log_shun']) |
| 392 |
if result == 'yes': |
| 393 |
return 'enabled' |
| 394 |
if result == 'no': |
| 395 |
return 'disabled' |
| 396 |
return result |
| 397 |
|
| 398 |
@property |
| 399 |
def ip_log_translation_fields(self): |
| 400 |
if self._values['ip_intelligence'] is None: |
| 401 |
return None |
| 402 |
result = flatten_boolean(self._values['ip_intelligence']['log_translation_fields']) |
| 403 |
if result == 'yes': |
| 404 |
return 'enabled' |
| 405 |
if result == 'no': |
| 406 |
return 'disabled' |
| 407 |
return result |
| 408 |
|
| 409 |
@property |
| 410 |
def ip_log_publisher(self): |
| 411 |
if self._values['ip_intelligence'] is None: |
| 412 |
return None |
| 413 |
result = self._transform_log_publisher(self._values['ip_intelligence']['log_publisher']) |
| 414 |
return result |
| 415 |
|
| 416 |
@property |
| 417 |
def ip_rate_limit(self): |
| 418 |
if self._values['ip_intelligence'] is None: |
| 419 |
return None |
| 420 |
return self._validate_rate_limit(self._values['ip_intelligence']['rate_limit']) |
| 421 |
|
| 422 |
@property |
| 423 |
def port_rate_limit(self): |
| 424 |
if self._values['port_misuse'] is None: |
| 425 |
return None |
| 426 |
return self._validate_rate_limit(self._values['port_misuse']['rate_limit']) |
| 427 |
|
| 428 |
@property |
| 429 |
def port_log_publisher(self): |
| 430 |
if self._values['port_misuse'] is None: |
| 431 |
return None |
| 432 |
result = self._transform_log_publisher(self._values['port_misuse']['log_publisher']) |
| 433 |
return result |
| 434 |
|
| 435 |
@property |
| 436 |
def dns_publisher(self): |
| 437 |
if self._values['dos_protection'] is None: |
| 438 |
return None |
| 439 |
result = self._transform_log_publisher(self._values['dos_protection']['dns_publisher']) |
| 440 |
return result |
| 441 |
|
| 442 |
@property |
| 443 |
def sip_publisher(self): |
| 444 |
if self._values['dos_protection'] is None: |
| 445 |
return None |
| 446 |
result = self._transform_log_publisher(self._values['dos_protection']['sip_publisher']) |
| 447 |
return result |
| 448 |
|
| 449 |
@property |
| 450 |
def network_publisher(self): |
| 451 |
if self._values['dos_protection'] is None: |
| 452 |
return None |
| 453 |
result = self._transform_log_publisher(self._values['dos_protection']['network_publisher']) |
| 454 |
return result |
| 455 |
|
| 456 |
|
| 457 |
class Changes(Parameters): |
| 458 |
def to_return(self): |
| 459 |
result = {} |
| 460 |
try: |
| 461 |
for returnable in self.returnables: |
| 462 |
result[returnable] = getattr(self, returnable) |
| 463 |
result = self._filter_params(result) |
| 464 |
except Exception: |
| 465 |
raise |
| 466 |
return result |
| 467 |
|
| 468 |
|
| 469 |
class UsableChanges(Changes): |
| 470 |
@property |
| 471 |
def ip_intelligence(self): |
| 472 |
to_filter = dict( |
| 473 |
logPublisher=self._values['ip_log_publisher'], |
| 474 |
aggregateRate=self._values['ip_rate_limit'], |
| 475 |
logRtbh=self._values['ip_log_rtbh'], |
| 476 |
logShun=self._values['ip_log_shun'], |
| 477 |
logTranslationFields=self._values['ip_log_translation_fields'] |
| 478 |
) |
| 479 |
result = self._filter_params(to_filter) |
| 480 |
if result: |
| 481 |
return result |
| 482 |
|
| 483 |
@property |
| 484 |
def port_misuse(self): |
| 485 |
to_filter = dict( |
| 486 |
logPublisher=self._values['port_log_publisher'], |
| 487 |
aggregateRate=self._values['port_rate_limit'] |
| 488 |
) |
| 489 |
result = self._filter_params(to_filter) |
| 490 |
if result: |
| 491 |
return result |
| 492 |
|
| 493 |
|
| 494 |
class ReportableChanges(Changes): |
| 495 |
returnables = [ |
| 496 |
'ip_intelligence', |
| 497 |
'port_misuse', |
| 498 |
'description', |
| 499 |
'dos_protection', |
| 500 |
] |
| 501 |
|
| 502 |
def _change_rate_limit_value(self, value): |
| 503 |
if value == 4294967295: |
| 504 |
return 'indefinite' |
| 505 |
else: |
| 506 |
return value |
| 507 |
|
| 508 |
@property |
| 509 |
def ip_log_rthb(self): |
| 510 |
result = flatten_boolean(self._values['ip_log_rtbh']) |
| 511 |
return result |
| 512 |
|
| 513 |
@property |
| 514 |
def ip_log_shun(self): |
| 515 |
result = flatten_boolean(self._values['ip_log_shun']) |
| 516 |
return result |
| 517 |
|
| 518 |
@property |
| 519 |
def ip_log_translation_fields(self): |
| 520 |
result = flatten_boolean(self._values['ip_log_translation_fields']) |
| 521 |
return result |
| 522 |
|
| 523 |
@property |
| 524 |
def ip_intelligence(self): |
| 525 |
if self._values['ip_intelligence'] is None: |
| 526 |
return None |
| 527 |
to_filter = dict( |
| 528 |
log_publisher=self._values['ip_log_publisher'], |
| 529 |
rate_limit=self._change_rate_limit_value(self._values['ip_rate_limit']), |
| 530 |
log_rtbh=self.ip_log_rtbh, |
| 531 |
log_shun=self.ip_log_shun, |
| 532 |
log_translation_fields=self.ip_log_translation_fields |
| 533 |
) |
| 534 |
result = self._filter_params(to_filter) |
| 535 |
if result: |
| 536 |
return result |
| 537 |
|
| 538 |
@property |
| 539 |
def port_misuse(self): |
| 540 |
if self._values['port_misuse'] is None: |
| 541 |
return None |
| 542 |
to_filter = dict( |
| 543 |
log_publisher=self._values['port_log_publisher'], |
| 544 |
rate_limit=self._change_rate_limit_value(self._values['port_rate_limit']), |
| 545 |
) |
| 546 |
result = self._filter_params(to_filter) |
| 547 |
if result: |
| 548 |
return result |
| 549 |
|
| 550 |
@property |
| 551 |
def dos_protection(self): |
| 552 |
to_filter = dict( |
| 553 |
dns_publisher=self._values['dns_publisher'], |
| 554 |
sip_publisher=self._values['sip_publisher'], |
| 555 |
network_publisher=self._values['network_publisher'], |
| 556 |
) |
| 557 |
result = self._filter_params(to_filter) |
| 558 |
return result |
| 559 |
|
| 560 |
|
| 561 |
class Difference(object): |
| 562 |
def __init__(self, want, have=None): |
| 563 |
self.want = want |
| 564 |
self.have = have |
| 565 |
|
| 566 |
def compare(self, param): |
| 567 |
try: |
| 568 |
result = getattr(self, param) |
| 569 |
return result |
| 570 |
except AttributeError: |
| 571 |
return self.__default(param) |
| 572 |
|
| 573 |
def __default(self, param): |
| 574 |
attr1 = getattr(self.want, param) |
| 575 |
try: |
| 576 |
attr2 = getattr(self.have, param) |
| 577 |
if attr1 != attr2: |
| 578 |
return attr1 |
| 579 |
except AttributeError: |
| 580 |
return attr1 |
| 581 |
|
| 582 |
@property |
| 583 |
def ip_log_publisher(self): |
| 584 |
result = compare_dictionary(self.want.ip_log_publisher, self.have.ip_log_publisher) |
| 585 |
return result |
| 586 |
|
| 587 |
@property |
| 588 |
def port_log_publisher(self): |
| 589 |
result = compare_dictionary(self.want.port_log_publisher, self.have.port_log_publisher) |
| 590 |
return result |
| 591 |
|
| 592 |
@property |
| 593 |
def dns_publisher(self): |
| 594 |
result = compare_dictionary(self.want.dns_publisher, self.have.dns_publisher) |
| 595 |
return result |
| 596 |
|
| 597 |
@property |
| 598 |
def sip_publisher(self): |
| 599 |
result = compare_dictionary(self.want.sip_publisher, self.have.sip_publisher) |
| 600 |
return result |
| 601 |
|
| 602 |
@property |
| 603 |
def network_publisher(self): |
| 604 |
result = compare_dictionary(self.want.network_publisher, self.have.network_publisher) |
| 605 |
return result |
| 606 |
|
| 607 |
|
| 608 |
class ModuleManager(object): |
| 609 |
def __init__(self, *args, **kwargs): |
| 610 |
self.module = kwargs.get('module', None) |
| 611 |
self.client = F5RestClient(**self.module.params) |
| 612 |
self.want = ModuleParameters(params=self.module.params) |
| 613 |
self.have = ApiParameters() |
| 614 |
self.changes = UsableChanges() |
| 615 |
|
| 616 |
def _set_changed_options(self): |
| 617 |
changed = {} |
| 618 |
for key in Parameters.returnables: |
| 619 |
if getattr(self.want, key) is not None: |
| 620 |
changed[key] = getattr(self.want, key) |
| 621 |
if changed: |
| 622 |
self.changes = UsableChanges(params=changed) |
| 623 |
|
| 624 |
def _update_changed_options(self): |
| 625 |
diff = Difference(self.want, self.have) |
| 626 |
updatables = Parameters.updatables |
| 627 |
changed = dict() |
| 628 |
for k in updatables: |
| 629 |
change = diff.compare(k) |
| 630 |
if change is None: |
| 631 |
continue |
| 632 |
else: |
| 633 |
if isinstance(change, dict): |
| 634 |
changed.update(change) |
| 635 |
else: |
| 636 |
changed[k] = change |
| 637 |
if changed: |
| 638 |
self.changes = UsableChanges(params=changed) |
| 639 |
return True |
| 640 |
return False |
| 641 |
|
| 642 |
def _announce_deprecations(self, result): |
| 643 |
warnings = result.pop('__warnings', []) |
| 644 |
for warning in warnings: |
| 645 |
self.client.module.deprecate( |
| 646 |
msg=warning['msg'], |
| 647 |
version=warning['version'] |
| 648 |
) |
| 649 |
|
| 650 |
def exec_module(self): |
| 651 |
start = datetime.now().isoformat() |
| 652 |
version = tmos_version(self.client) |
| 653 |
changed = False |
| 654 |
result = dict() |
| 655 |
state = self.want.state |
| 656 |
|
| 657 |
if state == "present": |
| 658 |
changed = self.present() |
| 659 |
elif state == "absent": |
| 660 |
changed = self.absent() |
| 661 |
|
| 662 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 663 |
changes = reportable.to_return() |
| 664 |
result.update(**changes) |
| 665 |
result.update(dict(changed=changed)) |
| 666 |
self._announce_deprecations(result) |
| 667 |
send_teem(start, self.client, self.module, version) |
| 668 |
return result |
| 669 |
|
| 670 |
def present(self): |
| 671 |
if self.exists(): |
| 672 |
return self.update() |
| 673 |
else: |
| 674 |
return self.create() |
| 675 |
|
| 676 |
def absent(self): |
| 677 |
|
| 678 |
built_ins = [ |
| 679 |
'Log all requests', 'Log illegal requests', |
| 680 |
'global-network', 'local-dos' |
| 681 |
] |
| 682 |
if self.want.name in built_ins: |
| 683 |
return False |
| 684 |
if self.exists(): |
| 685 |
return self.remove() |
| 686 |
return False |
| 687 |
|
| 688 |
def should_update(self): |
| 689 |
result = self._update_changed_options() |
| 690 |
if result: |
| 691 |
return True |
| 692 |
return False |
| 693 |
|
| 694 |
def update(self): |
| 695 |
self.have = self.read_current_from_device() |
| 696 |
if not self.should_update(): |
| 697 |
return False |
| 698 |
if self.module.check_mode: |
| 699 |
return True |
| 700 |
self.update_on_device() |
| 701 |
return True |
| 702 |
|
| 703 |
def remove(self): |
| 704 |
if self.module.check_mode: |
| 705 |
return True |
| 706 |
self.remove_from_device() |
| 707 |
if self.exists(): |
| 708 |
raise F5ModuleError("Failed to delete the resource.") |
| 709 |
return True |
| 710 |
|
| 711 |
def create(self): |
| 712 |
self._set_changed_options() |
| 713 |
if self.module.check_mode: |
| 714 |
return True |
| 715 |
self.create_on_device() |
| 716 |
return True |
| 717 |
|
| 718 |
def exists(self): |
| 719 |
uri = "https://{0}:{1}/mgmt/tm/security/log/profile/{2}".format( |
| 720 |
self.client.provider['server'], |
| 721 |
self.client.provider['server_port'], |
| 722 |
transform_name(self.want.partition, self.want.name) |
| 723 |
) |
| 724 |
resp = self.client.api.get(uri) |
| 725 |
try: |
| 726 |
response = resp.json() |
| 727 |
except ValueError as ex: |
| 728 |
raise F5ModuleError(str(ex)) |
| 729 |
|
| 730 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 731 |
return False |
| 732 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 733 |
return True |
| 734 |
|
| 735 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 736 |
|
| 737 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 738 |
if 'message' in response: |
| 739 |
raise F5ModuleError(response['message']) |
| 740 |
else: |
| 741 |
raise F5ModuleError(resp.content) |
| 742 |
|
| 743 |
def create_on_device(self): |
| 744 |
params = self.changes.api_params() |
| 745 |
params['name'] = self.want.name |
| 746 |
params['partition'] = self.want.partition |
| 747 |
uri = "https://{0}:{1}/mgmt/tm/security/log/profile/".format( |
| 748 |
self.client.provider['server'], |
| 749 |
self.client.provider['server_port'], |
| 750 |
) |
| 751 |
resp = self.client.api.post(uri, json=params) |
| 752 |
try: |
| 753 |
response = resp.json() |
| 754 |
except ValueError as ex: |
| 755 |
raise F5ModuleError(str(ex)) |
| 756 |
|
| 757 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 758 |
return True |
| 759 |
raise F5ModuleError(resp.content) |
| 760 |
|
| 761 |
def update_on_device(self): |
| 762 |
params = self.changes.api_params() |
| 763 |
uri = "https://{0}:{1}/mgmt/tm/security/log/profile/{2}".format( |
| 764 |
self.client.provider['server'], |
| 765 |
self.client.provider['server_port'], |
| 766 |
transform_name(self.want.partition, self.want.name) |
| 767 |
) |
| 768 |
resp = self.client.api.patch(uri, json=params) |
| 769 |
try: |
| 770 |
response = resp.json() |
| 771 |
except ValueError as ex: |
| 772 |
raise F5ModuleError(str(ex)) |
| 773 |
|
| 774 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 775 |
return True |
| 776 |
raise F5ModuleError(resp.content) |
| 777 |
|
| 778 |
def remove_from_device(self): |
| 779 |
uri = "https://{0}:{1}/mgmt/tm/security/log/profile/{2}".format( |
| 780 |
self.client.provider['server'], |
| 781 |
self.client.provider['server_port'], |
| 782 |
transform_name(self.want.partition, self.want.name) |
| 783 |
) |
| 784 |
response = self.client.api.delete(uri) |
| 785 |
if response.status == 200: |
| 786 |
return True |
| 787 |
raise F5ModuleError(response.content) |
| 788 |
|
| 789 |
def read_current_from_device(self): |
| 790 |
uri = "https://{0}:{1}/mgmt/tm/security/log/profile/{2}".format( |
| 791 |
self.client.provider['server'], |
| 792 |
self.client.provider['server_port'], |
| 793 |
transform_name(self.want.partition, self.want.name) |
| 794 |
) |
| 795 |
resp = self.client.api.get(uri) |
| 796 |
try: |
| 797 |
response = resp.json() |
| 798 |
except ValueError as ex: |
| 799 |
raise F5ModuleError(str(ex)) |
| 800 |
|
| 801 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 802 |
return ApiParameters(params=response) |
| 803 |
raise F5ModuleError(resp.content) |
| 804 |
|
| 805 |
|
| 806 |
class ArgumentSpec(object): |
| 807 |
def __init__(self): |
| 808 |
self.supports_check_mode = True |
| 809 |
argument_spec = dict( |
| 810 |
name=dict( |
| 811 |
required=True |
| 812 |
), |
| 813 |
description=dict(), |
| 814 |
dos_protection=dict( |
| 815 |
type='dict', |
| 816 |
options=dict( |
| 817 |
dns_publisher=dict(), |
| 818 |
sip_publisher=dict(), |
| 819 |
network_publisher=dict() |
| 820 |
) |
| 821 |
), |
| 822 |
ip_intelligence=dict( |
| 823 |
type='dict', |
| 824 |
options=dict( |
| 825 |
log_publisher=dict(), |
| 826 |
log_translation_fields=dict(type='bool'), |
| 827 |
rate_limit=dict(), |
| 828 |
log_rtbh=dict(type='bool'), |
| 829 |
log_shun=dict(type='bool') |
| 830 |
) |
| 831 |
), |
| 832 |
port_misuse=dict( |
| 833 |
type='dict', |
| 834 |
options=dict( |
| 835 |
log_publisher=dict(), |
| 836 |
rate_limit=dict() |
| 837 |
) |
| 838 |
), |
| 839 |
partition=dict( |
| 840 |
default='Common', |
| 841 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 842 |
), |
| 843 |
state=dict( |
| 844 |
default='present', |
| 845 |
choices=['present', 'absent'] |
| 846 |
) |
| 847 |
) |
| 848 |
self.argument_spec = {} |
| 849 |
self.argument_spec.update(f5_argument_spec) |
| 850 |
self.argument_spec.update(argument_spec) |
| 851 |
|
| 852 |
|
| 853 |
def main(): |
| 854 |
spec = ArgumentSpec() |
| 855 |
|
| 856 |
module = AnsibleModule( |
| 857 |
argument_spec=spec.argument_spec, |
| 858 |
supports_check_mode=spec.supports_check_mode, |
| 859 |
) |
| 860 |
|
| 861 |
try: |
| 862 |
mm = ModuleManager(module=module) |
| 863 |
results = mm.exec_module() |
| 864 |
module.exit_json(**results) |
| 865 |
except F5ModuleError as ex: |
| 866 |
module.fail_json(msg=str(ex)) |
| 867 |
|
| 868 |
|
| 869 |
if __name__ == '__main__': |
| 870 |
main() |