Skip to content

Commit

Permalink
PuppetDB: fix regex matching
Browse files Browse the repository at this point in the history
* Fix regex matching in PuppetDB queries that requires that all
  backslashes are escaped according to the PuppetDB API.
  See: https://puppet.com/docs/puppetdb/4.4/api/query/v4/ast.html#regexp-match

Change-Id: I931ac436c524bca9a784f3e33d5dd79c30424871
  • Loading branch information
volans- committed Oct 11, 2018
1 parent df94409 commit d5442b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cumin/backends/puppetdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def _add_category(self, category, key, value=None, operator='=', neg=False):
"""
self.endpoint = self.endpoints[category]
if operator == '~':
value = value.replace(r'\\', r'\\\\') # Required by PuppetDB API
# PuppetDB API requires to escape every backslash
# See: https://puppet.com/docs/puppetdb/4.4/api/query/v4/ast.html#regexp-match
value = value.replace('\\', '\\\\')

if category in ('C', 'O', 'P'):
query = self._get_special_resource_query(category, key, value, operator)
Expand Down
10 changes: 8 additions & 2 deletions cumin/tests/unit/backends/test_puppetdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,12 @@ def setup_method(self, _):
( # Different operator
'F:key >= value',
'[">=", ["fact", "key"], "value"]'),
( # Regex operator
( # Regex with backslash escaped
r'F:key ~ value\\escaped',
r'["~", ["fact", "key"], "value\\\\escaped"]'),
( # Regex with dot escaped
r'F:key ~ value\.escaped',
r'["~", ["fact", "key"], "value\\.escaped"]'),
))
def test_add_category_fact(self, mocked_api_call, query, expected):
"""A fact query should add the proper query token to the current_group."""
Expand All @@ -199,9 +202,12 @@ def test_add_category_fact(self, mocked_api_call, query, expected):
( # Negated
'not R:key = value',
'["not", ["and", ["=", "type", "Key"], ["=", "title", "value"]]]'),
( # Regex
( # Regex backslash escaped
r'R:key ~ value\\escaped',
r'["and", ["=", "type", "Key"], ["~", "title", "value\\\\escaped"]]'),
( # Regex dot escaped
r'R:key ~ value\.escaped',
r'["and", ["=", "type", "Key"], ["~", "title", "value\\.escaped"]]'),
( # Regex class
r'R:Class ~ "Role::(One|Another)"',
r'["and", ["=", "type", "Class"], ["~", "title", "Role::(One|Another)"]]'),
Expand Down

0 comments on commit d5442b9

Please sign in to comment.