UNPKG

6.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.searchEnvVariablesForDestination = exports.getDestinationsEnvVariable = exports.getDestinationConfig = exports.getDestinationByName = exports.getDestinationFromEnvByName = exports.getDestinations = exports.getDestinationsFromEnv = void 0;
4var util_1 = require("@sap-cloud-sdk/util");
5var proxy_util_1 = require("../../../http-agent/proxy-util");
6var jwt_1 = require("../jwt");
7var destination_1 = require("./destination");
8var logger = util_1.createLogger({
9 package: 'core',
10 messageContext: 'env-destination-accessor'
11});
12/**
13 * Get all destinations from the environment variable "destinations".
14 * This is discouraged for productive use! Use [[useOrFetchDestination]] for fetching destinations from the Cloud Foundry destination service.
15 *
16 * @returns A list of destinations
17 */
18function getDestinationsFromEnv() {
19 var destinationsEnv = getDestinationsEnvVariable();
20 logger.debug("The value for the destination environment variable is: " + destinationsEnv);
21 if (destinationsEnv) {
22 var destinations = void 0;
23 try {
24 destinations = JSON.parse(destinationsEnv);
25 }
26 catch (err) {
27 throw new util_1.ErrorWithCause('Error in parsing the destinations from the environment variable.', err);
28 }
29 validateDestinations(destinations);
30 return destinations.map(function (destination) {
31 return destination_1.isDestinationConfiguration(destination)
32 ? destination_1.parseDestination(destination)
33 : destination_1.sanitizeDestination(destination);
34 });
35 }
36 return [];
37}
38exports.getDestinationsFromEnv = getDestinationsFromEnv;
39/**
40 * @deprecated Since v1.4.2. Use [[getDestinationsFromEnv]] instead.
41 *
42 * Get all destinations from the environment variable "destinations".
43 * This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
44 * from the Cloud Foundry destination service.
45 *
46 * @returns A list of destinations
47 */
48function getDestinations() {
49 return getDestinationsFromEnv();
50}
51exports.getDestinations = getDestinations;
52/**
53 * Get a destination from the environment variables by name. If there are multiple destinations with the same name the first one will be used.
54 * This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
55 * from the Cloud Foundry destination service.
56 * @param name - Name of the destination
57 * @returns The requested destination if existent, otherwise `null`
58 */
59function getDestinationFromEnvByName(name) {
60 var matchingDestinations = getDestinationsFromEnv().filter(function (dest) { return dest.name === name; });
61 if (!matchingDestinations.length) {
62 return null;
63 }
64 if (matchingDestinations.length > 1) {
65 logger.warn("The 'destinations' env variable contains multiple destinations with the name '" + name + "'. Only the first entry will be respected.");
66 }
67 var destination = matchingDestinations[0];
68 return proxy_util_1.proxyStrategy(destination) === proxy_util_1.ProxyStrategy.INTERNET_PROXY ||
69 proxy_util_1.proxyStrategy(destination) === proxy_util_1.ProxyStrategy.PRIVATELINK_PROXY
70 ? proxy_util_1.addProxyConfigurationInternet(destination)
71 : destination;
72}
73exports.getDestinationFromEnvByName = getDestinationFromEnvByName;
74/**
75 * @deprecated Since v1.4.2. Use [[getDestinationFromEnvByName]] instead.
76 *
77 * Get a destination from the environment variables by name. Throws an error if there are multiple destinations with the same name.
78 * This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
79 * from the Cloud Foundry destination service.
80 * @param name - Name of the destination
81 * @returns The requested destination if existent, otherwise `null`
82 */
83function getDestinationByName(name) {
84 return getDestinationFromEnvByName(name);
85}
86exports.getDestinationByName = getDestinationByName;
87/* eslint-disable valid-jsdoc */
88/**
89 * @hidden
90 */
91function getDestinationConfig(dest) {
92 if (dest === void 0) { dest = 'ErpQueryEndpoint'; }
93 return typeof dest === 'string' ? getDestinationFromEnvByName(dest) : dest;
94}
95exports.getDestinationConfig = getDestinationConfig;
96/**
97 * @hidden
98 */
99function getDestinationsEnvVariable() {
100 return process.env['destinations'];
101}
102exports.getDestinationsEnvVariable = getDestinationsEnvVariable;
103function validateDestinations(destinations) {
104 destinations.forEach(function (destination) {
105 if (typeof destination.name === 'undefined' &&
106 typeof destination.Name === 'undefined') {
107 logger.warn("Destination from 'destinations' env variable is missing 'name' or 'Name' property.");
108 }
109 });
110}
111/**
112 * @hidden
113 */
114function searchEnvVariablesForDestination(name, options) {
115 if (options === void 0) { options = {}; }
116 logger.info('Attempting to retrieve destination from environment variable.');
117 if (getDestinationsEnvVariable()) {
118 try {
119 var destination = getDestinationFromEnvByName(name);
120 if (destination) {
121 if (destination.forwardAuthToken) {
122 destination.authTokens = destinationAuthToken(options.userJwt);
123 logger.info("Successfully retrieved destination '" + name + "' from environment variable.");
124 }
125 else {
126 logger.warn("Successfully retrieved destination '" + name + "' from environment variable." +
127 'This is discouraged for productive applications. ' +
128 'Unset the variable to read destinations from the destination service on SAP Business Technology Platform.');
129 }
130 return destination;
131 }
132 }
133 catch (error) {
134 logger.error("Error in reading the given destinations from the environment variable " + error.message + ".");
135 }
136 }
137 logger.info('No environment variable set.');
138}
139exports.searchEnvVariablesForDestination = searchEnvVariablesForDestination;
140function destinationAuthToken(token) {
141 if (token) {
142 var decoded = jwt_1.decodeJwt(token);
143 logger.info("Option 'forwardAuthToken' enabled on destination. Using the initial token for the destination.");
144 return [
145 {
146 value: token,
147 expiresIn: decoded.exp.toString(),
148 error: null,
149 http_header: { key: 'Authorization', value: "Bearer " + token },
150 type: 'Bearer'
151 }
152 ];
153 }
154 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.");
155}
156//# sourceMappingURL=destination-from-env.js.map
\No newline at end of file