Skip to content

Commit a109536

Browse files
authored
fix: extract & pass correct headers & header values to exhort (#378)
1 parent 62f6bc7 commit a109536

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

src/analysis.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { RegexNotToBeLogged, getCustom } from "./tools.js";
99

1010
export default { requestComponent, requestStack, requestImages, validateToken }
1111

12-
const rhdaTokenHeader = "rhda-token";
13-
const rhdaTelemetryId = "rhda-telemetry-id";
14-
const rhdaSourceHeader = "rhda-source"
15-
const rhdaOperationTypeHeader = "rhda-operation-type"
16-
const rhdaPackageManagerHeader = "rhda-pkg-manager"
12+
const rhdaTokenHeader = "trust-da-token";
13+
const rhdaTelemetryId = "telemetry-anonymous-id";
14+
const rhdaSourceHeader = "trust-da-source"
15+
const rhdaOperationTypeHeader = "trust-da-operation-type"
16+
const rhdaPackageManagerHeader = "trust-da-pkg-manager"
1717

1818
/**
1919
* Adds proxy agent configuration to fetch options if a proxy URL is specified
@@ -229,11 +229,12 @@ async function validateToken(url, opts = {}) {
229229
*
230230
* @param {string} headerName - the header name to populate in request
231231
* @param headers
232-
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
232+
* @param {string} optsKey - key in the options object to use the value for
233+
* @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
233234
* @private
234235
*/
235-
function setRhdaHeader(headerName, headers, opts) {
236-
let rhdaHeaderValue = getCustom(headerName.toUpperCase().replaceAll("-", "_"), null, opts);
236+
function setRhdaHeader(headerName, headers, optsKey, opts) {
237+
let rhdaHeaderValue = getCustom(optsKey, null, opts);
237238
if (rhdaHeaderValue) {
238239
headers[headerName] = rhdaHeaderValue
239240
}
@@ -244,26 +245,15 @@ function setRhdaHeader(headerName, headers, opts) {
244245
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
245246
* @returns {{}}
246247
*/
247-
function getTokenHeaders(opts = {}) {
248-
let supportedTokens = ['snyk', 'oss-index']
248+
export function getTokenHeaders(opts = {}) {
249249
let headers = {}
250-
supportedTokens.forEach(vendor => {
251-
let token = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_TOKEN`, null, opts);
252-
if (token) {
253-
headers[`ex-${vendor}-token`] = token
254-
}
255-
let user = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_USER`, null, opts);
256-
if (user) {
257-
headers[`ex-${vendor}-user`] = user
258-
}
259-
})
260-
setRhdaHeader(rhdaTokenHeader, headers, opts);
261-
setRhdaHeader(rhdaSourceHeader, headers, opts);
262-
setRhdaHeader(rhdaOperationTypeHeader, headers, opts);
263-
setRhdaHeader(rhdaPackageManagerHeader, headers, opts)
264-
setRhdaHeader(rhdaTelemetryId, headers, opts);
250+
setRhdaHeader(rhdaTokenHeader, headers, 'TRUSTIFY_DA_TOKEN', opts);
251+
setRhdaHeader(rhdaSourceHeader, headers, 'TRUSTIFY_DA_SOURCE', opts);
252+
setRhdaHeader(rhdaOperationTypeHeader, headers, rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_"), opts);
253+
setRhdaHeader(rhdaPackageManagerHeader, headers, rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_"), opts)
254+
setRhdaHeader(rhdaTelemetryId, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
265255

266-
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
256+
if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
267257
console.log("Headers Values to be sent to Trustify DA backend:" + EOL)
268258
for (const headerKey in headers) {
269259
if (!headerKey.match(RegexNotToBeLogged)) {

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
3939
* TRUSTIFY_DA_SYFT_PATH?: string | undefined,
4040
* TRUSTIFY_DA_YARN_PATH?: string | undefined,
4141
* MATCH_MANIFEST_VERSIONS?: string | undefined,
42-
* RHDA_SOURCE?: string | undefined,
43-
* RHDA_TOKEN?: string | undefined,
44-
* RHDA_TELEMETRY_ID?: string | undefined,
42+
* TRUSTIFY_DA_SOURCE?: string | undefined,
43+
* TRUSTIFY_DA_TOKEN?: string | undefined,
44+
* TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
4545
* [key: string]: string | undefined,
4646
* }} Options
4747
*/

src/tools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EOL } from "os";
33

44
import { PackageURL } from "packageurl-js";
55

6-
export const RegexNotToBeLogged = /TRUSTIFY_DA_.*_TOKEN|ex-.*-token/
6+
export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/
77
/**
88
*
99
* @param {string} key to log its value from environment variables and from opts, if it exists
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { expect } from 'chai'
2+
import * as chai from 'chai'
3+
import * as sinon from 'sinon'
4+
import sinonChai from 'sinon-chai'
25

6+
import { getTokenHeaders } from '../src/analysis.js';
37
import { selectTrustifyDABackend } from '../src/index.js'
48

5-
const testUrl = 'https://trustify-da.example.com';
6-
const testUrl2 = 'https://dev.trustify-da.example.com';
9+
chai.use(sinonChai)
710

811
suite('testing Select Trustify DA Backend function', () => {
12+
const testUrl = 'https://trustify-da.example.com';
13+
const testUrl2 = 'https://dev.trustify-da.example.com';
914

1015
test('When TRUSTIFY_DA_BACKEND_URL is set in environment variable, should return that value', () => {
1116
process.env['TRUSTIFY_DA_BACKEND_URL'] = testUrl;
@@ -63,3 +68,14 @@ suite('testing Select Trustify DA Backend function', () => {
6368
}).afterAll(() => {
6469
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
6570
});
71+
72+
suite('verify token header logging', () => {
73+
test('don\'t log the token header', () => {
74+
getTokenHeaders({
75+
'TRUSTIFY_DA_TOKEN': 'banana',
76+
'TRUSTIFY_DA_DEBUG': 'true'
77+
})
78+
// Should only be called once with "Headers Values to be sent to Trustify DA backend:"
79+
expect(console.log).to.be.calledOnce
80+
})
81+
}).beforeAll(() => sinon.spy(console, 'log')).afterAll(() => console.log.restore())

0 commit comments

Comments
 (0)