11#!/usr/bin/env node
2-
2+ import { Buffer } from "node:buffer" ;
33import crypto from "node:crypto" ;
44import fs from "node:fs" ;
55import { basename , dirname , join , resolve } from "node:path" ;
66import process from "node:process" ;
7- import { URL } from "node:url" ;
87import { findUpSync } from "find-up" ;
98import globalAgent from "global-agent" ;
109import { load as _load } from "js-yaml" ;
@@ -781,6 +780,13 @@ const checkPermissions = (filePath, options) => {
781780 return true ;
782781} ;
783782
783+ const needsBomSigning = ( { generateKeyAndSign } ) =>
784+ generateKeyAndSign ||
785+ ( process . env . SBOM_SIGN_ALGORITHM &&
786+ process . env . SBOM_SIGN_ALGORITHM !== "none" &&
787+ process . env . SBOM_SIGN_PRIVATE_KEY &&
788+ safeExistsSync ( process . env . SBOM_SIGN_PRIVATE_KEY ) ) ;
789+
784790/**
785791 * Method to start the bom creation process
786792 */
@@ -838,14 +844,7 @@ const checkPermissions = (filePath, options) => {
838844 thoughtLog ( `Let's save the file to "${ jsonFile } ".` ) ;
839845 }
840846 }
841- if (
842- jsonPayload &&
843- ( options . generateKeyAndSign ||
844- ( process . env . SBOM_SIGN_ALGORITHM &&
845- process . env . SBOM_SIGN_ALGORITHM !== "none" &&
846- process . env . SBOM_SIGN_PRIVATE_KEY &&
847- safeExistsSync ( process . env . SBOM_SIGN_PRIVATE_KEY ) ) )
848- ) {
847+ if ( jsonPayload && needsBomSigning ( options ) ) {
849848 let alg = process . env . SBOM_SIGN_ALGORITHM || "RS512" ;
850849 if ( alg . includes ( "none" ) ) {
851850 alg = "RS512" ;
@@ -857,6 +856,7 @@ const checkPermissions = (filePath, options) => {
857856 const jdirName = dirname ( jsonFile ) ;
858857 publicKeyFile = join ( jdirName , "public.key" ) ;
859858 const privateKeyFile = join ( jdirName , "private.key" ) ;
859+ const privateKeyB64File = join ( jdirName , "private.key.base64" ) ;
860860 const { privateKey, publicKey } = crypto . generateKeyPairSync ( "rsa" , {
861861 modulusLength : 4096 ,
862862 publicKeyEncoding : {
@@ -870,10 +870,15 @@ const checkPermissions = (filePath, options) => {
870870 } ) ;
871871 fs . writeFileSync ( publicKeyFile , publicKey ) ;
872872 fs . writeFileSync ( privateKeyFile , privateKey ) ;
873+ fs . writeFileSync (
874+ privateKeyB64File ,
875+ Buffer . from ( privateKey , "utf8" ) . toString ( "base64" ) ,
876+ ) ;
873877 console . log (
874878 "Created public/private key pairs for testing purposes" ,
875879 publicKeyFile ,
876880 privateKeyFile ,
881+ privateKeyB64File ,
877882 ) ;
878883 privateKeyToUse = privateKey ;
879884 jwkPublicKey = crypto
0 commit comments