Skip to content

Commit

Permalink
Add support for yaml OSCAL files validation (#1091)
Browse files Browse the repository at this point in the history
* Add support for yaml OSCAL files validation
* Replace pyyaml library with ruamel.yaml This was done to address the concern raised in the PR - #1091 (comment)
* Pin specific versions of PyPI packages.

Co-authored-by: Alexander Stein <[email protected]>
  • Loading branch information
guyzyl and aj-stein-nist authored Jan 26, 2022
1 parent f40b56b commit c2848eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/utils/util/oscal-content-validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import argparse
from jsonschema import validate
import xmlschema
from ruamel.yaml import YAML


def _get_oscal_file_type(filename):
if filename.endswith("json"):
return "json"
elif filename.endswith("xml") or filename.endswith("xsd"):
return "xml"
elif filename.endswith("yaml") or filename.endswith("yml"):
return "yaml"
else:
raise("Not a valid OSCAL file.")

Expand All @@ -21,6 +24,9 @@ def read_file(filename, ftype):
with io.open(filename, 'r', encoding="utf-8") as f:
if ftype == "json":
filedata = json.load(f)
if ftype == "yaml":
yaml = YAML()
filedata = yaml.load(f)
else:
filedata = f.read()
return filedata, ftype
Expand All @@ -41,7 +47,8 @@ def oscal_validator(oscal_schema, oscal_data):
schema, stype = read_file(oscal_schema, _get_oscal_file_type(oscal_schema))
data, ftype = read_file(oscal_data, _get_oscal_file_type(oscal_data))

if ftype == 'json':
if ftype == 'json' or ftype == 'yaml':
# Yaml files are validated using the json schema
validate(data, schema)
if ftype == 'xml':
xmlschema.validate(data, schema)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/util/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonschema
xmlschema
jsonschema==4.4.0
ruamel.yaml==0.17.10
xmlschema==1.9.2

0 comments on commit c2848eb

Please sign in to comment.