Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of proxy environment variable (#191) #199

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ A lot of awesome people have contributed to this project! Here they are:

Sonatype internal people:

* [@ken-duck](https://github.com/ken-duck/) (Ken Duck)
* [@DarthHater](https://github.com/darthhater/) (Jeffry Hesse)
* [@allenhsieh](https://github.com/allenhsieh) (Allen Hsieh)
* [@ajurgenson55](https://github.com/ajurgenson55) (Artie)
- [@ken-duck](https://github.com/ken-duck/) (Ken Duck)
- [@DarthHater](https://github.com/darthhater/) (Jeffry Hesse)
- [@allenhsieh](https://github.com/allenhsieh) (Allen Hsieh)
- [@ajurgenson55](https://github.com/ajurgenson55) (Artie)

External contributors:

- [@francois-roget](https://github.com/francois-roget) (François Roget) for [Ingenico Group](https://github.com/ingenico-group)

Possibly You!
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ We've provided an example repo with a working TravisCI config on a "fake" but re

We've provided an example repo with a working GitHub Action on a "fake" but real project, you can see how it is all setup by clicking [this link](https://github.com/sonatype-nexus-community/example-auditjs-repo#usage-with-github-actions).

#### Proxy integration

The tool reads the `http_proxy` or `https_proxy` environment variables to perform network request through a Proxy.

### Usage As A NPM Script

`auditjs` can be added as a devDependency to your project, and then an npm script can be added so you can leverage it in your npm scripts.
Expand Down Expand Up @@ -291,9 +295,9 @@ We care a lot about making the world a safer place, and that's why we continue t

Remember:

* If you are a Sonatype customer, you may file Sonatype support tickets related to `AuditJS` support in regard to this project
* We suggest you file issues here on GitHub as well, so that the community can pitch in
* If you are not a Sonatype customer, Do NOT file Sonatype support tickets related to nancy support in regard to this project, file an issue here on GitHub
- If you are a Sonatype customer, you may file Sonatype support tickets related to `AuditJS` support in regard to this project
- We suggest you file issues here on GitHub as well, so that the community can pitch in
- If you are not a Sonatype customer, Do NOT file Sonatype support tickets related to nancy support in regard to this project, file an issue here on GitHub
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DarthHater bit of a typo here :P


Have fun creating and using `AuditJS` and the [Sonatype OSS Index](https://ossindex.sonatype.org/), we are glad to have you here!

Expand Down
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"chalk": "^3.0.0",
"colors": "^1.3.1",
"figlet": "^1.2.4",
"https-proxy-agent": "^5.0.0",
"js-yaml": "3.13.1",
"log4js": "^6.1.2",
"node-fetch": "^2.6.0",
Expand Down
3 changes: 3 additions & 0 deletions src/Services/IqRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class IqRequestService {
const response = await fetch(`${this.host}${APPLICATION_INTERNAL_ID_ENDPOINT}${this.application}`, {
method: 'get',
headers: [this.getBasicAuth(), RequestHelpers.getUserAgent()],
agent: RequestHelpers.getHttpAgent(),
});
if (response.ok) {
const res = await response.json();
Expand Down Expand Up @@ -81,6 +82,7 @@ export class IqRequestService {
method: 'post',
headers: [this.getBasicAuth(), RequestHelpers.getUserAgent(), ['Content-Type', 'application/xml']],
body: data,
agent: RequestHelpers.getHttpAgent(),
},
);
if (response.ok) {
Expand All @@ -107,6 +109,7 @@ export class IqRequestService {
const response = await fetch(mergeUrl.href, {
method: 'get',
headers: [this.getBasicAuth(), RequestHelpers.getUserAgent()],
agent: RequestHelpers.getHttpAgent(),
});

const body = response.ok;
Expand Down
1 change: 1 addition & 0 deletions src/Services/OssIndexRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class OssIndexRequestService {
method: 'post',
body: JSON.stringify(data),
headers: this.getHeaders(),
agent: RequestHelpers.getHttpAgent(),
})
.then(this.checkStatus)
.then((res) => res.json())
Expand Down
24 changes: 24 additions & 0 deletions src/Services/RequestHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import expect from '../Tests/TestHelper';
import { RequestHelpers } from './RequestHelpers';
import os from 'os';

const pack = require('../../package.json');

describe('RequestHelpers', () => {
Expand All @@ -29,4 +30,27 @@ describe('RequestHelpers', () => {

expect(res).to.include.members(expected);
});

francois-roget marked this conversation as resolved.
Show resolved Hide resolved
it('should return an httpAgent when env variable is set', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
process.env.http_proxy = 'http://test.local:8080';
const res = RequestHelpers.getHttpAgent();
expect(res).not.to.be.undefined;
if (res) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
expect(res.proxy.host).to.equal('test.local');
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
expect(res.proxy.port).to.equal(8080);
}
});

it('should return undefined when no env variable is set', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
process.env.http_proxy = 'no-proxy';

const res = RequestHelpers.getHttpAgent();
expect(res).to.be.undefined;
});
});
10 changes: 10 additions & 0 deletions src/Services/RequestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import os from 'os';
import { Agent } from 'http';
import { HttpsProxyAgent } from 'https-proxy-agent';
const pack = require('../../package.json');

export class RequestHelpers {
Expand All @@ -25,4 +27,12 @@ export class RequestHelpers {

return ['User-Agent', `AuditJS/${pack.version} (${environment} ${environmentVersion}; ${system})`];
}

public static getHttpAgent(): Agent | undefined {
const proxyUrl = process.env.http_proxy || process.env.https_proxy;
if (proxyUrl !== undefined && proxyUrl !== 'no-proxy') {
return new HttpsProxyAgent(proxyUrl);
}
return undefined;
}
}