From 7bd4d2abdb75ce1b89d6f12e7796ba8930ff95e2 Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Fri, 15 Dec 2023 23:48:11 -0600 Subject: [PATCH 1/2] Use authParamString to configure Pulsar authentication The Pulsar authentication is currently configured using a map of auth parameters. However, this avenue is deprecated in Pulsar and instead needs to use an encoded auth parameters string. This commit simply takes the auth params map and converts them to the expected encoded json string of auth parameters. Resolves https://github.com/spring-projects/spring-pulsar/issues/500 --- .../pulsar/PulsarPropertiesMapper.java | 22 ++++++++++++++----- .../pulsar/PulsarPropertiesMapperTests.java | 6 +++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java index 04246d0e91c2..3022eb7b02f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java @@ -19,6 +19,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Map; +import java.util.TreeMap; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -29,6 +30,7 @@ import org.apache.pulsar.client.api.ProducerBuilder; import org.apache.pulsar.client.api.PulsarClientException.UnsupportedAuthenticationException; import org.apache.pulsar.client.api.ReaderBuilder; +import org.apache.pulsar.common.util.ObjectMapperFactory; import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.pulsar.listener.PulsarContainerProperties; @@ -71,13 +73,23 @@ void customizeAdminBuilder(PulsarAdminBuilder adminBuilder, PulsarConnectionDeta private void customizeAuthentication(AuthenticationConsumer authentication, PulsarProperties.Authentication properties) { - if (StringUtils.hasText(properties.getPluginClassName())) { + if (!StringUtils.hasText(properties.getPluginClassName())) { + return; + } + try { + // sort keys for testing and readability + Map params = new TreeMap<>(properties.getParam()); + String authParamString; try { - authentication.accept(properties.getPluginClassName(), properties.getParam()); + authParamString = ObjectMapperFactory.create().writeValueAsString(params); } - catch (UnsupportedAuthenticationException ex) { - throw new IllegalStateException("Unable to configure Pulsar authentication", ex); + catch (Exception ex) { + throw new IllegalStateException("Could not convert auth parameters to encoded string", ex); } + authentication.accept(properties.getPluginClassName(), authParamString); + } + catch (UnsupportedAuthenticationException ex) { + throw new IllegalStateException("Unable to configure Pulsar authentication", ex); } } @@ -158,7 +170,7 @@ private Consumer timeoutProperty(BiConsumer setter) private interface AuthenticationConsumer { - void accept(String authPluginClassName, Map authParams) + void accept(String authPluginClassName, String authParamString) throws UnsupportedAuthenticationException; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java index b168d4f71306..458b3abf480b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java @@ -73,12 +73,13 @@ void customizeClientBuilderWhenHasNoAuthentication() { void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException { PulsarProperties properties = new PulsarProperties(); Map params = Map.of("param", "name"); + String authParamString = "{\"param\":\"name\"}"; properties.getClient().getAuthentication().setPluginClassName("myclass"); properties.getClient().getAuthentication().setParam(params); ClientBuilder builder = mock(ClientBuilder.class); new PulsarPropertiesMapper(properties).customizeClientBuilder(builder, new PropertiesPulsarConnectionDetails(properties)); - then(builder).should().authentication("myclass", params); + then(builder).should().authentication("myclass", authParamString); } @Test @@ -112,12 +113,13 @@ void customizeAdminBuilderWhenHasNoAuthentication() { void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException { PulsarProperties properties = new PulsarProperties(); Map params = Map.of("param", "name"); + String authParamString = "{\"param\":\"name\"}"; properties.getAdmin().getAuthentication().setPluginClassName("myclass"); properties.getAdmin().getAuthentication().setParam(params); PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class); new PulsarPropertiesMapper(properties).customizeAdminBuilder(builder, new PropertiesPulsarConnectionDetails(properties)); - then(builder).should().authentication("myclass", params); + then(builder).should().authentication("myclass", authParamString); } @Test From cf49dfd7e59a69f86dc106f4fc9e41cd0bcd69ab Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Sat, 16 Dec 2023 09:45:37 -0600 Subject: [PATCH 2/2] Format fix --- .../boot/autoconfigure/pulsar/PulsarPropertiesMapper.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java index 3022eb7b02f2..4d5c1827f53f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java @@ -170,8 +170,7 @@ private Consumer timeoutProperty(BiConsumer setter) private interface AuthenticationConsumer { - void accept(String authPluginClassName, String authParamString) - throws UnsupportedAuthenticationException; + void accept(String authPluginClassName, String authParamString) throws UnsupportedAuthenticationException; }