Skip to content

Commit

Permalink
Using and fixing daprdefaults for workflow api token default
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
  • Loading branch information
RyanLettieri committed Sep 7, 2023
1 parent fa5ab83 commit bfa2b86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/Dapr.Workflow/Dapr.Workflow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<PackageReference Include="Microsoft.DurableTask.Worker.Grpc" Version="1.0.*" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Shared\DaprDefaults.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dapr.Client\Dapr.Client.csproj" />
<ProjectReference Include="..\Dapr.AspNetCore\Dapr.AspNetCore.csproj" />
Expand Down
19 changes: 11 additions & 8 deletions src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ namespace Dapr.Workflow
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Net.Http;
using Dapr;

/// <summary>
/// Contains extension methods for using Dapr Workflow with dependency injection.
/// </summary>
public static class WorkflowServiceCollectionExtensions
{
private static string daprApiToken = "";

/// <summary>
/// Adds Dapr Workflow support to the service collection.
/// </summary>
Expand Down Expand Up @@ -59,11 +62,11 @@ public static IServiceCollection AddDaprWorkflow(
if (TryGetGrpcAddress(out string address))
{
var apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
if (!string.IsNullOrEmpty(daprApiToken))
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
client.DefaultRequestHeaders.Add("Dapr-Api-Token", daprApiToken);
builder.UseGrpc(CreateChannel(address, client));
}
else
Expand Down Expand Up @@ -98,11 +101,11 @@ public static IServiceCollection AddDaprWorkflowClient(this IServiceCollection s
{
if (TryGetGrpcAddress(out string address))
{
var apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
if (!string.IsNullOrEmpty(apiToken))
daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
if (!string.IsNullOrEmpty(daprApiToken))
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
client.DefaultRequestHeaders.Add("Dapr-Api-Token", daprApiToken);
builder.UseGrpc(CreateChannel(address, client));
}
else
Expand All @@ -128,7 +131,7 @@ static bool TryGetGrpcAddress(out string address)
// 1. DaprDefaults.cs uses 127.0.0.1 instead of localhost, which prevents testing with Dapr on WSL2 and the app on Windows
// 2. DaprDefaults.cs doesn't compile when the project has C# nullable reference types enabled.
// If the above issues are fixed (ensuring we don't regress anything) we should switch to using the logic in DaprDefaults.cs.
var daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
var daprEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
if (!String.IsNullOrEmpty(daprEndpoint)) {
address = daprEndpoint;
return true;
Expand All @@ -155,7 +158,7 @@ static GrpcChannel CreateChannel(string address, HttpClient client)
{

GrpcChannelOptions options = new() { HttpClient = client};
var daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
var daprEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
if (!String.IsNullOrEmpty(daprEndpoint)) {
return GrpcChannel.ForAddress(daprEndpoint, options);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Shared/DaprDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace Dapr
{
internal static class DaprDefaults
{
private static string httpEndpoint;
private static string grpcEndpoint;
private static string daprApiToken;
private static string appApiToken;
private static string httpEndpoint = string.Empty;
private static string grpcEndpoint = string.Empty;
private static string daprApiToken = string.Empty;
private static string appApiToken = string.Empty;

/// <summary>
/// Get the value of environment variable DAPR_API_TOKEN
Expand All @@ -31,11 +31,11 @@ public static string GetDefaultDaprApiToken()
// Lazy-init is safe because this is just populating the default
// We don't plan to support the case where the user changes environment variables
// for a running process.
if (daprApiToken == null)
if (string.IsNullOrEmpty(daprApiToken))
{
// Treat empty the same as null since it's an environment variable
var value = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
daprApiToken = (value == string.Empty) ? null : value;
daprApiToken = string.IsNullOrEmpty(value) ? string.Empty : value;
}

return daprApiToken;
Expand All @@ -47,10 +47,10 @@ public static string GetDefaultDaprApiToken()
/// <returns>The value of environment variable APP_API_TOKEN</returns>
public static string GetDefaultAppApiToken()
{
if (appApiToken == null)
if (string.IsNullOrEmpty(appApiToken))
{
var value = Environment.GetEnvironmentVariable("APP_API_TOKEN");
appApiToken = (value == string.Empty) ? null : value;
appApiToken = string.IsNullOrEmpty(value) ? string.Empty : value;
}

return appApiToken;
Expand All @@ -62,7 +62,7 @@ public static string GetDefaultAppApiToken()
/// <returns>The value of HTTP endpoint based off environment variables</returns>
public static string GetDefaultHttpEndpoint()
{
if (httpEndpoint == null)
if (string.IsNullOrEmpty(httpEndpoint))
{
var endpoint = Environment.GetEnvironmentVariable("DAPR_HTTP_ENDPOINT");
if (!string.IsNullOrEmpty(endpoint)) {
Expand All @@ -84,7 +84,7 @@ public static string GetDefaultHttpEndpoint()
/// <returns>The value of gRPC endpoint based off environment variables</returns>
public static string GetDefaultGrpcEndpoint()
{
if (grpcEndpoint == null)
if (string.IsNullOrEmpty(grpcEndpoint))
{
var endpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT");
if (!string.IsNullOrEmpty(endpoint)) {
Expand Down

0 comments on commit bfa2b86

Please sign in to comment.