Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added token credential to azure-storage if rbac is used #10593

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/storage-azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ export default buildConfig({
| `allowContainerCreate` | Whether or not to allow the container to be created if it does not exist | `false` |
| `baseURL` | Base URL for the Azure Blob storage account | |
| `connectionString` | Azure Blob storage connection string | |
| `credential` | Alternative to connection string you can use TokenCredential to connect | |
| `containerName` | Azure Blob storage container name | |
20 changes: 16 additions & 4 deletions packages/storage-azure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BatchSubRequest } from '@azure/storage-blob'
import type {
Adapter,
PluginOptions as CloudStoragePluginOptions,
Expand Down Expand Up @@ -35,13 +36,17 @@ export type AzureStorageOptions = {
/**
* Azure Blob storage connection string
*/
connectionString: string

connectionString?: string
/**
* Azure Blob storage container name
*/
containerName: string

/**
* Instead of a connection string you can chose to use any object that implements the TokenCredential interface if you are securing your storage with RBAC, such as AnonymousCredential, StorageSharedKeyCredential or any credential from the `@azure/identity` package to authenticate requests to the service.
*/
credential?: BatchSubRequest['credential']

/**
* Whether or not to enable the plugin
*
Expand Down Expand Up @@ -103,14 +108,21 @@ function azureStorageInternal({
baseURL,
connectionString,
containerName,
credential,
}: AzureStorageOptions): Adapter {
const createContainerIfNotExists = () => {
void getStorageClientFunc({ connectionString, containerName }).createIfNotExists({
void getStorageClientFunc({
baseURL,
connectionString,
containerName,
credential,
}).createIfNotExists({
access: 'blob',
})
}

const getStorageClient = () => getStorageClientFunc({ connectionString, containerName })
const getStorageClient = () =>
getStorageClientFunc({ baseURL, connectionString, containerName, credential })

return ({ collection, prefix }): GeneratedAdapter => {
return {
Expand Down
18 changes: 14 additions & 4 deletions packages/storage-azure/src/utils/getStorageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import type { AzureStorageOptions } from '../index.js'
let storageClient: ContainerClient | null = null

export function getStorageClient(
options: Pick<AzureStorageOptions, 'connectionString' | 'containerName'>,
options: Pick<
AzureStorageOptions,
'baseURL' | 'connectionString' | 'containerName' | 'credential'
>,
): ContainerClient {
if (storageClient) {
return storageClient
}

const { connectionString, containerName } = options

const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
const { baseURL, connectionString, containerName, credential } = options
let blobServiceClient: BlobServiceClient | undefined = undefined
if (typeof connectionString === 'string') {
blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
} else if (typeof credential === 'object') {
blobServiceClient = new BlobServiceClient(baseURL, credential, {})
}
if (!blobServiceClient) {
throw new Error('connectionString or credential must be provided')
}
storageClient = blobServiceClient.getContainerClient(containerName)
return storageClient
}
Loading