|
1
|
|
|
#!/usr/bin/env python2 |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import sys |
|
4
|
|
|
import csv |
|
5
|
|
|
|
|
6
|
|
|
from ssg._xml import ElementTree as ET |
|
|
|
|
|
|
7
|
|
|
from ssg._xml import parse_file as parse_xml_file |
|
8
|
|
|
from ssg._constants import XCCDF11_NS as xccdf_ns |
|
9
|
|
|
from ssg._constants import * |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
# This script creates a CSV file from an XCCDF file formatted in the |
|
12
|
|
|
# structure of a STIG. This should enable its ingestion into VMS, |
|
13
|
|
|
# as well as its comparison with VMS output. |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def _reflist(refs): |
|
17
|
|
|
refstring = ', '.join(refs) |
|
18
|
|
|
return refstring |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def _node_to_text(node): |
|
22
|
|
|
textslist = node.xpath(".//text()") |
|
23
|
|
|
return ''.join(textslist) |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def main(): |
|
|
|
|
|
|
27
|
|
|
if len(sys.argv) < 2: |
|
28
|
|
|
print("Provide an XCCDF file to convert into a CSV file.") |
|
29
|
|
|
sys.exit(1) |
|
30
|
|
|
|
|
31
|
|
|
xccdffile = sys.argv[1] |
|
32
|
|
|
xccdftree = parse_xml_file(xccdffile) |
|
33
|
|
|
rules = xccdftree.findall(".//{%s}Rule" % xccdf_ns) |
|
34
|
|
|
rulewriter = csv.writer(sys.stdout, quoting=csv.QUOTE_ALL) |
|
35
|
|
|
|
|
36
|
|
|
for rule in rules: |
|
37
|
|
|
args = (xccdf_ns, disa_cciuri) |
|
|
|
|
|
|
38
|
|
|
cci_refs = [ref.text for ref in rule.findall("{%s}ident[@system='%s']" |
|
39
|
|
|
% args)] |
|
40
|
|
|
srg_refs = [ref.text for ref in rule.findall("{%s}ident[@system='%s']" |
|
41
|
|
|
% args)] |
|
42
|
|
|
title = rule.find("{%s}title" % xccdf_ns).text |
|
43
|
|
|
description = _node_to_text(rule.find("{%s}description" % xccdf_ns)) |
|
44
|
|
|
fixtext = _node_to_text(rule.find("{%s}fixtext" % xccdf_ns)) |
|
45
|
|
|
checktext = _node_to_text(rule.find(".//{%s}check-content" % xccdf_ns)) |
|
46
|
|
|
row = [_reflist(cci_refs), _reflist(srg_refs), title, |
|
47
|
|
|
description, fixtext, checktext] |
|
48
|
|
|
rulewriter.writerow(row) |
|
49
|
|
|
|
|
50
|
|
|
sys.exit(0) |
|
51
|
|
|
|
|
52
|
|
|
if __name__ == "__main__": |
|
53
|
|
|
main() |
|
54
|
|
|
|
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.