forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (173 loc) · 7.16 KB
/
index.js
File metadata and controls
193 lines (173 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import path from "node:path";
import { EOL } from "os";
import { availableProviders, match } from './provider.js'
import analysis from './analysis.js'
import fs from 'node:fs'
import { getCustom } from "./tools.js";
import.meta.dirname
import * as url from 'url';
export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken }
export const exhortDevDefaultUrl = 'https://exhort.stage.devshift.net';
/** @type {string} The default production URL for the Exhort backend. */
export const exhortDefaultUrl = "https://rhda.rhcloud.com";
/**
* Logs messages to the console if the EXHORT_DEBUG environment variable is set to "true".
* @param {string} alongsideText - The text to prepend to the log message.
* @param {any} valueToBePrinted - The value to log.
* @private
*/
function logOptionsAndEnvironmentsVariables(alongsideText,valueToBePrinted) {
if (process.env["EXHORT_DEBUG"] === "true") {
console.log(`${alongsideText}: ${valueToBePrinted} ${EOL}`)
}
}
/**
* Reads the version from the package.json file and logs it if debug mode is enabled.
* @private
*/
function readAndPrintVersionFromPackageJson() {
let dirName
// new ESM way in nodeJS ( since node version 22 ) to bring module directory.
dirName = import.meta.dirname
// old ESM way in nodeJS ( before node versions 22.00 to bring module directory)
if (!dirName) {
dirName = url.fileURLToPath(new URL('.', import.meta.url));
}
try {
if (__dirname) {
dirName = __dirname;
}
} catch (e) {
console.log("__dirname is not defined, continue with fileUrlPath")
}
let packageJson = JSON.parse(fs.readFileSync(path.join(dirName, "..", "package.json")).toString())
logOptionsAndEnvironmentsVariables("exhort-javascript-api analysis started, version: ", packageJson.version)
}
/**
* This function is used to determine exhort theUrl backend according to the following logic:
* If EXHORT_DEV_MODE = true, then take the value of the EXHORT BACKEND URL of dev/staging environment in such a way:
* take it as environment variable if exists, otherwise, take it from opts object if exists, otherwise, use the hardcoded default of DEV environment.
* If EXHORT_DEV_MODE = false , then select the production theUrl of EXHORT Backend, which is hardcoded.
* EXHORT_DEV_MODE evaluated in the following order and selected when it finds it first:
* 1. Environment Variable
* 2. (key,value) from opts object
* 3. Default False ( points to production URL )
* @param {{}} [opts={}] - optional various options to override default EXHORT_DEV_MODE and DEV_EXHORT_BACKEND_URL.
* @return {string} - The selected exhort backend
* @private
*/
function selectExhortBackend(opts = {}) {
let result
if (process.env["EXHORT_DEBUG"] === "true") {
let packageJson = readAndPrintVersionFromPackageJson();
}
let exhortDevModeBundled = "false"
let exhortDevMode = getCustom("EXHORT_DEV_MODE", exhortDevModeBundled, opts)
if(exhortDevMode !== null && exhortDevMode.toString() === "true") {
result = getCustom('DEV_EXHORT_BACKEND_URL', exhortDevDefaultUrl, opts);
} else {
result = exhortDefaultUrl
}
logOptionsAndEnvironmentsVariables("Chosen exhort backend URL:", result)
return result;
}
/**
* Test function for selecting the Exhort backend URL.
* Primarily used for testing the backend selection logic.
* @param {object} [opts={}] - Optional configuration, similar to `selectExhortBackend`.
* @return {string} The selected exhort backend URL.
*/
export function testSelectExhortBackend(opts) {
return selectExhortBackend(opts)
}
/**
* @type {string} The URL of the Exhort backend to send requests to.
* @private
*/
let theUrl
/**
* @overload
* @param {string} manifest
* @param {true} html
* @param {object} [opts={}]
* @returns {Promise<string>}
* @throws {Error}
*/
/**
* @overload
* @param {string} manifest
* @param {false} html
* @param {object} [opts={}]
* @returns {Promise<import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport>}
* @throws {Error}
*/
/**
* Get stack analysis report for a manifest file.
* @overload
* @param {string} manifest - path for the manifest
* @param {boolean} [html=false] - true will return a html string, false will return AnalysisReport object.
* @param {object} [opts={}] - optional various options to pass along the application
* @returns {Promise<string|import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport>}
* @throws {Error} if manifest inaccessible, no matching provider, failed to get create content,
* or backend request failed
*/
async function stackAnalysis(manifest, html = false, opts = {}) {
theUrl = selectExhortBackend(opts)
fs.accessSync(manifest, fs.constants.R_OK) // throws error if file unreadable
let provider = match(manifest, availableProviders) // throws error if no matching provider
return await analysis.requestStack(provider, manifest, theUrl, html, opts) // throws error request sending failed
}
/**
* Get component analysis report for a manifest content.
* @param {string} manifest - path to the manifest
* @param {object} [opts={}] - optional various options to pass along the application
* @returns {Promise<import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport>}
* @throws {Error} if no matching provider, failed to get create content, or backend request failed
*/
async function componentAnalysis(manifest, opts = {}) {
theUrl = selectExhortBackend(opts)
fs.accessSync(manifest, fs.constants.R_OK)
opts["manifest-type"] = path.basename(manifest)
let provider = match(manifest, availableProviders) // throws error if no matching provider
return await analysis.requestComponent(provider, manifest, theUrl, opts) // throws error request sending failed
}
/**
* @overload
* @param {Array<string>} imageRefs
* @param {true} html
* @param {object} [opts={}]
* @returns {Promise<string>}
* @throws {Error}
*/
/**
* @overload
* @param {Array<string>} imageRefs
* @param {false} html
* @param {object} [opts={}]
* @returns {Promise<import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport}
* @throws {Error}
*/
/**
* Get image analysis report for a set of OCI image references.
* @overload
* @param {Array<string>} imageRefs - OCI image references
* @param {boolean} [html=false] - true will return a html string, false will return AnalysisReport
* @param {{}} [opts={}] - optional various options to pass along the application
* @returns {Promise<string|import('@trustification/exhort-api-spec/model/v4/AnalysisReport').AnalysisReport}
* @throws {Error} if manifest inaccessible, no matching provider, failed to get create content,
* or backend request failed
*/
async function imageAnalysis(imageRefs, html = false, opts = {}) {
theUrl = selectExhortBackend(opts)
return await analysis.requestImages(imageRefs, theUrl, opts)
}
/**
* Validates the Exhort token.
* @param {object} [opts={}] - Optional parameters, potentially including token override.
* @returns {Promise<object>} A promise that resolves with the validation result from the backend.
* @throws {Error} if the backend request failed.
*/
async function validateToken(opts = {}) {
theUrl = selectExhortBackend(opts)
return await analysis.validateToken(theUrl, opts) // throws error request sending failed
}