"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/policy/find-and-load-policy.ts" (20 Feb 2024, 1827 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as snykPolicyLib from 'snyk-policy';
    2 import * as debugModule from 'debug';
    3 import { PackageExpanded } from 'snyk-resolve-deps';
    4 
    5 import { pluckPolicies } from '.';
    6 import { SupportedPackageManagers } from '../package-managers';
    7 import { PackageJson, PolicyOptions } from '../types';
    8 import * as analytics from '../analytics';
    9 
   10 const debug = debugModule('snyk');
   11 
   12 export async function findAndLoadPolicy(
   13   root: string,
   14   scanType: SupportedPackageManagers | 'docker' | 'iac' | 'cpp',
   15   options: PolicyOptions,
   16   pkg?: PackageExpanded,
   17   scannedProjectFolder?: string,
   18 ): Promise<Policy | undefined> {
   19   const isDocker = scanType === 'docker';
   20   const isNodeProject = ['npm', 'yarn'].includes(scanType);
   21   // monitor
   22   let policyLocations: string[] = [
   23     options['policy-path'] || scannedProjectFolder || root,
   24   ];
   25   if (isDocker) {
   26     policyLocations = policyLocations.filter((loc) => loc !== root);
   27   } else if (isNodeProject) {
   28     // TODO: pluckPolicies expects a package.json object to
   29     // find and apply policies in node_modules
   30     policyLocations = policyLocations.concat(pluckPolicies(pkg as PackageJson));
   31   }
   32 
   33   debug('Potential policy locations found:', policyLocations);
   34   analytics.add('policies', policyLocations.length);
   35   analytics.add('policyLocations', policyLocations);
   36 
   37   if (policyLocations.length === 0) {
   38     return;
   39   }
   40   let policy;
   41   try {
   42     policy = await snykPolicyLib.load(policyLocations, options);
   43   } catch (err) {
   44     // note: inline catch, to handle error from .load
   45     // if the .snyk file wasn't found, it is fine
   46     if (err.code !== 'ENOENT' && err.code !== 'ENOTDIR') {
   47       throw err;
   48     }
   49   }
   50   return policy;
   51 }
   52 
   53 export interface Policy {
   54   filter(vulns: any, root?: string, matchStrategy?: string): any;
   55   exclude?: { [key: string]: string[] };
   56   ignore?: any;
   57 }