@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
156 lines • 7.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchEnvVariablesForDestination = exports.getDestinationsEnvVariable = exports.getDestinationConfig = exports.getDestinationByName = exports.getDestinationFromEnvByName = exports.getDestinations = exports.getDestinationsFromEnv = void 0;
var util_1 = require("@sap-cloud-sdk/util");
var proxy_util_1 = require("../../../http-agent/proxy-util");
var jwt_1 = require("../jwt");
var destination_1 = require("./destination");
var logger = (0, util_1.createLogger)({
package: 'core',
messageContext: 'env-destination-accessor'
});
/**
* Get all destinations from the environment variable "destinations".
* This is discouraged for productive use! Use [[useOrFetchDestination]] for fetching destinations from the Cloud Foundry destination service.
*
* @returns A list of destinations
*/
function getDestinationsFromEnv() {
var destinationsEnv = getDestinationsEnvVariable();
logger.debug("The value for the destination environment variable is: ".concat(destinationsEnv));
if (destinationsEnv) {
var destinations = void 0;
try {
destinations = JSON.parse(destinationsEnv);
}
catch (err) {
throw new util_1.ErrorWithCause('Error in parsing the destinations from the environment variable.', err);
}
validateDestinations(destinations);
return destinations.map(function (destination) {
return (0, destination_1.isDestinationConfiguration)(destination)
? (0, destination_1.parseDestination)(destination)
: (0, destination_1.sanitizeDestination)(destination);
});
}
return [];
}
exports.getDestinationsFromEnv = getDestinationsFromEnv;
/**
* @deprecated Since v1.4.2. Use [[getDestinationsFromEnv]] instead.
*
* Get all destinations from the environment variable "destinations".
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
*
* @returns A list of destinations
*/
function getDestinations() {
return getDestinationsFromEnv();
}
exports.getDestinations = getDestinations;
/**
* Get a destination from the environment variables by name. If there are multiple destinations with the same name the first one will be used.
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
* @param name - Name of the destination
* @returns The requested destination if existent, otherwise `null`
*/
function getDestinationFromEnvByName(name) {
var matchingDestinations = getDestinationsFromEnv().filter(function (dest) { return dest.name === name; });
if (!matchingDestinations.length) {
return null;
}
if (matchingDestinations.length > 1) {
logger.warn("The 'destinations' env variable contains multiple destinations with the name '".concat(name, "'. Only the first entry will be respected."));
}
var destination = matchingDestinations[0];
return (0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.INTERNET_PROXY ||
(0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.PRIVATELINK_PROXY
? (0, proxy_util_1.addProxyConfigurationInternet)(destination)
: destination;
}
exports.getDestinationFromEnvByName = getDestinationFromEnvByName;
/**
* @deprecated Since v1.4.2. Use [[getDestinationFromEnvByName]] instead.
*
* Get a destination from the environment variables by name. Throws an error if there are multiple destinations with the same name.
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
* @param name - Name of the destination
* @returns The requested destination if existent, otherwise `null`
*/
function getDestinationByName(name) {
return getDestinationFromEnvByName(name);
}
exports.getDestinationByName = getDestinationByName;
/* eslint-disable valid-jsdoc */
/**
* @hidden
*/
function getDestinationConfig(dest) {
if (dest === void 0) { dest = 'ErpQueryEndpoint'; }
return typeof dest === 'string' ? getDestinationFromEnvByName(dest) : dest;
}
exports.getDestinationConfig = getDestinationConfig;
/**
* @hidden
*/
function getDestinationsEnvVariable() {
return process.env['destinations'];
}
exports.getDestinationsEnvVariable = getDestinationsEnvVariable;
function validateDestinations(destinations) {
destinations.forEach(function (destination) {
if (typeof destination.name === 'undefined' &&
typeof destination.Name === 'undefined') {
logger.warn("Destination from 'destinations' env variable is missing 'name' or 'Name' property.");
}
});
}
/**
* @hidden
*/
function searchEnvVariablesForDestination(name, options) {
if (options === void 0) { options = {}; }
logger.info('Attempting to retrieve destination from environment variable.');
if (getDestinationsEnvVariable()) {
try {
var destination = getDestinationFromEnvByName(name);
if (destination) {
if (destination.forwardAuthToken) {
destination.authTokens = destinationAuthToken(options.userJwt);
logger.info("Successfully retrieved destination '".concat(name, "' from environment variable."));
}
else {
logger.warn("Successfully retrieved destination '".concat(name, "' from environment variable.") +
'This is discouraged for productive applications. ' +
'Unset the variable to read destinations from the destination service on SAP Business Technology Platform.');
}
return destination;
}
}
catch (error) {
logger.error("Error in reading the given destinations from the environment variable ".concat(error.message, "."));
}
}
logger.info('No environment variable set.');
}
exports.searchEnvVariablesForDestination = searchEnvVariablesForDestination;
function destinationAuthToken(token) {
if (token) {
var decoded = (0, jwt_1.decodeJwt)(token);
logger.info("Option 'forwardAuthToken' enabled on destination. Using the initial token for the destination.");
return [
{
value: token,
expiresIn: decoded.exp.toString(),
error: null,
http_header: { key: 'Authorization', value: "Bearer ".concat(token) },
type: 'Bearer'
}
];
}
logger.warn("Option 'forwardAuthToken' was set on destination but no token was provided to forward. This is most likely unintended and will lead to a authorization error on request execution.");
}
//# sourceMappingURL=destination-from-env.js.map
;