| 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 |
|
| 367 | 2 | def expand_xccdf_subs(fix, remediation_type, remediation_functions): |
|
| 368 | """For those remediation scripts utilizing some of the internal SCAP |
||
| 369 | Security Guide remediation functions expand the selected shell variables |
||
| 370 | and remediation functions calls with <xccdf:sub> element |
||
| 371 | |||
| 372 | This routine translates any instance of the 'populate' function call in |
||
| 373 | the form of: |
||
| 374 | |||
| 375 | populate variable_name |
||
| 376 | |||
| 377 | into |
||
| 378 | |||
| 379 | variable_name="<sub idref="variable_name"/>" |
||
| 380 | |||
| 381 | Also transforms any instance of the 'ansible-populate' function call in the |
||
| 382 | form of: |
||
| 383 | (ansible-populate variable_name) |
||
| 384 | into |
||
| 385 | |||
| 386 | <sub idref="variable_name"/> |
||
| 387 | |||
| 388 | Also transforms any instance of some other known remediation function (e.g. |
||
| 389 | 'replace_or_append' etc.) from the form of: |
||
| 390 | |||
| 391 | function_name "arg1" "arg2" ... "argN" |
||
| 392 | |||
| 393 | into: |
||
| 394 | |||
| 395 | <sub idref="function_function_name"/> |
||
| 396 | function_name "arg1" "arg2" ... "argN" |
||
| 397 | """ |
||
| 398 | |||
| 399 | if remediation_type == "ansible": |
||
| 400 | fix_text = fix.text |
||
| 401 | |||
| 402 | if "(ansible-populate " in fix_text: |
||
| 403 | raise RuntimeError( |
||
| 404 | "(ansible-populate VAR) has been deprecated. Please use " |
||
| 405 | "(xccdf-var VAR) instead. Keep in mind that the latter will " |
||
| 406 | "make an ansible variable out of XCCDF Value as opposed to " |
||
| 407 | "substituting directly." |
||
| 408 | ) |
||
| 409 | |||
| 410 | # If you change this string make sure it still matches the pattern |
||
| 411 | # defined in OpenSCAP. Otherwise you break variable handling in |
||
| 412 | # 'oscap xccdf generate fix' and the variables won't be customizable! |
||
| 413 | # https://github.com/OpenSCAP/openscap/blob/1.2.17/src/XCCDF_POLICY/xccdf_policy_remediate.c#L588 |
||
| 414 | # const char *pattern = |
||
| 415 | # "- name: XCCDF Value [^ ]+ # promote to variable\n set_fact:\n" |
||
| 416 | # " ([^:]+): (.+)\n tags:\n - always\n"; |
||
| 417 | # We use !!str typecast to prevent treating values as different types |
||
| 418 | # eg. yes as a bool or 077 as an octal number |
||
| 419 | fix_text = re.sub( |
||
| 420 | r"- \(xccdf-var\s+(\S+)\)", |
||
| 421 | r"- name: XCCDF Value \1 # promote to variable\n" |
||
| 422 | r" set_fact:\n" |
||
| 423 | r" \1: !!str (ansible-populate \1)\n" |
||
| 424 | r" tags:\n" |
||
| 425 | r" - always", |
||
| 426 | fix_text |
||
| 427 | ) |
||
| 428 | |||
| 429 | pattern = r'\(ansible-populate\s*(\S+)\)' |
||
| 430 | |||
| 431 | # we will get list what looks like |
||
| 432 | # [text, varname, text, varname, ..., text] |
||
| 433 | parts = re.split(pattern, fix_text) |
||
| 434 | |||
| 435 | fix.text = parts[0] # add first "text" |
||
| 436 | for index in range(1, len(parts), 2): |
||
| 437 | varname = parts[index] |
||
| 438 | text_between_vars = parts[index + 1] |
||
| 439 | |||
| 440 | # we cannot combine elements and text easily |
||
| 441 | # so text is in ".tail" of element |
||
| 442 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 443 | xccdfvarsub.tail = text_between_vars |
||
| 444 | return |
||
| 445 | |||
| 446 | elif remediation_type == "puppet": |
||
| 447 | pattern = r'\(puppet-populate\s*(\S+)\)' |
||
| 448 | |||
| 449 | # we will get list what looks like |
||
| 450 | # [text, varname, text, varname, ..., text] |
||
| 451 | parts = re.split(pattern, fix.text) |
||
| 452 | |||
| 453 | fix.text = parts[0] # add first "text" |
||
| 454 | for index in range(1, len(parts), 2): |
||
| 455 | varname = parts[index] |
||
| 456 | text_between_vars = parts[index + 1] |
||
| 457 | |||
| 458 | # we cannot combine elements and text easily |
||
| 459 | # so text is in ".tail" of element |
||
| 460 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 461 | xccdfvarsub.tail = text_between_vars |
||
| 462 | return |
||
| 463 | |||
| 464 | elif remediation_type == "anaconda": |
||
| 465 | pattern = r'\(anaconda-populate\s*(\S+)\)' |
||
| 466 | |||
| 467 | # we will get list what looks like |
||
| 468 | # [text, varname, text, varname, ..., text] |
||
| 469 | parts = re.split(pattern, fix.text) |
||
| 470 | |||
| 471 | fix.text = parts[0] # add first "text" |
||
| 472 | for index in range(1, len(parts), 2): |
||
| 473 | varname = parts[index] |
||
| 474 | text_between_vars = parts[index + 1] |
||
| 475 | |||
| 476 | # we cannot combine elements and text easily |
||
| 477 | # so text is in ".tail" of element |
||
| 478 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 479 | xccdfvarsub.tail = text_between_vars |
||
| 480 | return |
||
| 481 | |||
| 482 | elif remediation_type == "bash": |
||
| 483 | # This remediation script doesn't utilize internal remediation functions |
||
| 484 | # Skip it without any further processing |
||
| 485 | if 'remediation_functions' not in fix.text: |
||
| 486 | return |
||
| 487 | |||
| 488 | # This remediation script utilizes some of internal remediation functions |
||
| 489 | # Expand shell variables and remediation functions calls with <xccdf:sub> |
||
| 490 | # elements |
||
| 491 | pattern = r'\n+(\s*(?:' + r'|'.join(remediation_functions) + r')[^\n]*)\n' |
||
| 492 | patcomp = re.compile(pattern, re.DOTALL) |
||
| 493 | fixparts = re.split(patcomp, fix.text) |
||
| 494 | if fixparts[0] is not None: |
||
| 495 | # Split the portion of fix.text from fix start to first call of |
||
| 496 | # remediation function, keeping only the third part: |
||
| 497 | # * tail to hold part of the fix.text after inclusion, |
||
| 498 | # but before first call of remediation function |
||
| 499 | try: |
||
| 500 | rfpattern = '(.*remediation_functions)(.*)' |
||
| 501 | rfpatcomp = re.compile(rfpattern, re.DOTALL) |
||
| 502 | _, _, tail, _ = re.split(rfpatcomp, fixparts[0], maxsplit=2) |
||
| 503 | except ValueError: |
||
| 504 | sys.stderr.write("Processing fix.text for: %s rule\n" |
||
| 505 | % fix.get('rule')) |
||
| 506 | sys.stderr.write("Unable to extract part of the fix.text " |
||
| 507 | "after inclusion of remediation functions." |
||
| 508 | " Aborting..\n") |
||
| 509 | sys.exit(1) |
||
| 510 | # If the 'tail' is not empty, make it new fix.text. |
||
| 511 | # Otherwise use '' |
||
| 512 | fix.text = tail if tail is not None else '' |
||
|
|
|||
| 513 | # Drop the first element of 'fixparts' since it has been processed |
||
| 514 | fixparts.pop(0) |
||
| 515 | # Perform sanity check on new 'fixparts' list content (to continue |
||
| 516 | # successfully 'fixparts' has to contain even count of elements) |
||
| 517 | if len(fixparts) % 2 != 0: |
||
| 518 | sys.stderr.write("Error performing XCCDF expansion on " |
||
| 519 | "remediation script: %s\n" |
||
| 520 | % fix.get("rule")) |
||
| 521 | sys.stderr.write("Invalid count of elements. Exiting!\n") |
||
| 522 | sys.exit(1) |
||
| 523 | # Process remaining 'fixparts' elements in pairs |
||
| 524 | # First pair element is remediation function to be XCCDF expanded |
||
| 525 | # Second pair element (if not empty) is the portion of the original |
||
| 526 | # fix text to be used in newly added sublement's tail |
||
| 527 | for idx in range(0, len(fixparts), 2): |
||
| 528 | # We previously removed enclosing newlines when creating |
||
| 529 | # fixparts list. Add them back and reuse the above 'pattern' |
||
| 530 | fixparts[idx] = "\n%s\n" % fixparts[idx] |
||
| 531 | # Sanity check (verify the first field truly contains call of |
||
| 532 | # some of the remediation functions) |
||
| 533 | if re.match(pattern, fixparts[idx], re.DOTALL) is not None: |
||
| 534 | # This chunk contains call of 'populate' function |
||
| 535 | if "populate" in fixparts[idx]: |
||
| 536 | varname, fixtextcontrib = get_populate_replacement(remediation_type, |
||
| 537 | fixparts[idx]) |
||
| 538 | # Define new XCCDF <sub> element for the variable |
||
| 539 | xccdfvarsub = ElementTree.Element("sub", idref=varname) |
||
| 540 | |||
| 541 | # If this is first sub element, |
||
| 542 | # the textcontribution needs to go to fix text |
||
| 543 | # otherwise, append to last subelement |
||
| 544 | nfixchildren = len(list(fix)) |
||
| 545 | if nfixchildren == 0: |
||
| 546 | fix.text += fixtextcontrib |
||
| 547 | else: |
||
| 548 | previouselem = fix[nfixchildren-1] |
||
| 549 | previouselem.tail += fixtextcontrib |
||
| 550 | |||
| 551 | # If second pair element is not empty, append it as |
||
| 552 | # tail for the subelement (prefixed with closing '"') |
||
| 553 | if fixparts[idx + 1] is not None: |
||
| 554 | xccdfvarsub.tail = '"' + '\n' + fixparts[idx + 1] |
||
| 555 | # Otherwise append just enclosing '"' |
||
| 556 | else: |
||
| 557 | xccdfvarsub.tail = '"' + '\n' |
||
| 558 | # Append the new subelement to the fix element |
||
| 559 | fix.append(xccdfvarsub) |
||
| 560 | # This chunk contains call of other remediation function |
||
| 561 | else: |
||
| 562 | # Extract remediation function name |
||
| 563 | funcname = re.search(r'\n\s*(\S+)(| .*)\n', |
||
| 564 | fixparts[idx], |
||
| 565 | re.DOTALL).group(1) |
||
| 566 | # Define new XCCDF <sub> element for the function |
||
| 567 | xccdffuncsub = ElementTree.Element( |
||
| 568 | "sub", idref='function_%s' % funcname) |
||
| 569 | # Append original function call into tail of the |
||
| 570 | # subelement |
||
| 571 | xccdffuncsub.tail = fixparts[idx] |
||
| 572 | # If the second element of the pair is not empty, |
||
| 573 | # append it to the tail of the subelement too |
||
| 574 | if fixparts[idx + 1] is not None: |
||
| 575 | xccdffuncsub.tail += fixparts[idx + 1] |
||
| 576 | # Append the new subelement to the fix element |
||
| 577 | fix.append(xccdffuncsub) |
||
| 578 | # Ensure the newly added <xccdf:sub> element for the |
||
| 579 | # function will be always inserted at newline |
||
| 580 | # If xccdffuncsub is the first <xccdf:sub> element |
||
| 581 | # being added as child of <fix> and fix.text doesn't |
||
| 582 | # end up with newline character, append the newline |
||
| 583 | # to the fix.text |
||
| 584 | if list(fix).index(xccdffuncsub) == 0: |
||
| 585 | if re.search(r'.*\n$', fix.text) is None: |
||
| 586 | fix.text += '\n' |
||
| 587 | # If xccdffuncsub isn't the first child (first |
||
| 588 | # <xccdf:sub> being added), and tail of previous |
||
| 589 | # child doesn't end up with newline, append the newline |
||
| 590 | # to the tail of previous child |
||
| 591 | else: |
||
| 592 | previouselem = fix[list(fix).index(xccdffuncsub) - 1] |
||
| 593 | if re.search(r'.*\n$', previouselem.tail) is None: |
||
| 594 | previouselem.tail += '\n' |
||
| 595 | |||
| 596 | # Perform a sanity check if all known remediation function calls have been |
||
| 597 | # properly XCCDF substituted. Exit with failure if some wasn't |
||
| 598 | |||
| 599 | # First concat output form of modified fix text (including text appended |
||
| 600 | # to all children of the fix) |
||
| 601 | modfix = [fix.text] |
||
| 602 | for child in fix.getchildren(): |
||
| 603 | if child is not None and child.text is not None: |
||
| 604 | modfix.append(child.text) |
||
| 605 | modfixtext = "".join(modfix) |
||
| 606 | for func in remediation_functions: |
||
| 607 | # Then efine expected XCCDF sub element form for this function |
||
| 608 | funcxccdfsub = "<sub idref=\"function_%s\"" % func |
||
| 609 | # Finally perform the sanity check -- if function was properly XCCDF |
||
| 610 | # substituted both the original function call and XCCDF <sub> element |
||
| 611 | # for that function need to be present in the modified text of the fix |
||
| 612 | # Otherwise something went wrong, thus exit with failure |
||
| 613 | if func in modfixtext and funcxccdfsub not in modfixtext: |
||
| 614 | sys.stderr.write("Error performing XCCDF <sub> substitution " |
||
| 615 | "for function %s in %s fix. Exiting...\n" |
||
| 616 | % (func, fix.get("rule"))) |
||
| 617 | sys.exit(1) |
||
| 618 | else: |
||
| 619 | sys.stderr.write("Unknown remediation type '%s'\n" % (remediation_type)) |
||
| 620 | sys.exit(1) |
||
| 621 |