Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Adding proxy functionality for node module usage #885

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/commands/import.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,6 +14,7 @@ export default async function importCMD(params: ImportParams) {
config: configObj,
env: shouldInheritEnv = false,
secret: clientSecret,
proxy_url: proxyUrl,
} = params;

if (shouldInheritEnv) {
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 0 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,20 +11,6 @@ async function run(params: CliParams): Promise<void> {
// 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);
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
};
27 changes: 27 additions & 0 deletions test/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading