Skip to content

Commit 192d4b2

Browse files
port: [#4465][#6560] Allow TokenCredential authentication in CosmosDbPartitionedStorage (#4473)
* add tokenCredential authentication to cosmosDB * fix ts issues with new version * fix pipelines issues * add EOF --------- Co-authored-by: JhontSouth <[email protected]>
1 parent b110344 commit 192d4b2

File tree

13 files changed

+159
-39
lines changed

13 files changed

+159
-39
lines changed

libraries/botbuilder-azure-blobs/src/ignoreError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type IgnoreError = (err: Error) => boolean;
1919
export async function ignoreError<T>(promise: Promise<T>, ignore: IgnoreError): Promise<T | null> {
2020
try {
2121
return await promise;
22-
} catch (err) {
22+
} catch (err: any) {
2323
if (!ignore(err)) {
2424
throw err;
2525
} else {

libraries/botbuilder-azure/etc/botbuilder-azure.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CosmosClientOptions } from '@azure/cosmos';
99
import { PagedResult } from 'botbuilder';
1010
import { Storage as Storage_2 } from 'botbuilder';
1111
import { StoreItems } from 'botbuilder';
12+
import { TokenCredential } from '@azure/core-auth';
1213
import { TranscriptInfo } from 'botbuilder';
1314
import { TranscriptStore } from 'botbuilder';
1415

@@ -64,6 +65,7 @@ export interface CosmosDbPartitionedStorageOptions {
6465
cosmosDbEndpoint?: string;
6566
databaseId: string;
6667
keySuffix?: string;
68+
tokenCredential?: TokenCredential;
6769
}
6870

6971
// @public (undocumented)

libraries/botbuilder-azure/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
}
2929
},
3030
"dependencies": {
31-
"@azure/cosmos": "^3.3.1",
31+
"@azure/cosmos": "3.10.0",
32+
"@azure/core-auth": "^1.1.3",
3233
"azure-storage": "2.10.7",
3334
"botbuilder": "4.1.6",
3435
"lodash": "^4.17.20",

libraries/botbuilder-azure/src/cosmosDbPartitionedStorage.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Container, CosmosClient, CosmosClientOptions } from '@azure/cosmos';
1111
import { CosmosDbKeyEscape } from './cosmosDbKeyEscape';
1212
import { DoOnce } from './doOnce';
1313
import { Storage, StoreItems } from 'botbuilder';
14+
import { TokenCredential } from '@azure/core-auth';
1415

1516
// eslint-disable-next-line @typescript-eslint/no-var-requires
1617
const pjson: Record<'name' | 'version', string> = require('../package.json');
@@ -67,6 +68,10 @@ export interface CosmosDbPartitionedStorageOptions {
6768
* compatibilityMode cannot be true if keySuffix is used.
6869
*/
6970
compatibilityMode?: boolean;
71+
/**
72+
* The authentication tokenCredential for Cosmos DB.
73+
*/
74+
tokenCredential?: TokenCredential;
7075
}
7176

7277
//Internal data structure for storing items in a CosmosDB Collection.
@@ -143,8 +148,12 @@ export class CosmosDbPartitionedStorage implements Storage {
143148
throw new ReferenceError('cosmosDbEndpoint for CosmosDB is required.');
144149
}
145150
cosmosDbStorageOptions.authKey ??= cosmosClientOptions?.key;
146-
if (!cosmosDbStorageOptions.authKey && !cosmosClientOptions?.tokenProvider) {
147-
throw new ReferenceError('authKey for CosmosDB is required.');
151+
if (
152+
!cosmosDbStorageOptions.authKey &&
153+
!cosmosClientOptions?.tokenProvider &&
154+
!cosmosDbStorageOptions.tokenCredential
155+
) {
156+
throw new ReferenceError('authKey or tokenCredential for CosmosDB is required.');
148157
}
149158
if (!cosmosDbStorageOptions.databaseId) {
150159
throw new ReferenceError('databaseId is for CosmosDB required.');
@@ -322,12 +331,21 @@ export class CosmosDbPartitionedStorage implements Storage {
322331
async initialize(): Promise<void> {
323332
if (!this.container) {
324333
if (!this.client) {
325-
this.client = new CosmosClient({
326-
endpoint: this.cosmosDbStorageOptions.cosmosDbEndpoint,
327-
key: this.cosmosDbStorageOptions.authKey,
328-
userAgentSuffix: `${pjson.name} ${pjson.version}`,
329-
...this.cosmosDbStorageOptions.cosmosClientOptions,
330-
});
334+
if (this.cosmosDbStorageOptions.tokenCredential) {
335+
this.client = new CosmosClient({
336+
endpoint: this.cosmosDbStorageOptions.cosmosDbEndpoint,
337+
aadCredentials: this.cosmosDbStorageOptions.tokenCredential,
338+
userAgentSuffix: `${pjson.name} ${pjson.version}`,
339+
...this.cosmosDbStorageOptions.cosmosClientOptions,
340+
});
341+
} else {
342+
this.client = new CosmosClient({
343+
endpoint: this.cosmosDbStorageOptions.cosmosDbEndpoint,
344+
key: this.cosmosDbStorageOptions.authKey,
345+
userAgentSuffix: `${pjson.name} ${pjson.version}`,
346+
...this.cosmosDbStorageOptions.cosmosClientOptions,
347+
});
348+
}
331349
}
332350
const dbAndContainerKey = `${this.cosmosDbStorageOptions.databaseId}-${this.cosmosDbStorageOptions.containerId}`;
333351
this.container = await _doOnce.waitFor(

libraries/botbuilder-azure/tests/cosmosDbPartitionedStorage.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ describe('CosmosDbPartitionedStorage - Constructor Tests', function () {
146146
);
147147
});
148148

149-
it('throws when no authKey provided', function () {
149+
it('throws when no authKey or tokenCredential provided', function () {
150150
const noAuthKey = getSettings();
151151
noAuthKey.authKey = null;
152152
assert.throws(
153153
() => new CosmosDbPartitionedStorage(noAuthKey),
154-
ReferenceError('authKey for CosmosDB is required.')
154+
ReferenceError('authKey or tokenCredential for CosmosDB is required.')
155155
);
156156
});
157157

libraries/botbuilder-dialogs-adaptive-runtime-core/test/serviceCollection.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('ServiceCollection', function () {
5757
services.addFactory<number[]>('values', (values = [1]) => values.concat(2));
5858
services.composeFactory<number[]>('values', (values) => values.concat(3));
5959

60-
const { values } = services.makeInstances();
60+
const { values } = services.makeInstances<{ values: number[] }>();
6161
assert.deepStrictEqual(values, [1, 2, 3]);
6262
});
6363

@@ -69,7 +69,7 @@ describe('ServiceCollection', function () {
6969
services.composeFactory<number[]>('values', (values) => values.concat(2));
7070
services.composeFactory<number[]>('values', (values) => values.concat(3));
7171

72-
const { values } = services.makeInstances();
72+
const { values } = services.makeInstances<{ values: number[] }>();
7373
assert.deepStrictEqual(values, [1, 2, 3]);
7474
});
7575
});
@@ -95,8 +95,7 @@ describe('ServiceCollection', function () {
9595

9696
it('optionally fully reconstructs dependencies', function () {
9797
const services = makeServiceCollection();
98-
99-
const { foo, bar, baz } = services.makeInstances();
98+
const { foo, bar, baz } = services.makeInstances<{ foo: Foo; bar: Bar; baz: Baz }>();
10099
assert.ok(foo);
101100
assert.ok(bar);
102101
assert.ok(baz);

libraries/botbuilder-dialogs-adaptive-runtime-integration-azure-functions/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export function makeTriggers(
209209
res.status(200);
210210
res.set('Content-Type', contentType);
211211
res.end(contents);
212-
} catch (err) {
212+
} catch (err: any) {
213213
if (err.message.includes('ENOENT')) {
214214
return res.status(404).end();
215215
}

libraries/botbuilder-dialogs-adaptive-runtime-integration-express/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export async function makeApp(
144144
await adapter.process(req, res, async (turnContext) => {
145145
await bot.run(turnContext);
146146
});
147-
} catch (err) {
147+
} catch (err: any) {
148148
return errorHandler(err, res);
149149
}
150150
});
@@ -173,7 +173,7 @@ export async function makeApp(
173173
await adapter.process(req, res, async (turnContext) => {
174174
await bot.run(turnContext);
175175
});
176-
} catch (err) {
176+
} catch (err: any) {
177177
return errorHandler(err, res);
178178
}
179179
});
@@ -200,7 +200,7 @@ export async function makeApp(
200200
await adapter.process(req, socket, head, async (context) => {
201201
await bot.run(context);
202202
});
203-
} catch (err) {
203+
} catch (err: any) {
204204
return errorHandler(err);
205205
}
206206
});

libraries/botbuilder-dialogs-adaptive-runtime-integration-restify/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export async function makeServer(
140140
await adapter.process(req, res, async (turnContext) => {
141141
await bot.run(turnContext);
142142
});
143-
} catch (err) {
143+
} catch (err: any) {
144144
return errorHandler(err, res);
145145
}
146146
});
@@ -169,7 +169,7 @@ export async function makeServer(
169169
await adapter.process(req, res, async (turnContext) => {
170170
await bot.run(turnContext);
171171
});
172-
} catch (err) {
172+
} catch (err: any) {
173173
return errorHandler(err, res);
174174
}
175175
});
@@ -197,7 +197,7 @@ export async function makeServer(
197197
await adapter.process(req, socket, head, async (context) => {
198198
await bot.run(context);
199199
});
200-
} catch (err) {
200+
} catch (err: any) {
201201
return errorHandler(err);
202202
}
203203
});

