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

Fix environment variable substitutions in list setting #28106

Merged

Conversation

mayya-sharipova
Copy link
Contributor

Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work
This commit fixes it.

Closes #27926

Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work
This commit fixes it.

Closes elastic#27926
Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

Thanks @mayya-sharipova; the changes to production code look great I left a bunch of suggestions.

Settings settings = Settings.builder()
.put("property.placeholder", value)
.putList("setting1", "${property.placeholder}", "${property.placeholder}")
.replacePropertyPlaceholders()
Copy link
Member

Choose a reason for hiding this comment

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

We have replacePropertyPlaceholders(Function<String, String>); we use this rather than pulling from the same settings object as I think this will be a more realistic test?

if (entry.getValue() instanceof List) {
List<String> ls = (List<String>) entry.getValue();
ListIterator<String> li = ls.listIterator();
while(li.hasNext()){
Copy link
Member

Choose a reason for hiding this comment

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

Nit: spacing between while and (.

while(li.hasNext()){
String value = li.next();
String value2 = propertyPlaceholder.replacePlaceholders(value, placeholderResolver);
if (! value.equals(value2)){
Copy link
Member

Choose a reason for hiding this comment

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

Nit: spacing between ! and value.

while(li.hasNext()){
String value = li.next();
String value2 = propertyPlaceholder.replacePlaceholders(value, placeholderResolver);
if (! value.equals(value2)){
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a clearer name than value2?

// a null value obviously can't be replaced
continue;
}
if (entry.getValue() instanceof List) {
List<String> ls = (List<String>) entry.getValue();
Copy link
Member

Choose a reason for hiding this comment

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

ls is never used again after the next line; do we need this local variable?

// a null value obviously can't be replaced
continue;
}
if (entry.getValue() instanceof List) {
List<String> ls = (List<String>) entry.getValue();
ListIterator<String> li = ls.listIterator();
Copy link
Member

Choose a reason for hiding this comment

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

Can this be a local final variable?

Copy link
Contributor Author

@mayya-sharipova mayya-sharipova Jan 8, 2018

Choose a reason for hiding this comment

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

+1 for final. I am not very clear what you meant by local ? Do you recommend to declare li before the while loop on the line 1213?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for not being clear. What I mean is that these variables (which are local variables, they only have scope of the current) block I think would clearer if they were final because they I know by the declaration they are never mutated instead of having to keep checking the code "is this the same object as when the variable was first declared?".

List<String> ls = (List<String>) entry.getValue();
ListIterator<String> li = ls.listIterator();
while(li.hasNext()){
String value = li.next();
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a clearer name than value?

.putList("setting1", "${property.placeholder}", "${property.placeholder}")
.replacePropertyPlaceholders()
.build();
assertThat(settings.getAsList("setting1"), contains(value, value));
Copy link
Member

Choose a reason for hiding this comment

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

Can we test replacing different placeholders?

@@ -68,6 +68,17 @@ public void testReplacePropertiesPlaceholderSystemProperty() {
assertThat(settings.get("setting1"), equalTo(value));
}

public void testReplacePropertiesPlaceholderSystemPropertyList() {
String value = System.getProperty("java.home");
assertFalse(value.isEmpty());
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this will be needed if you take the suggestion below, but in general assertFalse and assertTrue should be avoided because they do not give good error messages (a failing expectation would only say AssertionError). Here, we could use assertThat(value, not(isEmptyOrNullString()) as then the expectation would say

Expected: not (null or an empty string)
     but: was ""

which is a lot more helpful!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you, this is very helpful to know

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

Thanks @mayya-sharipova; looks good. I left a few more comments.

.replacePropertyPlaceholders()
final String hostname = randomAlphaOfLength(16);
final Settings settings = Settings.builder()
.putList("setting1", "${HOSTNAME}", "${HOSTNAME}")
Copy link
Member

Choose a reason for hiding this comment

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

Can we use two different placeholders here to test that that works?

while (li.hasNext()){
final String settingValueRaw = li.next();
final String settingValueResolved = propertyPlaceholder.replacePlaceholders(settingValueRaw, placeholderResolver);
if (!settingValueRaw.equals(settingValueResolved)){
Copy link
Member

Choose a reason for hiding this comment

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

Nit: space after )) so )) }

if (! value.equals(value2)){
li.set(value2);
final ListIterator<String> li = ((List<String>) entry.getValue()).listIterator();
while (li.hasNext()){
Copy link
Member

Choose a reason for hiding this comment

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

Nit: space after )) so )) }

while (li.hasNext()){
final String settingValueRaw = li.next();
final String settingValueResolved = propertyPlaceholder.replacePlaceholders(settingValueRaw, placeholderResolver);
if (!settingValueRaw.equals(settingValueResolved)){
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's simpler to do away with this if? If they are equal, it's okay to do the set?

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

LGTM. However, I think the commit message could be more thorough (right now it basically says: "something was broken, this fixes it"). When you merge, can you write a more thorough commit message please?

@mayya-sharipova
Copy link
Contributor Author

@jasontedor thanks a lot for comprehensive review, I learned useful stuff.

Just to confirm, I intend to backport this commit to 6.1 and 6.x

@mayya-sharipova mayya-sharipova merged commit caa63ad into elastic:master Jan 10, 2018
@jasontedor
Copy link
Member

It is not clear if we will do a 6.1 release that has this fix but please backport to both 6.1 (in case), and 6.x. The label on 6.1 should be 6.1.3.

@jasontedor
Copy link
Member

@mayya-sharipova If this is backported, can you remove the backport-pending label. Separately, can you fix the 6.1 label?

mayya-sharipova added a commit that referenced this pull request Jan 11, 2018
Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work.
Environment variables in a list setting were not resolved, because settings with a list type were skipped during variables resolution.
This commit fixes by processing list settings as well.

Closes #27926
mayya-sharipova added a commit that referenced this pull request Jan 11, 2018
Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work.
Environment variables in a list setting were not resolved, because settings with a list type were skipped during variables resolution.
This commit fixes by processing list settings as well.

Closes #27926
jasontedor added a commit that referenced this pull request Jan 11, 2018
* master: (43 commits)
  Rename core module to server (#28180)
  upgraded jna from 4.4.0-1 to 4.5.1 (#28183)
  [TEST] Do not call RandomizedTest.scaledRandomIntBetween from multiple threads
  Primary send safe commit in file-based recovery (#28038)
  [Docs] Correct response json in rank-eval.asciidoc
  Add scroll parameter to _reindex API (#28041)
  Include all sentences smaller than fragment_size in the unified highlighter (#28132)
  Modifies the JavaAPI docs related to AggregationBuilder
  [Docs] Improvements in script-fields.asciidoc (#28174)
  [Docs] Remove Kerberos/SPNEGO Shield plugin (#28019)
  Ignore null value for range field (#27845) (#28116)
  Fix environment variable substitutions in list setting (#28106)
  docs: Replaces indexed script java api docs with stored script api docs
  test: ensure we endup with a single segment
  Make sure that we don't detect files as maven coordinate when installing a plugin (#28163)
  [Tests] temporary disable meta plugin rest tests #28163
  meta-plugin should install bin and config at the top level (#28162)
  Painless: Add public member read/write access test. (#28156)
  Docs: Clarify password protection support with keystore (#28157)
  [Docs] fix plugin properties inclusion for plugins authors
  ...
jasontedor added a commit that referenced this pull request Jan 11, 2018
* 6.x: (41 commits)
  Rename core module to server (#28190)
  [Test] Remove superfluous lines
  [Test] Add skip test for index range field with null values
  upgraded jna from 4.4.0-1 to 4.5.1 (#28183)
  [TEST] Do not call RandomizedTest.scaledRandomIntBetween from multiple threads
  [Docs] Correct response json in rank-eval.asciidoc
  Fix environment variable substitutions in list setting (#28106)
  Add scroll parameter to _reindex API (#28041)
  Include all sentences smaller than fragment_size in the unified highlighter (#28132)
  [Docs] Improvements in script-fields.asciidoc (#28174)
  [Docs] Remove Kerberos/SPNEGO Shield plugin (#28019)
  Ignore null value for range field (#27845) (#28116)
  docs: Replaces indexed script java api docs with stored script api docs
  test: ensure we endup with a single segment
  Make sure that we don't detect files as maven coordinate when installing a plugin (#28163)
  [Tests] temporary disable meta plugin rest tests #28163
  meta-plugin should install bin and config at the top level (#28162)
  Painless: Add public member read/write access test. (#28156)
  Docs: Clarify password protection support with keystore (#28157)
  [Docs] fix plugin properties inclusion for plugins authors
  ...
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Jan 13, 2018
* master: (30 commits)
  Fix lock accounting in releasable lock
  Add ability to associate an ID with tasks  (elastic#27764)
  [DOCS] Removed differencies between text and code (elastic#27993)
  text fixes (elastic#28136)
  Update getting-started.asciidoc (elastic#28145)
  [Docs] Spelling fix in painless-getting-started.asciidoc (elastic#28187)
  Fixed the cat.health REST test to accept 4ms, not just 4.0ms (elastic#28186)
  Do not keep 5.x commits once having 6.x commits (elastic#28188)
  Rename core module to server (elastic#28180)
  upgraded jna from 4.4.0-1 to 4.5.1 (elastic#28183)
  [TEST] Do not call RandomizedTest.scaledRandomIntBetween from multiple threads
  Primary send safe commit in file-based recovery (elastic#28038)
  [Docs] Correct response json in rank-eval.asciidoc
  Add scroll parameter to _reindex API (elastic#28041)
  Include all sentences smaller than fragment_size in the unified highlighter (elastic#28132)
  Modifies the JavaAPI docs related to AggregationBuilder
  [Docs] Improvements in script-fields.asciidoc (elastic#28174)
  [Docs] Remove Kerberos/SPNEGO Shield plugin (elastic#28019)
  Ignore null value for range field (elastic#27845) (elastic#28116)
  Fix environment variable substitutions in list setting (elastic#28106)
  ...
@colings86 colings86 added the >bug label Jan 22, 2018
@jimczi jimczi added v7.0.0-beta1 and removed v7.0.0 labels Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work
4 participants