This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Support https requests without certificate authentication #19
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,11 @@ | |
*/ | ||
|
||
var http = require('http'); | ||
var https = require('https'); | ||
var url = require('url'); | ||
|
||
/** | ||
* Makes a HTTP request to "{endpoint}/_opendistro/_performanceanalyzer/metrics?metrics=${metrics}&agg=${aggregates}&dim=${dimensions}&nodes=all" | ||
* Makes a HTTP(S) request to "{endpoint}/_opendistro/_performanceanalyzer/metrics?metrics=${metrics}&agg=${aggregates}&dim=${dimensions}&nodes=all" | ||
* and parses the response to a hashmap object. | ||
* | ||
* @param {string} endpoint - endpoint for the metric query. | ||
|
@@ -31,8 +32,9 @@ function getMetricData (endpoint, metrics, aggregates, dimensions, done) { | |
var aggParam = (aggregates) ? `&agg=${aggregates}` : '&agg='; | ||
var dimParam = (dimensions) ? `&dim=${dimensions}` : '&dim='; | ||
|
||
var httpOptions = getHttpURLOptions(endpoint, `/_opendistro/_performanceanalyzer/metrics?${metricParam}${aggParam}${dimParam}&nodes=all`); | ||
makeHttpRequest(httpOptions, function (response) { | ||
var urlOptions = getURLOptions(endpoint, `/_opendistro/_performanceanalyzer/metrics?${metricParam}${aggParam}${dimParam}&nodes=all`); | ||
|
||
makeRequest(urlOptions, function (response) { | ||
if (response === '') { | ||
done({}); | ||
} else { | ||
|
@@ -42,28 +44,29 @@ function getMetricData (endpoint, metrics, aggregates, dimensions, done) { | |
} | ||
|
||
/** | ||
* Gets a URL option with host, port (default = 80), and path for http.request(). | ||
* Gets a URL option with host, port (default = 80), and path for http(s).request(). | ||
* | ||
* @param {string} endpoint - endpoint for the query. | ||
* @param {string} path - metric query request path. | ||
*/ | ||
function getHttpURLOptions (endpoint, path) { | ||
endpoint = endpoint.startsWith('http://') ? endpoint : 'http://' + endpoint; | ||
var httpURL = url.parse(url.resolve(endpoint, path)); | ||
function getURLOptions (endpoint, path) { | ||
endpoint = ( endpoint.startsWith('http://') || endpoint.startsWith('https://') ) ? endpoint : 'http://' + endpoint; | ||
var parsedURL = url.parse(url.resolve(endpoint, path)); | ||
return { | ||
host: httpURL.hostname, | ||
port: (httpURL.port === null) ? 80 : httpURL.port, | ||
path: httpURL.path | ||
protocol: parsedURL.protocol, | ||
host: parsedURL.hostname, | ||
port: (parsedURL.port === null) ? 80 : parsedURL.port, | ||
path: parsedURL.path | ||
}; | ||
} | ||
|
||
/** | ||
* Makes a HTTP request and returns the response. | ||
* Makes a HTTP(S) request and returns the response. | ||
* | ||
* @param {string} httpOptions - URL option for the HTTP request. | ||
* @param {string} done - callback for the HTTP response. | ||
* @param {string} urlOptions - URL option for the HTTP(S) request. | ||
* @param {string} done - callback for the HTTP(S) response. | ||
*/ | ||
function makeHttpRequest (httpOptions, done) { | ||
function makeRequest (urlOptions, done) { | ||
var rawData = ''; | ||
var respond = function (response) { | ||
response.on('data', function (chunk) { | ||
|
@@ -73,8 +76,13 @@ function makeHttpRequest (httpOptions, done) { | |
done(rawData); | ||
}); | ||
}; | ||
|
||
var req = http.request(httpOptions, respond); | ||
var protocol = http; | ||
if (urlOptions.protocol == "https:") { | ||
protocol = https; | ||
urlOptions.rejectUnauthorized = false; | ||
} | ||
var protocol = (urlOptions.protocol == "https:") ? https : http; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seem to be doing this twice. We can remove the if block There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Removed the repetition. |
||
var req = protocol.request(urlOptions, respond); | ||
req.on('error', function (error) { | ||
console.error(error); | ||
done(''); | ||
|
@@ -83,29 +91,29 @@ function makeHttpRequest (httpOptions, done) { | |
} | ||
|
||
/** | ||
* Makes a HTTP request to "{endpoint}/_opendistro/_performanceanalyzer/metrics/units" | ||
* Makes a HTTP(S) request to "{endpoint}/_opendistro/_performanceanalyzer/metrics/units" | ||
* and parses the response to a hashmap object. | ||
* | ||
* @param {string} endpoint - endpoint for the query. | ||
* @param {string} done - callback for the HTTP response. | ||
* @param {string} done - callback for the HTTP(S) response. | ||
*/ | ||
function getMetricUnits (endpoint, done) { | ||
var httpOptions = getHttpURLOptions(endpoint, '/_opendistro/_performanceanalyzer/metrics/units'); | ||
makeHttpRequest(httpOptions, function (response) { | ||
var urlOptions = getURLOptions(endpoint, '/_opendistro/_performanceanalyzer/metrics/units'); | ||
makeRequest(urlOptions, function (response) { | ||
if (response === '') { | ||
console.error('Failed to retrieve units for metrics. HTTP response was empty.'); | ||
console.error('Failed to retrieve units for metrics. HTTP(S) response was empty.'); | ||
done({}); | ||
} | ||
try { | ||
var jsonData = JSON.parse(response); | ||
if (Object.keys(jsonData).length === 1 && 'error' in jsonData) { | ||
console.error(`Failed to retrieve units for metrics. HTTP response was: | ||
console.error(`Failed to retrieve units for metrics. HTTP(S) response was: | ||
${response}`); | ||
done({}); | ||
} | ||
done(jsonData); | ||
} catch (e) { | ||
console.error(`HTTP Response for metricUnits was not in JSON format: | ||
console.error(`HTTP(S) Response for metricUnits was not in JSON format: | ||
${response}`); | ||
done({}); | ||
} | ||
|
@@ -156,7 +164,7 @@ function sortDataByDecreasingOrder (dimensions, data, sortBy) { | |
* @param {Array} data - 2D array to modify. | ||
*/ | ||
function addCommaDelimiter (data) { | ||
if (data.length == 0) { | ||
if (data.length === 0) { | ||
return; | ||
} | ||
var numColumn = data[0].length; | ||
|
@@ -208,9 +216,9 @@ function aggregateMetricData (metricData) { | |
} | ||
|
||
/** | ||
* Given a string HTTP response, parse and return the data into a hashmap | ||
* Given a string HTTP(S) response, parse and return the data into a hashmap | ||
* | ||
* @param {object} rawData - string response from the HTTP request that can be parsed into a JSON object. | ||
* @param {object} rawData - string response from the HTTP(S) request that can be parsed into a JSON object. | ||
* @returns {object} - returns a hashmap in the format of | ||
* { nodeName1: { fields: [dim1, dim2, ...] , | ||
* data: [[data], [data], ...], | ||
|
@@ -224,12 +232,12 @@ function getDataPerNode (rawData) { | |
try { | ||
jsonData = JSON.parse(rawData); | ||
if (Object.keys(jsonData).length === 1 && 'error' in jsonData) { | ||
console.error(`Failed to retrieve data for metrics. HTTP response was: | ||
console.error(`Failed to retrieve data for metrics. HTTP(S) response was: | ||
${rawData}`); | ||
return {}; | ||
} | ||
} catch (e) { | ||
console.error(`HTTP Response for per-node data was not in JSON format: | ||
console.error(`HTTP(S) Response for per-node data was not in JSON format: | ||
${rawData}`); | ||
return {}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think client might be a better name for this variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done