Skip to content

Commit 4c0a19e

Browse files
onobcphilwebb
authored andcommitted
Use authParamString to configure Pulsar authentication
Update `PulsarPropertiesMapper` to use JSON encoded parameters rather than a `Map` since the `Map` method is deprecated in Pulsar. This commit simply takes the auth params map and converts them to the expected encoded JSON string of auth parameters. See gh-38839
1 parent 6ae113c commit 4c0a19e

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.ArrayList;
2121
import java.util.Map;
22+
import java.util.TreeMap;
2223
import java.util.concurrent.TimeUnit;
2324
import java.util.function.BiConsumer;
2425
import java.util.function.Consumer;
@@ -29,6 +30,7 @@
2930
import org.apache.pulsar.client.api.ProducerBuilder;
3031
import org.apache.pulsar.client.api.PulsarClientException.UnsupportedAuthenticationException;
3132
import org.apache.pulsar.client.api.ReaderBuilder;
33+
import org.apache.pulsar.common.util.ObjectMapperFactory;
3234

3335
import org.springframework.boot.context.properties.PropertyMapper;
3436
import org.springframework.pulsar.listener.PulsarContainerProperties;
@@ -71,13 +73,23 @@ void customizeAdminBuilder(PulsarAdminBuilder adminBuilder, PulsarConnectionDeta
7173

7274
private void customizeAuthentication(AuthenticationConsumer authentication,
7375
PulsarProperties.Authentication properties) {
74-
if (StringUtils.hasText(properties.getPluginClassName())) {
76+
if (!StringUtils.hasText(properties.getPluginClassName())) {
77+
return;
78+
}
79+
try {
80+
// sort keys for testing and readability
81+
Map<String, String> params = new TreeMap<>(properties.getParam());
82+
String authParamString;
7583
try {
76-
authentication.accept(properties.getPluginClassName(), properties.getParam());
84+
authParamString = ObjectMapperFactory.create().writeValueAsString(params);
7785
}
78-
catch (UnsupportedAuthenticationException ex) {
79-
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
86+
catch (Exception ex) {
87+
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
8088
}
89+
authentication.accept(properties.getPluginClassName(), authParamString);
90+
}
91+
catch (UnsupportedAuthenticationException ex) {
92+
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
8193
}
8294
}
8395

@@ -158,8 +170,7 @@ private Consumer<Duration> timeoutProperty(BiConsumer<Integer, TimeUnit> setter)
158170

159171
private interface AuthenticationConsumer {
160172

161-
void accept(String authPluginClassName, Map<String, String> authParams)
162-
throws UnsupportedAuthenticationException;
173+
void accept(String authPluginClassName, String authParamString) throws UnsupportedAuthenticationException;
163174

164175
}
165176

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ void customizeClientBuilderWhenHasNoAuthentication() {
7373
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
7474
PulsarProperties properties = new PulsarProperties();
7575
Map<String, String> params = Map.of("param", "name");
76+
String authParamString = "{\"param\":\"name\"}";
7677
properties.getClient().getAuthentication().setPluginClassName("myclass");
7778
properties.getClient().getAuthentication().setParam(params);
7879
ClientBuilder builder = mock(ClientBuilder.class);
7980
new PulsarPropertiesMapper(properties).customizeClientBuilder(builder,
8081
new PropertiesPulsarConnectionDetails(properties));
81-
then(builder).should().authentication("myclass", params);
82+
then(builder).should().authentication("myclass", authParamString);
8283
}
8384

8485
@Test
@@ -112,12 +113,13 @@ void customizeAdminBuilderWhenHasNoAuthentication() {
112113
void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
113114
PulsarProperties properties = new PulsarProperties();
114115
Map<String, String> params = Map.of("param", "name");
116+
String authParamString = "{\"param\":\"name\"}";
115117
properties.getAdmin().getAuthentication().setPluginClassName("myclass");
116118
properties.getAdmin().getAuthentication().setParam(params);
117119
PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);
118120
new PulsarPropertiesMapper(properties).customizeAdminBuilder(builder,
119121
new PropertiesPulsarConnectionDetails(properties));
120-
then(builder).should().authentication("myclass", params);
122+
then(builder).should().authentication("myclass", authParamString);
121123
}
122124

123125
@Test

0 commit comments

Comments
 (0)