|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | | -import {EOL} from "os"; |
4 | | -import {RegexNotToBeLogged, getCustom} from "./tools.js"; |
| 3 | +import { EOL } from "os"; |
| 4 | +import { RegexNotToBeLogged, getCustom } from "./tools.js"; |
| 5 | +import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js"; |
5 | 6 | import http from 'node:http'; |
6 | 7 | import https from 'node:https'; |
7 | 8 |
|
8 | | -export default { requestComponent, requestStack, validateToken } |
| 9 | +export default { requestComponent, requestStack, requestImages, validateToken } |
9 | 10 |
|
10 | 11 | const rhdaTokenHeader = "rhda-token"; |
11 | 12 | const rhdaSourceHeader = "rhda-source" |
@@ -137,6 +138,54 @@ async function requestComponent(provider, manifest, url, opts = {}) { |
137 | 138 | return Promise.resolve(result) |
138 | 139 | } |
139 | 140 |
|
| 141 | +/** |
| 142 | + * |
| 143 | + * @param {Array<string>} imageRefs |
| 144 | + * @param {string} url |
| 145 | + * @param {{}} [opts={}] - optional various options to pass along the application |
| 146 | + * @returns {Promise<string|import('../generated/backend/AnalysisReport').AnalysisReport>} |
| 147 | + */ |
| 148 | +async function requestImages(imageRefs, url, html = false, opts = {}) { |
| 149 | + const imageSboms = {} |
| 150 | + for (const image of imageRefs) { |
| 151 | + const parsedImageRef = parseImageRef(image) |
| 152 | + imageSboms[parsedImageRef.getPackageURL().toString()] = generateImageSBOM(parsedImageRef) |
| 153 | + } |
| 154 | + |
| 155 | + const resp = await fetch(`${url}/api/v4/batch-analysis`, { |
| 156 | + method: 'POST', |
| 157 | + headers: { |
| 158 | + 'Accept': html ? 'text/html' : 'application/json', |
| 159 | + 'Content-Type': 'application/vnd.cyclonedx+json', |
| 160 | + ...getTokenHeaders(opts) |
| 161 | + }, |
| 162 | + body: JSON.stringify(imageSboms), |
| 163 | + }) |
| 164 | + |
| 165 | + console.error(JSON.stringify(imageSboms, '', '\t')) |
| 166 | + |
| 167 | + if(resp.status === 200) { |
| 168 | + let result; |
| 169 | + if (!html) { |
| 170 | + result = await resp.json() |
| 171 | + } else { |
| 172 | + result = await resp.text() |
| 173 | + } |
| 174 | + if (process.env["EXHORT_DEBUG"] === "true") { |
| 175 | + let exRequestId = resp.headers.get("ex-request-id"); |
| 176 | + if (exRequestId) { |
| 177 | + console.log("Unique Identifier associated with this request - ex-request-id=" + exRequestId) |
| 178 | + } |
| 179 | + console.log("Response body received from exhort server : " + EOL + EOL) |
| 180 | + console.log(JSON.stringify(result, null, 4)) |
| 181 | + console.log("Ending time of sending component analysis request to exhort server= " + new Date()) |
| 182 | + } |
| 183 | + return result |
| 184 | + } else { |
| 185 | + 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()}`) |
| 186 | + } |
| 187 | +} |
| 188 | + |
140 | 189 | /** |
141 | 190 | * |
142 | 191 | * @param url the backend url to send the request to |
|
0 commit comments