Skip to content

Commit 261b582

Browse files
authored
Validate proxy base path at parse time (#47912)
1 parent de7c680 commit 261b582

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
@@ -197,7 +197,20 @@ public Iterator<Setting<?>> settings() {
197197
*/
198198
public static final Setting.AffixSetting<String> PROXY_BASE_PATH_SETTING =
199199
Setting.affixKeySetting("xpack.monitoring.exporters.","proxy.base_path",
200-
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
200+
(key) -> Setting.simpleString(
201+
key,
202+
value -> {
203+
if (Strings.isNullOrEmpty(value) == false) {
204+
try {
205+
RestClientBuilder.cleanPathPrefix(value);
206+
} catch (RuntimeException e) {
207+
Setting<?> concreteSetting = HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(key);
208+
throw new SettingsException("[" + concreteSetting.getKey() + "] is malformed [" + value + "]", e);
209+
}
210+
}
211+
},
212+
Property.Dynamic,
213+
Property.NodeScope));
201214
/**
202215
* A boolean setting to enable or disable sniffing for extra connections.
203216
*/

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

+23
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,29 @@ public void testExporterWithHostOnly() throws Exception {
279279
new HttpExporter(config, sslService, threadContext).close();
280280
}
281281

282+
public void testExporterWithInvalidProxyBasePath() throws Exception {
283+
final String prefix = "xpack.monitoring.exporters._http";
284+
final String settingName = ".proxy.base_path";
285+
final String settingValue = "z//";
286+
final String expected = "[" + prefix + settingName + "] is malformed [" + settingValue + "]";
287+
final Settings settings = Settings.builder()
288+
.put(prefix + ".type", HttpExporter.TYPE)
289+
.put(prefix + ".host", "localhost:9200")
290+
.put(prefix + settingName, settingValue)
291+
.build();
292+
293+
final IllegalArgumentException e = expectThrows(
294+
IllegalArgumentException.class,
295+
() -> HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(prefix + settingName).get(settings));
296+
assertThat(
297+
e,
298+
hasToString(
299+
containsString("Failed to parse value [" + settingValue + "] for setting [" + prefix + settingName + "]")));
300+
301+
assertThat(e.getCause(), instanceOf(SettingsException.class));
302+
assertThat(e.getCause(), hasToString(containsString(expected)));
303+
}
304+
282305
public void testCreateRestClient() throws IOException {
283306
final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class);
284307

0 commit comments

Comments
 (0)