-
Notifications
You must be signed in to change notification settings - Fork 375
Updating workflow collection to allow for use of API Token validation #1141
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
Changes from 1 commit
ee2e3ee
bad483a
b4a1095
db364f1
daaeaec
980017f
fa5ab83
bfa2b86
5704c07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -57,7 +59,19 @@ public static IServiceCollection AddDaprWorkflow( | |
|
|
||
| if (TryGetGrpcAddress(out string address)) | ||
| { | ||
| builder.UseGrpc(address); | ||
| string? apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, please use |
||
| if (!string.IsNullOrEmpty(apiToken)) | ||
| { | ||
| serviceCollection.ConfigureDurableGrpcClient(apiToken); | ||
| HttpClient client = new HttpClient(); | ||
|
halspang marked this conversation as resolved.
Outdated
|
||
| client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); | ||
| builder.UseGrpc(CreateChannel(client)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to repeat the code in the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| else | ||
| { | ||
| builder.UseGrpc(address); | ||
| } | ||
|
|
||
| } | ||
| else | ||
| { | ||
|
|
@@ -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(); | ||
|
halspang marked this conversation as resolved.
Outdated
|
||
| client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); | ||
| builder.UseGrpc(CreateChannel(client)); | ||
| } | ||
| else | ||
| { | ||
| builder.UseGrpc(address); | ||
| } | ||
|
|
||
| } | ||
| else | ||
| { | ||
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my understanding: Why do we need to add HttpClient while configuring Grpc Client?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under the hood, the gRPC client uses the HttpClient. It's all HTTP/2 for transport iirc. |
||
| .ConfigureHttpClient(client => | ||
| { | ||
| client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); | ||
| }); | ||
| return services; | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need the
using. After the initialization, you can just wrap thewhilein this: