Skip to content

Commit

Permalink
feat(monosize-storage-azure): add support for DefaultAzureCredential (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell authored Nov 7, 2024
1 parent 3add983 commit 3e278df
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "feat(monosize-storage-azure): add support for DefaultAzureCredential",
"packageName": "monosize-storage-azure",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"vitest": "0.34.6"
},
"dependencies": {
"@azure/data-tables": "^13.0.0",
"@azure/identity": "^4.4.0",
"@azure/data-tables": "^13.2.2",
"@azure/identity": "^4.5.0",
"@upstash/redis": "^1.18.0",
"acorn": "^8.11.3",
"ci-info": "^3.7.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/monosize-storage-azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "MIT",
"types": "./src/index.d.mts",
"dependencies": {
"@azure/data-tables": "^13.0.0",
"@azure/identity": "^4.4.0",
"@azure/data-tables": "^13.2.2",
"@azure/identity": "^4.5.0",
"monosize": "^0.6.3",
"node-fetch": "^3.3.0",
"picocolors": "^1.0.0",
Expand Down
25 changes: 24 additions & 1 deletion packages/monosize-storage-azure/src/createTableClient.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AzureNamedKeyCredential, TableClient } from '@azure/data-tables';
import { AzurePipelinesCredential } from '@azure/identity';
import { AzurePipelinesCredential, DefaultAzureCredential } from '@azure/identity';
import type { AzureStorageConfig } from './types.mjs';

export function createTableClient(options: Required<Pick<AzureStorageConfig, 'authType' | 'tableName'>>): TableClient {
Expand Down Expand Up @@ -50,6 +50,29 @@ export function createTableClient(options: Required<Pick<AzureStorageConfig, 'au
);
}

if (authType === 'DefaultAzureCredential') {
// DefaultAzureCredential will obtain these from environment variables, thus why we need to assert on them while they are not used directly in code
const requiredEnvVars = [
'BUNDLESIZE_ACCOUNT_NAME',
'AZURE_TENANT_ID',
'AZURE_CLIENT_ID',
'AZURE_SERVICE_CONNECTION_ID',
];
validateRequiredEnvVariables({
requiredEnvVars,
authType,
});

const AZURE_STORAGE_ACCOUNT = process.env['BUNDLESIZE_ACCOUNT_NAME'] as string;
const TENANT_ID = process.env['AZURE_TENANT_ID'] as string;

return new TableClient(
`https://${AZURE_STORAGE_ACCOUNT}.table.core.windows.net`,
AZURE_STORAGE_TABLE_NAME,
new DefaultAzureCredential({ tenantId: TENANT_ID }),
);
}

throw new Error(`monosize-storage-azure: "authType: ${authType}" is not supported.`);
}

Expand Down
23 changes: 22 additions & 1 deletion packages/monosize-storage-azure/src/createTableClient.test.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import { AzureNamedKeyCredential, TableClient } from '@azure/data-tables';
import { AzurePipelinesCredential } from '@azure/identity';
import { AzurePipelinesCredential, DefaultAzureCredential } from '@azure/identity';
import { createTableClient } from './createTableClient.mjs';
import type { AzureAuthenticationType } from './types.mjs';

