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

fix: check proxy and sap connectivity auth tokens for caching #5554

Draft
wants to merge 6 commits into
base: v3-main
Choose a base branch
from

Conversation

ZhongpinWang
Copy link
Contributor

@ZhongpinWang ZhongpinWang commented Feb 24, 2025

Closes SAP/cloud-sdk-backlog#1255.
Fix #5131

  • I know which base branch I chose for this PR, as the default branch is v3-main now, which is not for v4 development.
  • If my change will be merged into the main branch (for v4), I've updated (V4-Upgrade-Guide.md)[./V4-Upgrade-Guide.md] in case my change has any implications for users updating to SDK v4

Use the minimum of authentication token, Proxy-Authorization token, and SAP-Connectivity-Authentication token to determine the expiration time.

@ZhongpinWang ZhongpinWang changed the title fix: Check proxy auth and sap connectivity auth tokens for caching fix: check proxy and sap connectivity auth tokens for caching expiration Feb 24, 2025
@ZhongpinWang ZhongpinWang changed the title fix: check proxy and sap connectivity auth tokens for caching expiration fix: check proxy and sap connectivity auth tokens for caching Feb 24, 2025
@ZhongpinWang ZhongpinWang marked this pull request as ready for review February 24, 2025 06:25
Copy link
Member

@tomfrenken tomfrenken left a comment

Choose a reason for hiding this comment

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

LGTM, but adjust the changeset before merging 👍

destination.proxyConfiguration?.headers?.['Proxy-Authorization']?.match(
/^Bearer (.+)/
)?.[1];
const sapConnectivityAuthenticationToken =
Copy link
Contributor

@deekshas8 deekshas8 Feb 24, 2025

Choose a reason for hiding this comment

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

[req/q] I'm not sure if we should cache based on SAP-Connectivity-Authentication. This token is basically the subscriberToken?.userJwt.encoded, which is passed by the user (would fail the verify jwt step if its expired). We have no control on how this is fetched and refreshed by the user. So basing our caching on this seems wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

[pp] I would have preferred the approach to refresh only the proxy token if its expired. Its a CC grant token, whereas for fetching the whole destination again we make two additional calls to the destination service and the destination service itself might do additional exchange calls to the target system.
We're also mixing concerns between destinationCache and clientCredentialTokenCache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[req/q] I'm not sure if we should cache based on SAP-Connectivity-Authentication. This token is basically the subscriberToken?.userJwt.encoded, which is passed by the user (would fail the verify jwt step if its expired). We have no control on how this is fetched and refreshed by the user. So basing our caching on this seems wrong.

I understand your concern. I don't have strong opinion on this. Only from a pure technical point of view, users are allowed to set destination cache with setDestinationCache() method. And both headers are defined as part of the Destination / ProxyConfigurationHeaders interface. Technically, it means we also can't be sure, in the end, how user sets SAP-Connectivity-Authentication token. Therefore, I would say we should consider both headers when calculating the expiration time.

If exp of SAP-Connectivity-Authentication token is shorter than the authTokens for destination service and got expired, the Destination object itself is anyway invalid.

[pp] I would have preferred the approach to refresh only the proxy token if its expired. Its a CC grant token, whereas for fetching the whole destination again we make two additional calls to the destination service and the destination service itself might do additional exchange calls to the target system.
We're also mixing concerns between destinationCache and clientCredentialTokenCache.

Thanks for this cool suggestion, and ideally yes, we should try renew the proxy auth token. And I tried out.

We can add a check when retrieveDestinationFromCache. But since we store only expires time in a CacheEntry, which is calculated based on the first authTokens expiresIn, it lacks expiration time for proxy authentication (and, if applicable, sap connectivity authentication). We will have to modify CacheEntry interface to store this additional information. But I feel CacheEntry is designed rather in a generic way, and adding information regarding proxy is not appropriate.

So by cache design, we anyway need to determine a expiration time, which should invalidate the cache if its entry cannot be used anymore. Judging from a cached Destination entry, we have no clue, if the proxy authentication token has expired or not, since it only provides us a time interval and we don't know the start time when retrieving the destination.

Based on the above consideration, I would accept, at this moment, the fact that we may make few additional calls to Destination service. Unless in general, beyond the caching concept, we always renew the proxy auth token if it got expired.

Copy link
Contributor

@deekshas8 deekshas8 Feb 25, 2025

Choose a reason for hiding this comment

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

Can you try what happens when we move the below line before we return the destination? I think this will automatically refresh the proxy auth and the other token in case they expired.

const withProxySetting = await da.addProxyConfiguration(destination);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please have a look at #5563.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also this PR is not totally correct as exp of the token is actually seconds after epoch, not expires in.

@ZhongpinWang ZhongpinWang marked this pull request as draft February 25, 2025 15:11
@ZhongpinWang ZhongpinWang added the don't merge Don't merge label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expired destinations through connectivity proxy remain in cache
3 participants