Skip to content

Commit ff59210

Browse files
authored
chore: added env vars for token and backend (#19)
* chore: added mechinsm for including token from env vars as request headers Signed-off-by: Tomer Figenblat <[email protected]> * chore: added option to replace backend url with an evn var Signed-off-by: Tomer Figenblat <[email protected]> --------- Signed-off-by: Tomer Figenblat <[email protected]>
1 parent 2f91659 commit ff59210

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* `npm run tests:rep` run unit tests and save the test results as _unit-tests-result.json_ (for ci)
2020
* `npm run gen:backend` generate the _Backend_ types from its _OpenAPI_ as _TS_ spec in the _generated/backend_ folder
2121

22+
### Good to know
23+
24+
* You can override the default backend url by setting another one in the _CRDA_BACKEND_URL_ environment variable.
25+
2226
### OpenAPI Specifications
2327

2428
We use our [Backend's OpenAPI spec file][1] for generating types used for deserialization of the Backend's

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ Excluding a package from any analysis can be achieved by marking the package for
149149

150150
</ul>
151151

152+
<h3>Tokens</h3>
153+
<p>
154+
If you wish the report to include other vulnerabilities data and resolutions which is only available to registered users.
155+
You can include the various vulnerability vendor data token as environment variables. Currently, only _Snyk_ is supported.
156+
157+
Available environment variables:
158+
</p>
159+
160+
<ul>
161+
<li><em>CRDA_SNYK_TOKEN</em></li>
162+
</ul>
163+
152164
<!-- Badge links -->
153165
[0]: https://img.shields.io/github/v/release/RHEcosystemAppEng/crda-javascript-api?color=green&label=latest
154166
[1]: https://img.shields.io/github/v/release/RHEcosystemAppEng/crda-javascript-api?color=yellow&include_prereleases&label=early-access

src/analysis.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ async function requestStack(provider, manifest, url, html = false) {
1414
method: 'POST',
1515
headers: {
1616
'Accept': html ? 'text/html' : 'application/json',
17-
'Content-Type': provided.contentType
17+
'Content-Type': provided.contentType,
18+
...getTokenHeaders()
1819
},
1920
body: provided.content
2021
})
@@ -34,9 +35,22 @@ async function requestComponent(provider, data, url) {
3435
method: 'POST',
3536
headers: {
3637
'Accept': 'application/json',
37-
'Content-Type': provided.contentType
38+
'Content-Type': provided.contentType,
39+
...getTokenHeaders(),
3840
},
3941
body: provided.content
4042
})
4143
return resp.json()
4244
}
45+
46+
function getTokenHeaders() {
47+
let supportedTokens = ['snyk']
48+
let headers = {}
49+
supportedTokens.forEach(vendor => {
50+
let token = process.env[`CRDA_${vendor.toUpperCase()}_TOKEN`]
51+
if (token) {
52+
headers[`crda-${vendor}-token`] = token
53+
}
54+
})
55+
return headers
56+
}

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export default { AnalysisReport, componentAnalysis, stackAnalysis }
99
* @type {string} backend url to send requests to
1010
* @private
1111
*/
12-
const url = 'http://crda-backend-crda.apps.sssc-cl01.appeng.rhecoeng.com/api/v3'
12+
const url = process.env.CRDA_BACKEND_URL ?
13+
process.env.CRDA_BACKEND_URL :
14+
'http://crda-backend-crda.apps.sssc-cl01.appeng.rhecoeng.com/api/v3'
1315

1416
/**
1517
* Get stack analysis report for a manifest file.

test/analysis.test.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import {afterEach} from 'mocha'
12
import analysis from '../src/analysis.js'
2-
import { expect} from 'chai'
3+
import { expect } from 'chai'
34
import { rest } from 'msw'
45
import { setupServer } from 'msw/node'
5-
import sinon from "sinon";
6+
import sinon from 'sinon'
67

78
// utility function creating a dummy server, intercepting a handler,
89
// running a test, and shutting the server down
@@ -96,4 +97,48 @@ suite('testing the analysis module for sending api requests', () => {
9697
}
9798
))
9899
})
100+
101+
suite('verify environment variables to token headers mechanism', () => {
102+
let fakeManifest = 'fake-file.typ'
103+
// stub the provideStack function to return the fake provided data for our fake manifest
104+
let stackProviderStub = sinon.stub()
105+
stackProviderStub.withArgs(fakeManifest).returns(fakeProvided)
106+
// fake providers hosts our stubbed provideStack function
107+
let fakeProvider = {
108+
provideComponent: () => {}, // not required for this test
109+
provideStack: stackProviderStub,
110+
isSupported: () => {} // not required for this test
111+
};
112+
113+
afterEach(() => delete process.env['CRDA_SNYK_TOKEN'])
114+
115+
test('when the relevant token environment variables are set, verify corresponding headers are included', interceptAndRun(
116+
// interception route, will return ok response if found the expected token
117+
rest.post(`${backendUrl}/dependency-analysis/${fakeProvided.ecosystem}`, (req, res, ctx) => {
118+
if ('dummy-snyk-token' === req.headers.get('crda-snyk-token')) {
119+
return res(ctx.json({ok: 'ok'}))
120+
}
121+
return res(ctx.status(400))
122+
}),
123+
async () => {
124+
process.env['CRDA_SNYK_TOKEN'] = 'dummy-snyk-token'
125+
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
126+
expect(res).to.deep.equal({ok: 'ok'})
127+
}
128+
))
129+
130+
test('when the relevant token environment variables are not set, verify no corresponding headers are included', interceptAndRun(
131+
// interception route, will return ok response if found the expected token
132+
rest.post(`${backendUrl}/dependency-analysis/${fakeProvided.ecosystem}`, (req, res, ctx) => {
133+
if (!req.headers.get('crda-snyk-token')) {
134+
return res(ctx.json({ok: 'ok'}))
135+
}
136+
return res(ctx.status(400))
137+
}),
138+
async () => {
139+
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
140+
expect(res).to.deep.equal({ok: 'ok'})
141+
}
142+
))
143+
})
99144
})

0 commit comments

Comments
 (0)