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

Improve consul_pillar configuration parsing #49903

Merged
merged 12 commits into from
Oct 16, 2018

Conversation

fraajad
Copy link
Contributor

@fraajad fraajad commented Oct 4, 2018

What does this PR do?

Ensure consistency with how the pillar paths are parsed from the consul_pillar configuration.

What issues does this PR fix or reference?

  1. Because of how the regex was, it was possible to get the wrong value for root if pillar_root was first in the list.

  2. A user could be using a trailing slash or not in the config parameters and consul_pillar would perform differently. The documentation does not use the trailing slash, but the test cases do. This especially matters in the API call to Consul because if that call does not have a trailing slash, Consul returns all data for keys that the given key is a substring of.

  3. Keys in Consul that contained special characters would get processed as regex ie data.yaml

Tests written?

I moved pillar_root to the beginning of the config in test_pillar_nest to cover case #1. test_pillar_data should cover case #2. I also removed an unnecessary assertion from the test_pillar_nest test.

Commits signed with GPG?

Yes

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

@fraajad fraajad requested a review from a team as a code owner October 4, 2018 20:45
@ghost ghost self-requested a review October 4, 2018 20:45
@@ -267,7 +267,7 @@ def fetch_tree(client, path, expand_keys):
are folders. Take the remaining data and send it to be formatted
in such a way as to be used as pillar data.
'''
_, items = consul_fetch(client, path)
_, items = consul_fetch(client, path + '/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isn't hidden. So I'd maybe rather check if path has training slash.

@rallytime rallytime requested a review from isbm October 9, 2018 13:44
@@ -267,7 +267,8 @@ def fetch_tree(client, path, expand_keys):
are folders. Take the remaining data and send it to be formatted
in such a way as to be used as pillar data.
'''
_, items = consul_fetch(client, path)
absolute_path = path.rstrip('/') + '/'
_, items = consul_fetch(client, absolute_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nooo, inside the consul_fetch. It should look on its own if the path is right or not. If you reuse it elsewhere, you don't know if trailing slash is needed or not, so you will do it every time you call it, and thus duplicate the code.

Please move that rstrip+/ inside the function. 😉

absolute_path = path
else:
absolute_path = path.rstrip('/') + '/'
return client.kv.get(absolute_path, recurse=True)
Copy link
Contributor

@isbm isbm Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost. 😉 If you are not sure path may be None, it might still crash. So:

return client.kv.get(os.path.join((path or '').rstrip(os.path.sep), ''),
                     recurse=True)

Otherwise just .rstrip it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right for back-slash. Still can be much less coding:

return client.kv.get((path or '').rstrip('/') + '/', recurse=True)

absolute_path = path
else:
absolute_path = path.rstrip('/') + '/'
return client.kv.get(absolute_path, recurse=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right for back-slash. Still can be much less coding:

return client.kv.get((path or '').rstrip('/') + '/', recurse=True)

@rallytime rallytime requested a review from isbm October 15, 2018 20:41
@cachedout
Copy link
Contributor

@isbm Re-review requested.

@rallytime rallytime merged commit 38a6b05 into saltstack:develop Oct 16, 2018
garethgreenaway added a commit to garethgreenaway/salt that referenced this pull request Sep 19, 2019
dwoz added a commit that referenced this pull request Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants