Skip to content

Commit

Permalink
add aad
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleysmall-microsoft committed Apr 22, 2024
1 parent 652af2c commit 9dabd90
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.2.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
<PackageVersion Include="Microsoft.Azure.StackExchangeRedis" Version="2.0.0" />
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.7.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
Expand Down
1 change: 1 addition & 0 deletions app/frontend/ClientApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Blazor.SpeechSynthesis.WebAssembly" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" />
<PackageReference Include="Microsoft.Azure.StackExchangeRedis" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
</ItemGroup>

Expand Down
12 changes: 11 additions & 1 deletion app/functions/EmbedFunctions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ uri is not null
services.AddSingleton<EmbeddingAggregateService>();

bool useRedis = Environment.GetEnvironmentVariable("USE_REDIS")?.ToLower() == "true";
string redisConnectionString = Environment.GetEnvironmentVariable("AZURE_CACHE_SERVICE_ENDPOINT") ?? "localhost";
string redisConnectionString = "";
string redisPrincipalId = "";
if (useRedis)
{
redisConnectionString = Environment.GetEnvironmentVariable("AZURE_CACHE_SERVICE_ENDPOINT") ??
throw new ArgumentNullException("AZURE_CACHE_SERVICE_ENDPOINT is null");
redisPrincipalId = Environment.GetEnvironmentVariable("AZURE_CACHE_SERVICE_PRINCIPAL_ID") ??
throw new ArgumentNullException("AZURE_CACHE_SERVICE_PRINCIPAL_ID is null");
}


