Skip to content

Commit

Permalink
fix: calculate the policy folder from targetFile
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Jun 19, 2020
1 parent 7c56e25 commit e75db65
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
27 changes: 27 additions & 0 deletions src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as Debug from 'debug';
import * as path from 'path';

import * as depGraphLib from '@snyk/dep-graph';
import * as snyk from '..';
import { apiTokenExists } from '../api-token';
Expand Down Expand Up @@ -185,13 +187,21 @@ async function monitorDepTree(
treeMissingDeps = missingDeps;
}

let targetFileDir;

if (targetFileRelativePath) {
const { dir } = path.parse(targetFileRelativePath);
targetFileDir = dir;
}

const policy = await findAndLoadPolicy(
root,
meta.isDocker ? 'docker' : packageManager!,
options,
// TODO: fix this and send only send when we used resolve-deps for node
// it should be a ExpandedPkgTree type instead
depTree,
targetFileDir,
);

const target = await projectMetadata.getInfo(scannedProject, meta, depTree);
Expand Down Expand Up @@ -328,10 +338,19 @@ export async function monitorDepGraph(
);
}

let targetFileDir;

if (targetFileRelativePath) {
const { dir } = path.parse(targetFileRelativePath);
targetFileDir = dir;
}

const policy = await findAndLoadPolicy(
root,
meta.isDocker ? 'docker' : packageManager!,
options,
undefined,
targetFileDir,
);

const target = await projectMetadata.getInfo(scannedProject, meta);
Expand Down Expand Up @@ -443,13 +462,21 @@ async function experimentalMonitorDepGraphFromDepTree(
);
}

let targetFileDir;

if (targetFileRelativePath) {
const { dir } = path.parse(targetFileRelativePath);
targetFileDir = dir;
}

const policy = await findAndLoadPolicy(
root,
meta.isDocker ? 'docker' : packageManager!,
options,
// TODO: fix this and send only send when we used resolve-deps for node
// it should be a ExpandedPkgTree type instead
depTree,
targetFileDir,
);

if (['npm', 'yarn'].includes(meta.packageManager)) {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/policy/find-and-load-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ export async function findAndLoadPolicy(
scanType: SupportedPackageManagers | 'docker',
options: PolicyOptions,
pkg?: PackageExpanded,
scannedProjectFolder?: string,
): Promise<string | undefined> {
const isDocker = scanType === 'docker';
const isNodeProject = ['npm', 'yarn'].includes(scanType);
// monitor
let policyLocations: string[] = [options['policy-path'] || root];
let policyLocations: string[] = [options['policy-path'] || scannedProjectFolder || root];
if (isDocker) {
policyLocations = policyLocations.filter((loc) => loc !== root);
} else if (isNodeProject) {
// TODO: pluckPolicies expects a package.json object to
// find and apply policies in node_modules
policyLocations = policyLocations.concat(pluckPolicies(pkg as PackageJson));
}

debug('Potential policy locations found:', policyLocations);
analytics.add('policies', policyLocations.length);
analytics.add('policyLocations', policyLocations);
Expand Down
27 changes: 17 additions & 10 deletions src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ async function runTest(
_.get(payload, 'body.originalProjectName');
const foundProjectCount = _.get(payload, 'body.foundProjectCount');
const displayTargetFile = _.get(payload, 'body.displayTargetFile');

let dockerfilePackages;
if (
payload.body &&
Expand Down Expand Up @@ -403,13 +402,30 @@ async function assembleLocalPayloads(
}
}

// todo: normalize what target file gets used across plugins and functions
const targetFile =
scannedProject.targetFile || deps.plugin.targetFile || options.file;

// Forcing options.path to be a string as pathUtil requires is to be stringified
const targetFileRelativePath = targetFile
? pathUtil.join(pathUtil.resolve(`${options.path || root}`), targetFile)
: '';

let targetFileDir;

if (targetFileRelativePath) {
const { dir } = path.parse(targetFileRelativePath);
targetFileDir = dir;
}

const policy = await findAndLoadPolicy(
root,
options.docker ? 'docker' : packageManager!,
options,
// TODO: fix this and send only send when we used resolve-deps for node
// it should be a ExpandedPkgTree type instead
pkg,
targetFileDir,
);

analytics.add('packageManager', packageManager);
Expand All @@ -422,15 +438,6 @@ async function assembleLocalPayloads(
addPackageAnalytics(depTree.name!, depTree.version!);
}

// todo: normalize what target file gets used across plugins and functions
const targetFile =
scannedProject.targetFile || deps.plugin.targetFile || options.file;

// Forcing options.path to be a string as pathUtil requires is to be stringified
const targetFileRelativePath = targetFile
? pathUtil.join(pathUtil.resolve(`${options.path}`), targetFile)
: '';

let target: GitTarget | ContainerTarget | null;
if (scannedProject.depGraph) {
target = await projectMetadata.getInfo(scannedProject, options);
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/cli-monitor/cli-monitor.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as _ from '@snyk/lodash';

// ensure this is required *after* the demo server, since this will
// configure our fake configuration too
import * as snykPolicy from 'snyk-policy';
import { AllProjectsTests } from './cli-monitor.all-projects.spec';

const { test, only } = tap;
Expand Down
Empty file.

0 comments on commit e75db65

Please sign in to comment.