Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode JSON string in Pulsar auth params #40493

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

onobc
Copy link
Contributor

@onobc onobc commented Apr 23, 2024

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.

Fixes #40492

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.

Fixes spring-projects#40492
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 23, 2024
@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 23, 2024
@philwebb philwebb added this to the 3.2.x milestone Apr 23, 2024
.collect(Collectors.joining(",", "{", "}"));
}
catch (Exception ex) {
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
}
}

private String escapeJson(String raw) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we do not directly bring in a JSON library (e.g. Gson or Jackson) we can not use the simple facilities that these libs have to "encode a JSON string".

Here were the options:

Use JSON lib

Add required dependency on Jackson or Gson and use their built-in methods

  • ✅ solid encode impl maintained by JSON lib
  • ❌ ❌ ❌ have to bring in direct dep on JSON lib and this forces it upon our users (not an option)

Add feature to Spring Boot JsonParser

Add "encode a JSON string" to our JsonParser abstraction

  • ✅ can be used in other areas of Spring Boot codebase
  • ❌ created and maintained by us (more work)
  • ❌ impl no more solid than if it were a private impl inside the mapper
  • ❌ nowhere else in the Boot codebase needs this AFAIK

Hand-roll basic encoder

Inline a simple encode JSON facility in the mapper.

  • ❌ basic impl maintained by us
  • ❌ ✅ not a perfect solution but will solve the majority of the cases
  • ✅ ✅ no direct dep on JSON lib
  • ✅ private impl localized to the mapper

@@ -81,8 +81,10 @@ void customizeClientBuilderWhenHasNoAuthentication() {
@Test
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
PulsarProperties properties = new PulsarProperties();
Map<String, String> params = Map.of("param", "name");
String authParamString = "{\"param\":\"name\"}";
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not exhausting all possible replacements characters but are covering the majority.

@philwebb should we add a bit more coverage here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PulsarPropertiesMapper.getAuthenticationParamsJson
3 participants