Skip to content

Commit 3a12219

Browse files
committed
chore: include package manager in exhort API request headers
1 parent d23acc7 commit 3a12219

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

src/analysis.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default { requestComponent, requestStack, requestImages, validateToken }
1212
const rhdaTokenHeader = "rhda-token";
1313
const rhdaSourceHeader = "rhda-source"
1414
const rhdaOperationTypeHeader = "rhda-operation-type"
15+
const rhdaPackageManagerHeader = "rhda-pkg-manager"
1516

1617
/**
1718
* Adds proxy agent configuration to fetch options if a proxy URL is specified
@@ -29,7 +30,7 @@ function addProxyAgent(options, opts) {
2930

3031
/**
3132
* Send a stack analysis request and get the report as 'text/html' or 'application/json'.
32-
* @param {import('./provider').Provider | import('./providers/base_java.js').default } provider - the provided data for constructing the request
33+
* @param {import('./provider').Provider} provider - the provided data for constructing the request
3334
* @param {string} manifest - path for the manifest
3435
* @param {string} url - the backend url to send the request to
3536
* @param {boolean} [html=false] - true will return 'text/html', false will return 'application/json'
@@ -43,17 +44,18 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
4344
opts["source-manifest"] = ""
4445
opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "stack-analysis"
4546
let startTime = new Date()
46-
let EndTime
47+
let endTime
4748
if (process.env["EXHORT_DEBUG"] === "true") {
4849
console.log("Starting time of sending stack analysis request to exhort server= " + startTime)
4950
}
51+
opts[rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_")] = provided.ecosystem
5052

5153
const fetchOptions = addProxyAgent({
5254
method: 'POST',
5355
headers: {
5456
'Accept': html ? 'text/html' : 'application/json',
5557
'Content-Type': provided.contentType,
56-
...getTokenHeaders(opts)
58+
...getTokenHeaders(opts),
5759
},
5860
body: provided.content
5961
}, opts);
@@ -76,11 +78,11 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
7678
if (exRequestId) {
7779
console.log("Unique Identifier associated with this request - ex-request-id=" + exRequestId)
7880
}
79-
EndTime = new Date()
81+
endTime = new Date()
8082
console.log("Response body received from exhort server : " + EOL + EOL)
8183
console.log(console.log(JSON.stringify(result, null, 4)))
82-
console.log("Ending time of sending stack analysis request to exhort server= " + EndTime)
83-
let time = (EndTime - startTime) / 1000
84+
console.log("Ending time of sending stack analysis request to exhort server= " + endTime)
85+
let time = (endTime - startTime) / 1000
8486
console.log("Total Time in seconds: " + time)
8587

8688
}
@@ -108,6 +110,7 @@ async function requestComponent(provider, manifest, url, opts = {}) {
108110
if (process.env["EXHORT_DEBUG"] === "true") {
109111
console.log("Starting time of sending component analysis request to exhort server= " + new Date())
110112
}
113+
opts[rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_")] = provided.ecosystem
111114

112115
const fetchOptions = addProxyAgent({
113116
method: 'POST',
@@ -256,6 +259,8 @@ function getTokenHeaders(opts = {}) {
256259
setRhdaHeader(rhdaTokenHeader, headers, opts);
257260
setRhdaHeader(rhdaSourceHeader, headers, opts);
258261
setRhdaHeader(rhdaOperationTypeHeader, headers, opts);
262+
setRhdaHeader(rhdaPackageManagerHeader, headers, opts)
263+
259264
if (process.env["EXHORT_DEBUG"] === "true") {
260265
console.log("Headers Values to be sent to exhort:" + EOL)
261266
for (const headerKey in headers) {

src/providers/base_javascript.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Manifest from './manifest.js';
1515
* The ecosystem identifier for JavaScript/npm packages
1616
* @type {string}
1717
*/
18-
export const ecosystem = 'npm';
18+
export const purlType = 'npm';
1919

