From 2551d5c38a8ce889934fe259bd5019c946104f7d Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 31 Jan 2024 08:30:36 -0500 Subject: [PATCH] Adding proxy functionality for node module usage --- src/commands/export.ts | 5 ++++- src/commands/import.ts | 4 ++++ src/index.ts | 16 ---------------- src/utils.ts | 15 +++++++++++++++ test/e2e/e2e.test.ts | 27 +++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 7acdab51f..6e4861ac9 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -3,7 +3,7 @@ import nconf from 'nconf'; import mkdirp from 'mkdirp'; import log from '../logger'; -import { isDirectory } from '../utils'; +import { isDirectory, setupProxy } from '../utils'; import { setupContext } from '../context/index'; import { Config } from '../types'; import { ExportParams } from '../args'; @@ -17,6 +17,7 @@ export default async function exportCMD(params: ExportParams) { export_ids: exportIds, secret: clientSecret, env: shouldInheritEnv = false, + proxy_url: proxyUrl, } = params; if (shouldInheritEnv) { @@ -56,6 +57,8 @@ export default async function exportCMD(params: ExportParams) { nconf.overrides(overrides); + setupProxy(proxyUrl); + // Setup context and load const context = await setupContext(nconf.get(), 'export'); await context.dump(); diff --git a/src/commands/import.ts b/src/commands/import.ts index 58c598459..f59ce29a0 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -1,5 +1,6 @@ import nconf from 'nconf'; import { configFactory } from '../configFactory'; +import { setupProxy } from '../utils'; import { deploy as toolsDeploy } from '../tools'; import log from '../logger'; import { setupContext } from '../context'; @@ -13,6 +14,7 @@ export default async function importCMD(params: ImportParams) { config: configObj, env: shouldInheritEnv = false, secret: clientSecret, + proxy_url: proxyUrl, } = params; if (shouldInheritEnv) { @@ -41,6 +43,8 @@ export default async function importCMD(params: ImportParams) { nconf.overrides(overrides); + setupProxy(proxyUrl); + // Setup context and load const context = await setupContext(nconf.get(), 'import'); await context.loadAssetsFromLocal(); diff --git a/src/index.ts b/src/index.ts index dfeb70930..806a24057 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ #!/usr/bin/env node -import { bootstrap } from 'global-agent'; - import { getParams, CliParams } from './args'; import log from './logger'; import tools from './tools'; @@ -13,20 +11,6 @@ async function run(params: CliParams): Promise { // Run command const command = params._[0]; - const proxy = params.proxy_url; - - if (proxy) { - const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10); - - if (MAJOR_NODEJS_VERSION < 10) { - // `global-agent` works with Node.js v10 and above. - throw new Error('The --proxy_url option is only supported on Node >= 10'); - } - - process.env.GLOBAL_AGENT_HTTP_PROXY = proxy; - bootstrap(); - } - log.debug(`Start command ${command}`); if (['deploy', 'import'].includes(command) && 'input_file' in params) { await importCMD(params); diff --git a/src/utils.ts b/src/utils.ts index 46e254242..a7145e13d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,7 @@ import path from 'path'; import fs from 'fs-extra'; import sanitizeName from 'sanitize-filename'; import dotProp from 'dot-prop'; +import { bootstrap } from 'global-agent'; import { loadFileAndReplaceKeywords, Auth0 } from './tools'; import log from './logger'; import { Asset, Assets, Config, KeywordMappings } from './types'; @@ -205,3 +206,17 @@ export function mapClientID2NameSorted(enabledClients: string[], knownClients: A ...(enabledClients || []).map((clientId) => convertClientIdToName(clientId, knownClients)), ].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); } + +export const setupProxy = (proxyUrl: string | undefined) => { + if (proxyUrl === undefined) return; + + const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10); + + if (MAJOR_NODEJS_VERSION < 10) { + // `global-agent` works with Node.js v10 and above. + throw new Error('The --proxy_url option is only supported on Node >= 10'); + } + + process.env.GLOBAL_AGENT_HTTP_PROXY = proxyUrl; + bootstrap(); +}; diff --git a/test/e2e/e2e.test.ts b/test/e2e/e2e.test.ts index 5eba23187..b1a3032db 100644 --- a/test/e2e/e2e.test.ts +++ b/test/e2e/e2e.test.ts @@ -17,6 +17,33 @@ const AUTH0_CLIENT_SECRET = process.env['AUTH0_E2E_CLIENT_SECRET'] || ''; const AUTH0_ACCESS_TOKEN = shouldUseRecordings ? 'insecure' : undefined; describe('#end-to-end dump', function () { + it('should be able to set proxy URL for both dump and deploy', async function () { + await dump({ + output_folder: testNameToWorkingDirectory(this.test?.title), + format: 'yaml', + config: { + AUTH0_DOMAIN, + AUTH0_CLIENT_ID, + AUTH0_CLIENT_SECRET, + AUTH0_ACCESS_TOKEN, + AUTH0_INCLUDED_ONLY: ['actions'], + }, + proxy_url: 'http://www.some-proxy.com', + }); + + await deploy({ + input_file: `${__dirname}/testdata/should-deploy-without-throwing-an-error/tenant.yaml`, + config: { + AUTH0_DOMAIN, + AUTH0_CLIENT_ID, + AUTH0_CLIENT_SECRET, + AUTH0_ACCESS_TOKEN, + AUTH0_INCLUDED_ONLY: ['actions'], + }, + proxy_url: 'http://www.some-proxy.com', + }); + }); + it('should dump without throwing an error', async function () { const workDirectory = testNameToWorkingDirectory(this.test?.title);