Skip to content

Commit

Permalink
Fixes #649. maxBuffer for spawnSync commands
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <prabhu@appthreat.com>
  • Loading branch information
prabhu committed Oct 20, 2023
1 parent 7a4ce1c commit 59c8252
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ cdxgen can retain the dependency tree under the `dependencies` attribute for a s
| USE_GOSUM | Set to `true` or `1` to generate BOMs for golang projects using go.sum as the dependency source of truth, instead of go.mod |
| CDXGEN_TIMEOUT_MS | Default timeout for known execution involving maven, gradle or sbt |
| CDXGEN_SERVER_TIMEOUT_MS | Default timeout in server mode |
| CDXGEN_MAX_BUFFER | Max buffer for stdout and stderr. Defaults to 100MB |
| CLJ_CMD | Set to override the clojure cli command |
| LEIN_CMD | Set to override the leiningen command |
| SBOM_SIGN_ALGORITHM | Signature algorithm. Some valid values are RS256, RS384, RS512, PS256, PS384, PS512, ES256 etc |
Expand Down
1 change: 1 addition & 0 deletions docs/ENV.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following environment variables are available to configure the bom generatio
| FETCH_LICENSE | Set this variable to `true` or `1` to fetch license information from the registry. npm and golang |
| USE_GOSUM | Set to `true` or `1` to generate BOMs for golang projects using go.sum as the dependency source of truth, instead of go.mod |
| CDXGEN_TIMEOUT_MS | Default timeout for known execution involving maven, gradle or sbt |
| CDXGEN_MAX_BUFFER | Max buffer for stdout and stderr. Defaults to 100MB |
| CDXGEN_SERVER_TIMEOUT_MS | Default timeout in server mode |
| CLJ_CMD | Set to override the clojure cli command |
| LEIN_CMD | Set to override the leiningen command |
Expand Down
47 changes: 32 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ import {
parseCmakeLikeFile,
getCppModules,
FETCH_LICENSE,
TIMEOUT_MS,
MAX_BUFFER,
getNugetMetadata
} from "./utils.js";
import { spawnSync } from "node:child_process";
Expand Down Expand Up @@ -176,9 +178,6 @@ const SBT_CACHE_DIR =
const HASH_PATTERN =
"^([a-fA-F0-9]{32}|[a-fA-F0-9]{40}|[a-fA-F0-9]{64}|[a-fA-F0-9]{96}|[a-fA-F0-9]{128})$";

// Timeout milliseconds. Default 10 mins
const TIMEOUT_MS = parseInt(process.env.CDXGEN_TIMEOUT_MS) || 10 * 60 * 1000;

/**
* Creates a default parent component based on the directory name.
*
Expand Down Expand Up @@ -1210,7 +1209,7 @@ export const createJavaBom = async (path, options) => {
shell: true,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: 50 * 1024 * 1024
maxBuffer: MAX_BUFFER
});
// Check if the cyclonedx plugin created the required bom.xml file
// Sometimes the plugin fails silently for complex maven projects
Expand All @@ -1235,7 +1234,8 @@ export const createJavaBom = async (path, options) => {
cwd: basePath,
shell: true,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
console.error(result.stdout, result.stderr);
Expand Down Expand Up @@ -1490,7 +1490,8 @@ export const createJavaBom = async (path, options) => {
const sresult = spawnSync(gradleCmd, gradleDepArgs, {
cwd: path,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (sresult.status !== 0 || sresult.error) {
if (options.failOnError || DEBUG_MODE) {
Expand Down Expand Up @@ -1594,7 +1595,8 @@ export const createJavaBom = async (path, options) => {
cwd: basePath,
shell: true,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
if (result.stderr) {
Expand Down Expand Up @@ -1626,7 +1628,7 @@ export const createJavaBom = async (path, options) => {
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: 1024 * 1024 * 100
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
console.error(result.stdout, result.stderr);
Expand Down Expand Up @@ -1783,7 +1785,8 @@ export const createJavaBom = async (path, options) => {
cwd: basePath,
shell: true,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
console.error(result.stdout, result.stderr);
Expand Down Expand Up @@ -2651,7 +2654,12 @@ export const createGoBom = async (path, options) => {
const mresult = spawnSync(
"go",
["mod", "why", "-m", "-vendor", pkgFullName],
{ cwd: path, encoding: "utf-8", timeout: TIMEOUT_MS }
{
cwd: path,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
}
);
if (mresult.status !== 0 || mresult.error) {
if (DEBUG_MODE) {
Expand Down Expand Up @@ -2740,7 +2748,12 @@ export const createGoBom = async (path, options) => {
"'{{with .Module}}{{.Path}} {{.Version}} {{.Indirect}} {{.GoMod}} {{.GoVersion}} {{.Main}}{{end}}'",
"./..."
],
{ cwd: basePath, encoding: "utf-8", timeout: TIMEOUT_MS }
{
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
}
);
if (DEBUG_MODE) {
console.log("Executing go mod graph in", basePath);
Expand Down Expand Up @@ -2774,7 +2787,8 @@ export const createGoBom = async (path, options) => {
result = spawnSync("go", ["mod", "graph"], {
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
// Check if got a mod graph successfully
if (result.status !== 0 || result.error) {
Expand Down Expand Up @@ -3162,7 +3176,8 @@ export const createClojureBom = (path, options) => {
const result = spawnSync(LEIN_CMD, LEIN_ARGS, {
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
if (result.stderr) {
Expand Down Expand Up @@ -3209,7 +3224,8 @@ export const createClojureBom = (path, options) => {
const result = spawnSync(CLJ_CMD, CLJ_ARGS, {
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
});
if (result.status !== 0 || result.error) {
if (result.stderr) {
Expand Down Expand Up @@ -3556,7 +3572,8 @@ export const createSwiftBom = (path, options) => {
{
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER
}
);
if (result.status === 0 && result.stdout) {
Expand Down
7 changes: 6 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ export const DEBUG_MODE =
process.env.NODE_ENV === "development";

// Timeout milliseconds. Default 20 mins
const TIMEOUT_MS = parseInt(process.env.CDXGEN_TIMEOUT_MS) || 20 * 60 * 1000;
export const TIMEOUT_MS =
parseInt(process.env.CDXGEN_TIMEOUT_MS) || 20 * 60 * 1000;

// Max buffer for stdout and stderr. Defaults to 100MB
export const MAX_BUFFER =
parseInt(process.env.CDXGEN_MAX_BUFFER) || 100 * 1024 * 1024;

// Metadata cache
export let metadata_cache = {};
Expand Down

0 comments on commit 59c8252

Please sign in to comment.