Skip to content

Commit

Permalink
fix: Fix reading correct tenant for destination caching (#4941)
Browse files Browse the repository at this point in the history
  • Loading branch information
marikaner authored Aug 27, 2024
1 parent 2e88af5 commit 39eb88c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-candles-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/connectivity': patch
---

[Fixed Issue] Throw an error if a JWT for caching was provided, but doesn't contain tenant information.
5 changes: 5 additions & 0 deletions .changeset/selfish-cougars-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/connectivity': patch
---

[Fixed Issue] Derive tenant information for destination caching also for IAS tokens.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`register-destination without XSUAA binding throws an error if there is a JWT, but no tenant ID could be identified 1`] = `"Could not determine tenant from JWT nor XSUAA, identity or destination service binding. Destination is registered without tenant information."`;
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,28 @@ describe('register-destination', () => {
).toEqual(testDestination);
});

it('registers destination with a dummy ID, if there is a JWT, but no tenant ID could be identified', async () => {
const logger = createLogger('register-destination');
jest.spyOn(logger, 'error');
it('registers destination and retrieves it with IAS JWT', async () => {
const iasJwt = signedJwt({ app_tid: 'test', iat: 123 });
await registerDestination(testDestination, { jwt: iasJwt });
expect(
await searchRegisteredDestination({
destinationName: testDestination.name,
jwt: iasJwt
})
).toEqual(testDestination);
});

it('throws an error if there is a JWT, but no tenant ID could be identified', async () => {
const dummyTenantId = 'provider-tenant';

await registerDestination(testDestination, { jwt: signedJwt({}) });
expect(
registerDestination(testDestination, { jwt: signedJwt({}) })
).rejects.toThrowErrorMatchingSnapshot();
const registeredDestination = await registerDestinationCache.destination
.getCacheInstance()
.get(`${dummyTenantId}::${testDestination.name}`);

expect(registeredDestination).toEqual(testDestination);

expect(logger.error).toHaveBeenCalledWith(
'Could not determine tenant from JWT nor XSUAA, identity or destination service binding. Destination is registered without tenant information.'
);
expect(registeredDestination).toEqual(undefined);
});

it('registers destination with a dummy ID, if there is no JWT and no tenant ID can be identified', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createLogger } from '@sap-cloud-sdk/util';
import { decodeJwt, decodeOrMakeJwt, defaultTenantId } from '../jwt';
import {
decodeJwt,
decodeOrMakeJwt,
defaultTenantId,
getTenantId
} from '../jwt';
import { DestinationFetchOptions } from './destination-accessor-types';
import {
IsolationStrategy,
Expand Down Expand Up @@ -58,9 +63,9 @@ export async function registerDestination(

function getJwtForCaching(options: RegisterDestinationOptions | undefined) {
const jwt = decodeOrMakeJwt(options?.jwt);
if (!jwt?.zid) {
if (!getTenantId(jwt)) {
if (options?.jwt) {
logger.error(
throw Error(
'Could not determine tenant from JWT nor XSUAA, identity or destination service binding. Destination is registered without tenant information.'
);
} else {
Expand Down

0 comments on commit 39eb88c

Please sign in to comment.