Validate proxy base path at parse time (#47912) (#48825)

This commit is contained in:
Dan Hermann 2019-11-04 09:51:13 -06:00 committed by GitHub
parent 020ff0fef9
commit c85cf7a6de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 5 deletions

View File

@ -135,6 +135,11 @@ public final class RestClientBuilder {
* @throws IllegalArgumentException if {@code pathPrefix} is empty, or ends with more than one '/'. * @throws IllegalArgumentException if {@code pathPrefix} is empty, or ends with more than one '/'.
*/ */
public RestClientBuilder setPathPrefix(String pathPrefix) { 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"); Objects.requireNonNull(pathPrefix, "pathPrefix must not be null");
if (pathPrefix.isEmpty()) { if (pathPrefix.isEmpty()) {
@ -154,10 +159,7 @@ public final class RestClientBuilder {
throw new IllegalArgumentException("pathPrefix is malformed. too many trailing slashes: [" + pathPrefix + "]"); throw new IllegalArgumentException("pathPrefix is malformed. too many trailing slashes: [" + pathPrefix + "]");
} }
} }
return cleanPathPrefix;
this.pathPrefix = cleanPathPrefix;
return this;
} }
/** /**

View File

@ -236,7 +236,20 @@ public class HttpExporter extends Exporter {
*/ */
public static final Setting.AffixSetting<String> PROXY_BASE_PATH_SETTING = public static final Setting.AffixSetting<String> PROXY_BASE_PATH_SETTING =
Setting.affixKeySetting("xpack.monitoring.exporters.","proxy.base_path", 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) {
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. * A boolean setting to enable or disable sniffing for extra connections.
*/ */

View File

@ -302,6 +302,29 @@ public class HttpExporterTests extends ESTestCase {
new HttpExporter(config, sslService, threadContext).close(); 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 { public void testCreateRestClient() throws IOException {
final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class); final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class);