services.AddSingleton<IEmbedService>(provider =>
{
Expand Down Expand Up @@ -103,6 +112,7 @@ uri is not null
{
return new AzureCacheEmbedService(
redisConnectionString: redisConnectionString,
redisPrincipalId: redisPrincipalId,
openAIClient: openAIClient,
embeddingModelName: embeddingModelName,
searchClient: searchClient,
Expand Down
1 change: 1 addition & 0 deletions app/prepdocs/PrepareDocs/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal record class AppOptions(
string? FormRecognizerServiceEndpoint,
string? ComputerVisionServiceEndpoint,
string? AzureCacheServiceEndpoint,
string? AzureCacheServicePrincipalId,
string? AzureCacheServiceIndex,
bool Verbose,
IConsole Console) : AppConsole(Console);
Expand Down
2 changes: 2 additions & 0 deletions app/prepdocs/PrepareDocs/Program.Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ private static Task<IEmbedService> GetAzureSearchEmbedService(AppOptions options
if (Environment.GetEnvironmentVariable("USE_REDIS") == "true")
{
var azureCacheServiceEndpoint = o.AzureCacheServiceEndpoint ?? throw new ArgumentNullException(nameof(o.AzureCacheServiceEndpoint));
var azureCachePrincipalId = o.AzureCacheServicePrincipalId ?? throw new ArgumentNullException(nameof(o.AzureCacheServicePrincipalId));

return new AzureCacheEmbedService(
redisConnectionString: azureCacheServiceEndpoint,
redisPrincipalId: azureCachePrincipalId,
openAIClient: openAIClient,
embeddingModelName: embeddingModelName,
searchClient: searchClient,
Expand Down
5 changes: 5 additions & 0 deletions app/prepdocs/PrepareDocs/Program.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal static partial class Program
private static readonly Option<string> s_azureCacheServiceEndpoint =
new(name: "--azurecacheendpoint", description: "Optional. The Azure Cache service endpoint which will be used to store and query embeddings");

private static readonly Option<string> s_azureCacheServicePrincipalId =
new(name: "--azurecacheprincipalid", description: "Optional. The Azure Cache service principal Id used for managed identity");

private static readonly Option<string> s_azureCacheServiceIndex =
new(name: "--azurecacheindex", description: "Optional. The Azure Cache service index name");

Expand Down Expand Up @@ -75,6 +78,7 @@ internal static partial class Program
s_computerVisionServiceEndpoint,
s_azureCacheServiceEndpoint,
s_azureCacheServiceIndex,
s_azureCacheServicePrincipalId,
s_verbose,
};

Expand All @@ -95,6 +99,7 @@ internal static partial class Program
ComputerVisionServiceEndpoint: context.ParseResult.GetValueForOption(s_computerVisionServiceEndpoint),
AzureCacheServiceEndpoint: context.ParseResult.GetValueForOption(s_azureCacheServiceEndpoint),
AzureCacheServiceIndex: context.ParseResult.GetValueForOption(s_azureCacheServiceIndex),
AzureCacheServicePrincipalId: context.ParseResult.GetValueForOption(s_azureCacheServicePrincipalId),
Verbose: context.ParseResult.GetValueForOption(s_verbose),
Console: context.Console);
}
3 changes: 2 additions & 1 deletion app/shared/Shared/Services/AzureCacheEmbedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class AzureCacheEmbedService(
string redisConnectionString,
string redisPrincipalId,
OpenAIClient openAIClient,
string embeddingModelName,
SearchClient searchClient,
Expand All @@ -35,7 +36,7 @@ public class AzureCacheEmbedService(
includeImageEmbeddingsField,
logger);

private readonly RedisConnection _connection = RedisConnection.InitializeAsync(redisConnectionString).Result;
private readonly RedisConnection _connection = RedisConnection.InitializeAsync(redisConnectionString, redisPrincipalId).Result;
private static readonly int s_embeddingDimension = 1536;

public async Task CreateSearchIndexAsync(string searchIndexName, CancellationToken ct = default)
Expand Down
15 changes: 11 additions & 4 deletions app/shared/Shared/Services/RedisConnection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using Azure.Identity;
using Microsoft.Azure.StackExchangeRedis;
using StackExchange.Redis;
using static System.Console;

namespace EmbedFunctions.Services
{
Expand All @@ -25,17 +29,19 @@ public class RedisConnection : IDisposable

private SemaphoreSlim _reconnectSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1);
private readonly string _connectionString;
private readonly string _principalId;
private ConnectionMultiplexer _connection;
private IDatabase _database;

private RedisConnection(string connectionString)
private RedisConnection(string connectionString, string principalId)

Check warning on line 36 in app/shared/Shared/Services/RedisConnection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_connection' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 36 in app/shared/Shared/Services/RedisConnection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_database' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 36 in app/shared/Shared/Services/RedisConnection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_connection' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 36 in app/shared/Shared/Services/RedisConnection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_database' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_connectionString = connectionString;
_principalId = principalId;
}

public static async Task<RedisConnection> InitializeAsync(string connectionString)
public static async Task<RedisConnection> InitializeAsync(string connectionString, string principalId)
{
var redisConnection = new RedisConnection(connectionString);
var redisConnection = new RedisConnection(connectionString, principalId);
await redisConnection.ForceReconnectAsync(initializing: true);

return redisConnection;
Expand Down Expand Up @@ -142,7 +148,8 @@ private async Task ForceReconnectAsync(bool initializing = false)
_previousErrorTime = DateTimeOffset.MinValue;

// Create a new connection
ConnectionMultiplexer _newConnection = await ConnectionMultiplexer.ConnectAsync(_connectionString);
var configurationOptions = await ConfigurationOptions.Parse(_connectionString).ConfigureForAzureWithSystemAssignedManagedIdentityAsync(_principalId);
ConnectionMultiplexer _newConnection = await ConnectionMultiplexer.ConnectAsync(configurationOptions);

// Swap current connection with the new connection
ConnectionMultiplexer oldConnection = Interlocked.Exchange(ref _connection, _newConnection);
Expand Down
1 change: 1 addition & 0 deletions app/shared/Shared/Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Azure.Search.Documents" />
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
<PackageReference Include="Microsoft.Azure.StackExchangeRedis" />
<PackageReference Include="PdfSharpCore" />
<PackageReference Include="StackExchange.Redis" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions infra/core/search/azure-cache.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ resource redisEnterprise_default 'Microsoft.Cache/redisEnterprise/databases@2024

output id string = redisEnterprise_resource.id
output endpoint string = '${redisEnterprise_resource.properties.hostName}:10000'
output principalId string = redisEnterprise_resource.identity.principalId
output name string = redisEnterprise_resource.name
output databaseName string = redisEnterprise_default.name
2 changes: 2 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ module function './app/function.bicep' = {
USE_AOAI: string(useAOAI)
USE_REDIS: string(useRedis)
AZURE_CACHE_SERVICE_ENDPOINT: useRedis ? azureCache.outputs.endpoint : ''
AZURE_CACHE_SERVICE_PRINCIPAL_ID: useRedis ? azureCache.outputs.principalId : ''
AZURE_CACHE_INDEX: azureCacheIndexName
AZURE_COMPUTER_VISION_ENDPOINT: useVision ? computerVision.outputs.endpoint : ''
OPENAI_API_KEY: useAOAI ? '' : openAIApiKey
Expand Down Expand Up @@ -808,6 +809,7 @@ output AZURE_SEARCH_SERVICE_RESOURCE_GROUP string = searchServiceResourceGroup.n
output AZURE_CACHE_INDEX string = azureCacheIndexName
output AZURE_CACHE_SERVICE string = azureCache.outputs.name
output AZURE_CACHE_SERVICE_ENDPOINT string = azureCache.outputs.endpoint
output AZURE_CACHE_SERVICE_PRINCIPAL_ID string = azureCache.outputs.principalId
output AZURE_CACHE_SERVICE_RESOURCE_GROUP string = azureCacheResourceGroup.name
output USE_REDIS bool = useRedis
output AZURE_STORAGE_ACCOUNT string = storage.outputs.name
Expand Down
1 change: 1 addition & 0 deletions scripts/prepdocs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ if ([string]::IsNullOrEmpty($env:AZD_PREPDOCS_RAN) -or $env:AZD_PREPDOCS_RAN -eq
Write-Host "Using Redis"
$dotnetArguments += " --azurecacheindex $($env:AZURE_CACHE_INDEX)"
$dotnetArguments += " --azurecacheendpoint $($env:AZURE_CACHE_SERVICE_ENDPOINT)"
$dotnetArguments += " --azurecacheprincipalid $($env:AZURE_CACHE_SERVICE_PRINCIPAL_ID)"
}

Write-Host "dotnet $dotnetArguments"
Expand Down
1 change: 1 addition & 0 deletions scripts/prepdocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if [ -z "$AZD_PREPDOCS_RAN" ] || [ "$AZD_PREPDOCS_RAN" = "false" ]; then
if [ "$USE_REDIS" = "true" ]; then
args="$args --azurecacheindex $AZURE_CACHE_INDEX"
args="$args --azurecacheendpoint $AZURE_CACHE_SERVICE_ENDPOINT"
args="$args --azurecacheprincipalid $AZURE_CACHE_SERVICE_PRINCIPAL_ID"
fi

args="$args --verbose"
Expand Down

0 comments on commit 9dabd90

Please sign in to comment.