Skip to content

Commit ae4103a

Browse files
authored
fix!: gathered_filter to process regex correctly (#583)
Removed backslash(\) escape char from shlex to avoid escaping and properly processing regex expressions. BREAKING CHANGE: `gathered_filter` in 'gathered' state now accepts valid regex expressions, the previous syntax with extra escape characters is not valid anymore.
1 parent 2375a93 commit ae4103a

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

docs/source/gatheredfilter.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ These all do the same thing, listed from fastest to slowest.
125125
gathered_filter: 'name matches-regex .*?'
126126
127127
128+
Example - Matching a Regex
129+
--------------------------
130+
131+
It is possible to write regex in the following formats;
132+
133+
* Standard regex in single quotation marks(`'`)
134+
* Escaped backslash in double quotation marks(`"`)
135+
* Using folded block scalar followed by a dash (`>-`) without any quotation marks
136+
137+
See examples below which correspond to the same regex:
138+
139+
.. code-block:: yaml
140+
141+
gathered_filter: 'name matches-regex \sPAN\s'
142+
143+
.. code-block:: yaml
144+
145+
gathered_filter: "name matches-regex \\sPAN\\s"
146+
147+
.. code-block:: yaml
148+
149+
gathered_filter: >-
150+
name matches-regex \sPAN\s
151+
152+
128153
Example - Matching a Suffix
129154
---------------------------
130155

plugins/module_utils/panos.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,18 @@ def _get_default_value(self, obj, key):
12001200

12011201
return default_value
12021202

1203+
def _shlex_split(self, logic):
1204+
"""Split string using shlex.split without escape char
1205+
1206+
Escape char '\' is removed from shlex class to correctly process regex.
1207+
"""
1208+
lex = shlex.shlex(logic, posix=True)
1209+
lex.whitespace_split = True
1210+
lex.commenters = ""
1211+
lex.escape = ""
1212+
1213+
return list(lex)
1214+
12031215
def matches_gathered_filter(self, item, logic):
12041216
"""Returns True if the item and its contents matches the logic given.
12051217
@@ -1223,7 +1235,7 @@ def matches_gathered_filter(self, item, logic):
12231235
evaler = []
12241236

12251237
pdepth = 0
1226-
logic_tokens = shlex.split(logic)
1238+
logic_tokens = self._shlex_split(logic)
12271239
token_iter = iter(logic_tokens)
12281240
while True:
12291241
end_parens = 0

0 commit comments

Comments
 (0)