Skip to content
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

Updating workflow collection to allow for use of API Token validation #1141

Merged
merged 9 commits into from
Sep 7, 2023

Conversation

RyanLettieri
Copy link
Contributor

@RyanLettieri RyanLettieri commented Aug 30, 2023

Description

Updated the service collection for workflow in the .NET SDK to allow for the use of api token authentication. Since the durable task framework allows for the passing in of a gRPC channel when configuring the gRPC client, we now create a gRPC channel object to have the API Token headers as well as the address we want to use and pass that through to the DTFx.

Issue reference

The issue this PR will close: #[1134]

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • [x ] Code compiles correctly
  • Created/updated tests
  • Extended the documentation

Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
@RyanLettieri RyanLettieri requested review from a team as code owners August 30, 2023 19:17
@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Patch coverage: 80.00% and project coverage change: +0.02% 🎉

Comparison is base (fd7168f) 66.45% compared to head (5704c07) 66.47%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1141      +/-   ##
==========================================
+ Coverage   66.45%   66.47%   +0.02%     
==========================================
  Files         171      171              
  Lines        5750     5754       +4     
  Branches      624      624              
==========================================
+ Hits         3821     3825       +4     
  Misses       1782     1782              
  Partials      147      147              
Flag Coverage Δ
net6 66.47% <80.00%> (+0.02%) ⬆️
net7 66.47% <80.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/Shared/DaprDefaults.cs 66.66% <80.00%> (+5.12%) ⬆️

☔ View full report in Codecov by Sentry.

📢 Have feedback on the report? Share it here.


public static IServiceCollection ConfigureDurableGrpcClient(this IServiceCollection services, string apiToken)
{
services.AddHttpClient(ClientName)

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@halspang halspang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the example readme with how to use the API token?

Comment on lines +50 to +59
DaprClient daprClient;
string apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
{
daprClient = new DaprClientBuilder().UseDaprApiToken(apiToken).Build();
}
else
{
daprClient = new DaprClientBuilder().Build();
}
Copy link
Contributor

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 the while in this:

using (daprClient)
{
    // Loop here.
}

@@ -57,7 +59,19 @@ public static class WorkflowServiceCollectionExtensions

if (TryGetGrpcAddress(out string address))
{
builder.UseGrpc(address);
string? apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, please use var where you can.

Comment on lines 65 to 68
serviceCollection.ConfigureDurableGrpcClient(apiToken);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
builder.UseGrpc(CreateChannel(client));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to repeat the code in the ConfigureDurableGrpcClient do we need both methods?

Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
// serviceCollection.ConfigureDurableGrpcClient(apiToken);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
builder.UseGrpc(CreateChannel(client));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The address variable should be passed to the builder, otherwise if users configured Dapr to use a non-localhost endpoint, this will not be propagated correctly to the underlying Durable Task client. Good find by @alicejgibbons

Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Copy link
Contributor

@halspang halspang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question and a few nits. Did you ensure that the various places where we call the DaprDefault methods all handle an empty/null case instead of just a null case? It shouldn't be too many places to check.

src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated Show resolved Hide resolved
src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated Show resolved Hide resolved
src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated Show resolved Hide resolved
@halspang halspang merged commit 87329f6 into dapr:master Sep 7, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants