Skip to content

Commit

Permalink
Merge pull request #3863 from snyk/feat/pass-http-client-to-plugins
Browse files Browse the repository at this point in the history
feat: pass snykHttpClient to plugin.inspect
  • Loading branch information
Jdunsby authored Sep 23, 2022
2 parents 4fe1808 + 17b1273 commit b69d4b3
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 82 deletions.
81 changes: 32 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"snyk-cpp-plugin": "2.20.1",
"snyk-docker-plugin": "^5.4.3",
"snyk-go-plugin": "1.19.2",
"snyk-gradle-plugin": "3.22.2",
"snyk-gradle-plugin": "3.23.0",
"snyk-module": "3.1.0",
"snyk-mvn-plugin": "2.31.0",
"snyk-nodejs-lockfile-parser": "1.38.0",
Expand Down
1 change: 1 addition & 0 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export function args(rawArgv: string[]): Args {
'prune-repeated-subdependencies',
'dry-run',
'sequential',
'gradle-normalize-deps',
];
for (const dashedArg of argumentsToTransform) {
if (argv[dashedArg]) {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/module-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function ModuleInfo(plugin, policy) {
root,
targetFile,
options,
snykHttpClient,
): Promise<pluginApi.SinglePackageResult | pluginApi.MultiProjectResult> {
const pluginOptions = merge(
{
Expand All @@ -19,7 +20,12 @@ export function ModuleInfo(plugin, policy) {
);

debug('calling plugin inspect()', { root, targetFile, pluginOptions });
const info = await plugin.inspect(root, targetFile, pluginOptions);
const info = await plugin.inspect(
root,
targetFile,
pluginOptions,
snykHttpClient,
);
debug('plugin inspect() done');

// attach policy if not provided by plugin
Expand Down
2 changes: 2 additions & 0 deletions src/lib/plugins/get-single-plugin-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import plugins = require('.');
import { ModuleInfo } from '../module-info';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { TestOptions, Options, MonitorOptions } from '../types';
import { snykHttpClient } from '../request/snyk-http-client';

export async function getSinglePluginResult(
root: string,
Expand All @@ -14,6 +15,7 @@ export async function getSinglePluginResult(
root,
targetFile || options.file,
{ ...options },
snykHttpClient,
);
return inspectRes;
}
39 changes: 39 additions & 0 deletions src/lib/request/snyk-http-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as needle from 'needle';
import { OutgoingHttpHeaders } from 'http';
import { NeedleHttpVerbs } from 'needle';
import { makeRequest } from '../request/index';
import { getAuthHeader } from '../api-token';
import config from '../config';
import { Payload } from '../request/types';

interface RequestInfo {
method: NeedleHttpVerbs;
path: string;
body: any;
headers?: OutgoingHttpHeaders;
qs?: {};
json?: boolean;
timeout?: number;
family?: number;
}

export async function snykHttpClient(
requestInfo: RequestInfo,
): Promise<{
res: needle.NeedleResponse;
body: any;
}> {
let { path } = requestInfo;
if (!path.startsWith('/')) path = `/${path}`;

const payload: Payload = {
...requestInfo,
url: `${config.API_REST_URL}${path}`,
headers: {
...requestInfo.headers,
Authorization: getAuthHeader(),
},
};

return makeRequest(payload);
}
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ export type SupportedUserReachableFacingCliArgs =
| 'sub-project'
| 'trust-policies'
| 'yarn-workspaces'
| 'maven-aggregate-project';
| 'maven-aggregate-project'
| 'gradle-normalize-deps';

export enum SupportedCliCommands {
version = 'version',
Expand Down
14 changes: 14 additions & 0 deletions test/tap/cli-monitor.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { DepGraphBuilder } from '@snyk/dep-graph';
import * as depGraphLib from '@snyk/dep-graph';
import { getFixturePath } from '../jest/util/getFixturePath';
import { getWorkspacePath } from '../jest/util/getWorkspacePath';
import { snykHttpClient } from '../../src/lib/request/snyk-http-client';

/*
TODO: enable these tests, once we switch from node-tap
Expand Down Expand Up @@ -927,6 +928,7 @@ if (!isWindows) {
packageManager: 'pip',
path: 'pip-app',
},
snykHttpClient,
],
'calls python plugin',
);
Expand Down Expand Up @@ -990,6 +992,7 @@ if (!isWindows) {
packageManager: 'pip',
path: 'pip-app',
},
snykHttpClient,
],
'calls python plugin',
);
Expand Down Expand Up @@ -1042,6 +1045,7 @@ if (!isWindows) {
file: 'build.gradle',
path: 'gradle-app',
},
snykHttpClient,
],
'calls gradle plugin',
);
Expand Down Expand Up @@ -1111,6 +1115,7 @@ if (!isWindows) {
packageManager: 'gradle',
path: 'gradle-app',
},
snykHttpClient,
],
'calls gradle plugin',
);
Expand Down Expand Up @@ -1170,6 +1175,7 @@ if (!isWindows) {
packageManager: 'gradle',
path: 'gradle-app',
},
snykHttpClient,
],
'calls gradle plugin',
);
Expand Down Expand Up @@ -1224,6 +1230,7 @@ if (!isWindows) {
packageManager: 'gradle',
path: 'gradle-app',
},
snykHttpClient,
],
'calls plugin for the 1st path',
);
Expand All @@ -1239,6 +1246,7 @@ if (!isWindows) {
packageManager: 'pip',
path: 'pip-app',
},
snykHttpClient,
],
'calls plugin for the 2nd path',
);
Expand Down Expand Up @@ -1316,6 +1324,7 @@ if (!isWindows) {
packageManager: 'gomodules',
path: 'golang-gomodules',
},
snykHttpClient,
],
'calls golang plugin',
);
Expand Down Expand Up @@ -1364,6 +1373,7 @@ if (!isWindows) {
packageManager: 'golangdep',
path: 'golang-app',
},
snykHttpClient,
],
'calls golang plugin',
);
Expand Down Expand Up @@ -1412,6 +1422,7 @@ if (!isWindows) {
packageManager: 'govendor',
path: 'golang-app',
},
snykHttpClient,
],
'calls golang plugin',
);
Expand Down Expand Up @@ -1458,6 +1469,7 @@ if (!isWindows) {
packageManager: 'cocoapods',
path: './',
},
snykHttpClient,
],
'calls CocoaPods plugin',
);
Expand Down Expand Up @@ -1506,6 +1518,7 @@ if (!isWindows) {
packageManager: 'cocoapods',
path: './',
},
snykHttpClient,
],
'calls CocoaPods plugin',
);
Expand Down Expand Up @@ -1560,6 +1573,7 @@ if (!isWindows) {
packageManager: 'cocoapods',
path: './',
},
snykHttpClient,
],
'calls CocoaPods plugin',
);
Expand Down
2 changes: 2 additions & 0 deletions test/tap/cli-test.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const after = tap.runOnly ? only : test;
// Should be after `process.env` setup.
import * as plugins from '../../src/lib/plugins/index';
import * as ecoSystemPlugins from '../../src/lib/ecosystems/plugins';
import { snykHttpClient } from '../../src/lib/request/snyk-http-client';

/*
TODO: enable these tests, once we switch from node-tap
Expand Down Expand Up @@ -134,6 +135,7 @@ test('Languages', async (t) => {
languageTest.tests[testName](
{ server, plugins, ecoSystemPlugins, versionNumber, cli },
{ chdirWorkspaces },
snykHttpClient,
),
);
server.restore();
Expand Down
Loading

0 comments on commit b69d4b3

Please sign in to comment.