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

Batch Csrf token error #617

Closed
d-sooter opened this issue Oct 23, 2020 · 10 comments
Closed

Batch Csrf token error #617

d-sooter opened this issue Oct 23, 2020 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@d-sooter
Copy link

We are facing issues using the cloud sdk to access an on premise system through the cloud connector.

The first request (to fetch the x-csrf-token) returns without a body and causes an error and the actual post never happens.

If I fetch the token myself I am able to pass it in the batch as a header.

The get request needs to be able to handle an empty body.

an easy way to reproduce it is to use
executeHttpRequest() and only pass the odata service (without an entity in the url)

a simple get request should fail.
kind regards

David

@d-sooter d-sooter added the bug Something isn't working label Oct 23, 2020
@artemkovalyov
Copy link
Member

Hi @d-sooter ,

We'll try to reproduce it on our side and get back to you ASAP. Can you share the code snippet that fails and the SDK version you use?

cc @marikaner

Best,
Artem

@marikaner
Copy link
Contributor

marikaner commented Oct 27, 2020

Hey @d-sooter ,

do you mean that there is an internal request that fails without body or are you executing it on your own? An actual error message would be very helpful to understand what is going wrong.

@d-sooter
Copy link
Author

d-sooter commented Oct 28, 2020

Hi Guys sorry i was not able to answer sooner.
Ill try and document it all here. i Hope it helps

Enviroment: SAP Business application Studio
App: Nodejs app
Target System: Sap S4/Hana
Requests are passes through a cloud connector using the build in proxy and a destination service

OData service version: V2
OData service: API_BUSINESSPARTNER

Coding:
` public async findAllByIdsBatch(idList: string[], destination?: string): Promise<BusinessPartnerType[]> {

    try {
        const retrieveRequests = idList.map(id =>
            BusinessPartner
                .requestBuilder()
                .getByKey(id));

        const retrieveResponses = await batch(...retrieveRequests)
            .execute({ destinationName: destination || 'SapBackend', jwt });

        return retrieveResponses.reduce(
            (bp, response: ReadResponse) =>
                [...bp, response.as(BusinessPartner)]
            , []
        );

    } catch (error) {
        console.log(error)
    }

`

Error Stack
"Error: Batch request failed! at Object.errorWithCause (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20) at /home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/v2/request-builder/batch-request-builder.js:87:54 at process._tickCallback (internal/process/next_tick.js:68:7) Caused by: Error: Constructing headers for OData request failed! at Object.errorWithCause (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20) at ODataRequest.<anonymous> (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:203:69) at step (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:44:23) at Object.throw (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:25:53) at rejected (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:17:65) at process._tickCallback (internal/process/next_tick.js:68:7) Caused by: Error: The error response is undefined. at /home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/header-builder/csrf-token-header.js:87:19 at process._tickCallback (internal/process/next_tick.js:68:7)"

As far as i can tell it happens because the Get request to retrieve the x-csrf-token returns an empty body as the call is not made to any entity set but rather the entity root itself. With in itself doesnt return a body. This causes the request to return an empty body and breaks the processing.

I hope this helps if you need anything else let me know.

@marikaner
Copy link
Contributor

marikaner commented Oct 28, 2020

Hey @d-sooter,

there should be another "Caused by" following the logs that you sent. Can you check this as well? It could give us some insight why this is failing. I think the problem is not that we do not handle empty responses, but the CSRF request that we make has no headers.

You are right, we are executing the CSRF fetch against the service url (e. g. /API_BUSINESSPARTNER_SRV). You mentioned that getting the CSRF token from the entity set works when you do it manually. I assume you tried it against /API_BUSINESSPARTNER_SRV/A_BusinessPartner. Can you check whether it also works for the service url?

@d-sooter
Copy link
Author

d-sooter commented Oct 28, 2020

Hey @marikaner ,

getting the CSRF token manually works. I was able to do it when calling the service url.

i think in general fetching a token again the service url is not wrong. But the framework seems to have issues when the body is empty. ( as far as i can tell)

here is the stack once again
"Error: Batch request failed! at Object.errorWithCause (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20) at /home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/v2/request-builder/batch-request-builder.js:87:54 at process._tickCallback (internal/process/next_tick.js:68:7) Caused by: Error: Constructing headers for OData request failed! at Object.errorWithCause (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20) at ODataRequest.<anonymous> (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:203:69) at step (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:44:23) at Object.throw (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:25:53) at rejected (/home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:17:65) at process._tickCallback (internal/process/next_tick.js:68:7) Caused by: Error: The error response is undefined. at /home/user/projects/doppstadt-migration/app/doppstadt-data-migrator/node_modules/@sap-cloud-sdk/core/dist/header-builder/csrf-token-header.js:87:19 at process._tickCallback (internal/process/next_tick.js:68:7)

thats all im getting in the catch.
Anothing thing with the batch is that it seems like the same connector has to be used in order for the token to be valid in the sap backend.

if i get the token myself and pass it as a header then the system says its invalid.

Kind regards

David

@marikaner
Copy link
Contributor

Thanks David,

we are investigating this. The log statement that I was looking for cannot be present in your logs, because we only added it last week and it is not yet released. I missed that.

@marikaner
Copy link
Contributor

Hey @d-sooter,

I think I am getting closer to the problem. Do you have a possibility to try fetching the CSRF token from /API_BUSINESSPARTNER_SRV or /API_BUSINESSPARTNER_SRV/ differs?

@marikaner
Copy link
Contributor

Hey @d-sooter,

Good news! I found the issue - it is related to an issue in the underlying library that executes the request (axios/axios#3369). However, I found a workaround. I will inform you once it is done.

@marikaner
Copy link
Contributor

Hey @d-sooter,

this issue has been fixed with version 1.31.0. Let us know if you still encounter any issues.

@marikaner marikaner self-assigned this Nov 5, 2020
@d-sooter
Copy link
Author

d-sooter commented Nov 5, 2020

Thank you very much ill try it out.

Kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants