-
Notifications
You must be signed in to change notification settings - Fork 45
/
connectivity-service.ts
124 lines (111 loc) · 3.65 KB
/
connectivity-service.ts
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
import { createLogger, errorWithCause } from '@sap-cloud-sdk/util';
import { Protocol } from '../protocol';
import { ProxyConfiguration } from './connectivity-service-types';
import { Destination } from './destination/destination-service-types';
import { EnvironmentAccessor } from './environment-accessor';
import { Service } from './environment-accessor-types';
import { serviceToken } from './token-accessor';
const logger = createLogger({
package: 'core',
messageContext: 'connectivity-service'
});
/**
* Given a destination and a JWT (required for subscriber destinations), this function will add a proxy configuration to a destination.
* See also [[ProxyConfiguration]].
*
* This function will reject if no connectivity service is bound, no XSUAA service with plan application is bound or the client credentials grant with the XSUAA service fails.
*
* @Deprecated Since v1.16.0. Use [[addProxyConfigurationOnPrem]] instead.
*
* @param destination - The destination to which the proxy configuration should be added.
* @param jwt - The JWT of the current user.
* @returns A promise resolving to the destiation with the added proxy configuration.
*/
export function addProxyConfiguration(
destination: Destination,
jwt?: string
): Promise<Destination> {
return Promise.resolve()
.then(() => proxyHostAndPort())
.then(hostAndPort => addHeaders(hostAndPort, jwt))
.then(proxyConfiguration => ({
...destination,
proxyConfiguration
}));
}
export function addProxyConfigurationOnPrem(
destination: Destination,
jwt?: string
): Promise<Destination> {
return addProxyConfiguration(destination, jwt);
}
interface HostAndPort {
host: string;
port: number;
protocol: Protocol.HTTP;
}
function proxyHostAndPort(): HostAndPort {
const service = readConnectivityServiceBinding();
return {
host: service.credentials.onpremise_proxy_host,
port: service.credentials.onpremise_proxy_port,
protocol: Protocol.HTTP
};
}
function readConnectivityServiceBinding(): Service {
const serviceBindings = EnvironmentAccessor.getServiceList('connectivity');
if (!serviceBindings.length) {
throw new Error(
'No binding to a connectivity service found! Please make sure to bind an instance of the connectivity service to your app if you want to connect to on-premise destinations.'
);
}
return serviceBindings[0];
}
function addHeaders(
hostAndPort: HostAndPort,
jwt?: string
): Promise<ProxyConfiguration> {
const connServiceBinding = readConnectivityServiceBinding();
return Promise.resolve()
.then(() => proxyAuthorizationHeader(connServiceBinding, jwt))
.then(proxyAuthHeader => ({
...proxyAuthHeader,
...sapConnectivityAuthenticationHeader(jwt)
}))
.then(
headers =>
({
...hostAndPort,
headers
} as ProxyConfiguration)
);
}
function proxyAuthorizationHeader(
connectivityServiceBinding: Service,
userJwt?
): Promise<Record<string, string>> {
return serviceToken(connectivityServiceBinding, { userJwt })
.then(token => ({
'Proxy-Authorization': `Bearer ${token}`
}))
.catch(error => {
throw errorWithCause(
'Failed to add proxy authorization header - client credentials grant failed!',
error
);
});
}
function sapConnectivityAuthenticationHeader(
jwt?: string
): Record<string, string> {
if (jwt) {
return {
'SAP-Connectivity-Authentication': `Bearer ${jwt}`
};
}
logger.warn(
`Unable to create "SAP-Connectivity-Authentication" header: no JWT found on the current request.
Continuing without header. Connecting to on-premise systems may not be possible.`
);
return {};
}