-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
334 lines (313 loc) · 12.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const OAuth2 = require("oauth").OAuth2
const cfenv = require("cfenv")
const rp = require("request-promise")
const http_verbs = require("./lib/http-verbs")
module.exports = workOn
/**
*
* @param {string} clientId
* @param {string} clientSecret
* @param {string} baseUrl - url to the OAuth server, emitting "/oauth/*" endpoints
* @returns {Promise<any>}
*/
async function getAccessTokenForDestinationInstance(clientId, clientSecret, baseUrl) {
if (cfenv.getAppEnv().isLocal) {
return Promise.resolve("mockLocalAccessToken")
}
return new Promise((resolve, reject) => {
const oAuthClient = new OAuth2(clientId, clientSecret, `${baseUrl}/`, "/oauth/authorize", "oauth/token", null)
oAuthClient.getOAuthAccessToken(
"",
{ grant_type: "client_credentials" },
(err, accessToken, refreshToken, results) => {
if (err) {
reject(err)
}
resolve(accessToken)
}
)
})
}
/**
*
* @param {string} clientId
* @param {string} clientSecret
* @param {string} baseUrl - url to the OAuth server, emitting "/oauth/*" endpoints
* @returns {Promise<any>}
*/
async function getAccessTokenForProxy(clientId, clientSecret, baseUrl) {
if (cfenv.getAppEnv().isLocal) {
return Promise.resolve("mockLocalProxyToken")
}
return new Promise((resolve, reject) => {
const oAuthClient = new OAuth2(clientId, clientSecret, `${baseUrl}/`, "/oauth/authorize", "oauth/token", null)
oAuthClient.getOAuthAccessToken(
"",
{ grant_type: "client_credentials" },
(err, accessToken, refreshToken, results) => {
if (err) {
reject(err)
}
resolve(accessToken)
}
)
})
}
/**
* retrieve destination configuration
*
* @param {string} destinationName
* @param {string} destinationApiUrl
* @param {string} accessToken - OAuth2.0 Bearer token ("client_credentials" grant type)
* @returns {Promise<T | never>}
*/
async function getDestination(destinationName, destinationApiUrl, accessToken) {
if (cfenv.getAppEnv().isLocal) {
let object = {
destinationConfiguration: {
URL: destinationName
}
}
return Promise.resolve(JSON.stringify(object))
}
const options = {
url: `${destinationApiUrl}/${destinationName}`,
headers: {
Authorization: `Bearer ${accessToken}`
}
}
return rp(options).catch(err => {
throw err // bubble-up
})
}
/**
* call a url in a destination via CF's included proxy
*
* @param {Map} parameters - various configuration options
* @param {string} parameters.url - the absolute path (e.g. /my/api) to call in the destination
* @param {object} parameters.destination - CF destination configuration object
* @param {string} parameters.proxy - CF's integrated proxy as FQDN, e.g. http://10.0.1.23:20003
* @param {string} parameters.proxyAccessToken - OAuth2.0 Bearer token ("client_credentials" grant type)
* @param {string} [parameters.contentType]
* @param {('GET'|'POST'|'PUT'|'PATCH'|'DELETE'|'HEAD'|'OPTIONS')} parameters.http_method
* @param {object} [parameters.payload] - payload for POST, PUT or PATCH
* @param {object} [parameters.formData] - play a browser a submit a form!
* @param {boolean} [parameters.fullResponse] - pass entire reponse through from BE via proxy
* @param {boolean} [parameters.techErrorOnly] - get a rejection only if the request failed for technical reasons
* @param {boolean} [parameters.binary] - whether to expect (and deliver) a binary at @param url
* @param {string} [parameters.scc_name] - location ID of SAP Cloud Connector
* @returns {Promise<T | never>}
*/
function callViaDestination(parameters) {
let {
url,
destination,
proxy,
proxyAccessToken,
contentType = "application/json",
http_method,
payload,
fullResponse,
formData,
techErrorOnly,
binary,
scc_name
} = parameters
let headers = {}
let options = {
url: `${destination.destinationConfiguration.URL}${url}`,
resolveWithFullResponse: fullResponse,
simple: !techErrorOnly
}
// this allows binary downloads
if (binary) {
Object.assign(options, {
encoding: null
})
}
// enhance only if running in CF
if (!cfenv.getAppEnv().isLocal) {
// add auth for proxy
headers = {
"Proxy-Authorization": `Bearer ${proxyAccessToken}`
}
// add proxy
Object.assign(options, {
proxy: proxy
})
}
// if configured in CF cockpit,
// use auth data
if (destination.authTokens && destination.authTokens[0]) {
headers["Authorization"] = `${destination.authTokens[0].type} ${destination.authTokens[0].value}`
}
// adding cloud connector name header if passed from request
if (scc_name) {
headers["SAP-Connectivity-SCC-Location_ID"] = `${scc_name}`
}
// enrich query option based on http verb
switch (http_method) {
case http_verbs.GET:
Object.assign(options, {
method: http_verbs.GET,
headers: Object.assign(headers, {
"Content-type": contentType
})
})
break
case http_verbs.HEAD:
Object.assign(options, {
method: http_verbs.HEAD,
headers: Object.assign(headers, {
"Content-type": contentType
})
})
break
case http_verbs.OPTIONS:
Object.assign(options, {
method: http_verbs.OPTIONS,
headers: headers
})
break
case http_verbs.POST:
// processing of "browser submitting form" behaviour
// and regular (JSON) post is different
if (parameters.formData) {
Object.assign(options, {
method: http_verbs.POST,
headers: headers,
formData: formData
})
} else {
Object.assign(options, {
method: http_verbs.POST,
headers: Object.assign(headers, {
"Content-type": contentType
}),
body: payload,
json: true
})
}
break
case http_verbs.PUT:
Object.assign(options, {
method: http_verbs.PUT,
headers: Object.assign(headers, {
"Content-type": contentType
}),
body: payload,
json: true
})
break
case http_verbs.PATCH:
Object.assign(options, {
method: http_verbs.PATCH,
headers: Object.assign(headers, {
"Content-type": contentType
}),
body: payload,
json: true
})
break
case http_verbs.DELETE:
Object.assign(options, {
method: http_verbs.DELETE,
headers: headers
})
break
}
return rp(options).catch(err => {
throw err // bubble-up
})
}
/**
*
* @param {Map} options - configuration options for several CF service instances
* @param {string} options.url - the url to call in the destination, absolute path (including leading slash)
* e.g. /api/v1/json
* @param {string} options.connectivity_instance - name of the instance of the connectivity service
* @param {string} options.uaa_instance - name of the instance of the uaa service
* @param {string} options.destination_instance - name of the instance of the destination service
* @param {string} options.destination_name - name of the destination to use
* @param {('GET'|'POST'|'PUT'|'PATCH'|'DELETE'|'HEAD'|'OPTIONS')} options.http_verb - HTTP method to use
* @param {object} [options.payload] - payload for POST, PUT or PATCH
* @param {object} [options.formData] - mimic a browser for POSTing a form to the destination; implies http verb POST
* @param {string} [options.content_type] - value for "Content-Type" http header, e.g. "application/json"
* @param {boolean} [options.full_response] - whether to have the full response (including all headers etc)
* pass through to the caller (BE -> proxy -> client)
* @param {boolean} [options.tech_error_only] - get a rejection only if the request failed for technical reasons,
* so e.g. 404 is considered a valid response
* @param {boolean} [options.binary] - whether to expect (and deliver) a binary at @param url
* @param {string} [options.scc_name] - location ID of the SAP Cloud Connector
* @returns {Promise<any | never>}
*/
async function workOn(options) {
// safeguards
if (!http_verbs.hasOwnProperty(options.http_verb)) {
throw Error(`unknown http method: ${options.http_verb}; allowed values: ${JSON.stringify(http_verbs)}`)
}
// build up necessary variables
let connectivityInstance
let connectivityClientId
let connectivityClientSecret
let proxy
let xsuaaInstance
let xsuaaUrl
let destinationInstance
let destinationApi
let destinationClientId
let destinationClientSecret
// differentiate between running in non-CF and CF environment
if (!cfenv.getAppEnv().isLocal) {
connectivityInstance = cfenv.getAppEnv().getService(options.connectivity_instance)
connectivityClientId = connectivityInstance.credentials.clientid
connectivityClientSecret = connectivityInstance.credentials.clientsecret
proxy = `http://${connectivityInstance.credentials.onpremise_proxy_host}:${connectivityInstance.credentials.onpremise_proxy_port}`
xsuaaInstance = cfenv.getAppEnv().getService(options.uaa_instance)
xsuaaUrl = xsuaaInstance.credentials.url
destinationInstance = cfenv.getAppEnv().getService(options.destination_instance)
destinationApi = `${destinationInstance.credentials.uri}/destination-configuration/v1/destinations`
destinationClientId = destinationInstance.credentials.clientid
destinationClientSecret = destinationInstance.credentials.clientsecret
} else {
connectivityClientId = "connectivityClientId"
connectivityClientSecret = "connectivityClientSecret"
proxy = null
xsuaaUrl = "http://localhost"
destinationApi = `http://localhost/destination-configuration/v1/destinations`
destinationClientId = "destinationClientId"
destinationClientSecret = "destinationClientSecret"
}
let queriedDestination = {}
return getAccessTokenForDestinationInstance(destinationClientId, destinationClientSecret, xsuaaUrl)
.then(accessTokenForDestination => {
return getDestination(options.destination_name, destinationApi, accessTokenForDestination)
})
.then(destination => {
queriedDestination = JSON.parse(destination)
return getAccessTokenForProxy(connectivityClientId, connectivityClientSecret, xsuaaUrl)
})
.then(accessTokenForProxy => {
return callViaDestination({
url: options.url,
destination: queriedDestination,
proxy: proxy,
proxyAccessToken: String(accessTokenForProxy),
contentType: options.content_type || undefined,
http_method: options.http_verb,
payload: options.payload || undefined,
formData: options.formData || undefined,
fullResponse: options.full_response || false,
techErrorOnly: options.tech_error_only || false,
binary: options.binary || false,
scc_name: options.scc_name || undefined
})
})
.then(data => {
return data
})
.catch(err => {
console.error(`couldn't query BE resource via destination: ${JSON.stringify(err)}`)
throw err // re-throw for bubble-up
})
}