Files
Deyaaeldeen AlmahallawiandGitHub 07131a27f7 [document intelligence] Use safe secrets (#33551)
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
2025-03-27 17:02:52 -07:00

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}`;
}