@cratis/chronicle 6.64.0 → 6.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/connection/ChronicleConnection.ts +17 -7
- package/connection/ChronicleConnectionString.spec.ts +85 -0
- package/connection/ChronicleConnectionString.ts +186 -12
- package/connection/ChronicleTlsMaterial.spec.ts +160 -0
- package/connection/ChronicleTlsMaterial.ts +121 -0
- package/connection/CompatibilityPreflight.ts +9 -1
- package/connection/LeastConnectionsLoadBalancerStrategy.ts +4 -2
- package/connection/LoadBalancerStrategyFactory.ts +7 -2
- package/connection/TokenProvider.ts +4 -1
- package/connection/fetchOAuthAccessToken.ts +4 -2
- package/connection/for_CompatibilityPreflight/when_verifying/with_the_check_skipped.spec.ts +23 -0
- package/dist/connection/ChronicleConnection.d.ts +1 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +15 -7
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleConnectionString.d.ts +50 -1
- package/dist/connection/ChronicleConnectionString.d.ts.map +1 -1
- package/dist/connection/ChronicleConnectionString.js +154 -12
- package/dist/connection/ChronicleConnectionString.js.map +1 -1
- package/dist/connection/ChronicleTlsMaterial.d.ts +54 -0
- package/dist/connection/ChronicleTlsMaterial.d.ts.map +1 -0
- package/dist/connection/ChronicleTlsMaterial.js +88 -0
- package/dist/connection/ChronicleTlsMaterial.js.map +1 -0
- package/dist/connection/CompatibilityPreflight.d.ts +2 -1
- package/dist/connection/CompatibilityPreflight.d.ts.map +1 -1
- package/dist/connection/CompatibilityPreflight.js +6 -1
- package/dist/connection/CompatibilityPreflight.js.map +1 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.d.ts +2 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.d.ts.map +1 -1
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.js +3 -2
- package/dist/connection/LeastConnectionsLoadBalancerStrategy.js.map +1 -1
- package/dist/connection/LoadBalancerStrategyFactory.d.ts +2 -1
- package/dist/connection/LoadBalancerStrategyFactory.d.ts.map +1 -1
- package/dist/connection/LoadBalancerStrategyFactory.js +3 -2
- package/dist/connection/LoadBalancerStrategyFactory.js.map +1 -1
- package/dist/connection/TokenProvider.d.ts +4 -1
- package/dist/connection/TokenProvider.d.ts.map +1 -1
- package/dist/connection/TokenProvider.js +5 -1
- package/dist/connection/TokenProvider.js.map +1 -1
- package/dist/connection/fetchOAuthAccessToken.d.ts +2 -1
- package/dist/connection/fetchOAuthAccessToken.d.ts.map +1 -1
- package/dist/connection/fetchOAuthAccessToken.js +3 -2
- package/dist/connection/fetchOAuthAccessToken.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/for_ChronicleOptions/when_configuring_keep_alive/with_skip_keep_alive.spec.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ Chronicle decorators work with both TC39 standard decorators (TypeScript 5.2+; d
|
|
|
72
72
|
|
|
73
73
|
Read models are inferred from `@projection('id', Model)`, `@reducer('id', sequenceId, Model)`, or an exported model with `@fromEvent(Event)` or other model-bound property mappings. Their schema comes from the model type; the default identifier is its class name. See [read models](https://github.com/Cratis/Chronicle.TypeScript/blob/main/Documentation/read-models.md) for preserving existing custom identifiers.
|
|
74
74
|
|
|
75
|
-
Decorated artifacts register when their modules are imported. When the
|
|
75
|
+
Decorated artifacts register when their modules are imported. When the process runs TypeScript (a `.ts`, `.tsx`, `.mts` or `.cts` entry file, a loader such as `tsx` or `ts-node`, Node's `--experimental-strip-types` or `--experimental-transform-types`, or Vitest), the client also scans `**/*.ts` and `**/*.tsx` files by default, excluding declaration files, specs, tests, `node_modules`, `dist` and `build`. Otherwise, as for compiled JavaScript, it scans nothing unless you set `discoveryPatterns`. See [artifact discovery](https://github.com/Cratis/Chronicle.TypeScript/blob/main/Documentation/getting-started.md#artifact-discovery).
|
|
76
76
|
|
|
77
77
|
The client skips TLS certificate validation unless the connection string sets `skipTlsValidation=false` (the default stays `true` in this release; an explicit `false` requires validation). Set it for every server other than a local development kernel; the client logs a warning when it skips validation for a non-loopback host; see [Connect to Chronicle](https://github.com/Cratis/Chronicle.TypeScript/blob/main/Documentation/connecting.md).
|
|
78
78
|
|
|
@@ -42,6 +42,7 @@ import { ChronicleConnectionString, type ChronicleServerAddress } from './Chroni
|
|
|
42
42
|
import { ChronicleServerAddressResolver } from './ChronicleServerAddressResolver.js';
|
|
43
43
|
import { ChronicleServices } from './ChronicleServices.js';
|
|
44
44
|
import './encodeExplicitFalse.js';
|
|
45
|
+
import { ChronicleTlsMaterial } from './ChronicleTlsMaterial.js';
|
|
45
46
|
import { CompatibilityPreflight } from './CompatibilityPreflight.js';
|
|
46
47
|
import { formatServerAddress } from './formatServerAddress.js';
|
|
47
48
|
import type { ILoadBalancerStrategy } from './ILoadBalancerStrategy.js';
|
|
@@ -114,6 +115,7 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
114
115
|
private readonly _tokenProviders = new Map<string, ITokenProvider>();
|
|
115
116
|
private readonly _addressResolver: ChronicleServerAddressResolver;
|
|
116
117
|
private readonly _loadBalancerStrategy: ILoadBalancerStrategy;
|
|
118
|
+
private readonly _tlsMaterial: ChronicleTlsMaterial;
|
|
117
119
|
private _isConnected = false;
|
|
118
120
|
private _warnedAboutSkippedTlsValidation = false;
|
|
119
121
|
private _clientsReady?: Promise<void>;
|
|
@@ -129,12 +131,15 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
129
131
|
this._connectionString = ChronicleConnectionString.Default;
|
|
130
132
|
}
|
|
131
133
|
|
|
132
|
-
if (!this._connectionString.apiKey && (!!this._connectionString.username !== !!this._connectionString.password)) {
|
|
134
|
+
if (!this._connectionString.noAuthentication && !this._connectionString.apiKey && (!!this._connectionString.username !== !!this._connectionString.password)) {
|
|
133
135
|
throw new Error('Connection string must contain both username and password, or neither');
|
|
134
136
|
}
|
|
135
137
|
|
|
136
138
|
this._addressResolver = new ChronicleServerAddressResolver();
|
|
137
|
-
|
|
139
|
+
// Loaded once, up front: a missing or unreadable certificate file fails the connection setup here rather
|
|
140
|
+
// than on every reconnect - or not at all until validation is switched on.
|
|
141
|
+
this._tlsMaterial = this._connectionString.loadTlsMaterial();
|
|
142
|
+
this._loadBalancerStrategy = createLoadBalancerStrategy(this._connectionString.loadBalancer, this._connectionString.skipTlsValidation, this._tlsMaterial);
|
|
138
143
|
|
|
139
144
|
if (!_options.deferClientCreation) {
|
|
140
145
|
this._clientsReady = this.startClientCreation();
|
|
@@ -319,14 +324,14 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
319
324
|
const selected = await this._loadBalancerStrategy.select(candidates);
|
|
320
325
|
const serverAddress = formatServerAddress(selected);
|
|
321
326
|
const tokenProvider = this.createTokenProvider(selected);
|
|
322
|
-
const credentials = this._options.credentials ?? this._connectionString.createCredentials();
|
|
327
|
+
const credentials = this._options.credentials ?? this._connectionString.createCredentials(this._tlsMaterial);
|
|
323
328
|
|
|
324
329
|
this._channel = createChannel(serverAddress, credentials, channelOptions);
|
|
325
330
|
|
|
326
331
|
// nice-grpc invokes the last middleware first: auth retries re-enter propagation.
|
|
327
332
|
const factory = createClientFactory().use(traceContextMiddleware).use(this.createAuthMiddleware(tokenProvider));
|
|
328
333
|
this._connections = factory.create(ConnectionServiceDefinition, this._channel);
|
|
329
|
-
this._compatibility = new CompatibilityPreflight(this._connections, this._options.connectTimeout ?? 10_000);
|
|
334
|
+
this._compatibility = new CompatibilityPreflight(this._connections, this._options.connectTimeout ?? 10_000, this._connectionString.skipCompatibilityCheck);
|
|
330
335
|
const eventSequenceFactory = factory.use(this._compatibility.middleware());
|
|
331
336
|
this._services = {
|
|
332
337
|
eventStores: factory.create(EventStoresDefinition, this._channel),
|
|
@@ -368,7 +373,7 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
368
373
|
const channelHosts = this._options.credentials ? [] : remoteOnly(candidateHosts);
|
|
369
374
|
// OAuth token requests (every mode except an API key) always skip validation, against
|
|
370
375
|
// the explicit authority when set, otherwise the selected server.
|
|
371
|
-
const tokenHosts = this._connectionString.apiKey
|
|
376
|
+
const tokenHosts = this._connectionString.apiKey || this._connectionString.noAuthentication
|
|
372
377
|
? []
|
|
373
378
|
: remoteOnly(this._options.authority ? [new URL(this._options.authority).hostname] : candidateHosts);
|
|
374
379
|
if (channelHosts.length === 0 && tokenHosts.length === 0) return;
|
|
@@ -391,6 +396,11 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
391
396
|
const hasUsername = !!this._connectionString.username;
|
|
392
397
|
const hasApiKey = !!this._connectionString.apiKey;
|
|
393
398
|
|
|
399
|
+
// Asking for no authentication is deliberate and beats anything else the connection string carries.
|
|
400
|
+
if (this._connectionString.noAuthentication) {
|
|
401
|
+
return new NoOpTokenProvider();
|
|
402
|
+
}
|
|
403
|
+
|
|
394
404
|
if (hasApiKey) {
|
|
395
405
|
return new NoOpTokenProvider();
|
|
396
406
|
}
|
|
@@ -426,7 +436,7 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
426
436
|
const endpoint = `${scheme}://${authorityHost.includes(':') && !authorityHost.startsWith('[') ? `[${authorityHost}]` : authorityHost}:${authorityPort}/connect/token`;
|
|
427
437
|
let provider = this._tokenProviders.get(endpoint);
|
|
428
438
|
if (!provider) {
|
|
429
|
-
provider = new OAuthTokenProvider(endpoint, username, password, this._connectionString.skipTlsValidation);
|
|
439
|
+
provider = new OAuthTokenProvider(endpoint, username, password, this._connectionString.skipTlsValidation, undefined, this._tlsMaterial);
|
|
430
440
|
this._tokenProviders.set(endpoint, provider);
|
|
431
441
|
}
|
|
432
442
|
return provider;
|
|
@@ -456,7 +466,7 @@ export class ChronicleConnection implements ChronicleServices {
|
|
|
456
466
|
const metadata = options.metadata ? Metadata(options.metadata) : Metadata();
|
|
457
467
|
metadata.set('authorization', `Bearer ${token}`);
|
|
458
468
|
options.metadata = metadata;
|
|
459
|
-
} else if (connectionString.apiKey) {
|
|
469
|
+
} else if (connectionString.apiKey && !connectionString.noAuthentication) {
|
|
460
470
|
const metadata = options.metadata ? Metadata(options.metadata) : Metadata();
|
|
461
471
|
metadata.set('api-key', connectionString.apiKey);
|
|
462
472
|
options.metadata = metadata;
|
|
@@ -177,6 +177,91 @@ describe('ChronicleConnectionStringBuilder', () => {
|
|
|
177
177
|
});
|
|
178
178
|
});
|
|
179
179
|
|
|
180
|
+
describe('ChronicleConnectionStringBuilder options', () => {
|
|
181
|
+
describe('when parsing caCertificatePath', () => {
|
|
182
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?caCertificatePath=%2Fetc%2Fca.pem');
|
|
183
|
+
it('should have the decoded path', () => expect(builder.caCertificatePath).toBe('/etc/ca.pem'));
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('when caCertificatePath holds an at sign', () => {
|
|
187
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?caCertificatePath=%2Fhome%2Fme@corp%2Fca.pem');
|
|
188
|
+
it('should keep it', () => expect(builder.caCertificatePath).toBe('/home/me@corp/ca.pem'));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe('when parsing auth=none together with credentials', () => {
|
|
192
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://user:pass@host:35000?auth=none');
|
|
193
|
+
it('should be flagged as no authentication', () => expect(builder.noAuthentication).toBe(true));
|
|
194
|
+
it('should beat the credentials', () => expect(builder.authenticationMode).toBe(AuthenticationMode.None));
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('when auth=none is spelled in another case', () => {
|
|
198
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?AUTH=None');
|
|
199
|
+
it('should still be no authentication', () => expect(builder.noAuthentication).toBe(true));
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('when there are no credentials', () => {
|
|
203
|
+
it('should use the development client credentials', () =>
|
|
204
|
+
expect(new ChronicleConnectionStringBuilder('chronicle://host:35000').authenticationMode).toBe(AuthenticationMode.ClientCredentials));
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('when only a username is given', () => {
|
|
208
|
+
it('should throw', () =>
|
|
209
|
+
expect(() => new ChronicleConnectionStringBuilder('chronicle://user@host:35000').authenticationMode).toThrow());
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('when parsing skipCompatibilityCheck and skipKeepAlive', () => {
|
|
213
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?skipCompatibilityCheck=true&skipKeepAlive=true');
|
|
214
|
+
it('should skip the compatibility check', () => expect(builder.skipCompatibilityCheck).toBe(true));
|
|
215
|
+
it('should skip the keep-alive', () => expect(builder.skipKeepAlive).toBe(true));
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('when the new flags are absent', () => {
|
|
219
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000');
|
|
220
|
+
it('should not skip the compatibility check', () => expect(builder.skipCompatibilityCheck).toBe(false));
|
|
221
|
+
it('should not be no authentication', () => expect(builder.noAuthentication).toBe(false));
|
|
222
|
+
it('should have no CA certificate path', () => expect(builder.caCertificatePath).toBeUndefined());
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('when a boolean option is neither true nor false', () => {
|
|
226
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?skipTlsValidation=maybe');
|
|
227
|
+
it('should throw naming the option', () => expect(() => builder.skipTlsValidation).toThrow(/skipTlsValidation/));
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('when option names are spelled in another case', () => {
|
|
231
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?APIKEY=abc&SkipTlsValidation=false&DISABLETLS=TRUE');
|
|
232
|
+
it('should read the api key', () => expect(builder.apiKey).toBe('abc'));
|
|
233
|
+
it('should read skipTlsValidation', () => expect(builder.skipTlsValidation).toBe(false));
|
|
234
|
+
it('should read disableTls', () => expect(builder.disableTls).toBe(true));
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe('when percent-decoding option values', () => {
|
|
238
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000?apiKey=a%2Bb+c%3D');
|
|
239
|
+
it('should decode once and keep a literal plus', () => expect(builder.apiKey).toBe('a+b+c='));
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
describe('when user information spilled an unencoded separator into the query', () => {
|
|
243
|
+
it('should throw', () =>
|
|
244
|
+
expect(() => new ChronicleConnectionStringBuilder('chronicle://user:pa?ss@host:35000')).toThrow(/percent-encoded/));
|
|
245
|
+
it('should throw for an at sign in an option that cannot hold one', () =>
|
|
246
|
+
expect(() => new ChronicleConnectionStringBuilder('chronicle://host:35000?disableTls=x@y')).toThrow(/percent-encoded/));
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe('when building credentials containing reserved characters', () => {
|
|
250
|
+
const builder = new ChronicleConnectionStringBuilder('chronicle://host:35000');
|
|
251
|
+
builder.username = 'al@ice';
|
|
252
|
+
builder.password = 'p:ss/w?rd';
|
|
253
|
+
const roundTripped = new ChronicleConnectionStringBuilder(builder.build());
|
|
254
|
+
it('should round trip the username', () => expect(roundTripped.username).toBe('al@ice'));
|
|
255
|
+
it('should round trip the password', () => expect(roundTripped.password).toBe('p:ss/w?rd'));
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('when round tripping every option', () => {
|
|
259
|
+
const original = 'chronicle://host:35000?auth=none&disableTls=true&skipCompatibilityCheck=true&skipKeepAlive=true' +
|
|
260
|
+
'&certificatePath=%2Fc.p12&certificatePassword=pw&caCertificatePath=%2Fca.pem&custom=value';
|
|
261
|
+
it('should reproduce it', () => expect(new ChronicleConnectionStringBuilder(original).build()).toBe(original));
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
180
265
|
describe('ChronicleConnectionString', () => {
|
|
181
266
|
describe('when parsing a connection string with multiple hosts', () => {
|
|
182
267
|
const connectionString = new ChronicleConnectionString('chronicle://host1:35001,host2:35002');
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
4
|
import * as grpc from '@grpc/grpc-js';
|
|
5
|
+
import * as tls from 'node:tls';
|
|
5
6
|
import { formatServerAddress } from './formatServerAddress.js';
|
|
6
7
|
import { LoadBalancerMode } from './LoadBalancerMode.js';
|
|
8
|
+
import { loadTlsMaterial, type ChronicleTlsMaterial } from './ChronicleTlsMaterial.js';
|
|
7
9
|
|
|
8
10
|
const DEFAULT_PORT = 35000;
|
|
9
11
|
const SRV_SCHEME = 'chronicle+srv';
|
|
@@ -12,6 +14,8 @@ const SRV_SCHEME = 'chronicle+srv';
|
|
|
12
14
|
* Authentication mode for Chronicle connection.
|
|
13
15
|
*/
|
|
14
16
|
export enum AuthenticationMode {
|
|
17
|
+
/** No authentication at all (`auth=none`, or a cleartext connection without credentials). */
|
|
18
|
+
None = 'None',
|
|
15
19
|
ClientCredentials = 'ClientCredentials',
|
|
16
20
|
ApiKey = 'ApiKey'
|
|
17
21
|
}
|
|
@@ -105,6 +109,21 @@ export class ChronicleConnectionStringBuilder {
|
|
|
105
109
|
private static readonly _apiKeyKey = 'apiKey';
|
|
106
110
|
private static readonly _disableTlsKey = 'disableTls';
|
|
107
111
|
private static readonly _skipKeepAliveKey = 'skipKeepAlive';
|
|
112
|
+
private static readonly _authKey = 'auth';
|
|
113
|
+
private static readonly _skipCompatibilityCheckKey = 'skipCompatibilityCheck';
|
|
114
|
+
private static readonly _caCertificatePathKey = 'caCertificatePath';
|
|
115
|
+
private static readonly _noAuthenticationValue = 'none';
|
|
116
|
+
|
|
117
|
+
/** Every option the builder knows, so a differently cased spelling folds to its canonical one. */
|
|
118
|
+
private static readonly _knownKeys = new Map<string, string>([
|
|
119
|
+
'apiKey', 'auth', 'skipTlsValidation', 'disableTls', 'skipCompatibilityCheck', 'skipKeepAlive',
|
|
120
|
+
'loadBalancer', 'srvNameServer', 'certificatePath', 'certificatePassword', 'caCertificatePath'
|
|
121
|
+
].map(key => [key.toLowerCase(), key]));
|
|
122
|
+
|
|
123
|
+
/** Options whose value may legitimately hold an at sign, such as a path or a name server. */
|
|
124
|
+
private static readonly _keysThatMayHoldAtSign = new Set([
|
|
125
|
+
'apikey', 'certificatepath', 'certificatepassword', 'cacertificatepath', 'loadbalancer', 'srvnameserver'
|
|
126
|
+
]);
|
|
108
127
|
private static readonly _skipTlsValidationKey = 'skipTlsValidation';
|
|
109
128
|
private static readonly _certificatePathKey = 'certificatePath';
|
|
110
129
|
private static readonly _certificatePasswordKey = 'certificatePassword';
|
|
@@ -198,14 +217,24 @@ export class ChronicleConnectionStringBuilder {
|
|
|
198
217
|
}
|
|
199
218
|
|
|
200
219
|
get authenticationMode(): AuthenticationMode {
|
|
201
|
-
const
|
|
220
|
+
const hasUsername = !!this.username;
|
|
221
|
+
const hasPassword = !!this.password;
|
|
222
|
+
const hasClientCredentials = hasUsername && hasPassword;
|
|
202
223
|
const hasApiKey = !!this.apiKey;
|
|
224
|
+
const hasNoCredentials = !hasUsername && !hasPassword && !hasApiKey;
|
|
225
|
+
|
|
226
|
+
// Asking for no authentication is deliberate and beats anything else the connection string carries -
|
|
227
|
+
// credentials left in place by a shared configuration must not quietly turn the exchange back on.
|
|
228
|
+
if (this.noAuthentication) {
|
|
229
|
+
return AuthenticationMode.None;
|
|
230
|
+
}
|
|
203
231
|
|
|
204
232
|
if (hasClientCredentials && hasApiKey) {
|
|
205
233
|
throw new Error('Cannot specify both username/password and apiKey in the connection string');
|
|
206
234
|
}
|
|
207
235
|
|
|
208
|
-
|
|
236
|
+
// No credentials at all means the development client credentials.
|
|
237
|
+
if (hasClientCredentials || hasNoCredentials) {
|
|
209
238
|
return AuthenticationMode.ClientCredentials;
|
|
210
239
|
}
|
|
211
240
|
|
|
@@ -213,7 +242,24 @@ export class ChronicleConnectionStringBuilder {
|
|
|
213
242
|
return AuthenticationMode.ApiKey;
|
|
214
243
|
}
|
|
215
244
|
|
|
216
|
-
throw new Error('
|
|
245
|
+
throw new Error('Incomplete authentication. Provide both username and password, or an apiKey, in the connection string');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Whether `auth=none` is set: no authentication at all. It wins over any credentials or API key the
|
|
250
|
+
* connection string also carries.
|
|
251
|
+
*/
|
|
252
|
+
get noAuthentication(): boolean {
|
|
253
|
+
return this._properties.get(ChronicleConnectionStringBuilder._authKey)?.toLowerCase() === ChronicleConnectionStringBuilder._noAuthenticationValue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
set noAuthentication(value: boolean) {
|
|
257
|
+
if (value) {
|
|
258
|
+
this._properties.set(ChronicleConnectionStringBuilder._authKey, ChronicleConnectionStringBuilder._noAuthenticationValue);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this._properties.delete(ChronicleConnectionStringBuilder._authKey);
|
|
217
263
|
}
|
|
218
264
|
|
|
219
265
|
get apiKey(): string | undefined {
|
|
@@ -230,7 +276,7 @@ export class ChronicleConnectionStringBuilder {
|
|
|
230
276
|
}
|
|
231
277
|
|
|
232
278
|
get disableTls(): boolean {
|
|
233
|
-
return this.
|
|
279
|
+
return this.booleanOption(ChronicleConnectionStringBuilder._disableTlsKey, false);
|
|
234
280
|
}
|
|
235
281
|
|
|
236
282
|
set disableTls(value: boolean) {
|
|
@@ -245,8 +291,7 @@ export class ChronicleConnectionStringBuilder {
|
|
|
245
291
|
* always requires validation. A later major release will require validation by default.
|
|
246
292
|
*/
|
|
247
293
|
get skipTlsValidation(): boolean {
|
|
248
|
-
|
|
249
|
-
return raw === undefined || raw === 'true';
|
|
294
|
+
return this.booleanOption(ChronicleConnectionStringBuilder._skipTlsValidationKey, true);
|
|
250
295
|
}
|
|
251
296
|
|
|
252
297
|
set skipTlsValidation(value: boolean) {
|
|
@@ -260,7 +305,7 @@ export class ChronicleConnectionStringBuilder {
|
|
|
260
305
|
|
|
261
306
|
/** Skip the client keep-alive connection for short-lived processes. Defaults to false. */
|
|
262
307
|
get skipKeepAlive(): boolean {
|
|
263
|
-
return this.
|
|
308
|
+
return this.booleanOption(ChronicleConnectionStringBuilder._skipKeepAliveKey, false);
|
|
264
309
|
}
|
|
265
310
|
|
|
266
311
|
set skipKeepAlive(value: boolean) {
|
|
@@ -293,6 +338,37 @@ export class ChronicleConnectionStringBuilder {
|
|
|
293
338
|
this._properties.delete(ChronicleConnectionStringBuilder._certificatePasswordKey);
|
|
294
339
|
}
|
|
295
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Whether to skip the server compatibility check on connect (`skipCompatibilityCheck=true`). An escape hatch
|
|
343
|
+
* for a known-safe client and kernel skew; it hides every other incompatibility too.
|
|
344
|
+
*/
|
|
345
|
+
get skipCompatibilityCheck(): boolean {
|
|
346
|
+
return this.booleanOption(ChronicleConnectionStringBuilder._skipCompatibilityCheckKey, false);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
set skipCompatibilityCheck(value: boolean) {
|
|
350
|
+
this._properties.set(ChronicleConnectionStringBuilder._skipCompatibilityCheckKey, value.toString());
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The path to a PEM file with the certificate authorities the server certificate must chain to, in place of
|
|
355
|
+
* the system trust store (`caCertificatePath=`). Used only when `skipTlsValidation=false`; the file is
|
|
356
|
+
* nevertheless read when the connection is created, so a missing or unreadable file, or one without
|
|
357
|
+
* certificates, fails the connection setup.
|
|
358
|
+
*/
|
|
359
|
+
get caCertificatePath(): string | undefined {
|
|
360
|
+
return this._properties.get(ChronicleConnectionStringBuilder._caCertificatePathKey);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
set caCertificatePath(value: string | undefined) {
|
|
364
|
+
if (value) {
|
|
365
|
+
this._properties.set(ChronicleConnectionStringBuilder._caCertificatePathKey, value);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this._properties.delete(ChronicleConnectionStringBuilder._caCertificatePathKey);
|
|
370
|
+
}
|
|
371
|
+
|
|
296
372
|
/**
|
|
297
373
|
* The strategy used to select one address from {@link hosts} (or the addresses
|
|
298
374
|
* resolved from a `chronicle+srv://` host) on every connect/reconnect attempt.
|
|
@@ -342,9 +418,9 @@ export class ChronicleConnectionStringBuilder {
|
|
|
342
418
|
let result = `${this.scheme}://`;
|
|
343
419
|
|
|
344
420
|
if (this.username) {
|
|
345
|
-
result += this.username;
|
|
421
|
+
result += encodeURIComponent(this.username);
|
|
346
422
|
if (this.password) {
|
|
347
|
-
result += `:${this.password}`;
|
|
423
|
+
result += `:${encodeURIComponent(this.password)}`;
|
|
348
424
|
}
|
|
349
425
|
result += '@';
|
|
350
426
|
}
|
|
@@ -357,10 +433,18 @@ export class ChronicleConnectionStringBuilder {
|
|
|
357
433
|
queryParameters.push(`apiKey=${encodeURIComponent(this.apiKey)}`);
|
|
358
434
|
}
|
|
359
435
|
|
|
436
|
+
if (this.noAuthentication) {
|
|
437
|
+
queryParameters.push(`auth=${ChronicleConnectionStringBuilder._noAuthenticationValue}`);
|
|
438
|
+
}
|
|
439
|
+
|
|
360
440
|
if (this.disableTls) {
|
|
361
441
|
queryParameters.push('disableTls=true');
|
|
362
442
|
}
|
|
363
443
|
|
|
444
|
+
if (this.skipCompatibilityCheck) {
|
|
445
|
+
queryParameters.push('skipCompatibilityCheck=true');
|
|
446
|
+
}
|
|
447
|
+
|
|
364
448
|
this.appendSkipKeepAlive(queryParameters);
|
|
365
449
|
|
|
366
450
|
if (this.hasExplicitSkipTlsValidation) {
|
|
@@ -375,6 +459,10 @@ export class ChronicleConnectionStringBuilder {
|
|
|
375
459
|
queryParameters.push(`certificatePassword=${encodeURIComponent(this.certificatePassword)}`);
|
|
376
460
|
}
|
|
377
461
|
|
|
462
|
+
if (this.caCertificatePath) {
|
|
463
|
+
queryParameters.push(`caCertificatePath=${encodeURIComponent(this.caCertificatePath)}`);
|
|
464
|
+
}
|
|
465
|
+
|
|
378
466
|
if (this._properties.has(ChronicleConnectionStringBuilder._loadBalancerKey)) {
|
|
379
467
|
queryParameters.push(`loadBalancer=${encodeURIComponent(this.loadBalancer)}`);
|
|
380
468
|
}
|
|
@@ -389,6 +477,9 @@ export class ChronicleConnectionStringBuilder {
|
|
|
389
477
|
key !== ChronicleConnectionStringBuilder._passwordKey &&
|
|
390
478
|
key !== ChronicleConnectionStringBuilder._schemeKey &&
|
|
391
479
|
key !== ChronicleConnectionStringBuilder._apiKeyKey &&
|
|
480
|
+
key !== ChronicleConnectionStringBuilder._authKey &&
|
|
481
|
+
key !== ChronicleConnectionStringBuilder._skipCompatibilityCheckKey &&
|
|
482
|
+
key !== ChronicleConnectionStringBuilder._caCertificatePathKey &&
|
|
392
483
|
key !== ChronicleConnectionStringBuilder._disableTlsKey &&
|
|
393
484
|
key !== ChronicleConnectionStringBuilder._skipTlsValidationKey &&
|
|
394
485
|
key !== ChronicleConnectionStringBuilder._certificatePathKey &&
|
|
@@ -407,6 +498,37 @@ export class ChronicleConnectionStringBuilder {
|
|
|
407
498
|
return result;
|
|
408
499
|
}
|
|
409
500
|
|
|
501
|
+
private booleanOption(key: string, defaultValue: boolean): boolean {
|
|
502
|
+
const raw = this._properties.get(key);
|
|
503
|
+
if (raw === undefined) {
|
|
504
|
+
return defaultValue;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const normalized = raw.toLowerCase();
|
|
508
|
+
if (normalized === 'true') {
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (normalized === 'false') {
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
throw new Error(`The value of the '${key}' option is invalid.`);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
private static hasMisplacedAtSign(pair: string): boolean {
|
|
520
|
+
if (!pair.includes('@')) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const equalsIndex = pair.indexOf('=');
|
|
525
|
+
if (equalsIndex === -1 || pair.indexOf('@') < equalsIndex) {
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return !ChronicleConnectionStringBuilder._keysThatMayHoldAtSign.has(decodeURIComponent(pair.slice(0, equalsIndex)).toLowerCase());
|
|
530
|
+
}
|
|
531
|
+
|
|
410
532
|
private appendSkipKeepAlive(queryParameters: string[]): void {
|
|
411
533
|
if (this.skipKeepAlive) queryParameters.push('skipKeepAlive=true');
|
|
412
534
|
}
|
|
@@ -442,6 +564,13 @@ export class ChronicleConnectionStringBuilder {
|
|
|
442
564
|
query = queryIndex === -1 ? '' : rest.slice(queryIndex + 1);
|
|
443
565
|
}
|
|
444
566
|
|
|
567
|
+
// An at sign in the query means the user information held a separator that was not percent-encoded
|
|
568
|
+
// and spilled into it - unless the option is one that may hold one.
|
|
569
|
+
const pairs = query.length === 0 ? [] : query.split('&');
|
|
570
|
+
if (pairs.some(pair => ChronicleConnectionStringBuilder.hasMisplacedAtSign(pair))) {
|
|
571
|
+
throw new Error('The user information must be percent-encoded: it contains a separator');
|
|
572
|
+
}
|
|
573
|
+
|
|
445
574
|
// The userinfo separator is the LAST '@' in the authority, so a host portion never
|
|
446
575
|
// legitimately containing '@' can't be confused for one.
|
|
447
576
|
const authoritySeparatorIndex = authority.lastIndexOf('@');
|
|
@@ -463,8 +592,17 @@ export class ChronicleConnectionStringBuilder {
|
|
|
463
592
|
}
|
|
464
593
|
this.hosts = hosts;
|
|
465
594
|
|
|
466
|
-
|
|
467
|
-
|
|
595
|
+
// Percent-decoding only: a literal '+' stays a plus, like the .NET client, and the value is decoded once.
|
|
596
|
+
for (const pair of pairs) {
|
|
597
|
+
const equalsIndex = pair.indexOf('=');
|
|
598
|
+
if (equalsIndex <= 0) {
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const key = decodeURIComponent(pair.slice(0, equalsIndex));
|
|
603
|
+
this._properties.set(
|
|
604
|
+
ChronicleConnectionStringBuilder._knownKeys.get(key.toLowerCase()) ?? key,
|
|
605
|
+
decodeURIComponent(pair.slice(equalsIndex + 1)));
|
|
468
606
|
}
|
|
469
607
|
} catch (error) {
|
|
470
608
|
throw new Error(`Invalid Chronicle connection string: ${error}`);
|
|
@@ -548,6 +686,21 @@ export class ChronicleConnectionString {
|
|
|
548
686
|
return this._builder.skipKeepAlive;
|
|
549
687
|
}
|
|
550
688
|
|
|
689
|
+
/** Whether `auth=none` is set: no authentication at all. */
|
|
690
|
+
get noAuthentication(): boolean {
|
|
691
|
+
return this._builder.noAuthentication;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** Whether to skip the server compatibility check on connect. */
|
|
695
|
+
get skipCompatibilityCheck(): boolean {
|
|
696
|
+
return this._builder.skipCompatibilityCheck;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/** The path to a PEM file with the certificate authorities the server certificate must chain to. */
|
|
700
|
+
get caCertificatePath(): string | undefined {
|
|
701
|
+
return this._builder.caCertificatePath;
|
|
702
|
+
}
|
|
703
|
+
|
|
551
704
|
/**
|
|
552
705
|
* Whether TLS certificate validation is skipped. See
|
|
553
706
|
* {@link ChronicleConnectionStringBuilder.skipTlsValidation}.
|
|
@@ -587,20 +740,41 @@ export class ChronicleConnectionString {
|
|
|
587
740
|
const builder = new ChronicleConnectionStringBuilder(this.toString());
|
|
588
741
|
builder.username = username;
|
|
589
742
|
builder.password = password;
|
|
743
|
+
builder.noAuthentication = false;
|
|
590
744
|
return new ChronicleConnectionString(builder.build());
|
|
591
745
|
}
|
|
592
746
|
|
|
593
747
|
withApiKey(apiKey: string): ChronicleConnectionString {
|
|
594
748
|
const builder = new ChronicleConnectionStringBuilder(this.toString());
|
|
595
749
|
builder.apiKey = apiKey;
|
|
750
|
+
builder.noAuthentication = false;
|
|
596
751
|
return new ChronicleConnectionString(builder.build());
|
|
597
752
|
}
|
|
598
753
|
|
|
599
|
-
|
|
754
|
+
/**
|
|
755
|
+
* Loads the TLS material the connection string names: the client certificate from `certificatePath` and the
|
|
756
|
+
* certificate authorities from `caCertificatePath`.
|
|
757
|
+
* @throws When a file is missing, unreadable or holds no usable material.
|
|
758
|
+
*/
|
|
759
|
+
loadTlsMaterial(): ChronicleTlsMaterial {
|
|
760
|
+
return loadTlsMaterial(this);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Creates the gRPC channel credentials for this connection string.
|
|
765
|
+
* @param tlsMaterial - The TLS material to use. Defaults to loading what the connection string names.
|
|
766
|
+
*/
|
|
767
|
+
createCredentials(tlsMaterial: ChronicleTlsMaterial = loadTlsMaterial(this)): grpc.ChannelCredentials {
|
|
600
768
|
if (this.disableTls) {
|
|
601
769
|
return grpc.credentials.createInsecure();
|
|
602
770
|
}
|
|
603
771
|
|
|
772
|
+
if (!tlsMaterial.isEmpty) {
|
|
773
|
+
return grpc.credentials.createFromSecureContext(
|
|
774
|
+
tls.createSecureContext(tlsMaterial.connectOptions(this.skipTlsValidation)),
|
|
775
|
+
{ rejectUnauthorized: !this.skipTlsValidation });
|
|
776
|
+
}
|
|
777
|
+
|
|
604
778
|
if (this.skipTlsValidation) {
|
|
605
779
|
// Chronicle generates a self-signed certificate in development, and skipTlsValidation
|
|
606
780
|
// defaults to true so this works without extra configuration. grpc-js has no
|