All files / src executor.ts

38.35% Statements 28/73
100% Branches 4/4
20% Functions 1/5
38.35% Lines 28/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x               1x                                   1x 1x                                   1x 1x 3x 3x 2x 2x 3x 1x 1x 3x 1x 1x 3x 3x  
import { hideSecrets } from "./util/misc.js";
import { Maybe, Nothing } from "purify-ts";
import { green } from "ansis";
import { createLogger } from "./util/log.js";
import { name } from "./info.js";
import { cleanDir } from "./util/fs.js";
import { setEnv, spawnSync } from "./util/proc.js";
 
const log = createLogger(`${name} Analysis`);
 
function logCommandExecution(executable: string, cmdArguments: string[]) {
  const command = `${executable} ${hideSecrets(cmdArguments.join(" "))}`;
  log.info("Running command:", command);
}
 
function executeVersionCheck(executable: string) {
  const versionCmdArguments = ["--version"];
  logCommandExecution(executable, versionCmdArguments);
  return spawnSync(executable, versionCmdArguments, Nothing).map(result =>
    result.stdout.trimEnd(),
  );
}
 
function executeAnalysis(
  executable: string,
  cmdArguments: string[],
  proxyUrl: Maybe<URL>,
  hideOwaspOutput: boolean,
) {
  setEnv("JAVA_OPTS", proxyUrl.map(buildJavaToolOptions), true, log);
  logCommandExecution(executable, cmdArguments);
  return spawnSync(
    executable,
    cmdArguments,
    Maybe.of(hideOwaspOutput ? "ignore" : "inherit"),
  ).map(result => {
    log.info(green`Done.`);
    return result.status;
  });
}
 
export function executeDependencyCheck(
  executable: string,
  cmdArguments: string[],
  outDir: string,
  proxyUrl: Maybe<URL>,
  hideOwaspOutput: boolean,
  javaBinary: Maybe<string>,
) {
  log.info("Dependency-Check Core path:", executable);
  cleanDir(outDir, log);

  setEnv("JAVACMD", javaBinary, false, log);

  return executeVersionCheck(executable).chain(version => {
    log.info(version);
    return executeAnalysis(executable, cmdArguments, proxyUrl, hideOwaspOutput);
  });
}
 
export function buildJavaToolOptions(proxyUrl: URL) {
  let javaToolOptions = `-Dhttps.proxyHost=${proxyUrl.hostname}`;
  if (proxyUrl.port) {
    javaToolOptions += ` -Dhttps.proxyPort=${proxyUrl.port}`;
  }
  if (proxyUrl.username) {
    javaToolOptions += ` -Dhttps.proxyUser=${proxyUrl.username}`;
  }
  if (proxyUrl.password) {
    javaToolOptions += ` -Dhttps.proxyPassword=${proxyUrl.password}`;
  }
  return javaToolOptions;
}