@zowe/imperative
Version:
framework for building configurable CLIs
192 lines • 8.37 kB
JavaScript
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxySettings = void 0;
const process_1 = require("process");
const url_1 = require("url");
const http_proxy_agent_1 = require("http-proxy-agent");
const https_proxy_agent_1 = require("https-proxy-agent");
const SessConstants_1 = require("../session/SessConstants");
/**
* Utility class to provide an http agent to REST APIs that is configured for
* a proxy server based on commonly used environment variables.
*
* Supports the usage of the environment variables HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxy.
* If any of these env variables is set and depending how the Zowe session is configured for http or
* https it instantiates an appropriate http agent utilizing a popular third party library. If the z/OS
* system uses self-signed certificates then the proxy server must be configured to accept them. If the
* proxy server itself is configured with self-signed certificates then the user needs to either import
* these certificates on their workstation, use rejectUnauthorized in their Zowe profile, or use the (not
* recommended) nodejs variable NODE_TLS_REJECT_UNAUTHORIZED=0. This class also looks for the environment
* variables NO_PROXY or no_proxy. These work with a simple comma separated list of hostnames that need
* to match with the hostname of the Zowe profile.
*/
class ProxySettings {
/**
* Retrieve an appropriate http.agent instance if proxy environment variables can be found.
* @static
* @param session Zowe `ISession` containing the hostname for the http request.
* Uses the session's `rejectUnauthorized` also for the proxy connection.
* @returns an instance of an appropriate subclass of node's https.agent if proxy
* settings were found. Returns `undefined` if no proxy settings are found.
* @memberof ProxySettings
*/
static getProxyAgent(session) {
var _a;
const proxySetting = this.getProxySettings(session);
const proxyOptions = {};
const authHeader = ProxySettings.getProxyAuthHeader(proxySetting);
if (authHeader) {
proxyOptions.headers = authHeader;
}
if (!(proxySetting === null || proxySetting === void 0 ? void 0 : proxySetting.protocol)) {
return;
}
if (proxySetting.protocol === SessConstants_1.HTTP_PROTOCOL) {
return new http_proxy_agent_1.HttpProxyAgent(proxySetting.proxyUrl, proxyOptions);
}
if (proxySetting.protocol === SessConstants_1.HTTPS_PROTOCOL) {
proxyOptions.rejectUnauthorized =
(_a = session.rejectUnauthorized) !== null && _a !== void 0 ? _a : true;
return new https_proxy_agent_1.HttpsProxyAgent(proxySetting.proxyUrl, proxyOptions);
}
}
/**
* Returns the URL to the proxy server if proxy environment variables can be found.
* Can be used for testing the settings and logging connection details.
* @static
* @param session Zowe `ISession` containing the hostname for the http request.
* @returns `URL` to proxy server
* @memberof ProxySettings
*/
static getSystemProxyUrl(session) {
var _a;
return (_a = this.getProxySettings(session)) === null || _a === void 0 ? void 0 : _a.proxyUrl;
}
/**
* If the NO_PROXY or no_proxy environment variables are set with a comma separated
* list of hostnames it will try to match the hostname of the Zowe `ISession` and
* return `true` if found. Performs a simple string compare ignoring casing and white
* spaces, but will not resolve hostnames to ip addressees and not perform wildcard matching.
* @static
* @param session Zowe `ISession` containing the hostname for the http request.
* @returns `true` if the Zowe session host matches an entry in the comma separated
* list of hostnames in the environment variable. `false` otherwise.
* @memberof ProxySettings
*/
static matchesNoProxySettings(session) {
var _a, _b;
const noProxyValues = (_b = (_a = session.proxy) === null || _a === void 0 ? void 0 : _a.no_proxy) !== null && _b !== void 0 ? _b : this.getNoProxyEnvVariables();
if (!noProxyValues) {
return false;
}
if (noProxyValues.includes(session.hostname.toLocaleLowerCase())) {
return true;
}
return false;
}
static getProxyAuthHeader(proxySetting) {
return (proxySetting === null || proxySetting === void 0 ? void 0 : proxySetting.authSetting)
? { "Proxy-Authorization": proxySetting.authSetting }
: undefined;
}
/**
* Parses environment variables for proxy servers.
* @private
* @static
* @param session Zowe `ISession` containing the hostname for the http request.
* @returns instance of private `ProxySetting` or `undefined`
* @memberof ProxySettings
*/
static getProxySettings(session) {
var _a, _b, _c, _d, _e, _f;
if (this.matchesNoProxySettings(session)) {
return;
}
const protocol = (_a = session.protocol) !== null && _a !== void 0 ? _a : SessConstants_1.HTTPS_PROTOCOL;
let envVariable;
if (protocol === SessConstants_1.HTTP_PROTOCOL) {
envVariable =
(_c = (_b = session.proxy) === null || _b === void 0 ? void 0 : _b.http_proxy) !== null && _c !== void 0 ? _c : this.getHttpEnvVariables();
}
else if (protocol === SessConstants_1.HTTPS_PROTOCOL) {
envVariable =
(_e = (_d = session.proxy) === null || _d === void 0 ? void 0 : _d.https_proxy) !== null && _e !== void 0 ? _e : this.getHttpsEnvVariables();
}
const proxyUrl = this.checkUrl(envVariable);
const authSetting = (_f = session.proxy) === null || _f === void 0 ? void 0 : _f.proxy_authorization;
if (authSetting) {
return { proxyUrl, protocol, authSetting };
}
if (proxyUrl) {
return { proxyUrl, protocol };
}
}
/**
* Parses environment variables valid for http requests.
* @private
* @static
* @returns `string` if valid variable is found or undefined.
* @memberof ProxySettings
*/
static getHttpEnvVariables() {
var _a;
return (_a = process_1.env.HTTP_PROXY) !== null && _a !== void 0 ? _a : process_1.env.http_proxy;
}
/**
* Parses environment variables valid for https requests.
* @private
* @static
* @returns `string` if valid variable is found or undefined.
* @memberof ProxySettings
*/
static getHttpsEnvVariables() {
var _a, _b;
return (_b = (_a = process_1.env.HTTPS_PROXY) !== null && _a !== void 0 ? _a : process_1.env.https_proxy) !== null && _b !== void 0 ? _b : this.getHttpEnvVariables();
}
/**
* Parses environment variables valid for no proxy exceptions.
* @private
* @static
* @returns `string[]` of all hostnames found in the comma separated list
* in lowercase without white spaces.
* @memberof ProxySettings
*/
static getNoProxyEnvVariables() {
var _a;
const noProxyValue = (_a = process_1.env.NO_PROXY) !== null && _a !== void 0 ? _a : process_1.env.no_proxy;
if (!noProxyValue) {
return;
}
return noProxyValue
.split(",")
.map((entry) => entry.trim().toLocaleLowerCase());
}
/**
* Parses a string to check if it is a valid URL.
* @private
* @static
* @param inputUrl a string with a URL
* @returns a URL instance or undefined if not a valid url.
* @memberof Proxy
*/
static checkUrl(inputUrl) {
try {
return new url_1.URL(inputUrl);
}
catch (_a) {
return;
}
}
}
exports.ProxySettings = ProxySettings;
//# sourceMappingURL=ProxySettings.js.map
;