UNPKG

auditjs

Version:

Audit dependencies to identify known vulnerabilities and maintenance problems

140 lines 5.94 kB
"use strict"; /* * Copyright 2019-Present Sonatype Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.IqRequestService = void 0; const RequestHelpers_1 = require("./RequestHelpers"); const Logger_1 = require("../Application/Logger/Logger"); const url_1 = require("url"); const APPLICATION_INTERNAL_ID_ENDPOINT = '/api/v2/applications?publicId='; class IqRequestService { constructor(user, password, host, application, stage, timeout, insecure) { this.user = user; this.password = password; this.host = host; this.application = application; this.stage = stage; this.timeout = timeout; this.insecure = insecure; this.internalId = ''; this.isInitialized = false; this.timeoutAttempts = 0; } async init() { try { this.internalId = await this.getApplicationInternalId(); this.isInitialized = true; } catch (e) { throw new Error(String(e)); } } async getApplicationInternalId() { const response = await fetch(`${this.host}${APPLICATION_INTERNAL_ID_ENDPOINT}${this.application}`, { method: 'get', headers: [this.getBasicAuth(), RequestHelpers_1.RequestHelpers.getUserAgent()], dispatcher: RequestHelpers_1.RequestHelpers.getAgent(this.insecure), }); if (response.ok) { const res = (await response.json()); try { return res.applications[0].id; } catch { throw new Error(`No valid ID on response from Sonatype Lifecycle, potentially check the public application ID you are using`); } } else { throw new Error('Unable to connect to Sonatype Lifecycle with http status ' + response.status + '. Check your credentials and network connectivity by hitting Sonatype Lifecycle at ' + this.host + ' in your browser.'); } } async submitToThirdPartyAPI(data) { if (!this.isInitialized) { await this.init(); } (0, Logger_1.logMessage)('Internal ID', Logger_1.DEBUG, { internalId: this.internalId }); const response = await fetch(`${this.host}/api/v2/scan/applications/${this.internalId}/sources/auditjs?stageId=${this.stage}`, { method: 'post', headers: [this.getBasicAuth(), RequestHelpers_1.RequestHelpers.getUserAgent(), ['Content-Type', 'application/xml']], body: data, dispatcher: RequestHelpers_1.RequestHelpers.getAgent(this.insecure), }); if (response.ok) { const json = (await response.json()); return json.statusUrl; } else { const body = await response.text(); (0, Logger_1.logMessage)('Response from third party API', Logger_1.DEBUG, { response: body }); throw new Error(`Unable to submit to Third Party API`); } } async asyncPollForResults(url, errorHandler, pollingFinished) { (0, Logger_1.logMessage)(url, Logger_1.DEBUG); let mergeUrl; try { mergeUrl = this.getURLOrMerge(url); // https://www.youtube.com/watch?v=Pubd-spHN-0 const response = await fetch(mergeUrl.href, { method: 'get', headers: [this.getBasicAuth(), RequestHelpers_1.RequestHelpers.getUserAgent()], dispatcher: RequestHelpers_1.RequestHelpers.getAgent(this.insecure), }); const body = response.ok; // TODO: right now I think we cover 500s and 400s the same and we'd continue polling as a result. We should likely switch // to checking explicitly for a 404 and if we get a 500/401 or other throw an error if (!body) { this.timeoutAttempts += 1; if (this.timeoutAttempts > this.timeout) { errorHandler({ message: 'Polling attempts exceeded, please either provide a higher limit via the command line using the timeout flag, or re-examine your project and logs to see if another error happened', }); } setTimeout(() => this.asyncPollForResults(url, errorHandler, pollingFinished), 1000); } else { const json = await response.json(); pollingFinished(json); } } catch (e) { const err = e instanceof Error ? e : new Error(String(e)); errorHandler({ message: err.message }); } } getURLOrMerge(url) { try { return new url_1.URL(url); } catch (e) { const err = e instanceof Error ? e : new Error(String(e)); (0, Logger_1.logMessage)(err.message, Logger_1.DEBUG); if (this.host.endsWith('/')) { return new url_1.URL(this.host.concat(url)); } return new url_1.URL(this.host.concat('/' + url)); } } getBasicAuth() { return ['Authorization', 'Basic ' + Buffer.from(this.user + ':' + this.password).toString('base64')]; } } exports.IqRequestService = IqRequestService; //# sourceMappingURL=IqRequestService.js.map