From e99d5630601fa50d398970b880d73b140ea571d6 Mon Sep 17 00:00:00 2001 From: Adam Azad Date: Tue, 17 Dec 2024 14:29:39 +0300 Subject: [PATCH] chore(gskms): add file support for `GCKMS_CONFIG` env var --- src/utils/GckmsUtils.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils/GckmsUtils.ts b/src/utils/GckmsUtils.ts index d219ef5a2..aef9ab652 100644 --- a/src/utils/GckmsUtils.ts +++ b/src/utils/GckmsUtils.ts @@ -3,6 +3,7 @@ import kms from "@google-cloud/kms"; import { Storage } from "@google-cloud/storage"; import fs from "fs"; +import path from "node:path"; export interface KeyConfig { projectId: string; locationId: string; @@ -29,12 +30,21 @@ const storageConfig = GCP_STORAGE_CONFIG ? JSON.parse(GCP_STORAGE_CONFIG) : unde export function getGckmsConfig(keys: string[]): KeyConfig[] { let configOverride: GckmsConfig = {}; if (process.env.GCKMS_CONFIG) { - configOverride = JSON.parse(process.env.GCKMS_CONFIG); + const isFile = fs.existsSync(process.env.GCKMS_CONFIG); + if (isFile) { + configOverride = JSON.parse(fs.readFileSync(process.env.GCKMS_CONFIG, "utf8")); + } else { + configOverride = JSON.parse(process.env.GCKMS_CONFIG); + } } else { const overrideFname = ".GckmsOverride.js"; try { - if (fs.existsSync(`${__dirname}/${overrideFname}`)) { - configOverride = require(`./${overrideFname}`); + const filePath = path.join(__dirname, overrideFname); + const doesFileExist = fs.existsSync(filePath); + if (doesFileExist) { + configOverride = require(filePath); + } else { + throw new Error(`GCKMS_CONFIG file not found at path: ${filePath}`); } } catch (err) { // eslint-disable-next-line no-console @@ -42,6 +52,10 @@ export function getGckmsConfig(keys: string[]): KeyConfig[] { } } + if (Object.keys(configOverride).length === 0) { + throw new Error("GCKMS_CONFIG is empty"); + } + const keyConfigs = keys.map((keyName: string): KeyConfig => { return (configOverride["mainnet"][keyName] || {}) as KeyConfig; // Hardcode to "mainnet" network. This makes no impact key retrieval. });