Skip to content

Commit

Permalink
Merge pull request #2975 from cipherboy/fix-scrutinizer-ssg-issues-1
Browse files Browse the repository at this point in the history
Fix Scrutinizer ssg module issues - #1
  • Loading branch information
mpreisler committed Jun 15, 2018
2 parents 1e1444b + 1d2a0e6 commit 442b6ba
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 36 deletions.
5 changes: 2 additions & 3 deletions ssg/_checks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from ssg._constants import *
from ssg._constants import XCCDF11_NS


def get_content_ref_if_exists_and_not_remote(check):
Expand All @@ -17,8 +17,7 @@ def get_content_ref_if_exists_and_not_remote(check):
return None
if is_content_href_remote(checkcontentref):
return None
else:
return checkcontentref
return checkcontentref


def is_content_href_remote(check_content_ref):
Expand Down
9 changes: 9 additions & 0 deletions ssg/_constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import datetime
import os.path

try:
from openscap import oscap_get_version
if oscap_get_version() < 1.2:
OSCAP_OVAL_VERSION = "5.10"
else:
OSCAP_OVAL_VERSION = "5.11"
except ImportError:
OSCAP_OVAL_VERSION = "5.10"


JINJA_MACROS_DEFINITIONS = os.path.join(os.path.dirname(os.path.dirname(
__file__)), "shared", "macros.jinja")
Expand Down
1 change: 0 additions & 1 deletion ssg/_contributors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import collections
import datetime
import re
import subprocess
import os.path

from ssg._shims import subprocess_check_output
Expand Down
3 changes: 1 addition & 2 deletions ssg/_id_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def _split_namespace(tag):
if tag[0] == "{":
namespace, name = tag[1:].split("}", 1)
return namespace.split("#")[0], name
else:
return (None, tag)
return (None, tag)


def _namespace_to_prefix(tag):
Expand Down
52 changes: 28 additions & 24 deletions ssg/_oval.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
from __future__ import print_function

import sys
import os
import re
import argparse
import tempfile
import subprocess

from ConfigParser import SafeConfigParser

from ssg._constants import oval_footer as footer
from ssg._constants import oval_namespace as ovalns
from ssg._constants import timestamp
from ssg._constants import OSCAP_OVAL_VERSION
from ssg._xml import ElementTree as ET
from ssg._xml import oval_generated_header

from ssg._id_translate import IDTranslator

SHARED_OVAL = re.sub('shared.*', 'shared', __file__) + '/checks/oval/'


try:
from openscap import oscap_get_version
if oscap_get_version() < 1.2:
oval_version = "5.10"
else:
oval_version = "5.11"
except ImportError:
oval_version = "5.10"

# globals, to make recursion easier in case we encounter extend_definition
definitions = ET.Element("definitions")
tests = ET.Element("tests")
objects = ET.Element("objects")
states = ET.Element("states")
variables = ET.Element("variables")
silent_mode = False


# append new child ONLY if it's not a duplicate
def append(element, newchild):
global silent_mode
newid = newchild.get("id")
existing = element.find(".//*[@id='" + newid + "']")
if existing is not None:
Expand All @@ -47,11 +40,17 @@ def append(element, newchild):
element.append(newchild)


def add_oval_elements(body, header):
def _add_elements(body, header):
"""Add oval elements to the global Elements defined above"""
global definitions
global tests
global objects
global states
global variables

tree = ET.fromstring(header + body + footer)
tree = replace_external_vars(tree)
defname = None
# parse new file(string) as an etree, so we can arrange elements
# appropriately
for childnode in tree.findall("./{%s}def-group/*" % ovalns):
Expand All @@ -69,7 +68,7 @@ def add_oval_elements(body, header):
extend_ref = find_testfile(defid+".xml")
includedbody = read_ovaldefgroup_file(extend_ref)
# recursively add the elements in the other file
add_oval_elements(includedbody, header)
_add_elements(includedbody, header)
if childnode.tag.endswith("_test"):
append(tests, childnode)
if childnode.tag.endswith("_object"):
Expand All @@ -95,7 +94,7 @@ def replace_external_vars(tree):
# sys.exit()
if extvar_id not in os.environ.keys():
print("External_variable specified, but no value provided via "
"environment variable")
"environment variable", file=sys.stderr)
sys.exit(2)
# replace tag name: external -> local
node.tag = "{%s}local_variable" % ovalns
Expand All @@ -110,7 +109,7 @@ def replace_external_vars(tree):
def find_testfile(testfile):
"""Find OVAL files in CWD or shared/oval"""
for path in ['.', SHARED_OVAL]:
for root, folder, files in os.walk(path):
for root, _, _ in os.walk(path):
searchfile = root + '/' + testfile
if not os.path.isfile(searchfile):
searchfile = ""
Expand All @@ -121,7 +120,7 @@ def find_testfile(testfile):

