Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: snyk/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.955.0
Choose a base ref
...
head repository: snyk/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.956.0
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Jun 21, 2022

  1. fix: support HTTP(S) proxies in iac-test

    Configure HTTP clients to respect HTTP(S)_PROXY environment variables.
    Ignore TLS identity verification errors (this is dangerous) when the
    `--insecure` flag is passed, matching the behaviour of `snyk test`.
    Craig Furman authored and francescomari committed Jun 21, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    francescomari Francesco Mari
    Copy the full SHA
    3ac3ad0 View commit details

Commits on Jun 22, 2022

  1. Merge pull request #3348 from snyk/fix/iac-bundle-proxy-support-stream

    fix: support HTTP(S) proxies in iac-test
    Craig Furman authored Jun 22, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0b90441 View commit details
Showing with 55 additions and 11 deletions.
  1. +7 −3 src/cli/commands/test/iac/local-execution/local-cache.ts
  2. +36 −3 src/lib/request/request.ts
  3. +12 −5 test/jest/unit/iac/local-cache.spec.ts
10 changes: 7 additions & 3 deletions src/cli/commands/test/iac/local-execution/local-cache.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import * as path from 'path';
import * as fs from 'fs';
import { EngineType, IaCErrorCodes } from './types';
import * as needle from 'needle';
import * as rimraf from 'rimraf';
import { createIacDir, extractBundle, isValidBundle } from './file-utils';
import * as Debug from 'debug';
import { CustomError } from '../../../../../lib/errors';
import * as analytics from '../../../../../lib/analytics';
import ReadableStream = NodeJS.ReadableStream;
import { getErrorStringCode } from './error-utils';
import config from '../../../../../lib/config';
import { streamRequest } from '../../../../../lib/request/request';

const debug = Debug('iac-local-cache');

@@ -141,7 +140,12 @@ export async function initLocalCache({
// always overwrite whatever might be there.
try {
const BUNDLE_URL = 'https://static.snyk.io/cli/wasm/bundle.tar.gz';
const response: ReadableStream = needle.get(BUNDLE_URL);
const response = await streamRequest({
method: 'get',
url: BUNDLE_URL,
body: null,
headers: {},
});
await extractBundle(response);
} catch (e) {
throw new FailedToDownloadRulesError();
39 changes: 36 additions & 3 deletions src/lib/request/request.ts
Original file line number Diff line number Diff line change
@@ -17,9 +17,7 @@ const snykDebug = debugModule('snyk');

declare const global: Global;

export async function makeRequest(
payload: Payload,
): Promise<{ res: needle.NeedleResponse; body: any }> {
function setupRequest(payload: Payload) {
// This ensures we support lowercase http(s)_proxy values as well
// The weird IF around it ensures we don't create an envvar with a value of undefined, which throws error when trying to use it as a proxy
if (process.env.HTTP_PROXY || process.env.http_proxy) {
@@ -129,6 +127,14 @@ export async function makeRequest(
options.rejectUnauthorized = false;
}

return { method, url, data, options };
}

export async function makeRequest(
payload: Payload,
): Promise<{ res: needle.NeedleResponse; body: any }> {
const { method, url, data, options } = setupRequest(payload);

return new Promise((resolve, reject) => {
needle.request(method, url, data, options, (err, res, respBody) => {
debug(err);
@@ -145,3 +151,30 @@ export async function makeRequest(
});
});
}

export async function streamRequest(
payload: Payload,
): Promise<needle.ReadableStream> {
const { method, url, data, options } = setupRequest(payload);

try {
const result = await needle.request(method, url, data, options);
const statusCode = await getStatusCode(result);
debug('response (%s): <stream>', statusCode);
return result;
} catch (e) {
debug(e);
throw e;
}
}

async function getStatusCode(stream: needle.ReadableStream): Promise<number> {
return new Promise((resolve, reject) => {
stream.on('header', (statusCode: number) => {
resolve(statusCode);
});
stream.on('err', (err: Error) => {
reject(err);
});
});
}
17 changes: 12 additions & 5 deletions test/jest/unit/iac/local-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -5,10 +5,10 @@ import {
} from '../../../../src/cli/commands/test/iac/local-execution/local-cache';
import * as fileUtilsModule from '../../../../src/cli/commands/test/iac/local-execution/file-utils';
import { PassThrough } from 'stream';
import * as needle from 'needle';
import * as rimraf from 'rimraf';
import * as fs from 'fs';
import * as path from 'path';
import * as request from '../../../../src/lib/request/request';

describe('initLocalCache - downloads bundle successfully', () => {
beforeEach(() => {
@@ -24,12 +24,17 @@ describe('initLocalCache - downloads bundle successfully', () => {
.spyOn(fileUtilsModule, 'extractBundle')
.mockResolvedValue();
jest.spyOn(fileUtilsModule, 'createIacDir').mockImplementation(() => null);
jest.spyOn(needle, 'get').mockReturnValue(mockReadable);
jest
.spyOn(request, 'streamRequest')
.mockReturnValue(Promise.resolve(mockReadable));

await localCacheModule.initLocalCache();

expect(needle.get).toHaveBeenCalledWith(
expect.stringContaining('bundle.tar.gz'),
expect(request.streamRequest).toHaveBeenCalledWith(
expect.objectContaining({
method: 'get',
url: expect.stringContaining('bundle.tar.gz'),
}),
);
expect(spy).toHaveBeenCalledWith(mockReadable);
});
@@ -43,7 +48,9 @@ describe('initLocalCache - downloads bundle successfully', () => {
.mockResolvedValue();
jest.spyOn(fileUtilsModule, 'isValidBundle').mockReturnValue(true);
jest.spyOn(fileUtilsModule, 'createIacDir').mockImplementation(() => null);
jest.spyOn(needle, 'get').mockReturnValue(new PassThrough());
jest
.spyOn(request, 'streamRequest')
.mockReturnValue(Promise.resolve(new PassThrough()));
jest.spyOn(fs, 'createReadStream').mockReturnValue(mockReadable);

await localCacheModule.initLocalCache({