Files
atomic-red-team/bin/validate/validate.py
T
Josh Rickard 284886292b Atomic Red Team - JSON Schema Validation CI (#2303)
* feat: Adding atomic-red-team JSON Schema defintions

* feat: Adding validate.py script to validate all atomics against the defined schema

* feat: Adding validate-schema GitHub Workflow action to validate on every push to the repo

* ci: Updated the validate-schema workflow to support and use Ruby instead of python

* fix: Updated schema to remove schema draft version (not necessarily needed) and update to remove elevation_required as a required defined property

* fix: Removed the yaml schema version

* docs: Adding start of README

* fix: Adding an updated/better version of the python validation but may ultimately be removed

* feat: Adding Ruby version of validate.rb script

* fix: Removing files not needed since we are changing to github action and using the new validation code

* fix: Adding the yaml schema file back and removed the json version

* docs: Updated README with documentation

* fix: Updating schema to use new format validator

* fix: Updated validate.rb to verify that the Technique IDs are in the correct format.

* fix: Upating validate.rb to raise execptions so that failures flow up to the GitHub Action workflow

* fix: Updated all tests that have input_arguments not conformaing to schema defintion for type value of path

* fix: Updating the Validaton README for typos

* fixL: Minor updates to the schema

* minor schema changes

* github actions fix

* schema changes

---------

Co-authored-by: MSAdministrator <MSAdministrator@users.noreply.github.com>
Co-authored-by: Carrie Roberts <clr2of8@gmail.com>
Co-authored-by: Hare Sudhan <code@0x6c.dev>
2023-05-12 15:33:47 -06:00

40 lines
1.3 KiB
Python

"""Validates atomics based on JSON Schema."""
import glob
import os.path
import sys
import yaml
from jsonschema import validate
from jsonschema.exceptions import ValidationError
is_exception = False
with open(f"{os.path.dirname(os.path.abspath(__file__))}/atomic-red-team.schema.yaml", "r") as f:
schema = yaml.safe_load(f)
for item in glob.glob("./atomics/T*/T*.yaml"):
with open(item, 'r') as file:
data = yaml.safe_load(file)
try:
validate(
instance=data,
schema=schema
)
except ValidationError as ve:
print(f"Error occurred with {item}")
print("Each of the following are why it failed:")
if (context := ve.context) and len(context) > 0:
print(f"\n\t{context[0].message}\n")
else:
print(f"\n\t{ve}\n")
print(f"The JSON Path is {ve.json_path}")
is_exception = True
except Exception as e:
print(f"Error occurred with {item}")
print("Each of the following are why it failed:")
print(f"\n\t{e}\n")
is_exception = True
if is_exception:
print("Validation Failed")
sys.exit(1)
else:
print("Validation Successful")