2020
/**
2121
* Base class for JavaScript package manager providers.
@@ -28,6 +28,8 @@ export default class Base_javascript {
2828
#manifest;
2929
/** @type {string} */
3030
#cmd;
31+
/** @type {string} */
32+
#ecosystem;
3133

3234
/**
3335
* Sets up the provider with the manifest path and options
@@ -38,17 +40,27 @@ export default class Base_javascript {
3840
_setUp(manifestPath, opts) {
3941
this.#cmd = getCustomPath(this._cmdName(), opts);
4042
this.#manifest = new Manifest(manifestPath);
43+
this.#ecosystem = purlType;
4144
}
4245

4346
/**
44-
* Gets the current manifest object
45-
* @returns {Manifest} The manifest object
46-
* @protected
47-
*/
47+
* Gets the current manifest object
48+
* @returns {Manifest} The manifest object
49+
* @protected
50+
*/
4851
_getManifest() {
4952
return this.#manifest;
5053
}
5154

55+
/**
56+
* Sets the ecosystem value
57+
* @param {string} ecosystem - The ecosystem identifier
58+
* @protected
59+
*/
60+
_setEcosystem(ecosystem) {
61+
this.#ecosystem = ecosystem;
62+
}
63+
5264
/**
5365
* Returns the name of the lock file for the specific implementation
5466
* @returns {string} The lock file name
@@ -117,7 +129,7 @@ export default class Base_javascript {
117129
provideStack(manifestPath, opts = {}) {
118130
this._setUp(manifestPath, opts);
119131
return {
120-
ecosystem,
132+
ecosystem: this.#ecosystem,
121133
content: this.#getSBOM(opts),
122134
contentType: 'application/vnd.cyclonedx+json'
123135
}
@@ -132,7 +144,7 @@ export default class Base_javascript {
132144
provideComponent(manifestPath, opts = {}) {
133145
this._setUp(manifestPath, opts);
134146
return {
135-
ecosystem,
147+
ecosystem: this.#ecosystem,
136148
content: this.#getDirectDependencySbom(opts),
137149
contentType: 'application/vnd.cyclonedx+json'
138150
}
@@ -163,7 +175,7 @@ export default class Base_javascript {
163175
#getSBOM(opts = {}) {
164176
const depsObject = this._buildDependencyTree(true);
165177

166-
let mainComponent = toPurl(ecosystem, this.#manifest.name, this.#manifest.version);
178+
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
167179

168180
let sbom = new Sbom();
169181
sbom.addRoot(mainComponent);
@@ -185,8 +197,8 @@ export default class Base_javascript {
185197
Object.entries(dependencies)
186198
.forEach(entry => {
187199
const [name, artifact] = entry;
188-
const target = toPurl(ecosystem, name, artifact.version);
189-
const rootPurl = toPurl(ecosystem, this.#manifest.name, this.#manifest.version);
200+
const target = toPurl(purlType, name, artifact.version);
201+
const rootPurl = toPurl(purlType, this.#manifest.name, this.#manifest.version);
190202
sbom.addDependency(rootPurl, target);
191203
this.#addDependenciesOf(sbom, target, artifact);
192204
});
@@ -205,7 +217,7 @@ export default class Base_javascript {
205217
.forEach(entry => {
206218
const [name, depArtifact] = entry;
207219
if(depArtifact.version !== undefined) {
208-
const target = toPurl(ecosystem, name, depArtifact.version);
220+
const target = toPurl(purlType, name, depArtifact.version);
209221
sbom.addDependency(from, target);
210222
this.#addDependenciesOf(sbom, target, depArtifact);
211223
}
@@ -220,7 +232,7 @@ export default class Base_javascript {
220232
*/
221233
#getDirectDependencySbom(opts = {}) {
222234
const depTree = this._buildDependencyTree(false);
223-
let mainComponent = toPurl(ecosystem, this.#manifest.name, this.#manifest.version);
235+
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
224236

225237
let sbom = new Sbom();
226238
sbom.addRoot(mainComponent);
@@ -251,7 +263,7 @@ export default class Base_javascript {
251263

252264
return new Map(
253265
Object.entries(depTree.dependencies).map(
254-
([key, value]) => [key, toPurl(ecosystem, key, value.version)]
266+
([key, value]) => [key, toPurl(purlType, key, value.version)]
255267
)
256268
);
257269
}

src/providers/javascript_yarn.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default class Javascript_yarn extends Base_javascript {
3535
}
3636

3737
const isClassic = matches[1] === '1';
38+
this._setEcosystem(isClassic ? 'yarn-classic' : 'yarn-berry');
3839
this.#processor = isClassic ? new Yarn_classic_processor(this._getManifest()) : new Yarn_berry_processor(this._getManifest());
3940
}
4041

