Skip to content

Commit d68d47d

Browse files
authored
Workflow api token fix (#1735)
* Changed visibility on method intended for testing only * Fix to add API token when using AddDaprWorkflow * Added support to use API token throughout Workflow project when communicating with runtime --------- Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
1 parent 85f1e7f commit d68d47d

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/Dapr.Workflow/DaprWorkflowClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ namespace Dapr.Workflow;
2828
public sealed class DaprWorkflowClient : IDaprWorkflowClient
2929
{
3030
private readonly WorkflowClient _innerClient;
31+
32+
/// <summary>
33+
/// Exposed only for testing.
34+
/// </summary>
35+
internal string? DaprApiToken { get; }
3136

3237
/// <summary>
3338
/// Initializes a new instance of the <see cref="DaprWorkflowClient"/> class.
3439
/// </summary>
3540
/// <param name="innerClient">The Durable Task client used to communicate with the Dapr sidecar.</param>
41+
/// <param name="daprApiToken">The API token used for validating requests to Dapr.</param>
3642
/// <exception cref="ArgumentNullException">Thrown if <paramref name="innerClient"/> is <c>null</c>.</exception>
37-
internal DaprWorkflowClient(WorkflowClient innerClient)
43+
internal DaprWorkflowClient(WorkflowClient innerClient, string? daprApiToken = null)
3844
{
3945
_innerClient = innerClient ?? throw new ArgumentNullException(nameof(innerClient));
46+
DaprApiToken = daprApiToken;
4047
}
4148

4249
/// <summary>

src/Dapr.Workflow/Registration/DaprWorkflowClientBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,6 @@ public override DaprWorkflowClient Build()
128128
}
129129

130130
var innerClient = new WorkflowGrpcClient(grpcClient, logger, serializer);
131-
return new DaprWorkflowClient(innerClient);
131+
return new DaprWorkflowClient(innerClient, DaprApiToken);
132132
}
133133
}

src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static DaprWorkflowBuilder AddDaprWorkflowBuilder(this IServiceCollection
132132
/// <param name="configure">Optional configuration for the workflow client (e.g., setting gRPC/HTTP endpoints).</param>
133133
/// <param name="lifetime">The lifetime of the registered services.</param>
134134
/// <returns>A builder for additional workflow configuration.</returns>
135-
public static DaprWorkflowBuilder AddDaprWorkflowClient(
135+
internal static DaprWorkflowBuilder AddDaprWorkflowClient(
136136
this IServiceCollection services,
137137
Action<IServiceProvider, DaprWorkflowClientBuilder>? configure = null,
138138
ServiceLifetime lifetime = ServiceLifetime.Singleton)

test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,31 @@ public void AddDaprWorkflowBuilder_ShouldApplyDaprApiToken_FromEnvironmentVariab
384384
Environment.SetEnvironmentVariable("DAPR_API_TOKEN", originalToken);
385385
}
386386
}
387+
388+
[Fact]
389+
public void AddDaprWorkflow_ShouldApplyDaprApiToken_FromEnvironmentVariable()
390+
{
391+
var originalToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");
392+
393+
try
394+
{
395+
Environment.SetEnvironmentVariable("DAPR_API_TOKEN", "workflow-env-token");
396+
397+
var services = new ServiceCollection();
398+
services.AddLogging(b => b.AddProvider(NullLoggerProvider.Instance));
399+
400+
services.AddDaprWorkflow(_ => { });
401+
402+
var sp = services.BuildServiceProvider();
403+
var client = sp.GetRequiredService<DaprWorkflowClient>();
404+
405+
Assert.Equal("workflow-env-token", client.DaprApiToken);
406+
}
407+
finally
408+
{
409+
Environment.SetEnvironmentVariable("DAPR_API_TOKEN", originalToken);
410+
}
411+
}
387412

388413
private sealed record SerializerDependency(string Value);
389414

0 commit comments

Comments
 (0)