This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathConfigurationHydrator.ts
315 lines (288 loc) · 9.56 KB
/
ConfigurationHydrator.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
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
import fs from 'fs'
import { Duration } from 'typed-duration'
import { parse } from 'url'
import { getEnv } from './EnvFunction'
import { CustomSSL } from './GrpcClient'
import * as ZB from './interfaces-1.0'
import { Loglevel, ZBClientOptions } from './interfaces-published-contract'
import { OAuthProviderConfig } from './OAuthProvider'
const CamundaCloudDefaultRegion = 'bru-2'
export class ConfigurationHydrator {
// These are the environment variables that can be used to configure the client
public static ENV = () =>
getEnv([
'ZEEBE_NODE_LOG_LEVEL',
'ZEEBE_GATEWAY_ADDRESS',
'ZEEBE_ADDRESS',
'ZEEBE_CLIENT_ID',
'ZEEBE_CLIENT_SECRET',
'ZEEBE_SECURE_CONNECTION',
'ZEEBE_TOKEN_AUDIENCE',
'ZEEBE_TOKEN_SCOPE',
'ZEEBE_AUTHORIZATION_SERVER_URL',
'ZEEBE_CAMUNDA_CLOUD_CLUSTER_ID',
'ZEEBE_BASIC_AUTH_PASSWORD',
'ZEEBE_BASIC_AUTH_USERNAME',
'ZEEBE_NODE_EAGER_CONNECT',
'ZEEBE_CLIENT_RETRY',
'ZEEBE_CLIENT_MAX_RETRIES',
'ZEEBE_CLIENT_MAX_RETRY_TIMEOUT',
'ZEEBE_CLIENT_SSL_ROOT_CERTS_PATH',
'ZEEBE_CLIENT_SSL_PRIVATE_KEY_PATH',
'ZEEBE_CLIENT_SSL_CERT_CHAIN_PATH',
'ZEEBE_TENANT_ID',
])
public static configure(
gatewayAddress: string | undefined,
options: ZBClientOptions | undefined
) {
// ConfigurationHydrator.warnOnAmbiguousConfig()
const configuration = {
hostname: 'localhost',
port: '26500',
...ConfigurationHydrator.readCustomSSLFromEnvironment(),
...ConfigurationHydrator.readBasicAuthFromEnvironment(),
...ConfigurationHydrator.readOAuthFromEnvironment(gatewayAddress),
...ConfigurationHydrator.getGatewayFromEnvironment(),
...ConfigurationHydrator.decodeConnectionString(gatewayAddress),
...ConfigurationHydrator.getCamundaCloudConfig(options),
...ConfigurationHydrator.readTLSFromEnvironment(options),
...ConfigurationHydrator.getEagerStatus(options),
...ConfigurationHydrator.getRetryConfiguration(options),
...ConfigurationHydrator.getTenantId(options),
}
// inherit oAuth custom root certificates, unless
// others are explicitly provided
if (
configuration?.oAuth &&
!configuration.oAuth.customRootCert &&
configuration.customSSL?.rootCerts
) {
configuration.oAuth.customRootCert =
configuration.customSSL.rootCerts
}
return configuration
}
private static readonly getTenantId = (options: ZBClientOptions | undefined) => ({tenantId: options?.tenantId ?? ConfigurationHydrator.ENV().ZEEBE_TENANT_ID })
public static readonly getLogLevelFromEnv = () =>
ConfigurationHydrator.ENV().ZEEBE_NODE_LOG_LEVEL as Loglevel | undefined
private static readonly DEFAULT_GATEWAY_PORT = '26500'
private static readonly CAMUNDA_CLOUD_AUTH_SERVER =
'https://login.cloud.camunda.io/oauth/token'
private static readonly getClientIdFromEnv = () =>
ConfigurationHydrator.ENV().ZEEBE_CLIENT_ID
private static readonly getZeebeAddressFromEnv = () =>
ConfigurationHydrator.ENV().ZEEBE_ADDRESS ||
ConfigurationHydrator.ENV().ZEEBE_GATEWAY_ADDRESS
private static readonly getClientSecretFromEnv = () =>
ConfigurationHydrator.ENV().ZEEBE_CLIENT_SECRET
private static readonly getTlsFromEnv = () =>
(
ConfigurationHydrator.ENV().ZEEBE_SECURE_CONNECTION || ''
).toLowerCase() === 'true'
? true
: (
ConfigurationHydrator.ENV().ZEEBE_SECURE_CONNECTION || ''
).toLowerCase() === 'false'
? false
: undefined
private static readTLSFromEnvironment(options: any = {}) {
const useTLS = options.useTLS ?? ConfigurationHydrator.getTlsFromEnv()
return {
useTLS,
}
}
private static readCustomSSLFromEnvironment(): {
customSSL: Partial<CustomSSL>
} {
const rootCerts = ConfigurationHydrator.ENV()
.ZEEBE_CLIENT_SSL_ROOT_CERTS_PATH
const certChain = ConfigurationHydrator.ENV()
.ZEEBE_CLIENT_SSL_CERT_CHAIN_PATH
const privateKey = ConfigurationHydrator.ENV()
.ZEEBE_CLIENT_SSL_PRIVATE_KEY_PATH
const customSSL = {
certChain: certChain ? fs.readFileSync(certChain) : undefined,
privateKey: privateKey ? fs.readFileSync(privateKey) : undefined,
rootCerts: rootCerts ? fs.readFileSync(rootCerts) : undefined,
}
return {
customSSL,
}
}
private static readOAuthFromEnvironment(
gatewayAddress
): OAuthProviderConfig | {} {
const clientId = ConfigurationHydrator.getClientIdFromEnv()
const clientSecret = ConfigurationHydrator.getClientSecretFromEnv()
const audience = ConfigurationHydrator.ENV().ZEEBE_TOKEN_AUDIENCE
const scope = ConfigurationHydrator.ENV().ZEEBE_TOKEN_SCOPE
const authServerUrl = ConfigurationHydrator.ENV()
.ZEEBE_AUTHORIZATION_SERVER_URL
const clusterId = ConfigurationHydrator.ENV()
.ZEEBE_CAMUNDA_CLOUD_CLUSTER_ID
const isCamundaCloudShortcutConfig =
clusterId || (clientId && clientSecret && !audience)
if (isCamundaCloudShortcutConfig) {
return ConfigurationHydrator.readCamundaClusterConfFromEnv(
gatewayAddress
)
}
return clientId && clientSecret && audience && authServerUrl
? {
oAuth: {
audience,
scope,
cacheOnDisk: true,
clientId: clientId!,
clientSecret,
url: authServerUrl,
},
useTLS: true,
}
: {}
}
private static readBasicAuthFromEnvironment(): ZB.BasicAuthConfig | {} {
const password = ConfigurationHydrator.ENV().ZEEBE_BASIC_AUTH_PASSWORD
const username = ConfigurationHydrator.ENV().ZEEBE_BASIC_AUTH_USERNAME
return password && username
? {
basicAuth: {
password,
username,
},
}
: {}
}
private static readCamundaClusterConfFromEnv(explicitGateway?: string) {
if (explicitGateway) {
return {}
}
// We can either take a simple clusterId, or else the whole Zeebe Address
// This env var is Node-client specific
const clusterId = ConfigurationHydrator.ENV()
.ZEEBE_CAMUNDA_CLOUD_CLUSTER_ID
// This env var is compatible with zbctl and the Java and Go clients
const zeebeAddress = ConfigurationHydrator.getZeebeAddressFromEnv()
const hostname = clusterId
? `${clusterId}.${CamundaCloudDefaultRegion}.zeebe.camunda.io`
: zeebeAddress.split(':443')[0]
const clientId = ConfigurationHydrator.getClientIdFromEnv()
const clientSecret = ConfigurationHydrator.getClientSecretFromEnv()
const url =
ConfigurationHydrator.ENV().ZEEBE_AUTHORIZATION_SERVER_URL ||
ConfigurationHydrator.CAMUNDA_CLOUD_AUTH_SERVER
return clientId
? {
hostname,
oAuth: {
audience: hostname,
cacheDir: undefined, // will be set in OAuthProvider
cacheOnDisk: true,
clientId,
clientSecret,
url,
},
port: '443',
useTLS: true,
}
: {}
}
private static getGatewayFromEnvironment() {
// ZEEBE_GATEWAY_ADDRESS is for backward compatibility. ZEEBE_ADDRESS is for compatibility with
// the Java / Go clients (including zbctl)
const connectionString = ConfigurationHydrator.getZeebeAddressFromEnv()
return connectionString
? ConfigurationHydrator.decodeConnectionString(connectionString)
: {}
}
private static decodeConnectionString(
connectionString: string | undefined
) {
if (!connectionString) {
connectionString = ConfigurationHydrator.getZeebeAddressFromEnv()
if (!connectionString) {
return {}
}
}
const includesProtocol = connectionString.includes('://')
const gatewayAddress = includesProtocol
? connectionString
: `zeebe://${connectionString}`
const url = parse(gatewayAddress)
url.port = url.port || ConfigurationHydrator.DEFAULT_GATEWAY_PORT
url.hostname = url.hostname || url.path
return {
hostname: url.hostname,
port: url.port,
}
}
private static getCamundaCloudConfig(options: ZBClientOptions = {} as any) {
if (options.camundaCloud) {
const { camundaCloud } = options
const clusterId = ConfigurationHydrator.justClusterId(
camundaCloud.clusterId
)
const clusterRegion =
camundaCloud.clusterRegion || CamundaCloudDefaultRegion
const configuration: ZBClientOptions = {
...options,
hostname: `${clusterId}.${clusterRegion}.zeebe.camunda.io`,
oAuth: {
audience: `${clusterId}.${clusterRegion}.zeebe.camunda.io`,
cacheDir: camundaCloud.cacheDir,
cacheOnDisk: camundaCloud.cacheOnDisk !== false,
clientId: camundaCloud.clientId,
clientSecret: camundaCloud.clientSecret,
url: ConfigurationHydrator.CAMUNDA_CLOUD_AUTH_SERVER,
},
port: '443',
useTLS: true,
}
return configuration
}
return options
}
private static justClusterId(maybeClusterId: string | undefined) {
// 'Be liberal in what you accept and conservative in what you emit'
// Here we account for users pasting in either the expected clusterId
// or the entire Zeebe ContactPoint from the Cloud Console.
return maybeClusterId
? maybeClusterId.split('.zeebe.camunda.io')[0]
: undefined
}
private static getEagerStatus(options: ZBClientOptions | undefined) {
return {
eagerConnection:
(
ConfigurationHydrator.ENV().ZEEBE_NODE_EAGER_CONNECT ||
'false'
).toLocaleLowerCase() === 'true' ||
options?.eagerConnection === true,
}
}
private static getRetryConfiguration(options: ZBClientOptions | undefined) {
const maxRetries = parseInt(
ConfigurationHydrator.ENV().ZEEBE_CLIENT_MAX_RETRIES,
10
)
const maxRetryTimeout = parseInt(
ConfigurationHydrator.ENV().ZEEBE_CLIENT_MAX_RETRY_TIMEOUT,
10
)
const retryFromEnv = ConfigurationHydrator.ENV().ZEEBE_CLIENT_RETRY
return {
retry:
retryFromEnv?.toLocaleLowerCase() === 'false' ||
options?.retry === false
? false
: true,
maxRetries: isNaN(maxRetries)
? options?.maxRetries
: Duration.seconds.from(maxRetries),
maxRetryTimeout: isNaN(maxRetryTimeout)
? options?.maxRetryTimeout
: Duration.seconds.from(maxRetryTimeout),
}
}
}