Skip to content

Commit 7a7bcd0

Browse files
onobcmhalbritter
authored andcommitted
Encode JSON string in Pulsar auth params
The values in the `spring.pulsar.client.authentication.param` config props map are not currently JSON encoded. For simple values this is fine. However, some custom auth modules may require more complex parameter values that may contain special characters that results in invalid JSON. This commmit encodes the parameter values using a very simple hand-rolled escape function. See gh-40493
1 parent d340f87 commit 7a7bcd0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,25 @@ private String getAuthenticationParamsJson(Map<String, String> params) {
8989
try {
9090
return sortedParams.entrySet()
9191
.stream()
92-
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), entry.getValue()))
92+
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
9393
.collect(Collectors.joining(",", "{", "}"));
9494
}
9595
catch (Exception ex) {
9696
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
9797
}
9898
}
9999

100+
private String escapeJson(String raw) {
101+
return raw.replace("\\", "\\\\")
102+
.replace("\"", "\\\"")
103+
.replace("/", "\\/")
104+
.replace("\b", "\\b")
105+
.replace("\t", "\\t")
106+
.replace("\n", "\\n")
107+
.replace("\f", "\\f")
108+
.replace("\r", "\\r");
109+
}
110+
100111
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
101112
PulsarProperties.Producer properties = this.properties.getProducer();
102113
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ void customizeClientBuilderWhenHasNoAuthentication() {
7272
@Test
7373
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
7474
PulsarProperties properties = new PulsarProperties();
75-
Map<String, String> params = Map.of("param", "name");
76-
String authParamString = "{\"param\":\"name\"}";
75+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
76+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
77+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
78+
+ ",\"simpleParam\":\"foo\"}";
7779
properties.getClient().getAuthentication().setPluginClassName("myclass");
7880
properties.getClient().getAuthentication().setParam(params);
7981
ClientBuilder builder = mock(ClientBuilder.class);
@@ -112,8 +114,10 @@ void customizeAdminBuilderWhenHasNoAuthentication() {
112114
@Test
113115
void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
114116
PulsarProperties properties = new PulsarProperties();
115-
Map<String, String> params = Map.of("param", "name");
116-
String authParamString = "{\"param\":\"name\"}";
117+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
118+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
119+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
120+
+ ",\"simpleParam\":\"foo\"}";
117121
properties.getAdmin().getAuthentication().setPluginClassName("myclass");
118122
properties.getAdmin().getAuthentication().setParam(params);
119123
PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);

0 commit comments

Comments
 (0)