Skip to content

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

Merged
halspang merged 9 commits into
dapr:masterfrom
RyanLettieri:workflow-api-token-fix
Sep 7, 2023
Merged

Updating workflow collection to allow for use of API Token validation#1141
halspang merged 9 commits into
dapr:masterfrom
RyanLettieri:workflow-api-token-fix

Conversation

@RyanLettieri
Copy link
Copy Markdown
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
Copy Markdown

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)
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.
}

if (TryGetGrpcAddress(out string address))
{
builder.UseGrpc(address);
string? apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
Copy link
Copy Markdown
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
Copy Markdown
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?

@RyanLettieri RyanLettieri force-pushed the workflow-api-token-fix branch from 8723c3b to b4a1095 Compare September 5, 2023 19:56
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
Copy Markdown
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>
Comment thread src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated
Copy link
Copy Markdown
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.

Comment thread src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated
Comment thread src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated
Comment thread src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs Outdated
@halspang halspang merged commit 87329f6 into dapr:master Sep 7, 2023
divzi-p pushed a commit to divzi-p/dotnet-sdk that referenced this pull request Dec 10, 2024
…dapr#1141)

Updating workflow collection to allow for use of API Token validation

Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
Signed-off-by: Divya Perumal <diperuma@microsoft.com>
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