| Conditions | 29 |
| Total Lines | 254 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 843.7856 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ssg.build_remediations.expand_xccdf_subs() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | 2 | from __future__ import absolute_import |
|
| 296 | 2 | def expand_xccdf_subs(fix, remediation_type, remediation_functions): |
|
| 297 | """For those remediation scripts utilizing some of the internal SCAP |
||
| 298 | Security Guide remediation functions expand the selected shell variables |
||
| 299 | and remediation functions calls with <xccdf:sub> element |
||
| 300 | |||
| 301 | This routine translates any instance of the 'populate' function call in |
||
| 302 | the form of: |
||
| 303 | |||
| 304 | populate variable_name |
||
| 305 | |||
| 306 | into |
||
| 307 | |||
| 308 | variable_name="<sub idref="variable_name"/>" |
||
| 309 | |||
| 310 | Also transforms any instance of the 'ansible-populate' function call in the |
||
| 311 | form of: |
||
| 312 | (ansible-populate variable_name) |
||
| 313 | into |
||
| 314 | |||
| 315 | <sub idref="variable_name"/> |
||
| 316 | |||
| 317 | Also transforms any instance of some other known remediation function (e.g. |
||
| 318 | 'replace_or_append' etc.) from the form of: |
||
| 319 | |||
| 320 | function_name "arg1" "arg2" ... "argN" |
||
| 321 | |||
| 322 | into: |
||
| 323 | |||
| 324 | <sub idref="function_function_name"/> |
||
| 325 | function_name "arg1" "arg2" ... "argN" |
||
| 326 | """ |
||
| 327 | |||
| 328 | if remediation_type == "ansible": |
||
| 329 | fix_text = fix.text |
||
| 330 | |||
| 331 | if "(ansible-populate " in fix_text: |
||
| 332 | raise RuntimeError( |
||
| 333 | "(ansible-populate VAR) has been deprecated. Please use " |
||
| 334 | "(xccdf-var VAR) instead. Keep in mind that the latter will " |
||
| 335 | "make an ansible variable out of XCCDF Value as opposed to " |
||
| 336 | "substituting directly." |
||
| 337 | ) |
||
| 338 | |||
| 339 | # If you change this string make sure it still matches the pattern |
||
| 340 | # defined in OpenSCAP. Otherwise you break variable handling in |
||
| 341 | # 'oscap xccdf generate fix' and the variables won't be customizable! |
||
| 342 | # https://github.com/OpenSCAP/openscap/blob/1.2.17/src/XCCDF_POLICY/xccdf_policy_remediate.c#L588 |
||
| 343 | # const char *pattern = |
||
| 344 | # "- name: XCCDF Value [^ ]+ # promote to variable\n set_fact:\n" |
||
| 345 | # " ([^:]+): (.+)\n tags:\n - always\n"; |
||
| 346 | # We use !!str typecast to prevent treating values as different types |
||
| 347 | # eg. yes as a bool or 077 as an octal number |
||
| 348 | fix_text = re.sub( |
||
| 349 | r"- \(xccdf-var\s+(\S+)\)", |
||
| 350 | r"- name: XCCDF Value \1 # promote to variable\n" |
||
| 351 | r" set_fact:\n" |
||
| 352 | r" \1: !!str (ansible-populate \1)\n" |
||
| 353 | r" tags:\n" |
||
| 354 | r" - always", |
||
| 355 | fix_text |
||
| 356 | ) |
||
| 357 | |||
| 358 | pattern = r'\(ansible-populate\s*(\S+)\)' |
||
| 359 | |||
| 360 | # we will get list what looks like |
||
| 361 | # [text, varname, text, varname, ..., text] |
||
| 362 | parts = re.split(pattern, fix_text) |
||
| 363 | |||
| 364 | fix.text = parts[0] # add first "text" |
||
| 365 | for index in range(1, len(parts), 2): |
||
| 366 | varname = parts[index] |
||
| 367 | text_between_vars = parts[index + 1] |
||
| 368 | |||
| 369 | # we cannot combine elements and text easily |
||
| 370 | # so text is in ".tail" of element |
||
| 371 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 372 | xccdfvarsub.tail = text_between_vars |
||
| 373 | return |
||
| 374 | |||
| 375 | elif remediation_type == "puppet": |
||
| 376 | pattern = r'\(puppet-populate\s*(\S+)\)' |
||
| 377 | |||
| 378 | # we will get list what looks like |
||
| 379 | # [text, varname, text, varname, ..., text] |
||
| 380 | parts = re.split(pattern, fix.text) |
||
| 381 | |||
| 382 | fix.text = parts[0] # add first "text" |
||
| 383 | for index in range(1, len(parts), 2): |
||
| 384 | varname = parts[index] |
||
| 385 | text_between_vars = parts[index + 1] |
||
| 386 | |||
| 387 | # we cannot combine elements and text easily |
||
| 388 | # so text is in ".tail" of element |
||
| 389 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 390 | xccdfvarsub.tail = text_between_vars |
||
| 391 | return |
||
| 392 | |||
| 393 | elif remediation_type == "anaconda": |
||
| 394 | pattern = r'\(anaconda-populate\s*(\S+)\)' |
||
| 395 | |||
| 396 | # we will get list what looks like |
||
| 397 | # [text, varname, text, varname, ..., text] |
||
| 398 | parts = re.split(pattern, fix.text) |
||
| 399 | |||
| 400 | fix.text = parts[0] # add first "text" |
||
| 401 | for index in range(1, len(parts), 2): |
||
| 402 | varname = parts[index] |
||
| 403 | text_between_vars = parts[index + 1] |
||
| 404 | |||
| 405 | # we cannot combine elements and text easily |
||
| 406 | # so text is in ".tail" of element |
||
| 407 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 408 | xccdfvarsub.tail = text_between_vars |
||
| 409 | return |
||
| 410 | |||
| 411 | elif remediation_type == "bash": |
||
| 412 | # This remediation script doesn't utilize internal remediation functions |
||
| 413 | # Skip it without any further processing |
||
| 414 | if 'remediation_functions' not in fix.text: |
||
| 415 | return |
||
| 416 | |||
| 417 | # This remediation script utilizes some of internal remediation functions |
||
| 418 | # Expand shell variables and remediation functions calls with <xccdf:sub> |
||
| 419 | # elements |
||
| 420 | pattern = r'\n+(\s*(?:' + r'|'.join(remediation_functions) + r')[^\n]*)\n' |
||
| 421 | patcomp = re.compile(pattern, re.DOTALL) |
||
| 422 | fixparts = re.split(patcomp, fix.text) |
||
| 423 | if fixparts[0] is not None: |
||
| 424 | # Split the portion of fix.text from fix start to first call of |
||
| 425 | # remediation function, keeping only the third part: |
||
| 426 | # * tail to hold part of the fix.text after inclusion, |
||
| 427 | # but before first call of remediation function |
||
| 428 | try: |
||
| 429 | rfpattern = '(.*remediation_functions)(.*)' |
||
| 430 | rfpatcomp = re.compile(rfpattern, re.DOTALL) |
||
| 431 | _, _, tail, _ = re.split(rfpatcomp, fixparts[0], maxsplit=2) |
||
| 432 | except ValueError: |
||
| 433 | sys.stderr.write("Processing fix.text for: %s rule\n" |
||
| 434 | % fix.get('rule')) |
||
| 435 | sys.stderr.write("Unable to extract part of the fix.text " |
||
| 436 | "after inclusion of remediation functions." |
||
| 437 | " Aborting..\n") |
||
| 438 | sys.exit(1) |
||
| 439 | # If the 'tail' is not empty, make it new fix.text. |
||
| 440 | # Otherwise use '' |
||
| 441 | fix.text = tail if tail is not None else '' |
||
|
|
|||
| 442 | # Drop the first element of 'fixparts' since it has been processed |
||
| 443 | fixparts.pop(0) |
||
| 444 | # Perform sanity check on new 'fixparts' list content (to continue |
||
| 445 | # successfully 'fixparts' has to contain even count of elements) |
||
| 446 | if len(fixparts) % 2 != 0: |
||
| 447 | sys.stderr.write("Error performing XCCDF expansion on " |
||
| 448 | "remediation script: %s\n" |
||
| 449 | % fix.get("rule")) |
||
| 450 | sys.stderr.write("Invalid count of elements. Exiting!\n") |
||
| 451 | sys.exit(1) |
||
| 452 | # Process remaining 'fixparts' elements in pairs |
||
| 453 | # First pair element is remediation function to be XCCDF expanded |
||
| 454 | # Second pair element (if not empty) is the portion of the original |
||
| 455 | # fix text to be used in newly added sublement's tail |
||
| 456 | for idx in range(0, len(fixparts), 2): |
||
| 457 | # We previously removed enclosing newlines when creating |
||
| 458 | # fixparts list. Add them back and reuse the above 'pattern' |
||
| 459 | fixparts[idx] = "\n%s\n" % fixparts[idx] |
||
| 460 | # Sanity check (verify the first field truly contains call of |
||
| 461 | # some of the remediation functions) |
||
| 462 | if re.match(pattern, fixparts[idx], re.DOTALL) is not None: |
||
| 463 | # This chunk contains call of 'populate' function |
||
| 464 | if "populate" in fixparts[idx]: |
||
| 465 | varname, fixtextcontrib = get_populate_replacement(remediation_type, |
||
| 466 | fixparts[idx]) |
||
| 467 | # Define new XCCDF <sub> element for the variable |
||
| 468 | xccdfvarsub = ElementTree.Element("sub", idref=varname) |
||
| 469 | |||
| 470 | # If this is first sub element, |
||
| 471 | # the textcontribution needs to go to fix text |
||
| 472 | # otherwise, append to last subelement |
||
| 473 | nfixchildren = len(list(fix)) |
||
| 474 | if nfixchildren == 0: |
||
| 475 | fix.text += fixtextcontrib |
||
| 476 | else: |
||
| 477 | previouselem = fix[nfixchildren-1] |
||
| 478 | previouselem.tail += fixtextcontrib |
||
| 479 | |||
| 480 | # If second pair element is not empty, append it as |
||
| 481 | # tail for the subelement (prefixed with closing '"') |
||
| 482 | if fixparts[idx + 1] is not None: |
||
| 483 | xccdfvarsub.tail = '"' + '\n' + fixparts[idx + 1] |
||
| 484 | # Otherwise append just enclosing '"' |
||
| 485 | else: |
||
| 486 | xccdfvarsub.tail = '"' + '\n' |
||
| 487 | # Append the new subelement to the fix element |
||
| 488 | fix.append(xccdfvarsub) |
||
| 489 | # This chunk contains call of other remediation function |
||
| 490 | else: |
||
| 491 | # Extract remediation function name |
||
| 492 | funcname = re.search(r'\n\s*(\S+)(| .*)\n', |
||
| 493 | fixparts[idx], |
||
| 494 | re.DOTALL).group(1) |
||
| 495 | # Define new XCCDF <sub> element for the function |
||
| 496 | xccdffuncsub = ElementTree.Element( |
||
| 497 | "sub", idref='function_%s' % funcname) |
||
| 498 | # Append original function call into tail of the |
||
| 499 | # subelement |
||
| 500 | xccdffuncsub.tail = fixparts[idx] |
||
| 501 | # If the second element of the pair is not empty, |
||
| 502 | # append it to the tail of the subelement too |
||
| 503 | if fixparts[idx + 1] is not None: |
||
| 504 | xccdffuncsub.tail += fixparts[idx + 1] |
||
| 505 | # Append the new subelement to the fix element |
||
| 506 | fix.append(xccdffuncsub) |
||
| 507 | # Ensure the newly added <xccdf:sub> element for the |
||
| 508 | # function will be always inserted at newline |
||
| 509 | # If xccdffuncsub is the first <xccdf:sub> element |
||
| 510 | # being added as child of <fix> and fix.text doesn't |
||
| 511 | # end up with newline character, append the newline |
||
| 512 | # to the fix.text |
||
| 513 | if list(fix).index(xccdffuncsub) == 0: |
||
| 514 | if re.search(r'.*\n$', fix.text) is None: |
||
| 515 | fix.text += '\n' |
||
| 516 | # If xccdffuncsub isn't the first child (first |
||
| 517 | # <xccdf:sub> being added), and tail of previous |
||
| 518 | # child doesn't end up with newline, append the newline |
||
| 519 | # to the tail of previous child |
||
| 520 | else: |
||
| 521 | previouselem = fix[list(fix).index(xccdffuncsub) - 1] |
||
| 522 | if re.search(r'.*\n$', previouselem.tail) is None: |
||
| 523 | previouselem.tail += '\n' |
||
| 524 | |||
| 525 | # Perform a sanity check if all known remediation function calls have been |
||
| 526 | # properly XCCDF substituted. Exit with failure if some wasn't |
||
| 527 | |||
| 528 | # First concat output form of modified fix text (including text appended |
||
| 529 | # to all children of the fix) |
||
| 530 | modfix = [fix.text] |
||
| 531 | for child in fix.getchildren(): |
||
| 532 | if child is not None and child.text is not None: |
||
| 533 | modfix.append(child.text) |
||
| 534 | modfixtext = "".join(modfix) |
||
| 535 | for func in remediation_functions: |
||
| 536 | # Then efine expected XCCDF sub element form for this function |
||
| 537 | funcxccdfsub = "<sub idref=\"function_%s\"" % func |
||
| 538 | # Finally perform the sanity check -- if function was properly XCCDF |
||
| 539 | # substituted both the original function call and XCCDF <sub> element |
||
| 540 | # for that function need to be present in the modified text of the fix |
||
| 541 | # Otherwise something went wrong, thus exit with failure |
||
| 542 | if func in modfixtext and funcxccdfsub not in modfixtext: |
||
| 543 | sys.stderr.write("Error performing XCCDF <sub> substitution " |
||
| 544 | "for function %s in %s fix. Exiting...\n" |
||
| 545 | % (func, fix.get("rule"))) |
||
| 546 | sys.exit(1) |
||
| 547 | else: |
||
| 548 | sys.stderr.write("Unknown remediation type '%s'\n" % (remediation_type)) |
||
| 549 | sys.exit(1) |
||
| 550 |