Skip to content

Validate proxy base path at parse time #47912

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

Merged
merged 3 commits into from
Oct 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public RestClientBuilder setRequestConfigCallback(RequestConfigCallback requestC
* @throws IllegalArgumentException if {@code pathPrefix} is empty, or ends with more than one '/'.
*/
public RestClientBuilder setPathPrefix(String pathPrefix) {
this.pathPrefix = cleanPathPrefix(pathPrefix);
return this;
}

public static String cleanPathPrefix(String pathPrefix) {
Objects.requireNonNull(pathPrefix, "pathPrefix must not be null");

if (pathPrefix.isEmpty()) {
Expand All @@ -154,10 +159,7 @@ public RestClientBuilder setPathPrefix(String pathPrefix) {
throw new IllegalArgumentException("pathPrefix is malformed. too many trailing slashes: [" + pathPrefix + "]");
}
}


this.pathPrefix = cleanPathPrefix;
return this;
return cleanPathPrefix;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,20 @@ public Iterator<Setting<?>> settings() {
*/
public static final Setting.AffixSetting<String> PROXY_BASE_PATH_SETTING =
Setting.affixKeySetting("xpack.monitoring.exporters.","proxy.base_path",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
(key) -> Setting.simpleString(
key,
value -> {
if (Strings.isNullOrEmpty(value) == false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if the value is null or empty ? I think the validation here should fail, else it may fail on cluster state application.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, the REST client builder does not set a proxy base path if the setting's value is null or empty. Do you think it should be changed so the setting itself refuses null or empty values?

Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized we actually need to support null as the way to un-set. If the underlying code already protects against empty (e.g. doesn't throw an exception) it should be fine as-is.

try {
RestClientBuilder.cleanPathPrefix(value);
} catch (RuntimeException e) {
Setting<?> concreteSetting = HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(key);
throw new SettingsException("[" + concreteSetting.getKey() + "] is malformed [" + value + "]", e);
}
}
},
Property.Dynamic,
Property.NodeScope));
/**
* A boolean setting to enable or disable sniffing for extra connections.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,29 @@ public void testExporterWithHostOnly() throws Exception {
new HttpExporter(config, sslService, threadContext).close();
}

public void testExporterWithInvalidProxyBasePath() throws Exception {
final String prefix = "xpack.monitoring.exporters._http";
final String settingName = ".proxy.base_path";
final String settingValue = "z//";
final String expected = "[" + prefix + settingName + "] is malformed [" + settingValue + "]";
final Settings settings = Settings.builder()
.put(prefix + ".type", HttpExporter.TYPE)
.put(prefix + ".host", "localhost:9200")
.put(prefix + settingName, settingValue)
.build();

final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(prefix + settingName).get(settings));
assertThat(
e,
hasToString(
containsString("Failed to parse value [" + settingValue + "] for setting [" + prefix + settingName + "]")));

assertThat(e.getCause(), instanceOf(SettingsException.class));
assertThat(e.getCause(), hasToString(containsString(expected)));
}

public void testCreateRestClient() throws IOException {
final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class);

Expand Down