-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoptions.ts
More file actions
372 lines (322 loc) · 11.7 KB
/
options.ts
File metadata and controls
372 lines (322 loc) · 11.7 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { TokenCredential } from "@azure/identity";
import * as grpc from "@grpc/grpc-js";
import { DurableTaskAzureManagedConnectionString } from "./connection-string";
import { AccessTokenCache } from "./access-token-cache";
import { getCredentialFromAuthenticationType } from "./credential-factory";
import { getUserAgent } from "./user-agent";
import { ClientRetryOptions, createServiceConfig, DEFAULT_SERVICE_CONFIG } from "./retry-policy";
/**
* Base options for configuring the Azure-managed Durable Task service.
* Contains properties common to both client and worker configurations.
*/
abstract class DurableTaskAzureManagedOptionsBase {
protected _endpointAddress: string = "";
protected _taskHubName: string = "";
protected _credential: TokenCredential | null = null;
protected _resourceId: string = "https://durabletask.io";
protected _allowInsecureCredentials: boolean = false;
protected _tokenRefreshMargin: number = 5 * 60 * 1000; // 5 minutes in milliseconds
protected _retryOptions: ClientRetryOptions | undefined = undefined;
/**
* Gets the endpoint address.
*/
getEndpointAddress(): string {
return this._endpointAddress;
}
/**
* Gets the task hub name.
*/
getTaskHubName(): string {
return this._taskHubName;
}
/**
* Gets the credential used for authentication.
*/
getCredential(): TokenCredential | null {
return this._credential;
}
/**
* Gets the resource ID.
*/
getResourceId(): string {
return this._resourceId;
}
/**
* Gets whether insecure credentials are allowed.
*/
isAllowInsecureCredentials(): boolean {
return this._allowInsecureCredentials;
}
/**
* Gets the token refresh margin in milliseconds.
*/
getTokenRefreshMargin(): number {
return this._tokenRefreshMargin;
}
/**
* Gets the retry options for gRPC calls.
*/
getRetryOptions(): ClientRetryOptions | undefined {
return this._retryOptions;
}
/**
* Gets the gRPC service config JSON string with retry policy.
*/
getServiceConfig(): string {
if (this._retryOptions) {
return createServiceConfig(this._retryOptions);
}
return DEFAULT_SERVICE_CONFIG;
}
/**
* Parses and normalizes the endpoint URL.
*
* @returns The normalized host address for gRPC connection.
*/
getHostAddress(): string {
let endpoint = this._endpointAddress;
// Add https:// prefix if no protocol is specified
if (!endpoint.startsWith("http://") && !endpoint.startsWith("https://")) {
endpoint = `https://${endpoint}`;
}
try {
const url = new URL(endpoint);
let authority = url.hostname;
if (url.port) {
authority = `${authority}:${url.port}`;
}
return authority;
} catch (e) {
throw new Error(`Invalid endpoint URL: ${endpoint}`, { cause: e });
}
}
/**
* Creates a gRPC channel metadata generator.
* @param callerType The type of caller for user-agent header.
* @param workerId Optional worker ID (only for workers).
*/
protected createMetadataGeneratorInternal(
callerType: string,
workerId?: string,
): () => Promise<grpc.Metadata> {
// Create token cache only if credential is not null
let tokenCache: AccessTokenCache | null = null;
if (this._credential) {
const scope = this._resourceId + "/.default";
tokenCache = new AccessTokenCache(this._credential, scope, this._tokenRefreshMargin);
}
const taskHubName = this._taskHubName;
const userAgent = getUserAgent(callerType);
return async (): Promise<grpc.Metadata> => {
const metadata = new grpc.Metadata();
metadata.set("taskhub", taskHubName);
metadata.set("x-user-agent", userAgent);
// Only add workerid for workers
if (workerId) {
metadata.set("workerid", workerId);
}
if (tokenCache) {
const token = await tokenCache.getToken();
metadata.set("Authorization", `Bearer ${token.token}`);
}
return metadata;
};
}
/**
* Creates gRPC channel credentials based on the configured options.
* For insecure connections, returns insecure credentials.
* For secure connections, returns SSL credentials.
* Note: Metadata (taskhub, auth token, etc.) is passed per-call via the metadataGenerator
* rather than being composed into the channel credentials. This ensures consistent behavior
* across both secure and insecure connections.
*/
protected createChannelCredentialsInternal(_callerType: string, _workerId?: string): grpc.ChannelCredentials {
if (this._allowInsecureCredentials) {
return grpc.ChannelCredentials.createInsecure();
}
// For secure connections, use SSL credentials
// Metadata is passed per-call via the client/worker's metadataGenerator parameter
return grpc.ChannelCredentials.createSsl();
}
/**
* Configures base options from a parsed connection string.
*/
protected configureFromConnectionString(connectionString: DurableTaskAzureManagedConnectionString): void {
this._endpointAddress = connectionString.getEndpoint();
this._taskHubName = connectionString.getTaskHubName();
const credential = getCredentialFromAuthenticationType(connectionString);
this._credential = credential;
this._allowInsecureCredentials = credential === null;
}
}
/**
* Options for configuring the Azure-managed Durable Task client.
*/
export class DurableTaskAzureManagedClientOptions extends DurableTaskAzureManagedOptionsBase {
/**
* Creates a new instance of DurableTaskAzureManagedClientOptions.
*/
constructor() {
super();
}
/**
* Creates a new instance from a connection string.
*/
static fromConnectionString(connectionString: string): DurableTaskAzureManagedClientOptions {
const parsedConnectionString = new DurableTaskAzureManagedConnectionString(connectionString);
return DurableTaskAzureManagedClientOptions.fromParsedConnectionString(parsedConnectionString);
}
/**
* Creates a new instance from a parsed connection string.
*/
static fromParsedConnectionString(
connectionString: DurableTaskAzureManagedConnectionString,
): DurableTaskAzureManagedClientOptions {
const options = new DurableTaskAzureManagedClientOptions();
options.configureFromConnectionString(connectionString);
return options;
}
setEndpointAddress(endpointAddress: string): DurableTaskAzureManagedClientOptions {
this._endpointAddress = endpointAddress;
return this;
}
setTaskHubName(taskHubName: string): DurableTaskAzureManagedClientOptions {
this._taskHubName = taskHubName;
return this;
}
setCredential(credential: TokenCredential | null): DurableTaskAzureManagedClientOptions {
this._credential = credential;
return this;
}
setResourceId(resourceId: string): DurableTaskAzureManagedClientOptions {
this._resourceId = resourceId;
return this;
}
setAllowInsecureCredentials(allowInsecureCredentials: boolean): DurableTaskAzureManagedClientOptions {
this._allowInsecureCredentials = allowInsecureCredentials;
return this;
}
setTokenRefreshMargin(tokenRefreshMargin: number): DurableTaskAzureManagedClientOptions {
this._tokenRefreshMargin = tokenRefreshMargin;
return this;
}
setRetryOptions(retryOptions: ClientRetryOptions): DurableTaskAzureManagedClientOptions {
this._retryOptions = retryOptions;
return this;
}
/**
* Creates a gRPC channel metadata generator for per-call metadata.
* Does NOT include workerid header (client only).
* This is used for insecure connections where metadata can't be added via channel credentials.
*/
createMetadataGenerator(): () => Promise<grpc.Metadata> {
return this.createMetadataGeneratorInternal("DurableTaskClient");
}
/**
* Creates gRPC channel credentials for the client.
* Does NOT include workerid header.
* For insecure connections, returns just insecure credentials (use createMetadataGenerator for metadata).
* For secure connections, returns SSL credentials composed with call credentials.
*/
createChannelCredentials(): grpc.ChannelCredentials {
return this.createChannelCredentialsInternal("DurableTaskClient");
}
}
/**
* Options for configuring the Azure-managed Durable Task worker.
*/
export class DurableTaskAzureManagedWorkerOptions extends DurableTaskAzureManagedOptionsBase {
private _workerId: string;
/**
* Creates a new instance of DurableTaskAzureManagedWorkerOptions.
*/
constructor() {
super();
this._workerId = DurableTaskAzureManagedWorkerOptions.generateDefaultWorkerId();
}
/**
* Generates a default worker ID in the format: hostname,pid,uniqueId
* This matches the .NET format: {MachineName},{ProcessId},{Guid}
*/
private static generateDefaultWorkerId(): string {
const os = require("os");
const crypto = require("crypto");
const hostname = os.hostname();
const pid = process.pid;
const uniqueId = crypto.randomUUID().replace(/-/g, "");
return `${hostname},${pid},${uniqueId}`;
}
/**
* Creates a new instance from a connection string.
*/
static fromConnectionString(connectionString: string): DurableTaskAzureManagedWorkerOptions {
const parsedConnectionString = new DurableTaskAzureManagedConnectionString(connectionString);
return DurableTaskAzureManagedWorkerOptions.fromParsedConnectionString(parsedConnectionString);
}
/**
* Creates a new instance from a parsed connection string.
*/
static fromParsedConnectionString(
connectionString: DurableTaskAzureManagedConnectionString,
): DurableTaskAzureManagedWorkerOptions {
const options = new DurableTaskAzureManagedWorkerOptions();
options.configureFromConnectionString(connectionString);
return options;
}
/**
* Gets the worker ID used to identify the worker instance.
*/
getWorkerId(): string {
return this._workerId;
}
setEndpointAddress(endpointAddress: string): DurableTaskAzureManagedWorkerOptions {
this._endpointAddress = endpointAddress;
return this;
}
setTaskHubName(taskHubName: string): DurableTaskAzureManagedWorkerOptions {
this._taskHubName = taskHubName;
return this;
}
setCredential(credential: TokenCredential | null): DurableTaskAzureManagedWorkerOptions {
this._credential = credential;
return this;
}
setResourceId(resourceId: string): DurableTaskAzureManagedWorkerOptions {
this._resourceId = resourceId;
return this;
}
setAllowInsecureCredentials(allowInsecureCredentials: boolean): DurableTaskAzureManagedWorkerOptions {
this._allowInsecureCredentials = allowInsecureCredentials;
return this;
}
setTokenRefreshMargin(tokenRefreshMargin: number): DurableTaskAzureManagedWorkerOptions {
this._tokenRefreshMargin = tokenRefreshMargin;
return this;
}
/**
* Sets the worker ID used to identify the worker instance.
*/
setWorkerId(workerId: string): DurableTaskAzureManagedWorkerOptions {
this._workerId = workerId;
return this;
}
/**
* Creates a gRPC channel metadata generator for per-call metadata.
* Includes workerid header (worker only).
* This is used for insecure connections where metadata can't be added via channel credentials.
*/
createMetadataGenerator(): () => Promise<grpc.Metadata> {
return this.createMetadataGeneratorInternal("DurableTaskWorker", this._workerId);
}
/**
* Creates gRPC channel credentials for the worker.
* Includes workerid header.
* For insecure connections, returns just insecure credentials (use createMetadataGenerator for metadata).
* For secure connections, returns SSL credentials composed with call credentials.
*/
createChannelCredentials(): grpc.ChannelCredentials {
return this.createChannelCredentialsInternal("DurableTaskWorker", this._workerId);
}
}