Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: gathered_filter to process regex correctly #583

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/source/gatheredfilter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ These all do the same thing, listed from fastest to slowest.
gathered_filter: 'name matches-regex .*?'


Example - Matching a Regex
--------------------------

It is possible to write regex in the following formats;

* Standard regex in single quotation marks(`'`)
* Escaped backslash in double quotation marks(`"`)
* Using folded block scalar followed by a dash (`>-`) without any quotation marks

See examples below which correspond to the same regex:

.. code-block:: yaml

gathered_filter: 'name matches-regex \sPAN\s'

.. code-block:: yaml

gathered_filter: "name matches-regex \\sPAN\\s"

.. code-block:: yaml

gathered_filter: >-
name matches-regex \sPAN\s


Example - Matching a Suffix
---------------------------

Expand Down
14 changes: 13 additions & 1 deletion plugins/module_utils/panos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,18 @@ def _get_default_value(self, obj, key):

return default_value

def _shlex_split(self, logic):
"""Split string using shlex.split without escape char

Escape char '\' is removed from shlex class to correctly process regex.
"""
lex = shlex.shlex(logic, posix=True)
lex.whitespace_split = True
lex.commenters = ""
lex.escape = ""

return list(lex)

def matches_gathered_filter(self, item, logic):
"""Returns True if the item and its contents matches the logic given.

Expand All @@ -1223,7 +1235,7 @@ def matches_gathered_filter(self, item, logic):
evaler = []

pdepth = 0
logic_tokens = shlex.split(logic)
logic_tokens = self._shlex_split(logic)
token_iter = iter(logic_tokens)
while True:
end_parens = 0
Expand Down
Loading