libraries/botbuilder-dialogs-adaptive-runtime/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ async function addSettingsBotComponents(services: ServiceCollection, configurati
442442
const botComponent = await loadBotComponent(name);
443443

444444
botComponent.configureServices(services, configuration.bind([settingsPrefix ?? name]));
445-
} catch (error) {
445+
} catch (error: any) {
446446
loadErrors.push({ error, name });
447447
}
448448
}

libraries/botbuilder-stdlib/src/retry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function retry<T>(
2525
try {
2626
// Note: return await intentional so we can catch errors
2727
return await promise(n);
28-
} catch (err) {
28+
} catch (err: any) {
2929
maybeError = err;
3030

3131
await new Promise((resolve) => setTimeout(resolve, delay));

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@
9191
"source-map-support": "^0.5.19",
9292
"sponge": "^0.1.0",
9393
"tinyify": "^3.0.0",
94-
"ts-node": "^9.0.0",
94+
"ts-node": "^10.0.0",
9595
"typedoc": "^0.19.2",
9696
"typedoc-plugin-external-module-name": "^4.0.3",
9797
"typedoc-plugin-markdown": "^3.0.11",
98-
"typescript": "^4.0.5",
98+
"typescript": "^4.1.0",
9999
"wsrun": "^5.2.4"
100100
},
101101
"nyc": {

0 commit comments

Comments
 (0)