Polish 'Use authParamString to configure Pulsar authentication'

See gh-38839
This commit is contained in:
Phillip Webb 2023-12-16 22:19:35 -08:00
parent 4c0a19e8c0
commit 2158f4cc43

View File

@ -73,23 +73,24 @@ final class PulsarPropertiesMapper {
private void customizeAuthentication(AuthenticationConsumer authentication, private void customizeAuthentication(AuthenticationConsumer authentication,
PulsarProperties.Authentication properties) { PulsarProperties.Authentication properties) {
if (!StringUtils.hasText(properties.getPluginClassName())) { if (StringUtils.hasText(properties.getPluginClassName())) {
return;
}
try {
// sort keys for testing and readability
Map<String, String> params = new TreeMap<>(properties.getParam());
String authParamString;
try { try {
authParamString = ObjectMapperFactory.create().writeValueAsString(params); authentication.accept(properties.getPluginClassName(),
getAuthenticationParamsJson(properties.getParam()));
} }
catch (Exception ex) { catch (UnsupportedAuthenticationException ex) {
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex); throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
} }
authentication.accept(properties.getPluginClassName(), authParamString);
} }
catch (UnsupportedAuthenticationException ex) { }
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
private String getAuthenticationParamsJson(Map<String, String> params) {
Map<String, String> sortedParams = new TreeMap<>(params);
try {
return ObjectMapperFactory.create().writeValueAsString(sortedParams);
}
catch (Exception ex) {
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
} }
} }