diff --git a/keyserver/src/cron/backups.js b/keyserver/src/cron/backups.js --- a/keyserver/src/cron/backups.js +++ b/keyserver/src/cron/backups.js @@ -18,7 +18,7 @@ async function backupDB() { const [backupConfig, dbConfig] = await Promise.all([ - importJSON('facts/backups'), + importJSON({ folder: 'facts', name: 'backups' }), getDBConfig(), ]); @@ -184,7 +184,7 @@ } async function deleteOldestBackup() { - const backupConfig = await importJSON('facts/backups'); + const backupConfig = await importJSON({ folder: 'facts', name: 'backups' }); invariant(backupConfig, 'backupConfig should be non-null'); const files = await readdir(backupConfig.directory); let oldestFile; diff --git a/keyserver/src/cron/update-geoip-db.js b/keyserver/src/cron/update-geoip-db.js --- a/keyserver/src/cron/update-geoip-db.js +++ b/keyserver/src/cron/update-geoip-db.js @@ -8,7 +8,10 @@ import { importJSON } from '../utils/import-json'; async function updateGeoipDB(): Promise { - const geoipLicense = await importJSON('secrets/geoip_license'); + const geoipLicense = await importJSON({ + folder: 'secrets', + name: 'geoip_license', + }); if (!geoipLicense) { console.log('no keyserver/secrets/geoip_license.json so skipping update'); return; diff --git a/keyserver/src/database/db-config.js b/keyserver/src/database/db-config.js --- a/keyserver/src/database/db-config.js +++ b/keyserver/src/database/db-config.js @@ -28,7 +28,10 @@ database: process.env.COMM_MYSQL_DATABASE, }; } else { - const importedDBConfig = await importJSON('secrets/db_config'); + const importedDBConfig = await importJSON({ + folder: 'secrets', + name: 'db_config', + }); invariant(importedDBConfig, 'DB config missing'); dbConfig = importedDBConfig; } diff --git a/keyserver/src/push/providers.js b/keyserver/src/push/providers.js --- a/keyserver/src/push/providers.js +++ b/keyserver/src/push/providers.js @@ -25,7 +25,7 @@ return provider; } try { - const apnConfig = await importJSON(`secrets/${profile}`); + const apnConfig = await importJSON({ folder: 'secrets', name: profile }); invariant(apnConfig, `APN config missing for ${profile}`); if (!cachedAPNProviders.has(profile)) { cachedAPNProviders.set(profile, new apn.Provider(apnConfig)); @@ -45,7 +45,7 @@ return provider; } try { - const fcmConfig = await importJSON(`secrets/${profile}`); + const fcmConfig = await importJSON({ folder: 'secrets', name: profile }); invariant(fcmConfig, `FCM config missed for ${profile}`); if (!cachedFCMProviders.has(profile)) { cachedFCMProviders.set( diff --git a/keyserver/src/utils/import-json.js b/keyserver/src/utils/import-json.js --- a/keyserver/src/utils/import-json.js +++ b/keyserver/src/utils/import-json.js @@ -1,27 +1,46 @@ // @flow +type ConfigName = { + +folder: 'secrets' | 'facts', + +name: string, +}; + +function getKeyForConfigName(configName: ConfigName): string { + return `${configName.folder}_${configName.name}`; +} + +function getPathForConfigName(configName: ConfigName): string { + return `${configName.folder}/${configName.name}.json`; +} + const cachedJSON = new Map(); -async function importJSON(path: string): Promise { - const cached = cachedJSON.get(path); +async function importJSON(configName: ConfigName): Promise { + const key = getKeyForConfigName(configName); + const cached = cachedJSON.get(key); if (cached !== undefined) { return cached; } - const json = await getJSON(path); - if (!cachedJSON.has(path)) { - cachedJSON.set(path, json); + const json = await getJSON(configName); + if (!cachedJSON.has(key)) { + cachedJSON.set(key, json); } - return cachedJSON.get(path); + return cachedJSON.get(key); } -async function getJSON(path: string): Promise { - const fromEnv = process.env[`COMM_JSONCONFIG_${path}`]; +async function getJSON(configName: ConfigName): Promise { + const key = getKeyForConfigName(configName); + const fromEnv = process.env[`COMM_JSONCONFIG_${key}`]; if (fromEnv) { try { return JSON.parse(fromEnv); } catch (e) { - console.log(`failed to parse JSON from env for ${path}`, e); + console.log( + `failed to parse JSON from env for ${JSON.stringify(configName)}`, + e, + ); } } + const path = getPathForConfigName(configName); try { // $FlowFixMe const importedJSON = await import(`../../${path}`); diff --git a/keyserver/src/utils/olm-utils.js b/keyserver/src/utils/olm-utils.js --- a/keyserver/src/utils/olm-utils.js +++ b/keyserver/src/utils/olm-utils.js @@ -10,7 +10,7 @@ }; async function getOlmConfig(): Promise { - const olmConfig = await importJSON('secrets/olm_config'); + const olmConfig = await importJSON({ folder: 'secrets', name: 'olm_config' }); invariant(olmConfig, 'OLM config missing'); return olmConfig; } diff --git a/keyserver/src/utils/urls.js b/keyserver/src/utils/urls.js --- a/keyserver/src/utils/urls.js +++ b/keyserver/src/utils/urls.js @@ -26,7 +26,10 @@ if (existing !== undefined) { return existing; } - const urlFacts: ?AppURLFacts = await importJSON(`facts/${site}_url`); + const urlFacts: ?AppURLFacts = await importJSON({ + folder: 'facts', + name: `${site}_url`, + }); cachedURLFacts.set(site, urlFacts); return urlFacts; }