Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unify policy handling & plucking
- Loading branch information
Showing
9 changed files
with
146 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as snykPolicyLib from 'snyk-policy'; | ||
import * as debugModule from 'debug'; | ||
|
||
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?: PackageJson, | ||
): Promise<string | undefined> { | ||
const isDocker = scanType === 'docker'; | ||
const isNodeProject = ['npm', 'yarn'].includes(scanType); | ||
// monitor | ||
let policyLocations: string[] = [options['policy-path'] || 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { pluckPolicies } from './pluck-policies'; | ||
export { findAndLoadPolicy } from './find-and-load-policy'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import * as _ from '@snyk/lodash'; | ||
import { PackageJson } from '../types'; | ||
|
||
export function pluckPolicies(pkg) { | ||
export function pluckPolicies(pkg: PackageJson): string[] { | ||
if (!pkg) { | ||
return null; | ||
return []; | ||
} | ||
|
||
if (pkg.snyk) { | ||
return pkg.snyk; | ||
return []; // why is this check here? | ||
} | ||
|
||
if (!pkg.dependencies) { | ||
return null; | ||
return []; | ||
} | ||
|
||
return _.flatten( | ||
Object.keys(pkg.dependencies) | ||
.map((name) => { | ||
return pluckPolicies(pkg.dependencies[name]); | ||
}) | ||
.map((name: string) => pluckPolicies(pkg.dependencies[name])) | ||
.filter(Boolean), | ||
); | ||
} |
Oops, something went wrong.