File tree 3 files changed +43
-5
lines changed
client/rest/src/main/java/org/elasticsearch/client
x-pack/plugin/monitoring/src
main/java/org/elasticsearch/xpack/monitoring/exporter/http
test/java/org/elasticsearch/xpack/monitoring/exporter/http
3 files changed +43
-5
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,11 @@ public RestClientBuilder setRequestConfigCallback(RequestConfigCallback requestC
135
135
* @throws IllegalArgumentException if {@code pathPrefix} is empty, or ends with more than one '/'.
136
136
*/
137
137
public RestClientBuilder setPathPrefix (String pathPrefix ) {
138
+ this .pathPrefix = cleanPathPrefix (pathPrefix );
139
+ return this ;
140
+ }
141
+
142
+ public static String cleanPathPrefix (String pathPrefix ) {
138
143
Objects .requireNonNull (pathPrefix , "pathPrefix must not be null" );
139
144
140
145
if (pathPrefix .isEmpty ()) {
@@ -154,10 +159,7 @@ public RestClientBuilder setPathPrefix(String pathPrefix) {
154
159
throw new IllegalArgumentException ("pathPrefix is malformed. too many trailing slashes: [" + pathPrefix + "]" );
155
160
}
156
161
}
157
-
158
-
159
- this .pathPrefix = cleanPathPrefix ;
160
- return this ;
162
+ return cleanPathPrefix ;
161
163
}
162
164
163
165
/**
Original file line number Diff line number Diff line change @@ -236,7 +236,20 @@ public Iterator<Setting<?>> settings() {
236
236
*/
237
237
public static final Setting .AffixSetting <String > PROXY_BASE_PATH_SETTING =
238
238
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 ));
240
253
/**
241
254
* A boolean setting to enable or disable sniffing for extra connections.
242
255
*/
Original file line number Diff line number Diff line change @@ -302,6 +302,29 @@ public void testExporterWithHostOnly() throws Exception {
302
302
new HttpExporter (config , sslService , threadContext ).close ();
303
303
}
304
304
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
+
305
328
public void testCreateRestClient () throws IOException {
306
329
final SSLIOSessionStrategy sslStrategy = mock (SSLIOSessionStrategy .class );
307
330
You can’t perform that action at this time.
0 commit comments