Skip to content

Commit

Permalink
Updating workflow collection to allow for use of API Token validation
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
  • Loading branch information
RyanLettieri committed Aug 30, 2023
1 parent 17f849b commit ee2e3ee
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
11 changes: 10 additions & 1 deletion examples/Workflow/WorkflowConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@
using var host = builder.Build();
host.Start();

using var daprClient = new DaprClientBuilder().Build();
DaprClient daprClient;
string apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
{
daprClient = new DaprClientBuilder().UseDaprApiToken(apiToken).Build();
}
else
{
daprClient = new DaprClientBuilder().Build();
}

// Wait for the sidecar to become available
while (!await daprClient.CheckHealthAsync())
Expand Down
64 changes: 62 additions & 2 deletions src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
namespace Dapr.Workflow
{
using System;
using Grpc.Net.Client;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Net.Http;

/// <summary>
/// Contains extension methods for using Dapr Workflow with dependency injection.
Expand Down Expand Up @@ -57,7 +59,19 @@ public static IServiceCollection AddDaprWorkflow(
if (TryGetGrpcAddress(out string address))
{
builder.UseGrpc(address);
string? apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
{
serviceCollection.ConfigureDurableGrpcClient(apiToken);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
builder.UseGrpc(CreateChannel(client));
}
else
{
builder.UseGrpc(address);
}
}
else
{
Expand Down Expand Up @@ -85,7 +99,19 @@ public static IServiceCollection AddDaprWorkflowClient(this IServiceCollection s
{
if (TryGetGrpcAddress(out string address))
{
builder.UseGrpc(address);
string? apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
{
services.ConfigureDurableGrpcClient(apiToken);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
builder.UseGrpc(CreateChannel(client));
}
else
{
builder.UseGrpc(address);
}
}
else
{
Expand Down Expand Up @@ -126,6 +152,40 @@ static bool TryGetGrpcAddress(out string address)
address = string.Empty;
return false;
}

static GrpcChannel CreateChannel(HttpClient client)
{
string address = "localhost";
string? daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
if (int.TryParse(daprPortStr, out int daprGrpcPort))
{
// There is a bug in the Durable Task SDK that requires us to change the format of the address
// depending on the version of .NET that we're targeting. For now, we work around this manually.
#if NET6_0_OR_GREATER
address = $"http://localhost:{daprGrpcPort}";
#else
address = $"localhost:{daprGrpcPort}";
#endif
}
GrpcChannelOptions options = new() { HttpClient = client};
return GrpcChannel.ForAddress(address, options);
}
}

static class DaprDurableGrpcExtensions
{
const string ClientName = "durabletask-grpc";

public static IServiceCollection ConfigureDurableGrpcClient(this IServiceCollection services, string apiToken)
{
services.AddHttpClient(ClientName)
.ConfigureHttpClient(client =>
{
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
});
return services;
}

}
}

0 comments on commit ee2e3ee

Please sign in to comment.