-
Notifications
You must be signed in to change notification settings - Fork 20
Adding the SDK Binding Support for Storage Blob #341
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c3f43cc
Adding SDK Binding for BlobTrigger
d5ed6aa
Adding Supported Binding Types Enum
0541ff5
Adding MI support
4e6848d
Renaming and Creating a Factory for StorageBlobClient
4b154d0
Adding support for input binding
3e98a8b
Adding unit tests
bde73cd
Removing Log Statements
80b8618
Updating the package.json for sinon testing dependencies
0e7efad
Original Without Singleton
cd584ba
Moving the StorageClientFactory to extensions base
a50c485
Updating package.json
81f6c60
Removing extra log
caa39e1
Adding generic call to support deferred binding
d1332ec
Code Review Comments
4dca789
Adding thje logs for Dashboard at function start
057e790
Fixing Package-loc.json
a5cc33b
Fixing Linting issue and tests
2c81f81
Ensure the release version is in the constant file
ebbc2ea
Adding SDK Binding for BlobTrigger
580212a
Adding Supported Binding Types Enum
380c3dd
Adding MI support
cf1be60
Renaming and Creating a Factory for StorageBlobClient
dfdafd9
Package.json conflict resolution
e2d7acb
Merge branch 'swapnil/SdkBindingBlob' of https://github.com/Azure/azu…
1d8c6f5
Removing the unecessory log
8c85817
Removing unecessory changes
326454d
Removing AzuriteConfig
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under the MIT License. | ||
|
||
import { ModelBindingData } from '@azure/functions-core'; | ||
|
||
export type BlobConnectionDetails = { | ||
Connection: string; | ||
ContainerName: string; | ||
BlobName: string; | ||
}; | ||
|
||
// Define the `ServiceBusConnectionInfo` type that extends `ConnectionInfo` | ||
//TODO Define other connectionInfo example ServiceBusConnectionDetails | ||
|
||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Type Guard to check if an object is of type BlobConnectionInfo | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
function isBlobConnectionDetails(obj: unknown): obj is BlobConnectionDetails { | ||
return ( | ||
obj !== null && | ||
typeof obj === 'object' && | ||
'Connection' in obj && | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
typeof (obj as any).Connection === 'string' && | ||
'ContainerName' in obj && | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
typeof (obj as any).ContainerName === 'string' && | ||
'BlobName' in obj && | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
typeof (obj as any).BlobName === 'string' | ||
); | ||
} | ||
|
||
/** | ||
* Function to parse JSON and determine its type | ||
* @param jsonBuffer Bufer that holds the JSON string to parse | ||
* @returns Either `BlobConnectionDetails` or `ServiceBusConnectionDetails` | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export function parseConnectionDetails(jsonBuffer: Buffer | null | undefined): BlobConnectionDetails { | ||
if (jsonBuffer === null || jsonBuffer === undefined) { | ||
throw new Error('Connection details content is null or undefined'); | ||
} | ||
const parsedObject: unknown = JSON.parse(jsonBuffer.toString()); | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (isBlobConnectionDetails(parsedObject)) { | ||
return parsedObject; | ||
} | ||
//TODO add other parser for different resource types | ||
else { | ||
throw new Error('Invalid connection info type'); | ||
} | ||
} | ||
|
||
/** | ||
* Type guard to check if an object conforms to the ModelBindingData interface | ||
* @param obj Object to check | ||
* @returns True if object is ModelBindingData | ||
*/ | ||
export function isModelBindingData(obj: unknown): obj is ModelBindingData { | ||
if (!obj || typeof obj !== 'object') { | ||
return false; | ||
} | ||
|
||
const candidate = obj as Record<string, unknown>; | ||
|
||
// Check content property if it exists | ||
if ( | ||
'content' in candidate && | ||
candidate.content !== null && | ||
candidate.content !== undefined && | ||
candidate.content instanceof Buffer | ||
) { | ||
return false; | ||
} | ||
|
||
// Check string properties if they exist | ||
const stringProps = ['contentType', 'source', 'version']; | ||
for (const prop of stringProps) { | ||
if ( | ||
prop in candidate && | ||
candidate[prop] !== null && | ||
candidate[prop] !== undefined && | ||
typeof candidate[prop] !== 'string' | ||
) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/deferred-binding/storage-blob/azureStorageBlobClient.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { BlobClient, ContainerClient, StoragePipelineOptions } from '@azure/storage-blob'; | ||
import { StorageBlobServiceClientStrategy } from './storageBlobServiceClientStrategy'; | ||
|
||
export class AzureStorageBlobClient { | ||
private blobClient: BlobClient | undefined; | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private containerClient: ContainerClient | undefined; | ||
|
||
/** | ||
* Creates a new AzureStorageBlobClient instance | ||
* | ||
* @param strategyOrAccountUrl - The strategy to use for creating the BlobServiceClient or the account URL | ||
* @param credentialOrOptions - The credential to use for authentication or storage pipeline options | ||
* @param options - Storage pipeline options (optional, only used when the first parameter is an account URL) | ||
*/ | ||
constructor( | ||
strategy: StorageBlobServiceClientStrategy, | ||
containerName?: string, | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
blobName?: string, | ||
options?: StoragePipelineOptions | ||
) { | ||
const storageBlobServiceClient = strategy.createStorageBlobServiceClient(options); | ||
// Initialize container and blob clients if names are provided | ||
if (containerName) { | ||
this.containerClient = storageBlobServiceClient.getContainerClient(containerName); | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (blobName) { | ||
this.blobClient = this.containerClient.getBlobClient(blobName); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Gets the BlobClient instance | ||
* @returns The BlobClient | ||
*/ | ||
getBlobClient(): BlobClient { | ||
if (!this.blobClient) { | ||
throw new Error('No blob client available. A blob name must be provided in the constructor.'); | ||
} | ||
return this.blobClient; | ||
} | ||
|
||
/** | ||
* Gets the ContainerClient instance | ||
* @returns The ContainerClient | ||
*/ | ||
getContainerClient(): ContainerClient { | ||
if (!this.containerClient) { | ||
throw new Error('No container client available. A container name must be provided in the constructor.'); | ||
} | ||
return this.containerClient; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/deferred-binding/storage-blob/azureStorageBlobClientFactory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { ModelBindingData } from '@azure/functions-core'; | ||
import { StorageBlobClient } from '../../../types/storageBlobClient'; | ||
import { BlobConnectionDetails, parseConnectionDetails } from '../connectionDetails'; | ||
import { AzureStorageBlobClient } from './azureStorageBlobClient'; | ||
import { ConnectionStringStrategy } from './connectionStringStrategy'; | ||
import { ManagedIdentitySystemStrategy } from './managedIdentitySystemStrategy'; | ||
import { ManagedIdentityUserStrategy } from './managedIdentityUserStartegy'; | ||
import { StorageBlobServiceClientStrategy } from './storageBlobServiceClientStrategy'; | ||
import { getConnectionString, isSystemBasedManagedIdentity, isUserBasedManagedIdentity } from './utils'; | ||
|
||
/** | ||
* Factory class for creating Azure Blob Storage clients | ||
*/ | ||
export class AzureStorageBlobClientFactory { | ||
static buildClientFromModelBindingData(modelBindingData: ModelBindingData): StorageBlobClient { | ||
const connectionDetails = parseConnectionDetails(modelBindingData.content); | ||
//TODO Add type check and parsing for other connection types | ||
return this.fromConnectionDetailsToBlobStorageClient(connectionDetails); | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Creates a StorageBlobClient directly from connection parameters | ||
* | ||
* @param connectionDetails - Connection details for the blob storage client | ||
* @returns - StorageBlobClient object containing the blob client and container client | ||
*/ | ||
static fromConnectionDetailsToBlobStorageClient(connectionDetails: BlobConnectionDetails): StorageBlobClient { | ||
swapnil-nagar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const connectionName: string = connectionDetails.Connection; | ||
const connectionUrl = getConnectionString(connectionName); | ||
const connectionStrategy = this.createConnectionStrategy(connectionName, connectionUrl); | ||
|
||
const azureStorageBlobClient = new AzureStorageBlobClient( | ||
connectionStrategy, | ||
connectionDetails.ContainerName, | ||
connectionDetails.BlobName | ||
); | ||
|
||
const storageBlobClient: StorageBlobClient = { | ||
blobClient: azureStorageBlobClient.getBlobClient(), | ||
conatinerClient: azureStorageBlobClient.getContainerClient(), | ||
}; | ||
return storageBlobClient; | ||
} catch (error) { | ||
const errorMessage = error instanceof Error ? error.message : 'Unknown error'; | ||
throw new Error(`Failed to create client from parameters: ${errorMessage}`); | ||
} | ||
} | ||
|
||
/** | ||
* Creates the appropriate connection strategy based on the connection name and URL | ||
* | ||
* @param connectionName - The connection name | ||
* @param connectionUrl - The resolved connection URL | ||
* @returns The appropriate StorageBlobServiceClientStrategy | ||
*/ | ||
static createConnectionStrategy(connectionName: string, connectionUrl: string): StorageBlobServiceClientStrategy { | ||
// User-assigned managed identity takes precedence | ||
if (isUserBasedManagedIdentity(connectionName)) { | ||
const clientId = process.env[`${connectionName}__clientId`]; | ||
if (!clientId) { | ||
throw new Error(`Environment variable ${connectionName}__clientId is not defined.`); | ||
} | ||
return new ManagedIdentityUserStrategy(connectionUrl, clientId); | ||
} | ||
|
||
// Next, check for system-assigned managed identity | ||
if (isSystemBasedManagedIdentity(connectionName)) { | ||
return new ManagedIdentitySystemStrategy(connectionUrl); | ||
} | ||
|
||
// Default to connection string | ||
return new ConnectionStringStrategy(connectionUrl); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/deferred-binding/storage-blob/connectionStringStrategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { BlobServiceClient, StoragePipelineOptions } from '@azure/storage-blob'; | ||
import { StorageBlobServiceClientStrategy } from './storageBlobServiceClientStrategy'; | ||
|
||
/** | ||
* Strategy for creating BlobServiceClient using connection string | ||
*/ | ||
export class ConnectionStringStrategy implements StorageBlobServiceClientStrategy { | ||
/** | ||
* @param connectionString - Azure Storage connection string | ||
*/ | ||
constructor(private connectionString: string) {} | ||
|
||
createStorageBlobServiceClient(options?: StoragePipelineOptions): BlobServiceClient { | ||
return BlobServiceClient.fromConnectionString(this.connectionString, options); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.