| Conditions | 29 |
| Total Lines | 249 |
| 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 | 1 | from __future__ import absolute_import |
|
| 251 | 1 | def expand_xccdf_subs(fix, remediation_type, remediation_functions): |
|
| 252 | """For those remediation scripts utilizing some of the internal SCAP |
||
| 253 | Security Guide remediation functions expand the selected shell variables |
||
| 254 | and remediation functions calls with <xccdf:sub> element |
||
| 255 | |||
| 256 | This routine translates any instance of the 'populate' function call in |
||
| 257 | the form of: |
||
| 258 | |||
| 259 | populate variable_name |
||
| 260 | |||
| 261 | into |
||
| 262 | |||
| 263 | variable_name="<sub idref="variable_name"/>" |
||
| 264 | |||
| 265 | Also transforms any instance of the 'ansible-populate' function call in the |
||
| 266 | form of: |
||
| 267 | (ansible-populate variable_name) |
||
| 268 | into |
||
| 269 | |||
| 270 | <sub idref="variable_name"/> |
||
| 271 | |||
| 272 | Also transforms any instance of some other known remediation function (e.g. |
||
| 273 | 'replace_or_append' etc.) from the form of: |
||
| 274 | |||
| 275 | function_name "arg1" "arg2" ... "argN" |
||
| 276 | |||
| 277 | into: |
||
| 278 | |||
| 279 | <sub idref="function_function_name"/> |
||
| 280 | function_name "arg1" "arg2" ... "argN" |
||
| 281 | """ |
||
| 282 | |||
| 283 | if remediation_type == "ansible": |
||
| 284 | fix_text = fix.text |
||
| 285 | |||
| 286 | if "(ansible-populate " in fix_text: |
||
| 287 | raise RuntimeError( |
||
| 288 | "(ansible-populate VAR) has been deprecated. Please use " |
||
| 289 | "(xccdf-var VAR) instead. Keep in mind that the latter will " |
||
| 290 | "make an ansible variable out of XCCDF Value as opposed to " |
||
| 291 | "substituting directly." |
||
| 292 | ) |
||
| 293 | |||
| 294 | # we use the horrid "!!str |-" syntax to force strings without using |
||
| 295 | # quotes. quotes enable yaml escaping rules so we'd have to escape all |
||
| 296 | # the backslashes and at this point we don't know if there are any. |
||
| 297 | fix_text = re.sub( |
||
| 298 | r"- \(xccdf-var\s+(\S+)\)", |
||
| 299 | r"- name: XCCDF Value \1 # promote to variable\n" |
||
| 300 | r" set_fact:\n" |
||
| 301 | r" \1: !!str |-\n" |
||
| 302 | r" (ansible-populate \1)\n" |
||
| 303 | r" tags:\n" |
||
| 304 | r" - always", |
||
| 305 | fix_text |
||
| 306 | ) |
||
| 307 | |||
| 308 | pattern = r'\(ansible-populate\s*(\S+)\)' |
||
| 309 | |||
| 310 | # we will get list what looks like |
||
| 311 | # [text, varname, text, varname, ..., text] |
||
| 312 | parts = re.split(pattern, fix_text) |
||
| 313 | |||
| 314 | fix.text = parts[0] # add first "text" |
||
| 315 | for index in range(1, len(parts), 2): |
||
| 316 | varname = parts[index] |
||
| 317 | text_between_vars = parts[index + 1] |
||
| 318 | |||
| 319 | # we cannot combine elements and text easily |
||
| 320 | # so text is in ".tail" of element |
||
| 321 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 322 | xccdfvarsub.tail = text_between_vars |
||
| 323 | return |
||
| 324 | |||
| 325 | elif remediation_type == "puppet": |
||
| 326 | pattern = r'\(puppet-populate\s*(\S+)\)' |
||
| 327 | |||
| 328 | # we will get list what looks like |
||
| 329 | # [text, varname, text, varname, ..., text] |
||
| 330 | parts = re.split(pattern, fix.text) |
||
| 331 | |||
| 332 | fix.text = parts[0] # add first "text" |
||
| 333 | for index in range(1, len(parts), 2): |
||
| 334 | varname = parts[index] |
||
| 335 | text_between_vars = parts[index + 1] |
||
| 336 | |||
| 337 | # we cannot combine elements and text easily |
||
| 338 | # so text is in ".tail" of element |
||
| 339 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 340 | xccdfvarsub.tail = text_between_vars |
||
| 341 | return |
||
| 342 | |||
| 343 | elif remediation_type == "anaconda": |
||
| 344 | pattern = r'\(anaconda-populate\s*(\S+)\)' |
||
| 345 | |||
| 346 | # we will get list what looks like |
||
| 347 | # [text, varname, text, varname, ..., text] |
||
| 348 | parts = re.split(pattern, fix.text) |
||
| 349 | |||
| 350 | fix.text = parts[0] # add first "text" |
||
| 351 | for index in range(1, len(parts), 2): |
||
| 352 | varname = parts[index] |
||
| 353 | text_between_vars = parts[index + 1] |
||
| 354 | |||
| 355 | # we cannot combine elements and text easily |
||
| 356 | # so text is in ".tail" of element |
||
| 357 | xccdfvarsub = ElementTree.SubElement(fix, "sub", idref=varname) |
||
| 358 | xccdfvarsub.tail = text_between_vars |
||
| 359 | return |
||
| 360 | |||
| 361 | elif remediation_type == "bash": |
||
| 362 | # This remediation script doesn't utilize internal remediation functions |
||
| 363 | # Skip it without any further processing |
||
| 364 | if 'remediation_functions' not in fix.text: |
||
| 365 | return |
||
| 366 | |||
| 367 | # This remediation script utilizes some of internal remediation functions |
||
| 368 | # Expand shell variables and remediation functions calls with <xccdf:sub> |
||
| 369 | # elements |
||
| 370 | pattern = r'\n+(\s*(?:' + r'|'.join(remediation_functions) + r')[^\n]*)\n' |
||
| 371 | patcomp = re.compile(pattern, re.DOTALL) |
||
| 372 | fixparts = re.split(patcomp, fix.text) |
||
| 373 | if fixparts[0] is not None: |
||
| 374 | # Split the portion of fix.text from fix start to first call of |
||
| 375 | # remediation function, keeping only the third part: |
||
| 376 | # * tail to hold part of the fix.text after inclusion, |
||
| 377 | # but before first call of remediation function |
||
| 378 | try: |
||
| 379 | rfpattern = '(.*remediation_functions)(.*)' |
||
| 380 | rfpatcomp = re.compile(rfpattern, re.DOTALL) |
||
| 381 | _, _, tail, _ = re.split(rfpatcomp, fixparts[0], maxsplit=2) |
||
| 382 | except ValueError: |
||
| 383 | sys.stderr.write("Processing fix.text for: %s rule\n" |
||
| 384 | % fix.get('rule')) |
||
| 385 | sys.stderr.write("Unable to extract part of the fix.text " |
||
| 386 | "after inclusion of remediation functions." |
||
| 387 | " Aborting..\n") |
||
| 388 | sys.exit(1) |
||
| 389 | # If the 'tail' is not empty, make it new fix.text. |
||
| 390 | # Otherwise use '' |
||
| 391 | fix.text = tail if tail is not None else '' |
||
| 392 | # Drop the first element of 'fixparts' since it has been processed |
||
| 393 | fixparts.pop(0) |
||
| 394 | # Perform sanity check on new 'fixparts' list content (to continue |
||
| 395 | # successfully 'fixparts' has to contain even count of elements) |
||
| 396 | if len(fixparts) % 2 != 0: |
||
| 397 | sys.stderr.write("Error performing XCCDF expansion on " |
||
| 398 | "remediation script: %s\n" |
||
| 399 | % fix.get("rule")) |
||
| 400 | sys.stderr.write("Invalid count of elements. Exiting!\n") |
||
| 401 | sys.exit(1) |
||
| 402 | # Process remaining 'fixparts' elements in pairs |
||
| 403 | # First pair element is remediation function to be XCCDF expanded |
||
| 404 | # Second pair element (if not empty) is the portion of the original |
||
| 405 | # fix text to be used in newly added sublement's tail |
||
| 406 | for idx in range(0, len(fixparts), 2): |
||
| 407 | # We previously removed enclosing newlines when creating |
||
| 408 | # fixparts list. Add them back and reuse the above 'pattern' |
||
| 409 | fixparts[idx] = "\n%s\n" % fixparts[idx] |
||
| 410 | # Sanity check (verify the first field truly contains call of |
||
| 411 | # some of the remediation functions) |
||
| 412 | if re.match(pattern, fixparts[idx], re.DOTALL) is not None: |
||
| 413 | # This chunk contains call of 'populate' function |
||
| 414 | if "populate" in fixparts[idx]: |
||
| 415 | varname, fixtextcontrib = get_populate_replacement(remediation_type, |
||
| 416 | fixparts[idx]) |
||
| 417 | # Define new XCCDF <sub> element for the variable |
||
| 418 | xccdfvarsub = ElementTree.Element("sub", idref=varname) |
||
| 419 | |||
| 420 | # If this is first sub element, |
||
| 421 | # the textcontribution needs to go to fix text |
||
| 422 | # otherwise, append to last subelement |
||
| 423 | nfixchildren = len(list(fix)) |
||
| 424 | if nfixchildren == 0: |
||
| 425 | fix.text += fixtextcontrib |
||
| 426 | else: |
||
| 427 | previouselem = fix[nfixchildren-1] |
||
| 428 | previouselem.tail += fixtextcontrib |
||
| 429 | |||
| 430 | # If second pair element is not empty, append it as |
||
| 431 | # tail for the subelement (prefixed with closing '"') |
||
| 432 | if fixparts[idx + 1] is not None: |
||
| 433 | xccdfvarsub.tail = '"' + '\n' + fixparts[idx + 1] |
||
| 434 | # Otherwise append just enclosing '"' |
||
| 435 | else: |
||
| 436 | xccdfvarsub.tail = '"' + '\n' |
||
| 437 | # Append the new subelement to the fix element |
||
| 438 | fix.append(xccdfvarsub) |
||
| 439 | # This chunk contains call of other remediation function |
||
| 440 | else: |
||
| 441 | # Extract remediation function name |
||
| 442 | funcname = re.search(r'\n\s*(\S+)(| .*)\n', |
||
| 443 | fixparts[idx], |
||
| 444 | re.DOTALL).group(1) |
||
| 445 | # Define new XCCDF <sub> element for the function |
||
| 446 | xccdffuncsub = ElementTree.Element( |
||
| 447 | "sub", idref='function_%s' % funcname) |
||
| 448 | # Append original function call into tail of the |
||
| 449 | # subelement |
||
| 450 | xccdffuncsub.tail = fixparts[idx] |
||
| 451 | # If the second element of the pair is not empty, |
||
| 452 | # append it to the tail of the subelement too |
||
| 453 | if fixparts[idx + 1] is not None: |
||
| 454 | xccdffuncsub.tail += fixparts[idx + 1] |
||
| 455 | # Append the new subelement to the fix element |
||
| 456 | fix.append(xccdffuncsub) |
||
| 457 | # Ensure the newly added <xccdf:sub> element for the |
||
| 458 | # function will be always inserted at newline |
||
| 459 | # If xccdffuncsub is the first <xccdf:sub> element |
||
| 460 | # being added as child of <fix> and fix.text doesn't |
||
| 461 | # end up with newline character, append the newline |
||
| 462 | # to the fix.text |
||
| 463 | if list(fix).index(xccdffuncsub) == 0: |
||
| 464 | if re.search(r'.*\n$', fix.text) is None: |
||
| 465 | fix.text += '\n' |
||
| 466 | # If xccdffuncsub isn't the first child (first |
||
| 467 | # <xccdf:sub> being added), and tail of previous |
||
| 468 | # child doesn't end up with newline, append the newline |
||
| 469 | # to the tail of previous child |
||
| 470 | else: |
||
| 471 | previouselem = fix[list(fix).index(xccdffuncsub) - 1] |
||
| 472 | if re.search(r'.*\n$', previouselem.tail) is None: |
||
| 473 | previouselem.tail += '\n' |
||
| 474 | |||
| 475 | # Perform a sanity check if all known remediation function calls have been |
||
| 476 | # properly XCCDF substituted. Exit with failure if some wasn't |
||
| 477 | |||
| 478 | # First concat output form of modified fix text (including text appended |
||
| 479 | # to all children of the fix) |
||
| 480 | modfix = [fix.text] |
||
| 481 | for child in fix.getchildren(): |
||
| 482 | if child is not None and child.text is not None: |
||
| 483 | modfix.append(child.text) |
||
| 484 | modfixtext = "".join(modfix) |
||
| 485 | for func in remediation_functions: |
||
| 486 | # Then efine expected XCCDF sub element form for this function |
||
| 487 | funcxccdfsub = "<sub idref=\"function_%s\"" % func |
||
| 488 | # Finally perform the sanity check -- if function was properly XCCDF |
||
| 489 | # substituted both the original function call and XCCDF <sub> element |
||
| 490 | # for that function need to be present in the modified text of the fix |
||
| 491 | # Otherwise something went wrong, thus exit with failure |
||
| 492 | if func in modfixtext and funcxccdfsub not in modfixtext: |
||
| 493 | sys.stderr.write("Error performing XCCDF <sub> substitution " |
||
| 494 | "for function %s in %s fix. Exiting...\n" |
||
| 495 | % (func, fix.get("rule"))) |
||
| 496 | sys.exit(1) |
||
| 497 | else: |
||
| 498 | sys.stderr.write("Unknown remediation type '%s'\n" % (remediation_type)) |
||
| 499 | sys.exit(1) |
||
| 500 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.