Skip to content

Commit ddd394a

Browse files
committed
Validate monitoring password at parse time (elastic#47740)
1 parent c46a0e8 commit ddd394a

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
@@ -222,7 +222,45 @@ public Iterator<Setting<?>> settings() {
222222
*/
223223
public static final Setting.AffixSetting<String> AUTH_PASSWORD_SETTING =
224224
Setting.affixKeySetting("xpack.monitoring.exporters.","auth.password",
225-
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope, Property.Filtered));
225+
(key) -> Setting.simpleString(key,
226+
new Setting.Validator<String>() {
227+
@Override
228+
public void validate(String password) {
229+
// no password validation that is independent of other settings
230+
}
231+
232+
@Override
233+
public void validate(String password, Map<Setting<?>, Object> settings) {
234+
final String namespace =
235+
HttpExporter.AUTH_PASSWORD_SETTING.getNamespace(
236+
HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSetting(key));
237+
final String username =
238+
(String) settings.get(AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace));
239+
240+
// username is required for any auth
241+
if (Strings.isNullOrEmpty(username)) {
242+
if (Strings.isNullOrEmpty(password) == false) {
243+
throw new IllegalArgumentException(
244+
"[" + AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "] without [" +
245+
AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace).getKey() + "]");
246+
}
247+
}
248+
}
249+
250+
@Override
251+
public Iterator<Setting<?>> settings() {
252+
final String namespace =
253+
HttpExporter.AUTH_PASSWORD_SETTING.getNamespace(
254+
HttpExporter.AUTH_PASSWORD_SETTING.getConcreteSetting(key));
255+
final List<Setting<?>> settings = List.of(
256+
HttpExporter.AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(namespace));
257+
return settings.iterator();
258+
}
259+
260+
},
261+
Property.Dynamic,
262+
Property.NodeScope,
263+
Property.Filtered));
226264
/**
227265
* The SSL settings.
228266
*
@@ -626,17 +664,6 @@ private static CredentialsProvider createCredentialsProvider(final Config config
626664
final String username = AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings());
627665
final String password = AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings());
628666

629-
// username is required for any auth
630-
if (Strings.isNullOrEmpty(username)) {
631-
if (Strings.isNullOrEmpty(password) == false) {
632-
throw new SettingsException(
633-
"[" + AUTH_PASSWORD_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + "] without [" +
634-
AUTH_USERNAME_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + "]");
635-
}
636-
// nothing to configure; default situation for most users
637-
return null;
638-
}
639-
640667
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
641668
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
642669

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
@@ -226,17 +226,17 @@ public void testExporterWithEmptyHeaders() {
226226
public void testExporterWithPasswordButNoUsername() {
227227
final String expected =
228228
"[xpack.monitoring.exporters._http.auth.password] without [xpack.monitoring.exporters._http.auth.username]";
229-
final Settings.Builder builder = Settings.builder()
230-
.put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE)
231-
.put("xpack.monitoring.exporters._http.host", "localhost:9200")
232-
.put("xpack.monitoring.exporters._http.auth.password", "_pass");
233-
234-
final Config config = createConfig(builder.build());
235-
236-
final SettingsException exception = expectThrows(SettingsException.class,
237-
() -> new HttpExporter(config, sslService, threadContext));
229+
final String prefix = "xpack.monitoring.exporters._http";
230+
final Settings settings = Settings.builder()
231+
.put(prefix + ".type", HttpExporter.TYPE)
232+
.put(prefix + ".host", "localhost:9200")
233+
.put(prefix + ".auth.password", "_pass")
234+
.build();
238235

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

242242
public void testExporterWithUsernameButNoPassword() {

0 commit comments

Comments
 (0)