Skip to content

Commit

Permalink
Validator changes (#2622)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbuff authored Nov 28, 2023
1 parent 7ae80c5 commit 201ad3b
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 216 deletions.
1 change: 0 additions & 1 deletion bin/validate/atomic-red-team.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ $defs:
"/": {}
patternProperties:
"^[\\w-]+$":
type: integer
type: object
required:
- description
Expand Down
16 changes: 9 additions & 7 deletions bin/validate/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import glob
import os
from os import DirEntry
import yaml
import sys
from jsonschema import validate, ValidationError
from collections import defaultdict
from ruamel.yaml import YAML

yaml = YAML(typ="safe")


class BaseError(Exception):
Expand All @@ -15,12 +17,12 @@ def __init__(self, path):

class InvalidPath(BaseError):
def __str__(self):
return f"Invalid path. `src` and `bin` are the only two directories supported."
return "Invalid path. `src` and `bin` are the only two directories supported."


class InvalidFileName(BaseError):
def __str__(self):
return f"Invalid filename. Rename file from .yml to .yaml"
return "Invalid filename. Rename file from .yml to .yaml"


class ReusedGuid(BaseError):
Expand Down Expand Up @@ -55,7 +57,7 @@ def __init__(self):
with open(used_guids_path, "r") as f:
self.used_guids = [x.strip() for x in f.readlines()]
with open(schema_path, "r") as f:
self.schema = yaml.safe_load(f)
self.schema = yaml.load(f)
self.guids = []

def validate(self, obj: DirEntry):
Expand All @@ -74,7 +76,7 @@ def validate_file(self, file: DirEntry):
def validate_atomic(self, file: DirEntry):
"""Validates whether the defined input args are used."""
with open(file.path, "r") as f:
atomic = yaml.safe_load(f)
atomic = yaml.load(f)
for index, t in enumerate(atomic["atomic_tests"]):
if t.get("auto_generated_guid"):
if t["auto_generated_guid"] not in self.guids:
Expand Down Expand Up @@ -111,7 +113,7 @@ def validate_yaml_extension(self, file: DirEntry):
def validate_json_schema(self, file: DirEntry):
"""Validates the yaml file against the schema."""
with open(file.path, "r") as f:
atomic = yaml.safe_load(f)
atomic = yaml.load(f)
try:
validate(
instance=atomic,
Expand All @@ -125,7 +127,7 @@ def validate_directory(self, directory: DirEntry):
self.validate_directory_path(directory)

def validate_directory_path(self, directory: DirEntry):
"""Validated whether the directory is a allowed directory name (`src` or `bin`)"""
"""Validated whether the directory is allowed directory name (`src` or `bin`)"""
if directory.name not in ["src", "bin"]:
self.errors[directory.path].append(InvalidPath(directory.path))

Expand Down
Loading

0 comments on commit 201ad3b

Please sign in to comment.