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

fix(api-gateway): Support proxy when fetching jwk for token validation #9286

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
14 changes: 12 additions & 2 deletions packages/cubejs-backend-shared/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exec } from 'child_process';
import HttpsProxyAgent from 'http-proxy-agent';
import { ProxyAgent } from 'proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';

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

/**
* @deprecated
* use ProxyAgent instead
*/
export async function getProxySettings() {
const [proxy] = (
await Promise.all([getCommandOutput('npm config -g get https-proxy'), getCommandOutput('npm config -g get proxy')])
Expand All @@ -27,5 +32,10 @@ export async function getProxySettings() {
export async function getHttpAgentForProxySettings() {
const proxy = await getProxySettings();

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

return new ProxyAgent();
}
Loading
Loading