1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.searchEnvVariablesForDestination = exports.getDestinationsEnvVariable = exports.getDestinationConfig = exports.getDestinationByName = exports.getDestinationFromEnvByName = exports.getDestinations = exports.getDestinationsFromEnv = void 0;
|
4 | var util_1 = require("@sap-cloud-sdk/util");
|
5 | var proxy_util_1 = require("../../../http-agent/proxy-util");
|
6 | var jwt_1 = require("../jwt");
|
7 | var destination_1 = require("./destination");
|
8 | var logger = (0, util_1.createLogger)({
|
9 | package: 'core',
|
10 | messageContext: 'env-destination-accessor'
|
11 | });
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function getDestinationsFromEnv() {
|
19 | var destinationsEnv = getDestinationsEnvVariable();
|
20 | logger.debug("The value for the destination environment variable is: ".concat(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 (0, destination_1.isDestinationConfiguration)(destination)
|
32 | ? (0, destination_1.parseDestination)(destination)
|
33 | : (0, destination_1.sanitizeDestination)(destination);
|
34 | });
|
35 | }
|
36 | return [];
|
37 | }
|
38 | exports.getDestinationsFromEnv = getDestinationsFromEnv;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function getDestinations() {
|
49 | return getDestinationsFromEnv();
|
50 | }
|
51 | exports.getDestinations = getDestinations;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function 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 '".concat(name, "'. Only the first entry will be respected."));
|
66 | }
|
67 | var destination = matchingDestinations[0];
|
68 | return (0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.INTERNET_PROXY ||
|
69 | (0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.PRIVATELINK_PROXY
|
70 | ? (0, proxy_util_1.addProxyConfigurationInternet)(destination)
|
71 | : destination;
|
72 | }
|
73 | exports.getDestinationFromEnvByName = getDestinationFromEnvByName;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function getDestinationByName(name) {
|
84 | return getDestinationFromEnvByName(name);
|
85 | }
|
86 | exports.getDestinationByName = getDestinationByName;
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function getDestinationConfig(dest) {
|
92 | if (dest === void 0) { dest = 'ErpQueryEndpoint'; }
|
93 | return typeof dest === 'string' ? getDestinationFromEnvByName(dest) : dest;
|
94 | }
|
95 | exports.getDestinationConfig = getDestinationConfig;
|
96 |
|
97 |
|
98 |
|
99 | function getDestinationsEnvVariable() {
|
100 | return process.env['destinations'];
|
101 | }
|
102 | exports.getDestinationsEnvVariable = getDestinationsEnvVariable;
|
103 | function 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 |
|
113 |
|
114 | function 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 '".concat(name, "' from environment variable."));
|
124 | }
|
125 | else {
|
126 | logger.warn("Successfully retrieved destination '".concat(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 ".concat(error.message, "."));
|
135 | }
|
136 | }
|
137 | logger.info('No environment variable set.');
|
138 | }
|
139 | exports.searchEnvVariablesForDestination = searchEnvVariablesForDestination;
|
140 | function destinationAuthToken(token) {
|
141 | if (token) {
|
142 | var decoded = (0, 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 ".concat(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 |
|
\ | No newline at end of file |