UNPKG

genkitx-cloud-sql-pg

Version:

Genkit AI framework plugin for Cloud SQL for PostgreSQL.

1 lines 2.5 kB
{"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GoogleAuth } from 'google-auth-library';\n\n/**\n * Get email address associated with current authenticated IAM principal.\n * Email will be used for automatic IAM database authentication to Cloud SQL.\n *\n * @param {GoogleAuth} auth - object to use in finding the associated IAM principal email address.\n * @returns {string} email - email address associated with the current authenticated IAM principal\n */\nexport const getIAMPrincipalEmail = async (\n auth: GoogleAuth\n): Promise<string> => {\n const credentials = await auth.getCredentials();\n\n if ('client_email' in credentials && credentials.client_email !== undefined) {\n return credentials.client_email.replace('.gserviceaccount.com', '');\n }\n\n const accessToken = await auth.getAccessToken();\n const client = await auth.getClient();\n\n const url = `https://oauth2.googleapis.com/tokeninfo?access_token=${accessToken}`;\n const clientResponse = await client\n .request({ url })\n .then((res: { data: any }) => res.data);\n\n if (!('email' in clientResponse)) {\n throw new Error(\n \"Failed to automatically obtain authenticated IAM principal's \" +\n \"email address using environment's ADC credentials!\"\n );\n }\n const email = clientResponse['email'];\n return email.replace('.gserviceaccount.com', '');\n};\n"],"mappings":"AAyBO,MAAM,uBAAuB,OAClC,SACoB;AACpB,QAAM,cAAc,MAAM,KAAK,eAAe;AAE9C,MAAI,kBAAkB,eAAe,YAAY,iBAAiB,QAAW;AAC3E,WAAO,YAAY,aAAa,QAAQ,wBAAwB,EAAE;AAAA,EACpE;AAEA,QAAM,cAAc,MAAM,KAAK,eAAe;AAC9C,QAAM,SAAS,MAAM,KAAK,UAAU;AAEpC,QAAM,MAAM,wDAAwD,WAAW;AAC/E,QAAM,iBAAiB,MAAM,OAC1B,QAAQ,EAAE,IAAI,CAAC,EACf,KAAK,CAAC,QAAuB,IAAI,IAAI;AAExC,MAAI,EAAE,WAAW,iBAAiB;AAChC,UAAM,IAAI;AAAA,MACR;AAAA,IAEF;AAAA,EACF;AACA,QAAM,QAAQ,eAAe,OAAO;AACpC,SAAO,MAAM,QAAQ,wBAAwB,EAAE;AACjD;","names":[]}