Skip to content

Commit dbe3bca

Browse files
committed
feature: add new function validateToken to check providers tokens
fix: fix maven artifacts' versions sometimes interpreted as integer instead of string Signed-off-by: Zvi Grinberg <[email protected]>
1 parent 0bafc6c commit dbe3bca

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

src/analysis.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {getCustom} from "./tools.js";
22

3-
export default { requestComponent, requestStack }
3+
export default { requestComponent, requestStack, validateToken }
44

55
/**
66
* Send a stack analysis request and get the report as 'text/html' or 'application/json'.
@@ -47,6 +47,23 @@ async function requestComponent(provider, data, url, opts = {}) {
4747
return resp.json()
4848
}
4949

50+
/**
51+
*
52+
* @param url the backend url to send the request to
53+
* @param {{}} [opts={}] - optional various options to pass headers for t he validateToken Request
54+
* @return {Promise<number>} return the HTTP status Code of the response from the validate token request.
55+
*/
56+
async function validateToken(url, opts = {}) {
57+
let resp = await fetch(`${url}/api/v3/token`, {
58+
method: 'GET',
59+
headers: {
60+
// 'Accept': 'text/plain',
61+
...getTokenHeaders(opts),
62+
}
63+
})
64+
return resp.status
65+
}
66+
5067
/**
5168
* Utility function for fetching vendor tokens
5269
* @param {{}} [opts={}] - optional various options to pass along the application

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import analysis from './analysis.js'
44
import fs from 'node:fs'
55
import {getCustom} from "./tools.js";
66

7-
export default { AnalysisReport, componentAnalysis, stackAnalysis }
7+
export default { AnalysisReport, componentAnalysis, stackAnalysis, validateToken }
88

99
/**
1010
* @type {string} backend url to send requests to
@@ -42,3 +42,7 @@ async function componentAnalysis(manifestType, data, opts = {}) {
4242
let provider = match(manifestType, availableProviders) // throws error if no matching provider
4343
return await analysis.requestComponent(provider, data, url, opts) // throws error request sending failed
4444
}
45+
46+
async function validateToken(opts = {}) {
47+
return await analysis.validateToken(url, opts) // throws error request sending failed
48+
}

src/providers/java_maven.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function getDependencies(manifest) {
265265
ignored.push({
266266
groupId: dep['groupId'],
267267
artifactId: dep['artifactId'],
268-
version: dep['version'] ? dep['version'] : '*',
268+
version: dep['version'] ? dep['version'].toString() : '*',
269269
scope: '*',
270270
ignore: ignore
271271
})

test/analysis.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ function interceptAndRun(handler, test) {
2020
};
2121
}
2222

23+
function determineResponse(req, res, ctx) {
24+
let response
25+
if (req.headers.get("ex-snyk-token") === null) {
26+
response = res(ctx.status(400));
27+
28+
} else if (req.headers.get("ex-snyk-token") === "good-dummy-token") {
29+
response = res(ctx.status(200));
30+
} else {
31+
response = res(ctx.status(401));
32+
}
33+
return response
34+
}
35+
2336
suite('testing the analysis module for sending api requests', () => {
2437
let backendUrl = 'http://url.lru' // dummy backend url will be used for fake server
2538
// fake provided data, in prod will be provided by the provider and used for creating requests
@@ -97,6 +110,54 @@ suite('testing the analysis module for sending api requests', () => {
97110
}
98111
))
99112
})
113+
suite('testing the validateToken function', () => {
114+
115+
test('invoking validateToken function with good token', interceptAndRun(
116+
// interception route, will return ok response for our fake content type
117+
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
118+
return determineResponse(req, res, ctx);
119+
120+
}),
121+
async () => {
122+
let options = {
123+
'EXHORT_SNYK_TOKEN': 'good-dummy-token'
124+
}
125+
// verify response as expected
126+
let res = await analysis.validateToken(backendUrl, options)
127+
expect(res).to.equal(200)
128+
}
129+
))
130+
test('invoking validateToken function with bad token', interceptAndRun(
131+
// interception route, will return ok response for our fake content type
132+
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
133+
return determineResponse(req, res, ctx);
134+
135+
}),
136+
async () => {
137+
let options = {
138+
'EXHORT_SNYK_TOKEN': 'bad-dummy-token'
139+
}
140+
// verify response as expected
141+
let res = await analysis.validateToken(backendUrl, options)
142+
expect(res).to.equal(401)
143+
}
144+
))
145+
test('invoking validateToken function without token', interceptAndRun(
146+
// interception route, will return ok response for our fake content type
147+
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
148+
return determineResponse(req, res, ctx);
149+
150+
}),
151+
async () => {
152+
let options = {
153+
}
154+
// verify response as expected
155+
let res = await analysis.validateToken(backendUrl, options)
156+
expect(res).to.equal(400)
157+
}
158+
))
159+
160+
})
100161

101162
suite('verify environment variables to token headers mechanism', () => {
102163
let fakeManifest = 'fake-file.typ'

test/providers/java_maven.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai'
22
import fs from 'fs'
33
import sinon from "sinon";
4+
// import exhort from "../../src/index.js"
45

56

67

@@ -27,6 +28,19 @@ suite('testing the java-maven data provider', () => {
2728
"poms_deps_with_no_ignore_long"
2829
].forEach(testCase => {
2930
let scenario = testCase.replace('pom_deps_', '').replaceAll('_', ' ')
31+
// test(`custom adhoc test`, async () => {
32+
//
33+
// let options = {
34+
// 'EXHORT_SNYK_TOKEN': 'insert-token'
35+
// }
36+
// let httpStatus = await exhort.validateToken(options);
37+
//
38+
// let pom = fs.readFileSync(`/tmp/exhort-maven/pom.xml`,).toString().trim()
39+
// let analysisReport = await exhort.componentAnalysis("pom.xml", pom);
40+
// console.log(analysisReport)
41+
// analysisReport = await exhort.stackAnalysis(`/tmp/exhort-maven/pom.xml`,true);
42+
// console.log(analysisReport)
43+
// }).timeout(process.env.GITHUB_ACTIONS ? 30000 : 5000)
3044

3145
test(`verify maven data provided for stack analysis with scenario ${scenario}`, async () => {
3246
// load the expected graph for the scenario

0 commit comments

Comments
 (0)