|
| 1 | +import "source-map-support/register"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import { PubSub } from "@google-cloud/pubsub"; |
| 4 | + |
| 5 | +const setupGoogleCloudCredentials = () => { |
| 6 | + const gcloudServiceAccountCredentials = { |
| 7 | + type: "external_account", |
| 8 | + subject_token_type: "urn:ietf:params:aws:token-type:aws4_request", |
| 9 | + credential_source: { |
| 10 | + environment_id: "aws1", |
| 11 | + region_url: |
| 12 | + "http://169.254.169.254/latest/meta-data/placement/availability-zone", |
| 13 | + url: "http://169.254.169.254/latest/meta-data/iam/security-credentials", |
| 14 | + regional_cred_verification_url: |
| 15 | + "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15", |
| 16 | + }, |
| 17 | + token_url: "https://sts.googleapis.com/v1/token", |
| 18 | + audience: `//iam.googleapis.com/projects/${process.env.GOOGLE_CLOUD_PROJECT_NUMBER}/locations/global/workloadIdentityPools/${process.env.GOOGLE_CLOUD_WEB_IDENTITY_POOL}/providers/${process.env.GOOGLE_CLOUD_WEB_IDENTITY_POOL_AWS_PROVIDER}`, |
| 19 | + service_account_impersonation_url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${process.env.GOOGLE_CLOUD_SERVICE_ACCOUNT_EMAIL}:generateAccessToken`, |
| 20 | + }; |
| 21 | + |
| 22 | + process.env.GOOGLE_APPLICATION_CREDENTIALS = |
| 23 | + "/tmp/gcloud-service-account-credentials.json"; |
| 24 | + |
| 25 | + fs.writeFileSync( |
| 26 | + process.env.GOOGLE_APPLICATION_CREDENTIALS, |
| 27 | + JSON.stringify(gcloudServiceAccountCredentials) |
| 28 | + ); |
| 29 | + |
| 30 | + console.log( |
| 31 | + "Google Cloud Service Account Credentials", |
| 32 | + gcloudServiceAccountCredentials |
| 33 | + ); |
| 34 | +}; |
| 35 | + |
| 36 | +const clients: { readonly [projectId: string]: PubSub } = (() => { |
| 37 | + const underlying: any = {}; |
| 38 | + let credentialsSetup = false; |
| 39 | + |
| 40 | + const projectIdRegexp = /^[a-z][a-z-0-9-]{4,28}[a-z0-9]$/; |
| 41 | + |
| 42 | + return new Proxy(underlying, { |
| 43 | + get: (_, projectId) => { |
| 44 | + if ("string" !== typeof projectId) { |
| 45 | + return underlying[projectId]; |
| 46 | + } |
| 47 | + |
| 48 | + if (!projectIdRegexp.test(projectId)) { |
| 49 | + throw new Error(`Invalid project id '${projectId}'`); |
| 50 | + } |
| 51 | + |
| 52 | + if (!credentialsSetup) { |
| 53 | + setupGoogleCloudCredentials(); |
| 54 | + |
| 55 | + credentialsSetup = true; |
| 56 | + } |
| 57 | + |
| 58 | + if (!underlying[projectId]) { |
| 59 | + underlying[projectId] = new PubSub({ projectId }); |
| 60 | + } |
| 61 | + |
| 62 | + return underlying[projectId]; |
| 63 | + }, |
| 64 | + set: () => { |
| 65 | + throw new Error("Read only object!"); |
| 66 | + }, |
| 67 | + }); |
| 68 | +})(); |
| 69 | + |
| 70 | +export const handler = async (event: { |
| 71 | + topic: string; |
| 72 | + message: string; |
| 73 | + projectId?: string; |
| 74 | + base64?: boolean; |
| 75 | +}) => { |
| 76 | + const projectId = |
| 77 | + event.projectId || process.env.GOOGLE_CLOUD_DEFAULT_PROJECT_ID || ""; |
| 78 | + |
| 79 | + try { |
| 80 | + const messageId = await clients[projectId] |
| 81 | + .topic(event.topic) |
| 82 | + .publish(Buffer.from(event.message, event.base64 ? "base64" : "utf-8")); |
| 83 | + |
| 84 | + return { |
| 85 | + messageId, |
| 86 | + projectId, |
| 87 | + topic: event.topic, |
| 88 | + }; |
| 89 | + } catch (e) { |
| 90 | + console.error( |
| 91 | + `Unable to publish message to topic '${event.topic}' on project '${projectId}'`, |
| 92 | + e |
| 93 | + ); |
| 94 | + |
| 95 | + throw e; |
| 96 | + } |
| 97 | +}; |
0 commit comments