Skip to content

Commit

Permalink
Merge pull request #216 from snyk/fix/PUL2-771/extra-headers-for-fedramp
Browse files Browse the repository at this point in the history
fix: add posibility to add extra headers to filters endpoint
  • Loading branch information
jozsef-armin-hamos committed Nov 21, 2023
2 parents 0364c79 + 2691043 commit 26f1ec5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,17 @@ export async function getSupportedFiles(
requestId?: string,
languages?: string[],
orgId?: string,
extraHeaders?: Record<string, string>,
): Promise<SupportedFiles> {
emitter.supportedFilesLoaded(null);
const resp = await getFilters(baseURL, source, MAX_RETRY_ATTEMPTS, requestId, orgId);
const resp = await getFilters({
baseURL,
source,
orgId,
requestId,
attempts: MAX_RETRY_ATTEMPTS,
extraHeaders: extraHeaders ?? {},
});
if (resp.type === 'error') {
throw resp.error;
}
Expand Down Expand Up @@ -272,6 +280,7 @@ export async function createBundleFromFolders(options: CreateBundleFromFoldersOp
options.requestId,
options.languages,
options.orgId,
options.extraHeaders,
);

// Collect files and create a remote bundle
Expand Down
32 changes: 23 additions & 9 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import pick from 'lodash.pick';
import { gzip } from 'zlib';
import { promisify } from 'util';

import { DEFAULT_ERROR_MESSAGES, ErrorCodes, GenericErrorTypes, MAX_RETRY_ATTEMPTS } from './constants';
import { DEFAULT_ERROR_MESSAGES, ErrorCodes, GenericErrorTypes } from './constants';

import { BundleFiles, SupportedFiles } from './interfaces/files.interface';
import { AnalysisResult, ReportResult } from './interfaces/analysis-result.interface';
Expand Down Expand Up @@ -180,13 +180,23 @@ export async function checkSession(options: CheckSessionOptions): Promise<Result
return generateError<CheckSessionErrorCodes>(res.errorCode, CHECK_SESSION_ERROR_MESSAGES, 'checkSession');
}

export async function getFilters(
baseURL: string,
source: string,
attempts = MAX_RETRY_ATTEMPTS,
requestId?: string,
orgId?: string,
): Promise<Result<SupportedFiles, GenericErrorTypes>> {
export interface FilterArgs {
extraHeaders: Record<string, string>;
attempts: number;
baseURL: string;
source: string;
requestId?: string;
orgId?: string;
}

export async function getFilters({
baseURL,
orgId,
attempts,
source,
extraHeaders,
requestId,
}: FilterArgs): Promise<Result<SupportedFiles, GenericErrorTypes>> {
const apiName = 'filters';
let url: string;

Expand All @@ -198,7 +208,11 @@ export async function getFilters(

const res = await makeRequest<SupportedFiles>(
{
headers: { source, ...(requestId && { 'snyk-request-id': requestId }) },
headers: {
source,
...extraHeaders,
...(requestId && { 'snyk-request-id': requestId }),
},
url,
method: 'get',
},
Expand Down
19 changes: 16 additions & 3 deletions tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import 'jest-extended';

import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base';
import { bundleFiles, bundleFilesFull, singleBundleFull } from './constants/sample';
import { getFilters, createBundle, checkBundle, extendBundle, getAnalysis, AnalysisStatus } from '../src/http';
import {
getFilters,
createBundle,
checkBundle,
extendBundle,
getAnalysis,
AnalysisStatus,
FilterArgs,
} from '../src/http';
import { BundleFiles } from '../src/interfaces/files.interface';

const fakeBundleHash = '7055a4c63c339c31bdf28defcced19a64e5e87905b896befc522a11d35fbcdc4';
Expand All @@ -30,7 +38,7 @@ const fakeMissingFiles = [

describe('Requests to public API', () => {
it('gets filters successfully', async () => {
const response = await getFilters(baseURL, '');
const response = await getFilters({ baseURL, source: 'api-test', attempts: 1, extraHeaders: {} });
expect(response.type).toEqual('success');
if (response.type === 'error') return;
expect(new Set(response.value.configFiles)).toEqual(new Set(['.dcignore', '.gitignore', '.snyk', '.snyk-rules']));
Expand Down Expand Up @@ -89,7 +97,12 @@ describe('Requests to public API', () => {
});

it('test network issues', async () => {
const response = await getFilters('https://faketest.snyk.io', 'test-source', 1);
const response = await getFilters({
baseURL: 'https://faketest.snyk.io',
source: 'test-source',
attempts: 1,
extraHeaders: {},
});
expect(response.type).toEqual('error');
if (response.type !== 'error') return;
expect(response.error).toMatchObject({
Expand Down
5 changes: 3 additions & 2 deletions tests/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { baseURL, source } from './constants/base';
import * as needle from '../src/needle';
import * as httpUtils from '../src/utils/httpUtils';
import { FilterArgs } from '../src/http';

const jsonApiError = {
status: '422',
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('HTTP', () => {
it('should return error on failed response', async () => {
jest.spyOn(needle, 'makeRequest').mockResolvedValue({ success: false, errorCode: 500, error });

const result = (await getFilters(baseURL, source)) as ResultError<number>;
const result = (await getFilters({ baseURL, source, attempts: 1, extraHeaders: {} })) as ResultError<number>;

expect(result.error.apiName).toEqual('filters');
expect(result.error.statusText).toBeTruthy();
Expand All @@ -81,7 +82,7 @@ describe('HTTP', () => {
jest.spyOn(needle, 'makeRequest').mockResolvedValue({ success: false, errorCode: 422, error, jsonApiError });
const spy = jest.spyOn(httpUtils, 'generateErrorWithDetail');

await getFilters(baseURL, source);
await getFilters({ baseURL, source, attempts: 1, extraHeaders: {} });

expect(spy).toHaveBeenCalledWith(jsonApiError, 422, 'filters');
});
Expand Down

0 comments on commit 26f1ec5

Please sign in to comment.