|
| 1 | +import { gaxios, Impersonated, JWT } from "google-auth-library"; |
| 2 | +import { GetAccessTokenResponse } from "google-auth-library/build/src/auth/oauth2client"; |
| 3 | + |
| 4 | +import { getConfig } from "@app/lib/config/env"; |
| 5 | +import { BadRequestError, InternalServerError } from "@app/lib/errors"; |
| 6 | +import { alphaNumericNanoId } from "@app/lib/nanoid"; |
| 7 | + |
| 8 | +import { DynamicSecretGcpIamSchema, TDynamicProviderFns } from "./models"; |
| 9 | + |
| 10 | +export const GcpIamProvider = (): TDynamicProviderFns => { |
| 11 | + const validateProviderInputs = async (inputs: unknown) => { |
| 12 | + const providerInputs = await DynamicSecretGcpIamSchema.parseAsync(inputs); |
| 13 | + return providerInputs; |
| 14 | + }; |
| 15 | + |
| 16 | + const $getToken = async (serviceAccountEmail: string, ttl: number): Promise<string> => { |
| 17 | + const appCfg = getConfig(); |
| 18 | + if (!appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL) { |
| 19 | + throw new InternalServerError({ |
| 20 | + message: "Environment variable has not been configured: INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL" |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + const credJson = JSON.parse(appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL) as { |
| 25 | + client_email: string; |
| 26 | + private_key: string; |
| 27 | + }; |
| 28 | + |
| 29 | + const sourceClient = new JWT({ |
| 30 | + email: credJson.client_email, |
| 31 | + key: credJson.private_key, |
| 32 | + scopes: ["https://www.googleapis.com/auth/cloud-platform"] |
| 33 | + }); |
| 34 | + |
| 35 | + const impersonatedCredentials = new Impersonated({ |
| 36 | + sourceClient, |
| 37 | + targetPrincipal: serviceAccountEmail, |
| 38 | + lifetime: ttl, |
| 39 | + delegates: [], |
| 40 | + targetScopes: ["https://www.googleapis.com/auth/iam", "https://www.googleapis.com/auth/cloud-platform"] |
| 41 | + }); |
| 42 | + |
| 43 | + let tokenResponse: GetAccessTokenResponse | undefined; |
| 44 | + try { |
| 45 | + tokenResponse = await impersonatedCredentials.getAccessToken(); |
| 46 | + } catch (error) { |
| 47 | + let message = "Unable to validate connection"; |
| 48 | + if (error instanceof gaxios.GaxiosError) { |
| 49 | + message = error.message; |
| 50 | + } |
| 51 | + |
| 52 | + throw new BadRequestError({ |
| 53 | + message |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + if (!tokenResponse || !tokenResponse.token) { |
| 58 | + throw new BadRequestError({ |
| 59 | + message: "Unable to validate connection" |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + return tokenResponse.token; |
| 64 | + }; |
| 65 | + |
| 66 | + const validateConnection = async (inputs: unknown) => { |
| 67 | + const providerInputs = await validateProviderInputs(inputs); |
| 68 | + await $getToken(providerInputs.serviceAccountEmail, 10); |
| 69 | + return true; |
| 70 | + }; |
| 71 | + |
| 72 | + const create = async (inputs: unknown, expireAt: number) => { |
| 73 | + const providerInputs = await validateProviderInputs(inputs); |
| 74 | + |
| 75 | + const now = Math.floor(Date.now() / 1000); |
| 76 | + const ttl = Math.max(Math.floor(expireAt / 1000) - now, 0); |
| 77 | + |
| 78 | + const token = await $getToken(providerInputs.serviceAccountEmail, ttl); |
| 79 | + const entityId = alphaNumericNanoId(32); |
| 80 | + |
| 81 | + return { entityId, data: { SERVICE_ACCOUNT_EMAIL: providerInputs.serviceAccountEmail, TOKEN: token } }; |
| 82 | + }; |
| 83 | + |
| 84 | + const revoke = async (_inputs: unknown, entityId: string) => { |
| 85 | + // There's no way to revoke GCP IAM access tokens |
| 86 | + return { entityId }; |
| 87 | + }; |
| 88 | + |
| 89 | + const renew = async (inputs: unknown, entityId: string, expireAt: number) => { |
| 90 | + // To renew a token it must be re-created |
| 91 | + const data = await create(inputs, expireAt); |
| 92 | + |
| 93 | + return { ...data, entityId }; |
| 94 | + }; |
| 95 | + |
| 96 | + return { |
| 97 | + validateProviderInputs, |
| 98 | + validateConnection, |
| 99 | + create, |
| 100 | + revoke, |
| 101 | + renew |
| 102 | + }; |
| 103 | +}; |
0 commit comments