Skip to content
Merged
9 changes: 7 additions & 2 deletions packages/cubejs-api-gateway/src/jwk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint-disable no-restricted-syntax */
import crypto from 'crypto';
import { asyncMemoizeBackground, asyncRetry, BackgroundMemoizeOptions } from '@cubejs-backend/shared';
import {
asyncMemoizeBackground,
asyncRetry,
BackgroundMemoizeOptions,
getHttpAgentForProxySettings
} from '@cubejs-backend/shared';
import fetch from 'node-fetch';
import jwkToPem from 'jwk-to-pem';
import { JWTOptions } from './interfaces';
Expand Down Expand Up @@ -51,7 +56,7 @@ export type JWKsFetcherOptions = Pick<BackgroundMemoizeOptions<any, any>, 'onBac

export const createJWKsFetcher = (jwtOptions: JWTOptions, options: JWKsFetcherOptions) => {
const fetchJwkUrl = asyncMemoizeBackground(async (url: string) => {
const response = await asyncRetry(() => fetch(url), {
const response = await asyncRetry(async () => fetch(url, { agent: await getHttpAgentForProxySettings() }), {
times: jwtOptions.jwkRetry || 3,
});
const json = await response.json();
Expand Down
3 changes: 2 additions & 1 deletion packages/cubejs-backend-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
"decompress": "^4.2.1",
"env-var": "^6.3.0",
"fs-extra": "^9.1.0",
"http-proxy-agent": "^4.0.1",
"https-proxy-agent": "^7.0.6",
"moment-range": "^4.0.2",
"moment-timezone": "^0.5.47",
"node-fetch": "^2.6.1",
"proxy-agent": "^6.5.0",
"shelljs": "^0.8.5",
"throttle-debounce": "^3.0.1",
"uuid": "^8.3.2"
Expand Down
25 changes: 21 additions & 4 deletions packages/cubejs-backend-shared/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { exec } from 'child_process';
import HttpsProxyAgent from 'http-proxy-agent';
import { ProxyAgent } from 'proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';

let npmProxy: string;
let npmProxyInitialized = false;

function getCommandOutput(command: string) {
return new Promise<string>((resolve, reject) => {
Expand All @@ -14,18 +18,31 @@ function getCommandOutput(command: string) {
});
}

export async function getProxySettings() {
/**
* @deprecated
* use ProxyAgent instead
*/
export async function getProxySettings(): Promise<string> {
const [proxy] = (
await Promise.all([getCommandOutput('npm config -g get https-proxy'), getCommandOutput('npm config -g get proxy')])
)
.map((s) => s.trim())
.filter((s) => !['null', 'undefined', ''].includes(s));

npmProxyInitialized = true;

return proxy;
}

export async function getHttpAgentForProxySettings() {
const proxy = await getProxySettings();
if (!npmProxyInitialized) {
npmProxy = await getProxySettings();
}

if (npmProxy) {
console.warn('Npm proxy settings are deprecated. Please use HTTP_PROXY, HTTPS_PROXY environment variables instead.');
return new HttpsProxyAgent(npmProxy);
}

return proxy ? HttpsProxyAgent(proxy) : undefined;
return new ProxyAgent();
}
Loading
Loading