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

Add headers for dataset request #1974

Merged
merged 5 commits into from
Dec 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function addZitadelHeaders({
headers,
zitadelParams,
}: {
headers: IncomingHttpHeaders;
headers: OutgoingHttpHeaders;
zitadelParams: ZitadelParams;
}) {
if (zitadelParams?.accessToken) {
Expand Down Expand Up @@ -237,7 +237,6 @@ export class DataFetcher {
fetchPromisesList.push(() =>
source
? DataFetcher.fetchSource({
req,
ctx,
sourceName,
source: isString(source) ? {url: source} : source,
Expand Down Expand Up @@ -458,7 +457,6 @@ export class DataFetcher {
private static async fetchSource({
sourceName,
source,
req,
ctx,
chartsEngine,
fetchingStartTime,
Expand All @@ -476,7 +474,6 @@ export class DataFetcher {
}: {
sourceName: string;
source: Source;
req: Request;
ctx: AppContext;
chartsEngine: ChartsEngine;
fetchingStartTime: number;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
}
}
Expand All @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ export default async (
},
) => {
const {
ctx,
source,
sourceName,
req,
ChartsEngine,
userId,
iamToken,
workbookId,
rejectFetchingSource,
pluginOptions,
zitadelParams,
requestHeaders,
} = args;

const ctx = req.ctx;

const cacheClient = ChartsEngine.cacheClient as Cache;

const [datasetId, layerId] = getDatasetIdAndLayerIdFromKey(sourceName);
Expand All @@ -49,14 +48,14 @@ export default async (
const datasetFieldsResponse = await getDatasetFields({
datasetId,
workbookId: workbookId ?? null,
req,
ctx,
cacheClient,
userId,
iamToken,
rejectFetchingSource,
pluginOptions,
zitadelParams,
requestHeaders,
});

revisionId = datasetFieldsResponse.revisionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,33 @@ export default async (
},
) => {
const {
ctx,
source,
req,
ChartsEngine,
userId,
iamToken,
workbookId,
rejectFetchingSource,
pluginOptions,
zitadelParams,
requestHeaders,
} = args;

const ctx = req.ctx;

const cacheClient = ChartsEngine.cacheClient as Cache;

const datasetId = source.datasetId || '';

const datasetFieldsResponse = await getDatasetFields({
datasetId,
workbookId: workbookId ?? null,
req,
ctx,
cacheClient,
userId,
iamToken,
rejectFetchingSource,
pluginOptions,
zitadelParams,
requestHeaders,
});

const datasetFields = datasetFieldsResponse.datasetFields;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<GetDataSetFieldsByIdResponse> => {
const {gatewayApi} = registry.getGatewayApi<DatalensGatewaySchemas>();

Expand All @@ -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,
Expand Down Expand Up @@ -98,26 +97,26 @@ const getDatasetFieldsById = async ({
export const getDatasetFields = async (args: {
datasetId: string;
workbookId: WorkbookId;
req: Request;
ctx: AppContext;
iamToken?: string;
cacheClient: Cache;
userId: string | null;
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}`;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/server/modes/charts/plugins/types.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
Loading