Skip to content

Commit 3d42dc7

Browse files
committed
Merge pull request #38839 from onobc
* pr/38839: Polish 'Use authParamString to configure Pulsar authentication' Use authParamString to configure Pulsar authentication Closes gh-38839
2 parents 6ae113c + 2158f4c commit 3d42dc7

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 15 additions & 3 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;
@@ -73,14 +75,25 @@ private void customizeAuthentication(AuthenticationConsumer authentication,
7375
PulsarProperties.Authentication properties) {
7476
if (StringUtils.hasText(properties.getPluginClassName())) {
7577
try {
76-
authentication.accept(properties.getPluginClassName(), properties.getParam());
78+
authentication.accept(properties.getPluginClassName(),
79+
getAuthenticationParamsJson(properties.getParam()));
7780
}
7881
catch (UnsupportedAuthenticationException ex) {
7982
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
8083
}
8184
}
8285
}
8386

87+
private String getAuthenticationParamsJson(Map<String, String> params) {
88+
Map<String, String> sortedParams = new TreeMap<>(params);
89+
try {
90+
return ObjectMapperFactory.create().writeValueAsString(sortedParams);
91+
}
92+
catch (Exception ex) {
93+
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
94+
}
95+
}
96+
8497
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
8598
PulsarProperties.Producer properties = this.properties.getProducer();
8699
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
@@ -158,8 +171,7 @@ private Consumer<Duration> timeoutProperty(BiConsumer<Integer, TimeUnit> setter)
158171

159172
private interface AuthenticationConsumer {
160173

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

164176
}
165177

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)