Skip to content

Commit

Permalink
fix: Adds insecure flag, implements (#213)
Browse files Browse the repository at this point in the history
* Adds insecure flag, implements
  • Loading branch information
DarthHater authored Dec 17, 2020
1 parent 4473818 commit 88e7d87
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export class Application {
args.application as string,
args.stage as string,
args.timeout as number,
args.insecure as boolean,
);
}
}
50 changes: 45 additions & 5 deletions src/Services/IqRequestService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ describe('IQRequestService', () => {
.get(`/api/v2/applications?publicId=testapp`)
.reply(404, applicationInternalIdResponse.body);

const requestService = new IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300);
const requestService = new IqRequestService(
'admin',
'admin123',
'http://testlocation:8070',
'testapp',
stage,
300,
false,
);
const coords = [new Coordinates('commander', '2.12.2', '@types')];

return expect(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejected;
Expand All @@ -42,7 +50,15 @@ describe('IQRequestService', () => {
.get(`/api/v2/applications?publicId=testapp`)
.reply(applicationInternalIdResponse.statusCode, { thereisnoid: 'none' });

const requestService = new IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300);
const requestService = new IqRequestService(
'admin',
'admin123',
'http://testlocation:8070',
'testapp',
stage,
300,
false,
);
const coords = [new Coordinates('commander', '2.12.2', '@types')];

return expect(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejectedWith(
Expand All @@ -66,7 +82,15 @@ describe('IQRequestService', () => {
.get(`/api/v2/applications?publicId=testapp`)
.reply(applicationInternalIdResponse.statusCode, applicationInternalIdResponse.body);

const requestService = new IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300);
const requestService = new IqRequestService(
'admin',
'admin123',
'http://testlocation:8070',
'testapp',
stage,
300,
false,
);
const coords = [new Coordinates('commander', '2.12.2', '@types')];

return expect(requestService.submitToThirdPartyAPI(coords)).to.eventually.equal(
Expand All @@ -90,7 +114,15 @@ describe('IQRequestService', () => {
.get(`/api/v2/applications?publicId=testapp`)
.reply(applicationInternalIdResponse.statusCode, applicationInternalIdResponse.body);

const requestService = new IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300);
const requestService = new IqRequestService(
'admin',
'admin123',
'http://testlocation:8070',
'testapp',
stage,
300,
false,
);
const coords = [new Coordinates('commander', '2.12.2', '@types')];

return expect(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejectedWith(
Expand All @@ -113,7 +145,15 @@ describe('IQRequestService', () => {
.get(`/api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb`)
.reply(response.statusCode, response.body);

const requestService = new IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300);
const requestService = new IqRequestService(
'admin',
'admin123',
'http://testlocation:8070',
'testapp',
stage,
300,
false,
);

requestService.asyncPollForResults(
'api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb',
Expand Down
7 changes: 4 additions & 3 deletions src/Services/IqRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class IqRequestService {
readonly application: string,
readonly stage: string,
readonly timeout: number,
readonly insecure: boolean,
) {}

private async init(): Promise<void> {
Expand All @@ -49,7 +50,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(),
agent: RequestHelpers.getAgent(this.insecure),
});
if (response.ok) {
const res = await response.json();
Expand Down Expand Up @@ -83,7 +84,7 @@ export class IqRequestService {
method: 'post',
headers: [this.getBasicAuth(), RequestHelpers.getUserAgent(), ['Content-Type', 'application/xml']],
body: data,
agent: RequestHelpers.getHttpAgent(),
agent: RequestHelpers.getAgent(this.insecure),
},
);
if (response.ok) {
Expand All @@ -110,7 +111,7 @@ export class IqRequestService {
const response = await fetch(mergeUrl.href, {
method: 'get',
headers: [this.getBasicAuth(), RequestHelpers.getUserAgent()],
agent: RequestHelpers.getHttpAgent(),
agent: RequestHelpers.getAgent(this.insecure),
});

const body = response.ok;
Expand Down
11 changes: 11 additions & 0 deletions src/Services/RequestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import os from 'os';
import { Agent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { HttpsProxyAgent } from 'https-proxy-agent';
const pack = require('../../package.json');

Expand All @@ -29,6 +30,16 @@ export class RequestHelpers {
return ['User-Agent', `AuditJS/${pack.version} (${environment} ${environmentVersion}; ${system})`];
}

public static getAgent(insecure = false): Agent | undefined {
if (insecure) {
return new HttpsAgent({
rejectUnauthorized: false,
});
}

return this.getHttpAgent();
}

public static getHttpAgent(): Agent | undefined {
const proxyUrl = process.env.http_proxy || process.env.https_proxy;
if (proxyUrl !== undefined && proxyUrl !== 'no-proxy') {
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ let argv = yargs
description: 'Include Development Dependencies',
demandOption: false,
},
insecure: {
type: 'boolean',
description: 'Allow insecure connections',
demandOption: false,
},
});
})
.command('config', 'Set config for OSS Index or Nexus IQ Server')
Expand Down

0 comments on commit 88e7d87

Please sign in to comment.