Skip to content

Commit

Permalink
Moved iscidr to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-forte-elastic committed Oct 30, 2023
1 parent 49fb9f3 commit e34e199
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
25 changes: 11 additions & 14 deletions eql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
from .errors import EqlError
from .signatures import SignatureMixin
from .types import TypeHint
from .utils import (fold_case, get_ipaddress, get_subnet, is_insensitive,
is_number, is_string, to_unicode)
from .utils import (
fold_case,
get_ipaddress,
get_subnet,
is_cidr_pattern,
is_insensitive,
is_number,
is_string,
to_unicode,
)

_registry = {}
REGEX_FLAGS = re.UNICODE | re.DOTALL
Expand Down Expand Up @@ -222,17 +230,6 @@ def run(cls, ip_address, *cidr_matches):

return False

@classmethod
def is_cidr(cls, cidr):
"""Check if a string is a valid CIDR notation."""
if "/" not in cidr:
return False
try:
get_subnet(cidr)
return True
except ValueError:
return False

@classmethod
def validate(cls, arguments):
"""Validate the calling convention and change the argument order if necessary."""
Expand All @@ -249,7 +246,7 @@ def validate(cls, arguments):
# overwrite the original node
text = argument.node.value.strip()

if not cls.is_cidr(text):
if not is_cidr_pattern(text):
return pos

# Since it does match, we should also rewrite the string to align to the base of the subnet
Expand Down
9 changes: 9 additions & 0 deletions eql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def is_insensitive():
"""Check if insensitivity is enabled."""
return CASE_INSENSITIVE

def is_cidr_pattern(cidr):
"""Check if a string is a valid CIDR notation."""
if "/" not in cidr:
return False
try:
get_subnet(cidr)
return True
except ValueError:
return False

def fold_case(s):
"""Helper function for normalizing case for strings."""
Expand Down

0 comments on commit e34e199

Please sign in to comment.