if not os.path.isfile(testfile):
print("ERROR: %s does not exist! Please specify a valid OVAL file."
% testfile)
% testfile, file=sys.stderr)
sys.exit(1)

return testfile
Expand All @@ -136,10 +135,10 @@ def read_ovaldefgroup_file(testfile):

def parse_options():
usage = "usage: %(prog)s [options] definition_file.xml"
parser = argparse.ArgumentParser(usage=usage, version="%(prog)s ")
parser = argparse.ArgumentParser(usage=usage)
# only some options are on by default

parser.add_argument("--oval_version", default=oval_version,
parser.add_argument("--oval_version", default=OSCAP_OVAL_VERSION,
dest="oval_version", action="store",
help="OVAL version to use. Example: 5.11, 5.10, ... \
[Default: %(default)s]")
Expand Down Expand Up @@ -168,16 +167,21 @@ def main():
header = oval_generated_header("testoval.py", oval_version, "0.0.1")
testfile = find_testfile(testfile)
body = read_ovaldefgroup_file(testfile)
defname = add_oval_elements(body, header)
defname = _add_elements(body, header)
if defname is None:
print("Error while evaluating oval: defname not set; missing "
"definitions section?")
sys.exit(1)

ovaltree = ET.fromstring(header + footer)

# append each major element type, if it has subelements
for element in [definitions, tests, objects, states, variables]:
if element.getchildren():
if list(element) > 0:
ovaltree.append(element)
# re-map all the element ids from meaningful names to meaningless
# numbers
testtranslator = idtranslate.IDTranslator("scap-security-guide.testing")
testtranslator = IDTranslator("scap-security-guide.testing")
ovaltree = testtranslator.translate(ovaltree)
(ovalfile, fname) = tempfile.mkstemp(prefix=defname, suffix=".xml")
os.write(ovalfile, ET.tostring(ovaltree))
Expand All @@ -190,7 +194,7 @@ def main():
oscap_child = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
cmd_out = oscap_child.communicate()[0]
if not silent_mode:
print cmd_out
print(cmd_out)
if oscap_child.returncode != 0:
if not silent_mode:
print("Error launching 'oscap' command: \n\t" + cmd)
Expand Down
1 change: 0 additions & 1 deletion ssg/_parse_oval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from __future__ import print_function

from ssg._xml import ElementTree as ET
Expand Down
4 changes: 2 additions & 2 deletions ssg/_stig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import sys
import csv

from ssg._xml import ElementTree as ET
from ssg._xml import parse_file as parse_xml_file
from ssg._constants import XCCDF11_NS as xccdf_ns
from ssg._constants import *
from ssg._constants import disa_cciuri

# This script creates a CSV file from an XCCDF file formatted in the
# structure of a STIG. This should enable its ingestion into VMS,
Expand Down Expand Up @@ -49,5 +48,6 @@ def main():

sys.exit(0)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion ssg/_xml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import platform

from ssg._constants import *
from ssg._constants import xml_version, oval_header, timestamp


try:
Expand Down
4 changes: 2 additions & 2 deletions ssg/_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from ssg._jinja import _extract_substitutions_dict_from_template
from ssg._jinja import _rename_items
from ssg._jinja import *
from ssg._constants import *
from ssg._jinja import process_file
from ssg._constants import PKG_MANAGER_TO_SYSTEM, JINJA_MACROS_DEFINITIONS

try:
from yaml import CSafeLoader as yaml_SafeLoader
Expand Down

0 comments on commit 442b6ba

Please sign in to comment.