Skip to content

Commit

Permalink
Merge pull request #54625 from garethgreenaway/2019_2_1_port_49903
Browse files Browse the repository at this point in the history
[master] Porting #49903 to master
  • Loading branch information
dwoz authored Dec 20, 2019
2 parents b5eeec1 + 51b99e9 commit ba9c023
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 9 additions & 7 deletions salt/pillar/consul_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ def ext_pillar(minion_id,
if minion_id not in minions:
return {}

root_re = re.compile('root=(\S*)') # pylint: disable=W1401
root_re = re.compile('(?<!_)root=(\S*)') # pylint: disable=W1401
match = root_re.search(temp)
if match:
opts['root'] = match.group(1)
opts['root'] = match.group(1).rstrip('/')
temp = temp.replace(match.group(0), '')
else:
opts['root'] = ""

pillar_root_re = re.compile('pillar_root=(\S*)') # pylint: disable=W1401
match = pillar_root_re.search(temp)
if match:
opts['pillar_root'] = match.group(1)
opts['pillar_root'] = match.group(1).rstrip('/')
temp = temp.replace(match.group(0), '')
else:
opts['pillar_root'] = ""
Expand Down Expand Up @@ -237,7 +237,7 @@ def ext_pillar(minion_id,

pillar = {}
branch = pillar
keys = opts['pillar_root'].rstrip('/').split('/')
keys = opts['pillar_root'].split('/')

for i, k in enumerate(keys):
if i == len(keys) - 1:
Expand All @@ -258,7 +258,9 @@ def consul_fetch(client, path):
'''
Query consul for all keys/values within base path
'''
return client.kv.get(path, recurse=True)
# Unless the root path is blank, it needs a trailing slash for
# the kv get from Consul to work as expected
return client.kv.get('' if not path else path.rstrip('/') + '/', recurse=True)


def fetch_tree(client, path, expand_keys):
Expand All @@ -276,9 +278,9 @@ def fetch_tree(client, path, expand_keys):
if items is None:
return ret
for item in reversed(items):
key = re.sub(r'^' + path + '/?', '', item['Key'])
key = re.sub(r'^' + re.escape(path) + '/?', '', item['Key'])
if key != '':
log.debug('key/path - %s: %s', path, key)
log.debug('path/key - %s: %s', path, key)
log.debug('has_children? %r', has_children.search(key))
if has_children.search(key) is None:
ret = pillar_format(ret, key.split('/'), item['Value'], expand_keys)
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/pillar/test_consul_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,23 @@ def test_pillar_data(self):
with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared/')
consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared')
assert sorted(pillar_data) == ['sites', 'user']
self.assertNotIn('blankvalue', pillar_data['user'])

def test_blank_root(self):
with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config')
consul_pillar.consul_fetch.assert_called_once_with('consul_connection', '')
assert sorted(pillar_data) == ['test-shared']

def test_pillar_nest(self):
with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
pillar_data = consul_pillar.ext_pillar(
'testminion', {}, 'consul_config root=test-shared/ pillar_root=nested-key/'
'testminion', {}, 'consul_config pillar_root=nested-key/ root=test-shared/ '
)
consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared/')
assert sorted(pillar_data['nested-key']) == ['sites', 'user']
self.assertNotIn('blankvalue', pillar_data['nested-key']['user'])

Expand Down

0 comments on commit ba9c023

Please sign in to comment.