"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/iac/drift.ts" (20 Feb 2024, 3373 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 fs from 'fs';
    2 import {
    3   DescribeOptions,
    4   DriftAnalysis,
    5   DriftctlExecutionResult,
    6   GenDriftIgnoreOptions,
    7 } from './types';
    8 import { Policy } from '../policy/find-and-load-policy';
    9 import snykLogoSVG from './assets/snyk-logo';
   10 import snykFaviconBase64 from './assets/snyk-favicon';
   11 import { getHumanReadableAnalysis } from './drift/output';
   12 import { runDriftCTL } from './drift/driftctl';
   13 
   14 export const parseDriftAnalysisResults = (input: string): DriftAnalysis => {
   15   return JSON.parse(input) as DriftAnalysis;
   16 };
   17 
   18 export function driftignoreFromPolicy(policy: Policy | undefined): string[] {
   19   const excludeSection = 'iac-drift';
   20   if (!policy || !policy.exclude || !(excludeSection in policy.exclude)) {
   21     return [];
   22   }
   23   return policy.exclude[excludeSection];
   24 }
   25 
   26 export const updateExcludeInPolicy = (
   27   policy: Policy,
   28   analysis: DriftAnalysis,
   29   options: GenDriftIgnoreOptions,
   30 ): void => {
   31   const excludedResources = driftignoreFromPolicy(policy);
   32   const addResource = (res) => excludedResources.push(`${res.type}.${res.id}`);
   33 
   34   if (!options['exclude-missing'] && analysis.summary.total_missing > 0) {
   35     analysis.missing?.forEach((res) => addResource(res));
   36   }
   37 
   38   if (!options['exclude-unmanaged'] && analysis.summary.total_unmanaged > 0) {
   39     analysis.unmanaged?.forEach((res) => addResource(res));
   40   }
   41 
   42   if (!policy.exclude) {
   43     policy.exclude = {};
   44   }
   45 
   46   policy.exclude['iac-drift'] = excludedResources;
   47 };
   48 
   49 export async function processAnalysis(
   50   options: DescribeOptions,
   51   describe: DriftctlExecutionResult,
   52 ): Promise<string> {
   53   if (options.html || options['html-file-output']) {
   54     // we use fmt for html output
   55     const fmtResult = await runDriftCTL({
   56       options: { ...options, kind: 'fmt' },
   57       input: describe.stdout,
   58     });
   59     const output = processHTMLOutput(options, fmtResult.stdout);
   60 
   61     if (options.html) {
   62       // html on stdout
   63       return output;
   64     }
   65     // should return an empty string if we use the html-file-output flag
   66     return '';
   67   }
   68 
   69   if (options.json) {
   70     // json on stdout
   71     return describe.stdout;
   72   }
   73 
   74   const analysis = parseDriftAnalysisResults(describe.stdout);
   75   return getHumanReadableAnalysis(options, analysis);
   76 }
   77 
   78 export function processHTMLOutput(
   79   options: DescribeOptions,
   80   stdout: string,
   81 ): string {
   82   if (options.html) {
   83     stdout = rebrandHTMLOutput(stdout);
   84   }
   85 
   86   if (options['html-file-output']) {
   87     const data = fs.readFileSync(options['html-file-output'], {
   88       encoding: 'utf8',
   89     });
   90     fs.writeFileSync(options['html-file-output'], rebrandHTMLOutput(data));
   91   }
   92 
   93   return stdout;
   94 }
   95 
   96 function rebrandHTMLOutput(data: string): string {
   97   // Replace favicon
   98   const faviconReplaceRegex = new RegExp(
   99     '(<link rel="shortcut icon")(.*)(\\/>)',
  100     'g',
  101   );
  102   data = data.replace(
  103     faviconReplaceRegex,
  104     `<link rel="shortcut icon" type="image/x-icon" href="${snykFaviconBase64}" />`,
  105   );
  106 
  107   // Replace HTML title
  108   const titleReplaceRegex = new RegExp('(<title>)(.*)(<\\/title>)', 'g');
  109   data = data.replace(
  110     titleReplaceRegex,
  111     `<title>Snyk IaC drift report</title>`,
  112   );
  113 
  114   // Replace header brand logo
  115   const logoReplaceRegex = new RegExp(
  116     '(<div id="brand_logo">)((.|\\r|\\n)*?)(<\\/div>)',
  117     'g',
  118   );
  119   data = data.replace(
  120     logoReplaceRegex,
  121     `<div id="brand_logo">${snykLogoSVG}</div>`,
  122   );
  123 
  124   return data;
  125 }