Google apis client library not working

I want to call Google Cloud APIs from Forge app by using @google-cloud/security-center library. But it is only working while using forge tunnel. The function is getting timed out when it is called from the deployed app.

Is the mentioned library supported in Forge?
What is the recommended way for calling Google APIs from Forge app?

manifest.yaml:

Hey Sagar,

Double check if you missing an await on an async call. The tunnel behaves slightly different to how a Forge Function works when deployed.

Cheers
Ben

Hi Benny,
I checked again and we are using await while making the async call. Below is the sample code snippet of how we are using the library. Still facing same issue.

import { SecurityCenterClient } from "@google-cloud/security-center";
import { GoogleSCCAPIException } from "../../exception/customException";
export const authenticate = async (credentials, orgId, googleSccUrl) => {
  try {
    const client = new SecurityCenterClient({
      credentials: {
        client_email: credentials.client_email,
        private_key: credentials.private_key,
      },
      apiEndpoint: googleSccUrl,
    });

    await client.listFindings(
      {
        parent: `organizations/${orgId}/sources/-`,
        pageSize: 1,
      },
      { autoPaginate: false }
    );
    return true;
  } catch (error) {
    const msg = `Error occurred in authentication. Reason: ${error.message}`;
    console.error(msg);
    throw new GoogleSCCAPIException(msg);
  }
};

Thanks for this, I cannot see anything obvious, do you have a trace ID for your failing request I could use to validate on my end.

Hi @SagarGujarati , currently the Forge Node runtime only supports network egress through either the inbuilt https module or global fetch (docs).
My assumption is that the @google-cloud/security-center library has it’s own network implementation which does not use https or global fetch. I would recommend having a close look at the library and seeing if there is a way for it to use either https, an https based client (e.g - node-fetch) or global fetch.

2 Likes

Hi @BoZhang I checked the @google-cloud/security-center library does not provide a built-in option to make calls using an HTTPS-based client like node-fetch or the global fetch API. The library is designed to use gRPC for communication with Google Cloud services and does not offer a direct way to switch to or use alternative HTTP clients.

Had a quick look, can you try pass in fallback as true in the options you provide for SecurityCenterClient?

i.e

    const client = new SecurityCenterClient({
       fallback: true
       ...
    });

reference: gax-nodejs/client-libraries.md at main · googleapis/gax-nodejs · GitHub

1 Like

I tried this and it is working now. Thanks for the suggestion.

1 Like