Expand All @@ -19,6 +19,7 @@ vitest.mock('@azure/data-tables', () => {
vitest.mock('@azure/identity', () => {
return {
AzurePipelinesCredential: vitest.fn(),
DefaultAzureCredential: vitest.fn(),
};
});

Expand Down Expand Up @@ -74,6 +75,26 @@ describe('createTableClient', () => {
);
});

it('should create TableClient with DefaultAzureCredential', () => {
vitest.stubEnv('BUNDLESIZE_ACCOUNT_NAME', 'test-account-name');
vitest.stubEnv('AZURE_TENANT_ID', 'test-tenant-id');
vitest.stubEnv('AZURE_CLIENT_ID', 'test-client-id');
vitest.stubEnv('AZURE_SERVICE_CONNECTION_ID', 'test-service-connection-id');
vitest.stubEnv('SYSTEM_ACCESSTOKEN', 'test-system-access-token');

const authType = 'DefaultAzureCredential';
const tableName = 'test-table';
createTableClient({ authType, tableName });

expect(DefaultAzureCredential).toHaveBeenCalledWith({ tenantId: process.env['AZURE_TENANT_ID'] });

expect(TableClient).toHaveBeenCalledWith(
'https://test-account-name.table.core.windows.net',
tableName,
expect.any(DefaultAzureCredential),
);
});

it('should throw an error for unsupported authType', () => {
const authType = 'AzureNamedKeyCredentail' as AzureAuthenticationType;
const tableName = 'test-table';
Expand Down
2 changes: 1 addition & 1 deletion packages/monosize-storage-azure/src/types.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @see https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-azure-hosted-applications
*/
export type AzureAuthenticationType = 'AzureNamedKeyCredential' | 'AzurePipelinesCredential';
export type AzureAuthenticationType = 'AzureNamedKeyCredential' | 'AzurePipelinesCredential' | 'DefaultAzureCredential';

export type AzureStorageConfig = {
endpoint: string;
Expand Down
138 changes: 78 additions & 60 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ __metadata:
languageName: node
linkType: hard

"@azure/abort-controller@npm:^1.0.0":
version: 1.1.0
resolution: "@azure/abort-controller@npm:1.1.0"
dependencies:
tslib: ^2.2.0
checksum: 0f45e504d4aea799486867179afe7589255f6c111951279958e9d0aa5faebb2c96b8f88e3e3c958ce07b02bcba0b0cddb1bbec94705f573a48ecdb93eec1a92a
languageName: node
linkType: hard

"@azure/abort-controller@npm:^2.0.0":
version: 2.1.2
resolution: "@azure/abort-controller@npm:2.1.2"
Expand All @@ -40,14 +31,14 @@ __metadata:
languageName: node
linkType: hard

"@azure/core-auth@npm:^1.3.0, @azure/core-auth@npm:^1.4.0, @azure/core-auth@npm:^1.5.0":
version: 1.7.2
resolution: "@azure/core-auth@npm:1.7.2"
"@azure/core-auth@npm:^1.3.0, @azure/core-auth@npm:^1.4.0, @azure/core-auth@npm:^1.8.0, @azure/core-auth@npm:^1.9.0":
version: 1.9.0
resolution: "@azure/core-auth@npm:1.9.0"
dependencies:
"@azure/abort-controller": ^2.0.0
"@azure/core-util": ^1.1.0
"@azure/core-util": ^1.11.0
tslib: ^2.6.2
checksum: dcbba47f32e4cc929e078fd5d714af43185f3781ff5c19f01cba9e0d9078690e716be8172dd77a13aa3a81380d338a678b974bc5cbaa2e0d8629fb262ee3f0df
checksum: 4050112188db093c5e01caca0175708c767054c0cea4202430ff43ee42a16430235752ccc0002caea1796c8f01b4f6369c878762bf4c1b2f61af1b7ac13182fc
languageName: node
linkType: hard

Expand Down Expand Up @@ -75,21 +66,19 @@ __metadata:
languageName: node
linkType: hard

"@azure/core-rest-pipeline@npm:^1.1.0, @azure/core-rest-pipeline@npm:^1.9.1":
version: 1.10.1
resolution: "@azure/core-rest-pipeline@npm:1.10.1"
"@azure/core-rest-pipeline@npm:^1.1.0, @azure/core-rest-pipeline@npm:^1.17.0, @azure/core-rest-pipeline@npm:^1.9.1":
version: 1.17.0
resolution: "@azure/core-rest-pipeline@npm:1.17.0"
dependencies:
"@azure/abort-controller": ^1.0.0
"@azure/core-auth": ^1.4.0
"@azure/abort-controller": ^2.0.0
"@azure/core-auth": ^1.8.0
"@azure/core-tracing": ^1.0.1
"@azure/core-util": ^1.0.0
"@azure/core-util": ^1.9.0
"@azure/logger": ^1.0.0
form-data: ^4.0.0
http-proxy-agent: ^5.0.0
https-proxy-agent: ^5.0.0
tslib: ^2.2.0
uuid: ^8.3.0
checksum: d1c165047519c05b702184ffadf900a1f3eca032795b0f4f5b174c8257b663c1b1eab847236579dddcf637ea380896e17328ebf5022050441ee80672ad6c449d
http-proxy-agent: ^7.0.0
https-proxy-agent: ^7.0.0
tslib: ^2.6.2
checksum: 8a79cbaaae295964bb8d18cb44873e705ebe3f9217fe74d83415b7266e46c3d6297c799d6e5e49516b165d273e0b794bf0ed14bb6aa875d09d4a90c3a559b6df
languageName: node
linkType: hard

Expand All @@ -102,13 +91,13 @@ __metadata:
languageName: node
linkType: hard

"@azure/core-util@npm:^1.0.0, @azure/core-util@npm:^1.1.0, @azure/core-util@npm:^1.3.0, @azure/core-util@npm:^1.6.1":
version: 1.9.2
resolution: "@azure/core-util@npm:1.9.2"
"@azure/core-util@npm:^1.11.0, @azure/core-util@npm:^1.6.1, @azure/core-util@npm:^1.9.0":
version: 1.11.0
resolution: "@azure/core-util@npm:1.11.0"
dependencies:
"@azure/abort-controller": ^2.0.0
tslib: ^2.6.2
checksum: 63c7ab2bdd6e75e38af33e37c9844515c546ed3e8f88fb98926ec88287dfabb249b9fd156658d42bfccbaeb46369254e7cf53eb6ef789b9d88880585eaabb298
checksum: 91e3ec329d9eddaa66be5efb1785dad68dcb48dd779fca36e39db041673230510158ff5ca9ccef9f19c3e4d8e9af29f66a367cfc31a7b94d2541f80ef94ec797
languageName: node
linkType: hard

Expand All @@ -122,9 +111,9 @@ __metadata:
languageName: node
linkType: hard

"@azure/data-tables@npm:^13.0.0":
version: 13.2.1
resolution: "@azure/data-tables@npm:13.2.1"
"@azure/data-tables@npm:^13.2.2":
version: 13.2.2
resolution: "@azure/data-tables@npm:13.2.2"
dependencies:
"@azure/core-auth": ^1.3.0
"@azure/core-client": ^1.0.0
Expand All @@ -135,29 +124,29 @@ __metadata:
"@azure/logger": ^1.0.0
tslib: ^2.2.0
uuid: ^8.3.0
checksum: 9a3a18f4334781d144544b7087a24447e1e20ceadf06954ee71ad83b8d4a3692799461614bd186255a68ee60c1c9a62a1e63fb3026181cf3c6ccf5e5d8b533f6
checksum: 8846ef61958ebd75e9377a107285d33132e8bc34d46517a0b263b58c5c9039d0161571ab544befd11fde1f3545c526d44698fe5ddc3cfeea3192a66de2aad2bc
languageName: node
linkType: hard

"@azure/identity@npm:^4.4.0":
version: 4.4.1
resolution: "@azure/identity@npm:4.4.1"
"@azure/identity@npm:^4.5.0":
version: 4.5.0
resolution: "@azure/identity@npm:4.5.0"
dependencies:
"@azure/abort-controller": ^1.0.0
"@azure/core-auth": ^1.5.0
"@azure/abort-controller": ^2.0.0
"@azure/core-auth": ^1.9.0
"@azure/core-client": ^1.9.2
"@azure/core-rest-pipeline": ^1.1.0
"@azure/core-rest-pipeline": ^1.17.0
"@azure/core-tracing": ^1.0.0
"@azure/core-util": ^1.3.0
"@azure/core-util": ^1.11.0
"@azure/logger": ^1.0.0
"@azure/msal-browser": ^3.14.0
"@azure/msal-node": ^2.9.2
"@azure/msal-browser": ^3.26.1
"@azure/msal-node": ^2.15.0
events: ^3.0.0
jws: ^4.0.0
open: ^8.0.0
stoppable: ^1.1.0
tslib: ^2.2.0
checksum: 8dba5a1e347b349eb999e46cce190dedef02c0a81a179d4c4bf67ea4407c9f44ace5eec08f1ac8c963f8723e1074d32a9878a0ae5d4d4a880b19389b5fb6e7a1
checksum: 07d15898f194a220376d8d9c0ee891c93c6da188e44e76810fb781bf3bb7424498a6c1fa5b92c5a4d31f62b7398953f8a5bcf0f0ed57ed72239ce1c4f594b355
languageName: node
linkType: hard

Expand All @@ -170,30 +159,30 @@ __metadata:
languageName: node
linkType: hard

"@azure/msal-browser@npm:^3.14.0":
version: 3.20.0
resolution: "@azure/msal-browser@npm:3.20.0"
"@azure/msal-browser@npm:^3.26.1":
version: 3.27.0
resolution: "@azure/msal-browser@npm:3.27.0"
dependencies:
"@azure/msal-common": 14.14.0
checksum: 237a041bbe898f46676ddc0e0a0cfb26c821efecc71de19168bd70e216da14f3e83bfad064e4c5e5491828ae3e096a2d46d58b5ba227b820fe716639f9d80f5c
"@azure/msal-common": 14.16.0
checksum: 22c7d087380405f87139a7dfa579b8a49a17d5493e748e1e609f5733bb7549dd5b8558d709f81500f8faa3feebbc2245f8978adc96dc2ce84c54825b37301465
languageName: node
linkType: hard

"@azure/msal-common@npm:14.14.0":
version: 14.14.0
resolution: "@azure/msal-common@npm:14.14.0"
checksum: c77f51bdddb34da008786d7517713232dc69b7de9deec438ef463098e535ebdb8241ac04e9ddaee859d788dee71d683bf7ef0acab47781457d5c4ea561a8addf
"@azure/msal-common@npm:14.16.0":
version: 14.16.0
resolution: "@azure/msal-common@npm:14.16.0"
checksum: 01ec26e22243c5c435b97db085e96f5488733336c142b65a118ee6e523a548d3f17d013147810948cceaee7bdc339362bb9b2799fc9ea53c9d4c9aa10d8987e3
languageName: node
linkType: hard

"@azure/msal-node@npm:^2.9.2":
version: 2.12.0
resolution: "@azure/msal-node@npm:2.12.0"
"@azure/msal-node@npm:^2.15.0":
version: 2.16.0
resolution: "@azure/msal-node@npm:2.16.0"
dependencies:
"@azure/msal-common": 14.14.0
"@azure/msal-common": 14.16.0
jsonwebtoken: ^9.0.0
uuid: ^8.3.0
checksum: ad02d84ff0510594165672f0a39ba78f962631046051daf4de16ad4f783e0ee4c8e372aa99d17ac461c9d52bcceb1215c0d527443d97d5028c5d738029c4e71c
checksum: a19f302ba32b832acc4be52f20b1f222509f54ba9316942e045c93206d345a95767736db86966c3104da84bfb5e433477e7addec773fb6b6e0974bcb1f0f7b6e
languageName: node
linkType: hard

Expand Down Expand Up @@ -3590,6 +3579,15 @@ __metadata:
languageName: node
linkType: hard

"agent-base@npm:^7.0.2, agent-base@npm:^7.1.0":
version: 7.1.1
resolution: "agent-base@npm:7.1.1"
dependencies:
debug: ^4.3.4
checksum: 51c158769c5c051482f9ca2e6e1ec085ac72b5a418a9b31b4e82fe6c0a6699adb94c1c42d246699a587b3335215037091c79e0de512c516f73b6ea844202f037
languageName: node
linkType: hard

"agentkeepalive@npm:^4.2.1":
version: 4.2.1
resolution: "agentkeepalive@npm:4.2.1"
Expand Down Expand Up @@ -6082,6 +6080,16 @@ __metadata:
languageName: node
linkType: hard

"http-proxy-agent@npm:^7.0.0":
version: 7.0.2
resolution: "http-proxy-agent@npm:7.0.2"
dependencies:
agent-base: ^7.1.0
debug: ^4.3.4
checksum: 670858c8f8f3146db5889e1fa117630910101db601fff7d5a8aa637da0abedf68c899f03d3451cac2f83bcc4c3d2dabf339b3aa00ff8080571cceb02c3ce02f3
languageName: node
linkType: hard

"https-proxy-agent@npm:^5.0.0":
version: 5.0.0
resolution: "https-proxy-agent@npm:5.0.0"
Expand All @@ -6092,6 +6100,16 @@ __metadata:
languageName: node
linkType: hard

"https-proxy-agent@npm:^7.0.0":
version: 7.0.5
resolution: "https-proxy-agent@npm:7.0.5"
dependencies:
agent-base: ^7.0.2
debug: 4
checksum: 2e1a28960f13b041a50702ee74f240add8e75146a5c37fc98f1960f0496710f6918b3a9fe1e5aba41e50f58e6df48d107edd9c405c5f0d73ac260dabf2210857
languageName: node
linkType: hard

"human-signals@npm:^2.1.0":
version: 2.1.0
resolution: "human-signals@npm:2.1.0"
Expand Down Expand Up @@ -7446,8 +7464,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "monosize-monorepo@workspace:."
dependencies:
"@azure/data-tables": ^13.0.0
"@azure/identity": ^4.4.0
"@azure/data-tables": ^13.2.2
"@azure/identity": ^4.5.0
"@nx/eslint": 17.3.2
"@nx/eslint-plugin": 17.3.2
"@nx/js": 17.3.2
Expand Down

0 comments on commit 3e278df

Please sign in to comment.