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 @@ -197,7 +197,20 @@ public Iterator<Setting<?>> settings() {
197
197
*/
198
198
public static final Setting .AffixSetting <String > PROXY_BASE_PATH_SETTING =
199
199
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 ));
201
214
/**
202
215
* A boolean setting to enable or disable sniffing for extra connections.
203
216
*/
Original file line number Diff line number Diff line change @@ -279,6 +279,29 @@ public void testExporterWithHostOnly() throws Exception {
279
279
new HttpExporter (config , sslService , threadContext ).close ();
280
280
}
281
281
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
+
282
305
public void testCreateRestClient () throws IOException {
283
306
final SSLIOSessionStrategy sslStrategy = mock (SSLIOSessionStrategy .class );
284
307
You can’t perform that action at this time.
0 commit comments