0

We have a SpringBoot application based on the Sap Cloud SDK (3.32.0) and are using PrincipalPropegation to our on-prem SAP environment.

Our application is also using the Axon Framework (an eventsourcing framework). This means our calls to our RestControllers are send as commands to the Aggregates, which in turn sends out events on the eventbus. Normally we pass the oauth token by adding metadata on the event messages. This is handled by the axon framework. Events are dispatched on different threads then the ones that process the commands.

However, we recently started using the cloud sdk and generated OData V2 clients to send/retrieve information to our on-prem SAP instances. The SAP cloud SDK tries to fetch the AuthToken from the ThreadContext, however, due to the async nature of the Axon framework, this does not work properly.

Is there a way pass the correct token in some other way and skip the default behaviour of the SDK? Since we have the token needed for doing the user token exchange for PrincipalPropegation in the event metadata (which can be accessed by the eventhandler).

Any suggestions would be great!

Danny

  • Could you please add a few more details about how you are using the API? Without that it's hard to provide more specific suggestions – MatKuhr Nov 23 '20 at 10:07

2 Answers2

1

You can conveniently propagate the thread context to new threads using the ThreadContextExecutor:

ThreadContextExecutor executor = new ThreadContextExecutor();
Callable operationWithContext = () -> executor.execute(() -> operation());

invokeAsynchronously(operationWithContext);

Check out the documentation on the topic.

MatKuhr
  • 505
  • 4
  • 13
  • This does not work in combination with the Axon Framework. All threading and thread handovers are handled by it. – Danny Kruitbosch Nov 13 '20 at 14:37
  • Okay, could you maybe sketch a bit how you are using the framework and how you would like to pass on the information? Then I'll try to expand this answer with how the Cloud SDK can play into that. – MatKuhr Nov 13 '20 at 14:50
0

Is there a way pass the correct token in some other way and skip the default behaviour of the SDK?

In case the solution with ThreadContextExecutor is not working for you, we can look for a workaround: If you are looking for a way to pass an access token inside the child thread, then use the following code sample:

import com.sap.cloud.sdk.cloudplatform.security.AuthTokenAccessor;
import com.sap.cloud.sdk.cloudplatform.security.AuthToken;

DecodedJWT jwt = JWT.decode("your-access-token");
AuthToken authToken = new AuthToken(jwt);
AuthTokenAccessor.executeWithAuthToken(authToken, () -> {
 // do things..
});

Please note: Besides current auth-token, the Cloud SDK may also extract principal and tenant information from the passed JWT.

Alexander Dümont
  • 903
  • 1
  • 5
  • 9