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

ReactiveSecurityContext is null if retrieved in RetrySpec.doBeforeRetryAsync since spring-boot 2.7.6 #33496

Closed
pascalgosselin opened this issue Dec 8, 2022 · 3 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@pascalgosselin
Copy link

pascalgosselin commented Dec 8, 2022

The following code was working with spring boot 2.7.5, but it does not work anymore with version 2.7.6.
ReactiveSecurityContextHolder.getContext() now returns empty.

@Component
public class BusinessClass {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private final ComponentClass componentClass;

  public BusinessClass(ComponentClass componentClass) {
    this.componentClass = componentClass;
  }

  public RetrySpec getRetrySpec() {
    return Retry.max(1)
        .doBeforeRetryAsync(
            retrySignal ->
                ReactiveSecurityContextHolder.getContext()
                    .doOnSuccess(
                        securityContext ->
                            LOGGER.info(
                                "The securityContext is null : " + Objects.isNull(securityContext)))
                    .flatMap(securityContext -> componentClass.doBeforeRetry()));
  }
}

@Component
public class ComponentClass {

  public Mono<String> getValue() {
    return Mono.just("String value");
  }

  public Mono<Void> doBeforeRetry() {
    return Mono.empty();
  }
}

You can test the problem with the following test class. The test succeed with version 2.7.5 but not with 2.7.6

@ExtendWith(MockitoExtension.class)
class BusinessClassTest {

  @Mock ComponentClass componentClass;
  @Mock SecurityContext securityContext;

  @InjectMocks BusinessClass businessClass;

  @Test
  @DisplayName("doBusinessLogic should call 'doBeforeRetry' before retry")
  void doBusinessLogic() {

    PublisherProbe<Void> doBeforeRetryProbe = PublisherProbe.empty();
    Context context = ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext));

    given(componentClass.getValue())
        .willReturn(Mono.error(new RuntimeException()))
        .willReturn(Mono.just("someValue"));
    given(componentClass.doBeforeRetry()).willReturn(doBeforeRetryProbe.mono());

    StepVerifier.create(
            Mono.defer(() -> componentClass.getValue())
                .retryWhen(businessClass.getRetrySpec())
                .contextWrite(context))
        .expectNext("someValue")
        .verifyComplete();

    doBeforeRetryProbe.assertWasSubscribed();
  }
}

I'm not sure if the problem is with spring-boot or project-reactor since project-reactor was updated in the new spring-boot version.
It could be this change: https://github.com/reactor/reactor-core/pull/3262/files#diff-a5639cea15f6765fecf7b44413831dc92cc0c8f45c1314ae8f4fb2959c60a623, but I'm really not sure.

Thanks in advance

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 8, 2022
@pascalgosselin
Copy link
Author

I was able to "correct" the problem with spring-boot 2.7.6 by downgrading reactor-core from 3.4.25 to 3.4.24. So I'm going to close this issue and open it in reactore-core

@scottfrederick scottfrederick added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 8, 2022
@scottfrederick
Copy link
Contributor

@pascalgosselin Thanks for letting us know. When you open the Reactor issue, you can add a comment here with a link to it in case others run into a similar problem.

@scottfrederick scottfrederick closed this as not planned Won't fix, can't repro, duplicate, stale Dec 8, 2022
@pascalgosselin
Copy link
Author

Here's the reactor-core issue: reactor/reactor-core#3314

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants