Skip to content

Commit

Permalink
[7.11] Fix NPE when validating composable template with no template (#…
Browse files Browse the repository at this point in the history
…67310) (#67355)

Internally we check for the presence of the hidden index setting, but if there is no template or no
settings object, a `NullPointerException` would be thrown. This fixes the problem and adds a unit
test.

Resolves #66949
  • Loading branch information
dakrone committed Jan 12, 2021
1 parent 944d6e6 commit 594d617
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ public ActionRequestValidationException validateIndexTemplate(@Nullable ActionRe
if (indexTemplate == null) {
validationException = addValidationError("an index template is required", validationException);
} else {
if (indexTemplate.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(indexTemplate.template().settings())) {
if (indexTemplate.template() != null && indexTemplate.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
if (indexTemplate.template().settings() != null &&
IndexMetadata.INDEX_HIDDEN_SETTING.exists(indexTemplate.template().settings())) {
validationException = addValidationError("global composable templates may not specify the setting "
+ IndexMetadata.INDEX_HIDDEN_SETTING.getKey(),
validationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public List<String> composedOf() {
return componentTemplates;
}

@Nullable
public Long priority() {
return priority;
}
Expand All @@ -181,14 +182,17 @@ public long priorityOrZero() {
return priority;
}

@Nullable
public Long version() {
return version;
}

@Nullable
public Map<String, Object> metadata() {
return metadata;
}

@Nullable
public DataStreamTemplate getDataStreamTemplate() {
return dataStreamTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ public Template(StreamInput in) throws IOException {
}
}

@Nullable
public Settings settings() {
return settings;
}

@Nullable
public CompressedXContent mappings() {
return mappings;
}

@Nullable
public Map<String, AliasMetadata> aliases() {
return aliases;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -93,4 +94,14 @@ public void testValidationOfPriority() {
String error = validationErrors.get(0);
assertThat(error, is("index template priority must be >= 0"));
}

public void testValidateNoTemplate() {
PutComposableIndexTemplateAction.Request req = new PutComposableIndexTemplateAction.Request("test");
req.indexTemplate(new ComposableIndexTemplate(Collections.singletonList("*"), null, null, null, null, null));
assertNull(req.validate());

req.indexTemplate(new ComposableIndexTemplate(Collections.singletonList("*"),
new Template(null, null, null), null, null, null, null));
assertNull(req.validate());
}
}

0 comments on commit 594d617

Please sign in to comment.