11import fs from "node:fs" ;
22import path from "node:path" ;
3- import { EOL } from "os" ;
4- import { RegexNotToBeLogged , getCustom } from "./tools.js" ;
5- import http from 'node:http' ;
6- import https from 'node:https' ;
3+ import { EOL } from "os" ;
4+ import { RegexNotToBeLogged , getCustom } from "./tools.js" ;
5+ import { HttpsProxyAgent } from "https-proxy-agent" ;
6+ import { generateImageSBOM , parseImageRef } from "./oci_image/utils.js" ;
77
8- export default { requestComponent, requestStack, validateToken }
8+ export default { requestComponent, requestStack, requestImages , validateToken }
99
1010const rhdaTokenHeader = "rhda-token" ;
1111const rhdaSourceHeader = "rhda-source"
@@ -20,10 +20,7 @@ const rhdaOperationTypeHeader = "rhda-operation-type"
2020function addProxyAgent ( options , opts ) {
2121 const proxyUrl = getCustom ( 'EXHORT_PROXY_URL' , null , opts ) ;
2222 if ( proxyUrl ) {
23- const proxyUrlObj = new URL ( proxyUrl ) ;
24- options . agent = proxyUrlObj . protocol === 'https:'
25- ? new https . Agent ( { proxy : proxyUrl } )
26- : new http . Agent ( { proxy : proxyUrl } ) ;
23+ options . agent = new HttpsProxyAgent ( proxyUrl ) ;
2724 }
2825 return options ;
2926}
@@ -137,6 +134,52 @@ async function requestComponent(provider, manifest, url, opts = {}) {
137134 return Promise . resolve ( result )
138135}
139136
137+ /**
138+ *
139+ * @param {Array<string> } imageRefs
140+ * @param {string } url
141+ * @param {{} } [opts={}] - optional various options to pass along the application
142+ * @returns {Promise<string|import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport> }
143+ */
144+ async function requestImages ( imageRefs , url , html = false , opts = { } ) {
145+ const imageSboms = { }
146+ for ( const image of imageRefs ) {
147+ const parsedImageRef = parseImageRef ( image )
148+ imageSboms [ parsedImageRef . getPackageURL ( ) . toString ( ) ] = generateImageSBOM ( parsedImageRef )
149+ }
150+
151+ const resp = await fetch ( `${ url } /api/v4/batch-analysis` , {
152+ method : 'POST' ,
153+ headers : {
154+ 'Accept' : html ? 'text/html' : 'application/json' ,
155+ 'Content-Type' : 'application/vnd.cyclonedx+json' ,
156+ ...getTokenHeaders ( opts )
157+ } ,
158+ body : JSON . stringify ( imageSboms ) ,
159+ } )
160+
161+ if ( resp . status === 200 ) {
162+ let result ;
163+ if ( ! html ) {
164+ result = await resp . json ( )
165+ } else {
166+ result = await resp . text ( )
167+ }
168+ if ( process . env [ "EXHORT_DEBUG" ] === "true" ) {
169+ let exRequestId = resp . headers . get ( "ex-request-id" ) ;
170+ if ( exRequestId ) {
171+ console . log ( "Unique Identifier associated with this request - ex-request-id=" + exRequestId )
172+ }
173+ console . log ( "Response body received from exhort server : " + EOL + EOL )
174+ console . log ( JSON . stringify ( result , null , 4 ) )
175+ console . log ( "Ending time of sending component analysis request to exhort server= " + new Date ( ) )
176+ }
177+ return result
178+ } else {
179+ throw new Error ( `Got error response from exhort backend - http return code : ${ resp . status } , ex-request-id: ${ resp . headers . get ( "ex-request-id" ) } error message => ${ await resp . text ( ) } ` )
180+ }
181+ }
182+
140183/**
141184 *
142185 * @param url the backend url to send the request to
0 commit comments