Skip to content

Commit 16c8abd

Browse files
committed
Validate proxy base path at parse time (elastic#47912)
1 parent e26d01e commit 16c8abd

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public RestClientBuilder setRequestConfigCallback(RequestConfigCallback requestC
135135
* @throws IllegalArgumentException if {@code pathPrefix} is empty, or ends with more than one '/'.
136136
*/
137137
public RestClientBuilder setPathPrefix(String pathPrefix) {
138+
this.pathPrefix = cleanPathPrefix(pathPrefix);
139+
return this;
140+
}
141+
142+
public static String cleanPathPrefix(String pathPrefix) {
138143
Objects.requireNonNull(pathPrefix, "pathPrefix must not be null");
139144

140145
if (pathPrefix.isEmpty()) {
@@ -154,10 +159,7 @@ public RestClientBuilder setPathPrefix(String pathPrefix) {
154159
throw new IllegalArgumentException("pathPrefix is malformed. too many trailing slashes: [" + pathPrefix + "]");
155160
}
156161
}
157-
158-
159-
this.pathPrefix = cleanPathPrefix;
160-
return this;
162+
return cleanPathPrefix;
161163
}
162164

163165
/**

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,20 @@ public Iterator<Setting<?>> settings() {
236236
*/
237237
public static final Setting.AffixSetting<String> PROXY_BASE_PATH_SETTING =
238238
Setting.affixKeySetting("xpack.monitoring.exporters.","proxy.base_path",
239-
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
239+
(key) -> Setting.simpleString(
240+
key,
241+
value -> {
242+
if (Strings.isNullOrEmpty(value) == false) {
243+
try {
244+
RestClientBuilder.cleanPathPrefix(value);
245+
} catch (RuntimeException e) {
246+
Setting<?> concreteSetting = HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(key);
247+
throw new SettingsException("[" + concreteSetting.getKey() + "] is malformed [" + value + "]", e);
248+
}
249+
}
250+
},
251+
Property.Dynamic,
252+
Property.NodeScope));
240253
/**
241254
* A boolean setting to enable or disable sniffing for extra connections.
242255
*/

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,29 @@ public void testExporterWithHostOnly() throws Exception {
302302
new HttpExporter(config, sslService, threadContext).close();
303303
}
304304

305+
public void testExporterWithInvalidProxyBasePath() throws Exception {
306+
final String prefix = "xpack.monitoring.exporters._http";
307+
final String settingName = ".proxy.base_path";
308+
final String settingValue = "z//";
309+
final String expected = "[" + prefix + settingName + "] is malformed [" + settingValue + "]";
310+
final Settings settings = Settings.builder()
311+
.put(prefix + ".type", HttpExporter.TYPE)
312+
.put(prefix + ".host", "localhost:9200")
313+
.put(prefix + settingName, settingValue)
314+
.build();
315+
316+
final IllegalArgumentException e = expectThrows(
317+
IllegalArgumentException.class,
318+
() -> HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(prefix + settingName).get(settings));
319+
assertThat(
320+
e,
321+
hasToString(
322+
containsString("Failed to parse value [" + settingValue + "] for setting [" + prefix + settingName + "]")));
323+
324+
assertThat(e.getCause(), instanceOf(SettingsException.class));
325+
assertThat(e.getCause(), hasToString(containsString(expected)));
326+
}
327+
305328
public void testCreateRestClient() throws IOException {
306329
final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class);
307330

0 commit comments

Comments
 (0)