@@ -26,6 +26,9 @@ import {
2626 SASProtocol ,
2727 generateBlobSASQueryParameters ,
2828} from '@azure/storage-blob' ;
29+ import {
30+ DefaultAzureCredential ,
31+ } from '@azure/identity' ;
2932
3033import { cancelCombinator } from './utils' ;
3134import {
@@ -52,9 +55,30 @@ import {
5255 ForeignKeysQueryResult ,
5356} from './driver.interface' ;
5457
58+ /**
59+ * @see {@link DefaultAzureCredential } constructor options
60+ */
5561export type AzureStorageClientConfig = {
56- azureKey : string ,
62+ azureKey ? : string ,
5763 sasToken ?: string ,
64+ /**
65+ * The client ID of a Microsoft Entra app registration.
66+ * In case of DefaultAzureCredential flow if it is omitted
67+ * the Azure library will try to use the AZURE_CLIENT_ID env
68+ */
69+ clientId ?: string ,
70+ /**
71+ * ID of the application's Microsoft Entra tenant. Also called its directory ID.
72+ * In case of DefaultAzureCredential flow if it is omitted
73+ * the Azure library will try to use the AZURE_TENANT_ID env
74+ */
75+ tenantId ?: string ,
76+ /**
77+ * The path to a file containing a Kubernetes service account token that authenticates the identity.
78+ * In case of DefaultAzureCredential flow if it is omitted
79+ * the Azure library will try to use the AZURE_FEDERATED_TOKEN_FILE env
80+ */
81+ tokenFilePath ?: string ,
5882} ;
5983
6084export type GoogleStorageClientConfig = {
@@ -730,7 +754,19 @@ export abstract class BaseDriver implements DriverInterface {
730754 const parts = bucketName . split ( '.blob.core.windows.net/' ) ;
731755 const account = parts [ 0 ] ;
732756 const container = parts [ 1 ] . split ( '/' ) [ 0 ] ;
733- const credential = new StorageSharedKeyCredential ( account , azureConfig . azureKey ) ;
757+ let credential : StorageSharedKeyCredential | DefaultAzureCredential ;
758+
759+ if ( azureConfig . azureKey ) {
760+ credential = new StorageSharedKeyCredential ( account , azureConfig . azureKey ) ;
761+ } else {
762+ const opts = {
763+ tenantId : azureConfig . tenantId ,
764+ clientId : azureConfig . clientId ,
765+ tokenFilePath : azureConfig . tokenFilePath ,
766+ } ;
767+ credential = new DefaultAzureCredential ( opts ) ;
768+ }
769+
734770 const url = `https://${ account } .blob.core.windows.net` ;
735771 const blobServiceClient = azureConfig . sasToken ?
736772 new BlobServiceClient ( `${ url } ?${ azureConfig . sasToken } ` ) :
@@ -741,18 +777,21 @@ export abstract class BaseDriver implements DriverInterface {
741777 const blobsList = containerClient . listBlobsFlat ( { prefix : `${ tableName } /` } ) ;
742778 for await ( const blob of blobsList ) {
743779 if ( blob . name && ( blob . name . endsWith ( '.csv.gz' ) || blob . name . endsWith ( '.csv' ) ) ) {
780+ const starts = new Date ( ) ;
781+ const expires = new Date ( new Date ( ) . valueOf ( ) + 1000 * 60 * 60 ) ;
782+ const userDelegationKey = await blobServiceClient . getUserDelegationKey ( starts , expires ) ;
744783 const sas = generateBlobSASQueryParameters (
745784 {
746785 containerName : container ,
747786 blobName : blob . name ,
748787 permissions : ContainerSASPermissions . parse ( 'r' ) ,
749- startsOn : new Date ( new Date ( ) . valueOf ( ) ) ,
750- expiresOn :
751- new Date ( new Date ( ) . valueOf ( ) + 1000 * 60 * 60 ) ,
788+ startsOn : starts ,
789+ expiresOn : expires ,
752790 protocol : SASProtocol . Https ,
753791 version : '2020-08-04' ,
754792 } ,
755- credential ,
793+ userDelegationKey ,
794+ account ,
756795 ) . toString ( ) ;
757796 csvFiles . push ( `${ url } /${ container } /${ blob . name } ?${ sas } ` ) ;
758797 }
0 commit comments