mirror of
https://github.com/hanzoai/azure-sdk-for-js.git
synced 2026-10-30 19:51:51 +00:00
Overhauls tests and samples for Azure Cognitive Services for Language SDKs by implementing safe secret standards: - Updates `test-resources.bicep` to not output API keys nor SAS tokens - Updates the tests to retrieve API keys using the arm client - Updates the tests to create SAS tokens for Storage container access - Updates the samples and snippets to use Entra ID authentication - Fix live tests by enabling SAS access to the persistent storage account Live tests run: https://dev.azure.com/azure-sdk/internal/_build/results?buildId=4694655&view=results
59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
import { StorageManagementClient, type ServiceSasParameters } from "@azure/arm-storage";
|
|
import type { TokenCredential } from "@azure/core-auth";
|
|
import type { EnvVarKeys } from "./constants.js";
|
|
|
|
type EnvVarType = (typeof EnvVarKeys)[keyof typeof EnvVarKeys];
|
|
|
|
export async function getContainerUrls(inputs: {
|
|
subscriptionId: string;
|
|
resourceGroupName: string;
|
|
accountName: string;
|
|
credential: TokenCredential;
|
|
containers: Array<{
|
|
name: string;
|
|
sasParams: ServiceSasParameters;
|
|
envVarName: EnvVarType;
|
|
}>;
|
|
}): Promise<Array<{ envVarName: EnvVarType; sasUrl: string }>> {
|
|
const { subscriptionId, resourceGroupName, accountName, credential, containers } = inputs;
|
|
const blobEndpoint = `https://${accountName}.blob.core.windows.net/`;
|
|
const storageClient = new StorageManagementClient(credential, subscriptionId);
|
|
return Promise.all(
|
|
containers.map(async ({ name: containerName, sasParams, envVarName }) => ({
|
|
envVarName,
|
|
sasUrl: await generateContainerSasUrl({
|
|
storageClient,
|
|
containerName,
|
|
resourceGroupName,
|
|
accountName,
|
|
blobEndpoint,
|
|
sasParams,
|
|
}),
|
|
})),
|
|
);
|
|
}
|
|
|
|
async function generateContainerSasUrl(inputs: {
|
|
storageClient: StorageManagementClient;
|
|
containerName: string;
|
|
resourceGroupName: string;
|
|
accountName: string;
|
|
blobEndpoint: string;
|
|
sasParams: ServiceSasParameters;
|
|
}): Promise<string> {
|
|
const { storageClient, containerName, resourceGroupName, accountName, blobEndpoint, sasParams } =
|
|
inputs;
|
|
const result = await storageClient.storageAccounts.listServiceSAS(
|
|
resourceGroupName,
|
|
accountName,
|
|
sasParams,
|
|
);
|
|
if (!result.serviceSasToken) {
|
|
throw new Error(`Failed to generate SAS token for container: ${containerName}`);
|
|
}
|
|
return `${blobEndpoint}${containerName}?${result.serviceSasToken}`;
|
|
}
|