| 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 |
|
| 502 | 2 | def expand_xccdf_subs(fix, remediation_type, remediation_functions): |
|
| 503 | """For those remediation scripts utilizing some of the internal SCAP |
||
| 504 | Security Guide remediation functions expand the selected shell variables |
||
| 505 | and remediation functions calls with <xccdf:sub> element |
||
| 506 | |||
| 507 | This routine translates any instance of the 'populate' function call in |
||
| 508 | the form of: |
||
| 509 | |||
| 510 | populate variable_name |
||
| 511 | |||
| 512 | into |
||
| 513 | |||
| 514 | variable_name="<sub idref="variable_name"/>" |
||
| 515 | |||
| 516 | Also transforms any instance of the 'ansible-populate' function call in the |
||
| 517 | form of: |
||
| 518 | (ansible-populate variable_name) |
||
| 519 | into |
||
| 520 | |||
| 521 | <sub idref="variable_name"/> |
||
| 522 | |||
| 523 | Also transforms any instance of some other known remediation function (e.g. |
||
| 524 | 'replace_or_append' etc.) from the form of: |
||
| 525 | |||
| 526 | function_name "arg1" "arg2" ... "argN" |
||
| 527 | |||
| 528 | into: |
||
| 529 | |||
| 530 | <sub idref="function_function_name"/> |
||
| 531 | function_name "arg1" "arg2" ... "argN" |
||
| 532 | """ |
||
| 533 | |||
| 534 | if remediation_type == "ansible": |
||
| 535 | fix_text = fix.text |
||
| 536 | |||
| 537 | if "(ansible-populate " in fix_text: |
||
| 538 | raise RuntimeError( |
||
| 539 | "(ansible-populate VAR) has been deprecated. Please use " |
||
| 540 | "(xccdf-var VAR) instead. Keep in mind that the latter will " |
||
| 541 | "make an ansible variable out of XCCDF Value as opposed to " |
||
| 542 | "substituting directly." |
||
| 543 | ) |
||
| 544 | |||
| 545 | # If you change this string make sure it still matches the pattern |
||
| 546 | # defined in OpenSCAP. Otherwise you break variable handling in |
||
| 547 | # 'oscap xccdf generate fix' and the variables won't be customizable! |
||
| 548 | # https://github.com/OpenSCAP/openscap/blob/1.2.17/src/XCCDF_POLICY/xccdf_policy_remediate.c#L588 |
||
| 549 | # const char *pattern = |
||
| 550 | # "- name: XCCDF Value [^ ]+ # promote to variable\n set_fact:\n" |
||
| 551 | # " ([^:]+): (.+)\n tags:\n - always\n"; |
||
| 552 | # We use !!str typecast to prevent treating values as different types |
||
| 553 | # eg. yes as a bool or 077 as an octal number |
||
| 554 | fix_text = re.sub( |
||
| 555 | r"- \(xccdf-var\s+(\S+)\)", |
||
| 556 | r"- name: XCCDF Value \1 # promote to variable\n" |
||
| 557 | r" set_fact:\n" |
||
| 558 | r" \1: !!str (ansible-populate \1)\n" |
||
| 559 | r" tags:\n" |
||
| 560 | r" - always", |
||
| 561 | fix_text |
||
| 562 | ) |
||
| 563 | |||
| 564 | pattern = r'\(ansible-populate\s*(\S+)\)' |
||
| 565 | |||
| 566 | # we will get list what looks like |
||
| 567 | # [text, varname, text, varname, ..., text] |
||
| 568 | parts = re.split(pattern, fix_text) |
||
| 569 | |||
| 570 | fix.text = parts[0] # add first "text" |
||
| 571 | for index in range(1, len(parts), 2): |
||
| 572 | varname = parts[index] |
||
| 573 | text_between_vars = parts[index + 1] |
||
| 574 | |||
| 575 | # we cannot combine elements and text easily |
||
| 576 | # so text is in ".tail" of element |
||
| 577 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 578 | xccdfvarsub.tail = text_between_vars |
||
| 579 | return |
||
| 580 | |||
| 581 | elif remediation_type == "puppet": |
||
| 582 | pattern = r'\(puppet-populate\s*(\S+)\)' |
||
| 583 | |||
| 584 | # we will get list what looks like |
||
| 585 | # [text, varname, text, varname, ..., text] |
||
| 586 | parts = re.split(pattern, fix.text) |
||
| 587 | |||
| 588 | fix.text = parts[0] # add first "text" |
||
| 589 | for index in range(1, len(parts), 2): |
||
| 590 | varname = parts[index] |
||
| 591 | text_between_vars = parts[index + 1] |
||
| 592 | |||
| 593 | # we cannot combine elements and text easily |
||
| 594 | # so text is in ".tail" of element |
||
| 595 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 596 | xccdfvarsub.tail = text_between_vars |
||
| 597 | return |
||
| 598 | |||
| 599 | elif remediation_type == "anaconda": |
||
| 600 | pattern = r'\(anaconda-populate\s*(\S+)\)' |
||
| 601 | |||
| 602 | # we will get list what looks like |
||
| 603 | # [text, varname, text, varname, ..., text] |
||
| 604 | parts = re.split(pattern, fix.text) |
||
| 605 | |||
| 606 | fix.text = parts[0] # add first "text" |
||
| 607 | for index in range(1, len(parts), 2): |
||
| 608 | varname = parts[index] |
||
| 609 | text_between_vars = parts[index + 1] |
||
| 610 | |||
| 611 | # we cannot combine elements and text easily |
||
| 612 | # so text is in ".tail" of element |
||
| 613 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 614 | xccdfvarsub.tail = text_between_vars |
||
| 615 | return |
||
| 616 | |||
| 617 | elif remediation_type == "bash": |
||
| 618 | # This remediation script doesn't utilize internal remediation functions |
||
| 619 | # Skip it without any further processing |
||
| 620 | if 'remediation_functions' not in fix.text: |
||
| 621 | return |
||
| 622 | |||
| 623 | # This remediation script utilizes some of internal remediation functions |
||
| 624 | # Expand shell variables and remediation functions calls with <xccdf:sub> |
||
| 625 | # elements |
||
| 626 | pattern = r'\n+(\s*(?:' + r'|'.join(remediation_functions) + r')[^\n]*)\n' |
||
| 627 | patcomp = re.compile(pattern, re.DOTALL) |
||
| 628 | fixparts = re.split(patcomp, fix.text) |
||
| 629 | if fixparts[0] is not None: |
||
| 630 | # Split the portion of fix.text from fix start to first call of |
||
| 631 | # remediation function, keeping only the third part: |
||
| 632 | # * tail to hold part of the fix.text after inclusion, |
||
| 633 | # but before first call of remediation function |
||
| 634 | try: |
||
| 635 | rfpattern = '(.*remediation_functions)(.*)' |
||
| 636 | rfpatcomp = re.compile(rfpattern, re.DOTALL) |
||
| 637 | _, _, tail, _ = re.split(rfpatcomp, fixparts[0], maxsplit=2) |
||
| 638 | except ValueError: |
||
| 639 | sys.stderr.write("Processing fix.text for: %s rule\n" |
||
| 640 | % fix.get('rule')) |
||
| 641 | sys.stderr.write("Unable to extract part of the fix.text " |
||
| 642 | "after inclusion of remediation functions." |
||
| 643 | " Aborting..\n") |
||
| 644 | sys.exit(1) |
||
| 645 | # If the 'tail' is not empty, make it new fix.text. |
||
| 646 | # Otherwise use '' |
||
| 647 | fix.text = tail if tail is not None else '' |
||
| 648 | # Drop the first element of 'fixparts' since it has been processed |
||
| 649 | fixparts.pop(0) |
||
| 650 | # Perform sanity check on new 'fixparts' list content (to continue |
||
| 651 | # successfully 'fixparts' has to contain even count of elements) |
||
| 652 | if len(fixparts) % 2 != 0: |
||
| 653 | sys.stderr.write("Error performing XCCDF expansion on " |
||
| 654 | "remediation script: %s\n" |
||
| 655 | % fix.get("rule")) |
||
| 656 | sys.stderr.write("Invalid count of elements. Exiting!\n") |
||
| 657 | sys.exit(1) |
||
| 658 | # Process remaining 'fixparts' elements in pairs |
||
| 659 | # First pair element is remediation function to be XCCDF expanded |
||
| 660 | # Second pair element (if not empty) is the portion of the original |
||
| 661 | # fix text to be used in newly added sublement's tail |
||
| 662 | for idx in range(0, len(fixparts), 2): |
||
| 663 | # We previously removed enclosing newlines when creating |
||
| 664 | # fixparts list. Add them back and reuse the above 'pattern' |
||
| 665 | fixparts[idx] = "\n%s\n" % fixparts[idx] |
||
| 666 | # Sanity check (verify the first field truly contains call of |
||
| 667 | # some of the remediation functions) |
||
| 668 | if re.match(pattern, fixparts[idx], re.DOTALL) is not None: |
||
| 669 | # This chunk contains call of 'populate' function |
||
| 670 | if "populate" in fixparts[idx]: |
||
| 671 | varname, fixtextcontrib = get_populate_replacement(remediation_type, |
||
| 672 | fixparts[idx]) |
||
| 673 | # Define new XCCDF <sub> element for the variable |
||
| 674 | xccdfvarsub = ElementTree.Element("sub", idref=varname) |
||
| 675 | |||
| 676 | # If this is first sub element, |
||
| 677 | # the textcontribution needs to go to fix text |
||
| 678 | # otherwise, append to last subelement |
||
| 679 | nfixchildren = len(list(fix)) |
||
| 680 | if nfixchildren == 0: |
||
| 681 | fix.text += fixtextcontrib |
||
| 682 | else: |
||
| 683 | previouselem = fix[nfixchildren-1] |
||
| 684 | previouselem.tail += fixtextcontrib |
||
| 685 | |||
| 686 | # If second pair element is not empty, append it as |
||
| 687 | # tail for the subelement (prefixed with closing '"') |
||
| 688 | if fixparts[idx + 1] is not None: |
||
| 689 | xccdfvarsub.tail = '"' + '\n' + fixparts[idx + 1] |
||
| 690 | # Otherwise append just enclosing '"' |
||
| 691 | else: |
||
| 692 | xccdfvarsub.tail = '"' + '\n' |
||
| 693 | # Append the new subelement to the fix element |
||
| 694 | fix.append(xccdfvarsub) |
||
| 695 | # This chunk contains call of other remediation function |
||
| 696 | else: |
||
| 697 | # Extract remediation function name |
||
| 698 | funcname = re.search(r'\n\s*(\S+)(| .*)\n', |
||
| 699 | fixparts[idx], |
||
| 700 | re.DOTALL).group(1) |
||
| 701 | # Define new XCCDF <sub> element for the function |
||
| 702 | xccdffuncsub = ElementTree.Element( |
||
| 703 | "sub", idref='function_%s' % funcname) |
||
| 704 | # Append original function call into tail of the |
||
| 705 | # subelement |
||
| 706 | xccdffuncsub.tail = fixparts[idx] |
||
| 707 | # If the second element of the pair is not empty, |
||
| 708 | # append it to the tail of the subelement too |
||
| 709 | if fixparts[idx + 1] is not None: |
||
| 710 | xccdffuncsub.tail += fixparts[idx + 1] |
||
| 711 | # Append the new subelement to the fix element |
||
| 712 | fix.append(xccdffuncsub) |
||
| 713 | # Ensure the newly added <xccdf:sub> element for the |
||
| 714 | # function will be always inserted at newline |
||
| 715 | # If xccdffuncsub is the first <xccdf:sub> element |
||
| 716 | # being added as child of <fix> and fix.text doesn't |
||
| 717 | # end up with newline character, append the newline |
||
| 718 | # to the fix.text |
||
| 719 | if list(fix).index(xccdffuncsub) == 0: |
||
| 720 | if re.search(r'.*\n$', fix.text) is None: |
||
| 721 | fix.text += '\n' |
||
| 722 | # If xccdffuncsub isn't the first child (first |
||
| 723 | # <xccdf:sub> being added), and tail of previous |
||
| 724 | # child doesn't end up with newline, append the newline |
||
| 725 | # to the tail of previous child |
||
| 726 | else: |
||
| 727 | previouselem = fix[list(fix).index(xccdffuncsub) - 1] |
||
| 728 | if re.search(r'.*\n$', previouselem.tail) is None: |
||
| 729 | previouselem.tail += '\n' |
||
| 730 | |||
| 731 | # Perform a sanity check if all known remediation function calls have been |
||
| 732 | # properly XCCDF substituted. Exit with failure if some wasn't |
||
| 733 | |||
| 734 | # First concat output form of modified fix text (including text appended |
||
| 735 | # to all children of the fix) |
||
| 736 | modfix = [fix.text] |
||
| 737 | for child in fix.getchildren(): |
||
| 738 | if child is not None and child.text is not None: |
||
| 739 | modfix.append(child.text) |
||
| 740 | modfixtext = "".join(modfix) |
||
| 741 | for func in remediation_functions: |
||
| 742 | # Then efine expected XCCDF sub element form for this function |
||
| 743 | funcxccdfsub = "<sub idref=\"function_%s\"" % func |
||
| 744 | # Finally perform the sanity check -- if function was properly XCCDF |
||
| 745 | # substituted both the original function call and XCCDF <sub> element |
||
| 746 | # for that function need to be present in the modified text of the fix |
||
| 747 | # Otherwise something went wrong, thus exit with failure |
||
| 748 | if func in modfixtext and funcxccdfsub not in modfixtext: |
||
| 749 | sys.stderr.write("Error performing XCCDF <sub> substitution " |
||
| 750 | "for function %s in %s fix. Exiting...\n" |
||
| 751 | % (func, fix.get("rule"))) |
||
| 752 | sys.exit(1) |
||
| 753 | else: |
||
| 754 | sys.stderr.write("Unknown remediation type '%s'\n" % (remediation_type)) |
||
| 755 | sys.exit(1) |
||
| 756 |