Skip to content

Commit

Permalink
Merge pull request #171 from snyk/feat/gradle-config-attributes-class…
Browse files Browse the repository at this point in the history
…path-filter

pass configattrs to javaCallGraphBuilder
  • Loading branch information
Dar Malovani authored May 20, 2021
2 parents bdbd479 + 9b173df commit 98b48cf
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 73 deletions.
7 changes: 7 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ export async function inspect(
);
}

let confAttrs: string | undefined;

if (options['configuration-attributes']) {
confAttrs = options['configuration-attributes'];
}

debugLog(`getting call graph from path ${targetPath}`);
callGraph = await javaCallGraphBuilder.getCallGraphGradle(
path.dirname(targetPath),
command,
initScriptPath,
confAttrs,
);
debugLog('got call graph successfully');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"@snyk/cli-interface": "2.11.0",
"@snyk/dep-graph": "^1.28.0",
"@snyk/java-call-graph-builder": "1.20.0",
"@snyk/java-call-graph-builder": "1.22.0",
"@types/debug": "^4.1.4",
"chalk": "^3.0.0",
"debug": "^4.1.1",
Expand Down
73 changes: 1 addition & 72 deletions test/system/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as path from 'path';
import { fixtureDir } from '../common';
import { test } from 'tap';
import { inspect, formatArgWithWhiteSpace } from '../../lib';
import * as fs from 'fs';
import * as sinon from 'sinon';
import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder';
import { CallGraph } from '@snyk/cli-interface/legacy/common';
import { inspect } from '../../lib';

const rootNoWrapper = fixtureDir('no wrapper');
const withInitScript = fixtureDir('with-init-script');
Expand Down Expand Up @@ -59,73 +55,6 @@ test('run inspect() with on project that depends on gradle init script', async (
);
});

test('run inspect() with reachableVulns', async (t) => {
const gradleCallGraph = JSON.parse(
fs.readFileSync(
path.join(fixtureDir('call-graphs'), 'simple.json'),
'utf-8',
),
);
const javaCallGraphBuilderStub = sinon
.stub(javaCallGraphBuilder, 'getCallGraphGradle')
.resolves(gradleCallGraph as CallGraph);
const result = await inspect('.', path.join(rootNoWrapper, 'build.gradle'), {
reachableVulns: true,
});

const pkgs = result.dependencyGraph.getDepPkgs();
const nodeIds: string[] = [];
Object.keys(pkgs).forEach((id) => {
nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`);
});

t.ok(
nodeIds.indexOf('com.android.tools:[email protected]') !== -1,
'correct version found',
);
t.ok(javaCallGraphBuilderStub.calledOnce, 'called to the call graph builder');
t.ok(
javaCallGraphBuilderStub.calledWith(
path.join('.', rootNoWrapper),
'gradle',
),
'call graph builder was called with the correct path',
);
t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph');

const resultWithInit = await inspect(
'.',
path.join(rootNoWrapper, 'build.gradle'),
{
reachableVulns: true,
initScript: path.join(rootNoWrapper, 'init.gradle'),
},
);

// test with init script param
t.ok(
javaCallGraphBuilderStub.calledTwice,
'called to the call graph builder',
);
t.ok(
javaCallGraphBuilderStub.calledWith(
path.join('.', rootNoWrapper),
'gradle',
formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes
),
'call graph builder was called with the correct path and init file',
);
t.same(
gradleCallGraph,
resultWithInit.callGraph,
'returns expected callgraph',
);

t.teardown(() => {
javaCallGraphBuilderStub.restore();
});
});

test('multi-config: both compile and runtime deps picked up by default', async (t) => {
const result = await inspect(
'.',
Expand Down
119 changes: 119 additions & 0 deletions test/system/reachability.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import * as path from 'path';
import { fixtureDir } from '../common';
import { test } from 'tap';
import { inspect, formatArgWithWhiteSpace } from '../../lib';
import * as fs from 'fs';
import * as sinon from 'sinon';
import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder';
import { CallGraph } from '@snyk/cli-interface/legacy/common';

const rootNoWrapper = fixtureDir('no wrapper');

test('reachableVulns', async (t) => {
const gradleCallGraph = JSON.parse(
fs.readFileSync(
path.join(fixtureDir('call-graphs'), 'simple.json'),
'utf-8',
),
);
const javaCallGraphBuilderStub = sinon
.stub(javaCallGraphBuilder, 'getCallGraphGradle')
.resolves(gradleCallGraph as CallGraph);

t.test('simple reachability scenario', async (t) => {
const result = await inspect(
'.',
path.join(rootNoWrapper, 'build.gradle'),
{
reachableVulns: true,
},
);

const pkgs = result.dependencyGraph.getDepPkgs();
const nodeIds: string[] = [];
Object.keys(pkgs).forEach((id) => {
nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`);
});

t.ok(
nodeIds.indexOf('com.android.tools:[email protected]') !== -1,
'correct version found',
);
t.ok(
javaCallGraphBuilderStub.calledOnce,
'called to the call graph builder',
);
t.ok(
javaCallGraphBuilderStub.calledWith(
path.join('.', rootNoWrapper),
'gradle',
),
'call graph builder was called with the correct path',
);
t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph');
});

t.test('with init script', async (t) => {
const resultWithInit = await inspect(
'.',
path.join(rootNoWrapper, 'build.gradle'),
{
reachableVulns: true,
initScript: path.join(rootNoWrapper, 'init.gradle'),
},
);

t.ok(
javaCallGraphBuilderStub.calledTwice,
'called to the call graph builder',
);
t.ok(
javaCallGraphBuilderStub.calledWith(
path.join('.', rootNoWrapper),
'gradle',
formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes
),
'call graph builder was called with the correct path and init file',
);
t.same(
gradleCallGraph,
resultWithInit.callGraph,
'returns expected callgraph',
);
});

t.test('with configuration attributes', async (t) => {
const resultWithConfigAttrs = await inspect(
'.',
path.join(rootNoWrapper, 'build.gradle'),
{
reachableVulns: true,
'configuration-attributes':
'buildtype:release,usage:java-runtime,newdim:appA',
},
);

t.ok(
javaCallGraphBuilderStub.calledThrice,
'called to the call graph builder',
);
t.ok(
javaCallGraphBuilderStub.calledWith(
path.join('.', rootNoWrapper),
'gradle',
undefined,
'buildtype:release,usage:java-runtime,newdim:appA',
),
'call graph builder was called with the correct path and init file',
);
t.same(
gradleCallGraph,
resultWithConfigAttrs.callGraph,
'returns expected callgraph',
);
});

t.teardown(() => {
javaCallGraphBuilderStub.restore();
});
});

0 comments on commit 98b48cf

Please sign in to comment.