-
Notifications
You must be signed in to change notification settings - Fork 533
/
find-and-load-policy.ts
51 lines (46 loc) · 1.59 KB
/
find-and-load-policy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as snykPolicyLib from 'snyk-policy';
import * as debugModule from 'debug';
import { PackageExpanded } from 'snyk-resolve-deps';
import { pluckPolicies } from '.';
import { SupportedPackageManagers } from '../package-managers';
import { PackageJson, PolicyOptions } from '../types';
import * as analytics from '../analytics';
const debug = debugModule('snyk');
export async function findAndLoadPolicy(
root: string,
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'] || 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);
if (policyLocations.length === 0) {
return;
}
let policy;
try {
policy = await snykPolicyLib.load(policyLocations, options);
} catch (err) {
// note: inline catch, to handle error from .load
// if the .snyk file wasn't found, it is fine
if (err.code !== 'ENOENT') {
throw err;
}
}
return policy;
}