diff --git a/.changeset/calm-candles-lie.md b/.changeset/calm-candles-lie.md new file mode 100644 index 0000000000..e2fa4e8e3f --- /dev/null +++ b/.changeset/calm-candles-lie.md @@ -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. diff --git a/.changeset/selfish-cougars-shout.md b/.changeset/selfish-cougars-shout.md new file mode 100644 index 0000000000..a3ab272767 --- /dev/null +++ b/.changeset/selfish-cougars-shout.md @@ -0,0 +1,5 @@ +--- +'@sap-cloud-sdk/connectivity': patch +--- + +[Fixed Issue] Derive tenant information for destination caching also for IAS tokens. diff --git a/packages/connectivity/src/scp-cf/destination/__snapshots__/destination-from-registration.spec.ts.snap b/packages/connectivity/src/scp-cf/destination/__snapshots__/destination-from-registration.spec.ts.snap new file mode 100644 index 0000000000..c70d11ef65 --- /dev/null +++ b/packages/connectivity/src/scp-cf/destination/__snapshots__/destination-from-registration.spec.ts.snap @@ -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."`; diff --git a/packages/connectivity/src/scp-cf/destination/destination-from-registration.spec.ts b/packages/connectivity/src/scp-cf/destination/destination-from-registration.spec.ts index 18833cfb5a..b7e9b6d713 100644 --- a/packages/connectivity/src/scp-cf/destination/destination-from-registration.spec.ts +++ b/packages/connectivity/src/scp-cf/destination/destination-from-registration.spec.ts @@ -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 () => { diff --git a/packages/connectivity/src/scp-cf/destination/destination-from-registration.ts b/packages/connectivity/src/scp-cf/destination/destination-from-registration.ts index 797d1a2619..ecc9289844 100644 --- a/packages/connectivity/src/scp-cf/destination/destination-from-registration.ts +++ b/packages/connectivity/src/scp-cf/destination/destination-from-registration.ts @@ -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, @@ -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 {