From 0121f461ef5fcea4603cd56e4eac41e791a585c8 Mon Sep 17 00:00:00 2001 From: Andrey Melikhov Date: Wed, 25 Dec 2024 14:04:25 +0300 Subject: [PATCH] Add headers for dataset request (#1974) --- .../components/processor/data-fetcher.ts | 16 +++++++--------- .../middlewareAdapters/charts-with-dataset.ts | 7 +++---- .../controls-with-dataset.ts | 7 +++---- .../request-with-dataset/request-dataset.ts | 19 +++++++++---------- src/server/modes/charts/plugins/types.ts | 6 +++++- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/server/components/charts-engine/components/processor/data-fetcher.ts b/src/server/components/charts-engine/components/processor/data-fetcher.ts index f9a757babd..b7f0ab17ab 100644 --- a/src/server/components/charts-engine/components/processor/data-fetcher.ts +++ b/src/server/components/charts-engine/components/processor/data-fetcher.ts @@ -158,7 +158,7 @@ export function addZitadelHeaders({ headers, zitadelParams, }: { - headers: IncomingHttpHeaders; + headers: OutgoingHttpHeaders; zitadelParams: ZitadelParams; }) { if (zitadelParams?.accessToken) { @@ -237,7 +237,6 @@ export class DataFetcher { fetchPromisesList.push(() => source ? DataFetcher.fetchSource({ - req, ctx, sourceName, source: isString(source) ? {url: source} : source, @@ -458,7 +457,6 @@ export class DataFetcher { private static async fetchSource({ sourceName, source, - req, ctx, chartsEngine, fetchingStartTime, @@ -476,7 +474,6 @@ export class DataFetcher { }: { sourceName: string; source: Source; - req: Request; ctx: AppContext; chartsEngine: ChartsEngine; fetchingStartTime: number; @@ -786,6 +783,10 @@ export class DataFetcher { requestOptions.headers['x-forwarded-for'] = originalReqHeaders.xForwardedFor; } + if (!requestOptions.headers['x-real-ip']) { + requestOptions.headers['x-real-ip'] = originalReqHeaders.xRealIP; + } + if (isSourceWithMiddlewareUrl(source)) { const middlewareSourceConfig = DataFetcher.getSourceConfig({ chartsEngine, @@ -794,15 +795,16 @@ export class DataFetcher { if (middlewareSourceConfig?.middlewareAdapter) { source = await middlewareSourceConfig.middlewareAdapter({ + ctx, source, sourceName, - req, iamToken: iamToken ?? undefined, workbookId, ChartsEngine: chartsEngine, userId: userId === undefined ? null : userId, rejectFetchingSource, zitadelParams, + requestHeaders: requestOptions.headers, }); } } @@ -824,10 +826,6 @@ export class DataFetcher { const publicTargetUri = hideSensitiveData(targetUri); const publicSourceData = hideSensitiveData(sourceData); - if (!requestOptions.headers['x-real-ip']) { - requestOptions.headers['x-real-ip'] = originalReqHeaders.xRealIP; - } - const traceId = ctx.getTraceId(); const tenantId = ctx.get('tenantId'); diff --git a/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/charts-with-dataset.ts b/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/charts-with-dataset.ts index 9fb132d35c..78a5f34e11 100644 --- a/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/charts-with-dataset.ts +++ b/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/charts-with-dataset.ts @@ -13,9 +13,9 @@ export default async ( }, ) => { const { + ctx, source, sourceName, - req, ChartsEngine, userId, iamToken, @@ -23,10 +23,9 @@ export default async ( rejectFetchingSource, pluginOptions, zitadelParams, + requestHeaders, } = args; - const ctx = req.ctx; - const cacheClient = ChartsEngine.cacheClient as Cache; const [datasetId, layerId] = getDatasetIdAndLayerIdFromKey(sourceName); @@ -49,7 +48,6 @@ export default async ( const datasetFieldsResponse = await getDatasetFields({ datasetId, workbookId: workbookId ?? null, - req, ctx, cacheClient, userId, @@ -57,6 +55,7 @@ export default async ( rejectFetchingSource, pluginOptions, zitadelParams, + requestHeaders, }); revisionId = datasetFieldsResponse.revisionId; diff --git a/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/controls-with-dataset.ts b/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/controls-with-dataset.ts index 6ffa3d4d16..9573d551e8 100644 --- a/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/controls-with-dataset.ts +++ b/src/server/modes/charts/plugins/request-with-dataset/middlewareAdapters/controls-with-dataset.ts @@ -11,8 +11,8 @@ export default async ( }, ) => { const { + ctx, source, - req, ChartsEngine, userId, iamToken, @@ -20,10 +20,9 @@ export default async ( rejectFetchingSource, pluginOptions, zitadelParams, + requestHeaders, } = args; - const ctx = req.ctx; - const cacheClient = ChartsEngine.cacheClient as Cache; const datasetId = source.datasetId || ''; @@ -31,7 +30,6 @@ export default async ( const datasetFieldsResponse = await getDatasetFields({ datasetId, workbookId: workbookId ?? null, - req, ctx, cacheClient, userId, @@ -39,6 +37,7 @@ export default async ( rejectFetchingSource, pluginOptions, zitadelParams, + requestHeaders, }); const datasetFields = datasetFieldsResponse.datasetFields; diff --git a/src/server/modes/charts/plugins/request-with-dataset/request-dataset.ts b/src/server/modes/charts/plugins/request-with-dataset/request-dataset.ts index 40b6c28ef3..895dbad513 100644 --- a/src/server/modes/charts/plugins/request-with-dataset/request-dataset.ts +++ b/src/server/modes/charts/plugins/request-with-dataset/request-dataset.ts @@ -1,4 +1,5 @@ -import type {Request} from '@gravity-ui/expresskit'; +import type {OutgoingHttpHeaders} from 'http'; + import type {AppContext} from '@gravity-ui/nodekit'; import {REQUEST_ID_PARAM_NAME} from '@gravity-ui/nodekit'; import {isObject} from 'lodash'; @@ -25,21 +26,21 @@ const getStatusFromError = (error: unknown) => const getDatasetFieldsById = async ({ datasetId, workbookId, - req, ctx, rejectFetchingSource, iamToken, pluginOptions, zitadelParams, + headers, }: { datasetId: string; workbookId: string | null; - req: Request; ctx: AppContext; rejectFetchingSource: (reason?: any) => void; iamToken?: string; pluginOptions?: ConfigurableRequestWithDatasetPluginOptions; zitadelParams: ZitadelParams | undefined; + headers: OutgoingHttpHeaders; }): Promise => { const {gatewayApi} = registry.getGatewayApi(); @@ -48,13 +49,11 @@ const getDatasetFieldsById = async ({ const requestDatasetFieldsByToken = gatewayApi.bi.embedsGetDataSetFieldsById; try { - const headers = {...req.headers}; - if (zitadelParams) { addZitadelHeaders({headers, zitadelParams}); } - const response = req.headers[DL_EMBED_TOKEN_HEADER] + const response = headers[DL_EMBED_TOKEN_HEADER] ? await requestDatasetFieldsByToken({ ctx, headers, @@ -98,7 +97,6 @@ const getDatasetFieldsById = async ({ export const getDatasetFields = async (args: { datasetId: string; workbookId: WorkbookId; - req: Request; ctx: AppContext; iamToken?: string; cacheClient: Cache; @@ -106,18 +104,19 @@ export const getDatasetFields = async (args: { rejectFetchingSource: (reason: any) => void; pluginOptions?: ConfigurableRequestWithDatasetPluginOptions; zitadelParams: ZitadelParams | undefined; + requestHeaders: OutgoingHttpHeaders; }): Promise<{datasetFields: PartialDatasetField[]; revisionId: string}> => { const { datasetId, workbookId, cacheClient, - req, ctx, userId, iamToken, rejectFetchingSource, pluginOptions, zitadelParams, + requestHeaders, } = args; const cacheKey = `${datasetId}__${userId}`; @@ -140,12 +139,12 @@ export const getDatasetFields = async (args: { const response = await getDatasetFieldsById({ datasetId, workbookId, - req, ctx, rejectFetchingSource, iamToken, pluginOptions, zitadelParams, + headers: requestHeaders, }); datasetFields = response.fields; revisionId = response.revision_id; @@ -173,12 +172,12 @@ export const getDatasetFields = async (args: { const response = await getDatasetFieldsById({ datasetId, workbookId, - req, ctx, rejectFetchingSource, iamToken, pluginOptions, zitadelParams, + headers: requestHeaders, }); datasetFields = response.fields; revisionId = response.revision_id; diff --git a/src/server/modes/charts/plugins/types.ts b/src/server/modes/charts/plugins/types.ts index 33cfdefe7a..f05abe61b2 100644 --- a/src/server/modes/charts/plugins/types.ts +++ b/src/server/modes/charts/plugins/types.ts @@ -1,4 +1,7 @@ +import type {OutgoingHttpHeaders} from 'http'; + import type {Request} from '@gravity-ui/expresskit'; +import type {AppContext} from '@gravity-ui/nodekit'; import type {WorkbookId} from '../../../../shared'; import type {ChartsEngine} from '../../../components/charts-engine'; @@ -42,19 +45,20 @@ export interface MiddlewareUrl { } export interface MiddlewareSourceAdapterArgs { + ctx: AppContext; sourceName: string; source: { sourceArgs: SourcesArgs; middlewareUrl: MiddlewareUrl; datasetId?: string; }; - req: Request; iamToken?: string; workbookId?: WorkbookId; ChartsEngine: ChartsEngine; userId: string | null; rejectFetchingSource: (reason: any) => void; zitadelParams: ZitadelParams | undefined; + requestHeaders: OutgoingHttpHeaders; } export interface ProcessorHookInitArgs {