Skip to content

Commit 9159af5

Browse files
authored
Validate monitoring password at parse time (#47740)
1 parent 0f2c1e8 commit 9159af5

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,45 @@ public Iterator<Setting<?>> settings() {
223223
*/
224224
public static final Setting.AffixSetting<String> AUTH_PASSWORD_SETTING =
225225
Setting.affixKeySetting("xpack.monitoring.exporters.","auth.password",
226-
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope, Property.Filtered));
226+
(key) -> Setting.simpleString(key,
227+
new Setting.Validator<String>() {
228+
@Override
229+
public void validate(String password) {
230+
// no password validation that is independent of other settings
231+
}
232+
233+
@Override
234+
public void validate(String password, Map<Setting<?>, Object> settings) {
235+
final String namespace =
236+
HttpExporter.AUTH_PASSWORD_SETTING.getNamespace(
237+
HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSetting(key));
238+
final String username =
239+
(String) settings.get(AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace));
240+
241+
// username is required for any auth
242+
if (Strings.isNullOrEmpty(username)) {
243+
if (Strings.isNullOrEmpty(password) == false) {
244+
throw new IllegalArgumentException(
245+
"[" + AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "] without [" +
246+
AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "]");
247+
}
248+
}
249+
}
250+
251+
@Override
252+
public Iterator<Setting<?>> settings() {
253+
final String namespace =
254+
HttpExporter.AUTH_PASSWORD_SETTING.getNamespace(
255+
HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSetting(key));
256+
final List<Setting<?>> settings = List.of(
257+
HttpExporter.AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace));
258+
return settings.iterator();
259+
}
260+
261+
},
262+
Property.Dynamic,
263+
Property.NodeScope,
264+
Property.Filtered));
227265
/**
228266
* The SSL settings.
229267
*
@@ -634,17 +672,6 @@ private static CredentialsProvider createCredentialsProvider(final Config config
634672
final String username = AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings());
635673
final String password = AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings());
636674

637-
// username is required for any auth
638-
if (Strings.isNullOrEmpty(username)) {
639-
if (Strings.isNullOrEmpty(password) == false) {
640-
throw new SettingsException(
641-
"[" + AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + "] without [" +
642-
AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + "]");
643-
}
644-
// nothing to configure; default situation for most users
645-
return null;
646-
}
647-
648675
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
649676
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
650677

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,17 @@ public void testExporterWithEmptyHeaders() {
225225
public void testExporterWithPasswordButNoUsername() {
226226
final String expected =
227227
"[xpack.monitoring.exporters._http.auth.password] without [xpack.monitoring.exporters._http.auth.username]";
228-
final Settings.Builder builder = Settings.builder()
229-
.put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE)
230-
.put("xpack.monitoring.exporters._http.host", "localhost:9200")
231-
.put("xpack.monitoring.exporters._http.auth.password", "_pass");
232-
233-
final Config config = createConfig(builder.build());
234-
235-
final SettingsException exception = expectThrows(SettingsException.class,
236-
() -> new HttpExporter(config, sslService, threadContext));
228+
final String prefix = "xpack.monitoring.exporters._http";
229+
final Settings settings = Settings.builder()
230+
.put(prefix + ".type", HttpExporter.TYPE)
231+
.put(prefix + ".host", "localhost:9200")
232+
.put(prefix + ".auth.password", "_pass")
233+
.build();
237234

238-
assertThat(exception.getMessage(), equalTo(expected));
235+
final IllegalArgumentException e = expectThrows(
236+
IllegalArgumentException.class,
237+
() -> HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSetting(prefix + ".auth.password").get(settings));
238+
assertThat(e, hasToString(containsString(expected)));
239239
}
240240

241241
public void testExporterWithUsernameButNoPassword() {

0 commit comments

Comments
 (0)