Skip to content

Commit b7f87a6

Browse files
authored
Validate monitoring username at parse time (#47821)
1 parent 4820361 commit b7f87a6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,47 @@ public Iterator<Setting<?>> settings() {
177177
*/
178178
public static final Setting.AffixSetting<String> AUTH_USERNAME_SETTING =
179179
Setting.affixKeySetting("xpack.monitoring.exporters.","auth.username",
180-
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope, Property.Filtered));
180+
(key) -> Setting.simpleString(
181+
key,
182+
new Setting.Validator<String>() {
183+
@Override
184+
public void validate(String password) {
185+
// no username validation that is independent of other settings
186+
}
187+
188+
@Override
189+
public void validate(String username, Map<Setting<?>, Object> settings) {
190+
final String namespace =
191+
HttpExporter.AUTH_USERNAME_SETTING.getNamespace(
192+
HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key));
193+
final String password =
194+
(String) settings.get(AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(namespace));
195+
196+
// password must be specified along with username for any auth
197+
if (Strings.isNullOrEmpty(username) == false) {
198+
if (Strings.isNullOrEmpty(password)) {
199+
throw new SettingsException(
200+
"[" + AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "] is set " +
201+
"but [" + AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "] is " +
202+
"missing");
203+
}
204+
}
205+
}
206+
207+
@Override
208+
public Iterator<Setting<?>> settings() {
209+
final String namespace =
210+
HttpExporter.AUTH_USERNAME_SETTING.getNamespace(
211+
HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key));
212+
final List<Setting<?>> settings = List.of(
213+
HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(namespace));
214+
return settings.iterator();
215+
}
216+
217+
},
218+
Property.Dynamic,
219+
Property.NodeScope,
220+
Property.Filtered));
181221
/**
182222
* Password for basic auth.
183223
*/

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,28 @@ public void testExporterWithPasswordButNoUsername() {
238238
assertThat(exception.getMessage(), equalTo(expected));
239239
}
240240

241+
public void testExporterWithUsernameButNoPassword() {
242+
final String expected =
243+
"[xpack.monitoring.exporters._http.auth.username] is set but [xpack.monitoring.exporters._http.auth.password] is missing";
244+
final String prefix = "xpack.monitoring.exporters._http";
245+
final Settings settings = Settings.builder()
246+
.put(prefix + ".type", HttpExporter.TYPE)
247+
.put(prefix + ".host", "localhost:9200")
248+
.put(prefix + ".auth.username", "_user")
249+
.build();
250+
251+
final IllegalArgumentException e = expectThrows(
252+
IllegalArgumentException.class,
253+
() -> HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(prefix + ".auth.username").get(settings));
254+
assertThat(
255+
e,
256+
hasToString(
257+
containsString("Failed to parse value for setting [xpack.monitoring.exporters._http.auth.username]")));
258+
259+
assertThat(e.getCause(), instanceOf(SettingsException.class));
260+
assertThat(e.getCause(), hasToString(containsString(expected)));
261+
}
262+
241263
public void testExporterWithUnknownBlacklistedClusterAlerts() {
242264
final SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class);
243265
when(sslService.sslIOSessionStrategy(any(Settings.class))).thenReturn(sslStrategy);

0 commit comments

Comments
 (0)