src/providers/processors/yarn_berry_processor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EOL } from 'os';
22

33
import { toPurl, toPurlFromString } from "../../tools.js";
4-
import { ecosystem } from "../base_javascript.js";
4+
import { purlType } from "../base_javascript.js";
55

66
import Yarn_processor from "./yarn_processor.js";
77

@@ -64,7 +64,7 @@ export default class Yarn_berry_processor extends Yarn_processor {
6464
const idx = depName.lastIndexOf('@');
6565
const name = depName.substring(0, idx);
6666
const version = dep.children.Version;
67-
return [name, toPurl(ecosystem, name, version)];
67+
return [name, toPurl(purlType, name, version)];
6868
}
6969
)
7070
);
@@ -120,12 +120,12 @@ export default class Yarn_berry_processor extends Yarn_processor {
120120

121121
const matches = Yarn_berry_processor.LOCATOR_PATTERN.exec(locator);
122122
if (matches) {
123-
return toPurl(ecosystem, matches[1], matches[2]);
123+
return toPurl(purlType, matches[1], matches[2]);
124124
}
125125

126126
const virtualMatches = Yarn_berry_processor.VIRTUAL_LOCATOR_PATTERN.exec(locator);
127127
if (virtualMatches) {
128-
return toPurl(ecosystem, virtualMatches[1], virtualMatches[2]);
128+
return toPurl(purlType, virtualMatches[1], virtualMatches[2]);
129129
}
130130

131131
return undefined;
@@ -145,6 +145,6 @@ export default class Yarn_berry_processor extends Yarn_processor {
145145

146146
const name = depName.substring(0, depName.lastIndexOf('@'));
147147
const version = node.children.Version;
148-
return toPurl(ecosystem, name, version);
148+
return toPurl(purlType, name, version);
149149
}
150150
}

src/providers/processors/yarn_classic_processor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toPurl, toPurlFromString } from "../../tools.js";
2-
import { ecosystem } from "../base_javascript.js";
2+
import { purlType } from "../base_javascript.js";
33

44
import Yarn_processor from "./yarn_processor.js";
55

@@ -52,7 +52,7 @@ export default class Yarn_classic_processor extends Yarn_processor {
5252
const idx = depName.lastIndexOf('@');
5353
const name = depName.substring(0, idx);
5454
const version = idx !== -1 ? depName.substring(idx + 1) : '';
55-
return [name, toPurl(ecosystem, name, version)];
55+
return [name, toPurl(purlType, name, version)];
5656
}
5757
)
5858
)
@@ -119,7 +119,7 @@ class NodeMetaData {
119119
const idx = this.nodeName.lastIndexOf('@');
120120
this.name = this.nodeName.substring(0, idx);
121121
this.version = idx !== -1 ? this.nodeName.substring(idx + 1) : '';
122-
this.purl = toPurl(ecosystem, this.name, this.version);
122+
this.purl = toPurl(purlType, this.name, this.version);
123123
const shadowNode = node.shadow;
124124
this.shadow = shadowNode ? shadowNode : false;
125125
}

0 commit comments

Comments
 (0)