@contrast/contrast 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ export const HIGH = 'HIGH';
17
17
  export const CRITICAL = 'CRITICAL';
18
18
  // App
19
19
  export const APP_NAME = 'contrast';
20
- const APP_VERSION = '3.1.0';
20
+ const APP_VERSION = '3.2.0';
21
21
  export const TIMEOUT = 120000;
22
22
  export const CRITICAL_PRIORITY = 1;
23
23
  export const HIGH_PRIORITY = 2;
@@ -74,6 +74,9 @@ export const calculateMavenCommand = projectData => {
74
74
  else {
75
75
  projectData.mvnCommand = 'mvn';
76
76
  }
77
+ if (projectData.mvnCommand.includes('//')) {
78
+ projectData.mvnCommand = projectData.mvnCommand.replace(/\/+/g, '/');
79
+ }
77
80
  };
78
81
  export function checkForErrors(cmdDepTree, config) {
79
82
  checkMavenExists(cmdDepTree, config);
@@ -1,30 +1,39 @@
1
1
  import scanConfig from './scanConfig.js';
2
- import { startScan } from './scanController.js';
2
+ import { startScan, startScanWithResults } from './scanController.js';
3
3
  import { saveScanFile } from '../utils/saveFile.js';
4
4
  import { ScanResultsModel } from './models/scanResultsModel.js';
5
5
  import { formatScanOutput } from './formatScanOutput.js';
6
6
  import { sendTelemetryConfigAsObject } from '../telemetry/telemetry.js';
7
7
  import { processFail } from '../common/fail.js';
8
8
  import { postRunMessage } from '../common/commonHelp.js';
9
- import { logInfo } from '../common/logging.js';
9
+ import { logDebug, logInfo } from '../common/logging.js';
10
10
  import { validateHost } from '../utils/validationCheck.js';
11
11
  export const processScan = async (contrastConf, argv) => {
12
12
  let config = await scanConfig.getScanConfig(contrastConf, 'scan', argv);
13
13
  validateHost(config);
14
- let output = undefined;
15
- const scanResults = new ScanResultsModel(await startScan(config));
16
- await sendTelemetryConfigAsObject(config, 'scan', argv, 'SUCCESS', scanResults.scanDetail.language);
17
- if (scanResults.scanResultsInstances !== undefined) {
18
- output = formatScanOutput(scanResults);
19
- }
20
- if (config.save !== undefined) {
21
- await saveScanFile(config, scanResults);
14
+ let scanResults;
15
+ if (config.ff) {
16
+ logDebug(config, 'Is fire and forget request');
17
+ await startScan(config);
22
18
  }
23
19
  else {
24
- logInfo('\nUse contrast scan --save to save results as a SARIF');
20
+ scanResults = new ScanResultsModel(await startScanWithResults(config));
25
21
  }
26
- if (config.fail) {
27
- processFail(config, output);
22
+ await sendTelemetryConfigAsObject(config, 'scan', argv, 'SUCCESS', config.ff ? 'UNKNOWN' : scanResults.scanDetail.language);
23
+ if (!config.ff) {
24
+ let output;
25
+ if (scanResults.scanResultsInstances !== undefined) {
26
+ output = formatScanOutput(scanResults);
27
+ }
28
+ if (config.save !== undefined) {
29
+ await saveScanFile(config, scanResults);
30
+ }
31
+ else {
32
+ logInfo('\nUse contrast scan --save to save results as a SARIF');
33
+ }
34
+ if (config.fail) {
35
+ processFail(config, output);
36
+ }
28
37
  }
29
38
  postRunMessage('scan');
30
39
  };
package/dist/scan/scan.js CHANGED
@@ -13,7 +13,7 @@ export const isFileAllowed = (scanOption) => {
13
13
  });
14
14
  return valid;
15
15
  };
16
- export const sendScan = async (config) => {
16
+ export const uploadArtifact = async (config) => {
17
17
  if (!isFileAllowed(config.file)) {
18
18
  const message = i18n.__('scanErrorFileMessage');
19
19
  logDebug(config, message);
@@ -1,12 +1,12 @@
1
1
  import i18n from 'i18n';
2
2
  import { populateProjectId } from './populateProjectIdAndProjectName.js';
3
- import { sendScan } from './scan.js';
3
+ import { uploadArtifact } from './scan.js';
4
4
  import { performance } from 'perf_hooks';
5
5
  import { fileExists, fileIsEmpty } from './fileUtils.js';
6
6
  import { returnOra, startSpinner, stopSpinner, succeedSpinner } from '../utils/oraWrapper.js';
7
7
  import { returnScanResults } from './scanResults.js';
8
8
  import { autoDetectFileAndLanguage } from '../common/autoDetection.js';
9
- import { getScanResultsInstances } from './scanRequests.js';
9
+ import { submitScanForArtifact, getScanResultsInstances } from './scanRequests.js';
10
10
  import { logDebug, logInfo } from '../common/logging.js';
11
11
  const getTimeout = config => {
12
12
  if (config.timeout) {
@@ -40,7 +40,6 @@ export const fileAndLanguageLogic = async (configToUse) => {
40
40
  }
41
41
  };
42
42
  export const startScan = async (configToUse) => {
43
- const startTime = performance.now();
44
43
  await fileAndLanguageLogic(configToUse);
45
44
  let newProject;
46
45
  if (!configToUse.projectId) {
@@ -51,28 +50,33 @@ export const startScan = async (configToUse) => {
51
50
  else {
52
51
  newProject = false;
53
52
  }
54
- const codeArtifactId = await sendScan(configToUse);
55
- if (!configToUse.ff) {
56
- const startScanSpinner = returnOra('🚀 Contrast Scan started');
57
- startSpinner(startScanSpinner);
58
- const scanDetail = await returnScanResults(configToUse, codeArtifactId, newProject, getTimeout(configToUse), startScanSpinner);
59
- const scanResultsInstances = await getScanResultsInstances(configToUse, scanDetail.id);
60
- const endTime = performance.now();
61
- const scanDurationMs = endTime - startTime;
62
- if (scanResultsInstances.statusCode !== 200) {
63
- stopSpinner(startScanSpinner);
64
- const message = 'Result Service is unavailable, please try again later';
65
- logInfo(message);
66
- logDebug(configToUse, message);
67
- process.exit(1);
68
- }
69
- else {
70
- succeedSpinner(startScanSpinner, 'Contrast Scan complete');
71
- logInfo(`----- Scan completed in ${(scanDurationMs / 1000).toFixed(2)}s -----`);
72
- return {
73
- scanDetail,
74
- scanResultsInstances: scanResultsInstances.body
75
- };
76
- }
53
+ const codeArtifactId = await uploadArtifact(configToUse);
54
+ const scanId = await submitScanForArtifact(configToUse, codeArtifactId);
55
+ logDebug(configToUse, `For proj: ${configToUse.projectId}, is new: ${newProject}, new scanId submitted: ${scanId}`);
56
+ return { newProject, scanId };
57
+ };
58
+ export const startScanWithResults = async (configToUse) => {
59
+ const startTime = performance.now();
60
+ const { newProject, scanId } = await startScan(configToUse);
61
+ const startScanSpinner = returnOra('🚀 Contrast Scan started');
62
+ startSpinner(startScanSpinner);
63
+ const scanDetail = await returnScanResults(configToUse, scanId, newProject, getTimeout(configToUse), startScanSpinner);
64
+ const scanResultsInstances = await getScanResultsInstances(configToUse, scanDetail.id);
65
+ const endTime = performance.now();
66
+ const scanDurationMs = endTime - startTime;
67
+ if (scanResultsInstances.statusCode !== 200) {
68
+ stopSpinner(startScanSpinner);
69
+ const message = 'Result Service is unavailable, please try again later';
70
+ logInfo(message);
71
+ logDebug(configToUse, message);
72
+ process.exit(1);
73
+ }
74
+ else {
75
+ succeedSpinner(startScanSpinner, 'Contrast Scan complete');
76
+ logInfo(`----- Scan completed in ${(scanDurationMs / 1000).toFixed(2)}s -----`);
77
+ return {
78
+ scanDetail,
79
+ scanResultsInstances: scanResultsInstances.body
80
+ };
77
81
  }
78
82
  };
@@ -2,6 +2,7 @@ import { buildBaseRequestOptions, ErrorType } from '../common/baseRequest.js';
2
2
  import { got } from 'got';
3
3
  import fs from 'fs';
4
4
  import FormData from 'form-data';
5
+ import { logDebug } from '../common/logging.js';
5
6
  export function createProjectId(config) {
6
7
  const options = buildBaseRequestOptions(config, ErrorType.SCAN);
7
8
  options.json = {
@@ -38,17 +39,18 @@ export async function sendArtifact(config) {
38
39
  function createHarmonyUrl(config) {
39
40
  return `${config.host}/Contrast/api/sast/v1/organizations/${config.organizationId}/projects/${config.projectId}/code-artifacts`;
40
41
  }
41
- export function getScanId(config, codeArtifactId) {
42
+ export const submitScanForArtifact = async (config, codeArtifactId) => {
42
43
  const options = buildBaseRequestOptions(config, ErrorType.SCAN);
43
44
  options.url = createGetScanIdURL(config);
44
45
  options.json = {
45
46
  codeArtifactId: codeArtifactId,
46
- label: config.label
47
- ? config.label
48
- : `Started by CLI tool at ${new Date().toString()}`
47
+ label: config.label ?? `Started by CLI tool at ${new Date().toString()}`
49
48
  };
50
- return got.post(options);
51
- }
49
+ logDebug(config, `Kicked off scan for project ${config.projectId} and codeArtifact: ${codeArtifactId}`);
50
+ const post = (await got.post(options));
51
+ const body = post.body;
52
+ return body.id;
53
+ };
52
54
  const createGetScanIdURL = (config) => {
53
55
  return `${config.host}/Contrast/api/sast/v1/organizations/${config.organizationId}/projects/${config.projectId}/scans/`;
54
56
  };
@@ -2,15 +2,13 @@ import { sleep, millisToSeconds, timeOutError } from '../utils/requestUtils.js';
2
2
  import i18n from 'i18n';
3
3
  import readline from 'readline';
4
4
  import { failSpinner, stopSpinner } from '../utils/oraWrapper.js';
5
- import { createNewEvent, getScanId, getSpecificScanResult } from './scanRequests.js';
5
+ import { createNewEvent, getSpecificScanResult } from './scanRequests.js';
6
6
  import { logDebug, logInfo } from '../common/logging.js';
7
7
  export const pollScanResults = async (config, scanId) => {
8
8
  await sleep(5000);
9
9
  return getSpecificScanResult(config, scanId);
10
10
  };
11
- export const returnScanResults = async (config, codeArtifactId, newProject, timeout, startScanSpinner) => {
12
- const scanIdRes = await getScanId(config, codeArtifactId);
13
- const scanId = scanIdRes.body.id;
11
+ export const returnScanResults = async (config, scanId, newProject, timeout, startScanSpinner) => {
14
12
  // send metrics event to sast-event-collector
15
13
  if (process.env.CODESEC_INVOCATION_ENVIRONMENT &&
16
14
  process.env.CODESEC_INVOCATION_ENVIRONMENT.toUpperCase() === 'GITHUB') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/contrast",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Contrast Security's command line tool",
5
5
  "exports": "./dist/index.js",
6
6
  "type": "module",