SAP / cloud-sdk-js

Use the SAP Cloud SDK for JavaScript / TypeScript to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
167 stars 57 forks source link

Error in @sap-cloud-sdk/connectivity version 3.18.0 #5026

Open BastiInnovation opened 1 month ago

BastiInnovation commented 1 month ago

Describe the bug After updating @sap-cloud-sdk/connectivity from version 3.17.0 to 3.18.0. The destination from getDestinationFromDestinationService() is null. In cloud foundry log i received a warning: "Cannot create cache key for client credentials token cache. The given tenant id is undefined".

To Reproduce

const getDestinationFromDestinationService = require('@sap-cloud-sdk/connectivity').getDestinationFromDestinationService;
const serviceToken = require('@sap-cloud-sdk/connectivity').serviceToken;
const getXsuaaServiceCredentials = require('@sap-cloud-sdk/connectivity/dist/scp-cf').getXsuaaServiceCredentials;
const getDestinationServiceCredentials = require('@sap-cloud-sdk/connectivity/dist/scp-cf').getDestinationServiceCredentials;
const requests = require('@sap/xssec').requests;

static async getDestination(tenant){
    return await getDestinationFromDestinationService({ 
        destinationName: 'myDestinationName',
        jwt: await this.fetchToken(tenant),
        iasToXsuaaTokenExchange: false });
  }

static async fetchToken(tenant){
    let token;
    if( getXsuaaServiceCredentials() ) {
      token = await serviceToken('xsuaa', { jwt: { iss: `https://${tenant}.localhost:8080/uaa/oauth/token` }, useCache: true });
    } else {
      token = await new Promise((resolve) => requests.requestClientCredentialsToken(tenant, getDestinationServiceCredentials(), null, (_err, tkn) => resolve(tkn)));
    }
    return token;
  }

Expected behavior I expected to receive the destination from getDestination() because the token from fetchToken() is the right one. I think there is a relation to 4799, but I found non solution in the related documentation.

Used Versions: node version: v20.11.0 sap-cloud-sdk/connectivity: v3.18.0 sap-cloud-sdk/util: v3.18.0 sap/cds: v7.9.4 sap/xssec: v3.6.1

tomfrenken commented 5 days ago

Hi @BastiInnovation, I agree that the issue you are facing is likely caused by the referenced PR, however, after reviewing our code I do not see any change that would lead to your issue.

The warning you are receiving indicates that the tenant look-up is failing. Since you provide a jwt in your call we check for 3 different properties: zid, app_tid, ext_atr.zdn, and iss in that order.

The only difference is that we now check for the zdn property ...

Could you debug what your JWT contains and perhaps share a redacted version with us?