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

[master] Porting #49903 to master #54625

Merged
merged 5 commits into from
Dec 20, 2019
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
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