Skip to content

Commit e07a12e

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Prioritize retrieval of environment variables from IConfiguration instead of directly (dapr#1363)
* Implemented against Dapr.Client.AspNetCore and Dapr.Client Signed-off-by: Whit Waldo <[email protected]> * SImplified DaprWorkflow DI registration and updated to use IConfiguration preference. Needs testing. Signed-off-by: Whit Waldo <[email protected]> * Added missing copyright header Signed-off-by: Whit Waldo <[email protected]> * Updated actor registration to prefer the updated IConfiguration-based approach for pulling the HTTP endpoint and API token Signed-off-by: Whit Waldo <[email protected]> * Adopted accepted proposal's guidelines for favoring different environment variables for determining the sidecar endpoint. Added notes to explain this in the code going forward. Signed-off-by: Whit Waldo <[email protected]> * Made some lines a little more concise, added hostname default to DaprDefaults to use when building endpoints. Signed-off-by: Whit Waldo <[email protected]> * Fixed and updated unit tests Signed-off-by: Whit Waldo <[email protected]> * Updated to put endpoint resolution mechanism in DaprDefaults within Dapr.Common - updating projects and unit tests Signed-off-by: Whit Waldo <[email protected]> * Updated packages to fix security advisory GHSA-447r-wph3-92pm Signed-off-by: Whit Waldo <[email protected]> * Updated Workflow builder to use DaprDefaults with IConfiguration Signed-off-by: Whit Waldo <[email protected]> * Updating global.json Signed-off-by: Whit Waldo <[email protected]> * Tweaked global.json comment Signed-off-by: Whit Waldo <[email protected]> * Adding braces per nit Signed-off-by: Whit Waldo <[email protected]> * Consolidated both registration extension methods to remove duplication Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]>
1 parent 453da80 commit e07a12e

21 files changed

+918
-265
lines changed

Directory.Packages.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
89
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
910
<PackageVersion Include="coverlet.msbuild" Version="6.0.2"/>
1011
<PackageVersion Include="FluentAssertions" Version="5.9.0" />
@@ -26,6 +27,7 @@
2627
<PackageVersion Include="Microsoft.DurableTask.Client.Grpc" Version="1.3.0" />
2728
<PackageVersion Include="Microsoft.DurableTask.Worker.Grpc" Version="1.3.0" />
2829
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
30+
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
2931
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
3032
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
3133
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0"/>
@@ -39,6 +41,7 @@
3941
<PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" />
4042
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />
4143
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
44+
<PackageVersion Include="System.Formats.Asn1" Version="6.0.1" />
4245
<PackageVersion Include="System.Text.Json" Version="6.0.10"/>
4346
<PackageVersion Include="xunit" Version="2.9.2" />
4447
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>

all.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Protos", "src\Dapr.Pro
122122
EndProject
123123
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Common\Dapr.Common.csproj", "{B445B19C-A925-4873-8CB7-8317898B6970}"
124124
EndProject
125+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}"
126+
EndProject
125127
Global
126128
GlobalSection(SolutionConfigurationPlatforms) = preSolution
127129
Debug|Any CPU = Debug|Any CPU
@@ -302,6 +304,10 @@ Global
302304
{B445B19C-A925-4873-8CB7-8317898B6970}.Debug|Any CPU.Build.0 = Debug|Any CPU
303305
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.ActiveCfg = Release|Any CPU
304306
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.Build.0 = Release|Any CPU
307+
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
308+
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
309+
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
310+
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
305311
EndGlobalSection
306312
GlobalSection(SolutionProperties) = preSolution
307313
HideSolutionNode = FALSE
@@ -357,6 +363,7 @@ Global
357363
{C74FBA78-13E8-407F-A173-4555AEE41FF3} = {A7F41094-8648-446B-AECD-DCC2CC871F73}
358364
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
359365
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
366+
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
360367
EndGlobalSection
361368
GlobalSection(ExtensibilityGlobals) = postSolution
362369
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}

global.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"_comment": "This policy allows the 7.0.101 SDK or patches in that family.",
2+
"_comment": "This policy allows the 8.0.100 SDK or patches in that family.",
33
"sdk": {
4-
"version": "7.0.101",
5-
"rollForward": "latestMajor"
4+
"version": "8.0.100",
5+
"rollForward": "minor"
66
}
77
}

src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,9 +11,13 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14+
#nullable enable
15+
1416
using System;
17+
using Dapr;
1518
using Dapr.Actors.Client;
1619
using Dapr.Actors.Runtime;
20+
using Microsoft.Extensions.Configuration;
1721
using Microsoft.Extensions.DependencyInjection.Extensions;
1822
using Microsoft.Extensions.Logging;
1923
using Microsoft.Extensions.Options;
@@ -30,12 +34,9 @@ public static class ActorsServiceCollectionExtensions
3034
/// </summary>
3135
/// <param name="services">The <see cref="IServiceCollection" />.</param>
3236
/// <param name="configure">A delegate used to configure actor options and register actor types.</param>
33-
public static void AddActors(this IServiceCollection services, Action<ActorRuntimeOptions> configure)
37+
public static void AddActors(this IServiceCollection? services, Action<ActorRuntimeOptions>? configure)
3438
{
35-
if (services is null)
36-
{
37-
throw new ArgumentNullException(nameof(services));
38-
}
39+
ArgumentNullException.ThrowIfNull(services, nameof(services));
3940

4041
// Routing and health checks are required dependencies.
4142
services.AddRouting();
@@ -45,6 +46,8 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
4546
services.TryAddSingleton<ActorRuntime>(s =>
4647
{
4748
var options = s.GetRequiredService<IOptions<ActorRuntimeOptions>>().Value;
49+
ConfigureActorOptions(s, options);
50+
4851
var loggerFactory = s.GetRequiredService<ILoggerFactory>();
4952
var activatorFactory = s.GetRequiredService<ActorActivatorFactory>();
5053
var proxyFactory = s.GetRequiredService<IActorProxyFactory>();
@@ -54,6 +57,8 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
5457
services.TryAddSingleton<IActorProxyFactory>(s =>
5558
{
5659
var options = s.GetRequiredService<IOptions<ActorRuntimeOptions>>().Value;
60+
ConfigureActorOptions(s, options);
61+
5762
var factory = new ActorProxyFactory()
5863
{
5964
DefaultOptions =
@@ -72,5 +77,16 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
7277
services.Configure<ActorRuntimeOptions>(configure);
7378
}
7479
}
80+
81+
private static void ConfigureActorOptions(IServiceProvider serviceProvider, ActorRuntimeOptions options)
82+
{
83+
var configuration = serviceProvider.GetService<IConfiguration>();
84+
options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken)
85+
? options.DaprApiToken
86+
: DaprDefaults.GetDefaultDaprApiToken(configuration);
87+
options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint)
88+
? options.HttpEndpoint
89+
: DaprDefaults.GetDefaultHttpEndpoint();
90+
}
7591
}
7692
}

src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
<ItemGroup>
1313
<ProjectReference Include="..\Dapr.Actors\Dapr.Actors.csproj" />
14+
<ProjectReference Include="..\Dapr.Common\Dapr.Common.csproj" />
1415
</ItemGroup>
1516
</Project>

src/Dapr.Actors/Client/ActorProxyOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public JsonSerializerOptions JsonSerializerOptions
4545
/// <summary>
4646
/// The Dapr Api Token that is added to the header for all requests.
4747
/// </summary>
48-
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken();
48+
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken(null);
4949

5050
/// <summary>
5151
/// Gets or sets the HTTP endpoint URI used to communicate with the Dapr sidecar.

src/Dapr.Actors/Dapr.Actors.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
1617
</ItemGroup>
1718

1819
</Project>

src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14+
#nullable enable
15+
1416
using System;
1517
using System.Text.Json;
1618

@@ -34,7 +36,7 @@ public sealed class ActorRuntimeOptions
3436
};
3537
private bool useJsonSerialization = false;
3638
private JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web;
37-
private string daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
39+
private string daprApiToken = string.Empty;
3840
private int? remindersStoragePartitions = null;
3941

4042
/// <summary>
@@ -180,19 +182,14 @@ public JsonSerializerOptions JsonSerializerOptions
180182

181183
set
182184
{
183-
if (value is null)
184-
{
185-
throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
186-
}
187-
188-
this.jsonSerializerOptions = value;
185+
this.jsonSerializerOptions = value ?? throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
189186
}
190187
}
191188

192189
/// <summary>
193190
/// The <see cref="DaprApiToken"/> to add to the headers in requests to Dapr runtime
194191
/// </summary>
195-
public string DaprApiToken
192+
public string? DaprApiToken
196193
{
197194
get
198195
{
@@ -201,12 +198,7 @@ public string DaprApiToken
201198

202199
set
203200
{
204-
if (value is null)
205-
{
206-
throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
207-
}
208-
209-
this.daprApiToken = value;
201+
this.daprApiToken = value ?? throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
210202
}
211203
}
212204

@@ -241,6 +233,6 @@ public int? RemindersStoragePartitions
241233
/// corresponding environment variables.
242234
/// </remarks>
243235
/// <value></value>
244-
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
236+
public string? HttpEndpoint { get; set; }
245237
}
246238
}

src/Dapr.AspNetCore/DaprAuthenticationOptions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14-
using System;
1514
using Microsoft.AspNetCore.Authentication;
1615

1716
namespace Dapr.AspNetCore
@@ -29,6 +28,6 @@ public class DaprAuthenticationOptions : AuthenticationSchemeOptions
2928
/// Gets or sets the App API token.
3029
/// By default, the token will be read from the APP_API_TOKEN environment variable.
3130
/// </summary>
32-
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken();
31+
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken(null);
3332
}
3433
}
Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,55 +11,76 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14-
namespace Microsoft.Extensions.DependencyInjection
15-
{
16-
using System;
17-
using System.Linq;
18-
using Dapr.Client;
19-
using Extensions;
14+
#nullable enable
15+
16+
namespace Microsoft.Extensions.DependencyInjection;
17+
18+
using System;
19+
using Dapr;
20+
using Dapr.Client;
21+
using Extensions;
22+
using Configuration;
2023

24+
/// <summary>
25+
/// Provides extension methods for <see cref="IServiceCollection" />.
26+
/// </summary>
27+
public static class DaprServiceCollectionExtensions
28+
{
2129
/// <summary>
22-
/// Provides extension methods for <see cref="IServiceCollection" />.
30+
/// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration
31+
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
2332
/// </summary>
24-
public static class DaprServiceCollectionExtensions
33+
/// <param name="services">The <see cref="IServiceCollection" />.</param>
34+
/// <param name="configure"></param>
35+
public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder>? configure = null)
2536
{
26-
/// <summary>
27-
/// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration
28-
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
29-
/// </summary>
30-
/// <param name="services">The <see cref="IServiceCollection" />.</param>
31-
/// <param name="configure"></param>
32-
public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder> configure = null)
37+
ArgumentNullException.ThrowIfNull(services, nameof(services));
38+
39+
services.TryAddSingleton(serviceProvider =>
3340
{
34-
ArgumentNullException.ThrowIfNull(services, nameof(services));
41+
var builder = CreateDaprClientBuilder(serviceProvider);
42+
configure?.Invoke(builder);
43+
return builder.Build();
44+
});
45+
}
3546

36-
services.TryAddSingleton(_ =>
37-
{
38-
var builder = new DaprClientBuilder();
39-
configure?.Invoke(builder);
47+
/// <summary>
48+
/// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
49+
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
50+
/// </summary>
51+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
52+
/// <param name="configure"></param>
53+
public static void AddDaprClient(this IServiceCollection services,
54+
Action<IServiceProvider, DaprClientBuilder> configure)
55+
{
56+
ArgumentNullException.ThrowIfNull(services, nameof(services));
4057

41-
return builder.Build();
42-
});
43-
}
44-
45-
/// <summary>
46-
/// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
47-
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
48-
/// </summary>
49-
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
50-
/// <param name="configure"></param>
51-
public static void AddDaprClient(this IServiceCollection services,
52-
Action<IServiceProvider, DaprClientBuilder> configure)
58+
services.TryAddSingleton(serviceProvider =>
5359
{
54-
ArgumentNullException.ThrowIfNull(services, nameof(services));
60+
var builder = CreateDaprClientBuilder(serviceProvider);
61+
configure?.Invoke(serviceProvider, builder);
62+
return builder.Build();
63+
});
64+
}
65+
66+
private static DaprClientBuilder CreateDaprClientBuilder(IServiceProvider serviceProvider)
67+
{
68+
var builder = new DaprClientBuilder();
69+
var configuration = serviceProvider.GetService<IConfiguration>();
70+
71+
// Set the HTTP endpoint, if provided, else use the default endpoint
72+
builder.UseHttpEndpoint(DaprDefaults.GetDefaultHttpEndpoint(configuration));
5573

56-
services.TryAddSingleton(serviceProvider =>
57-
{
58-
var builder = new DaprClientBuilder();
59-
configure?.Invoke(serviceProvider, builder);
74+
// Set the gRPC endpoint, if provided
75+
builder.UseGrpcEndpoint(DaprDefaults.GetDefaultGrpcEndpoint(configuration));
6076

61-
return builder.Build();
62-
});
77+
// Set the API token, if provided
78+
var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration);
79+
if (!string.IsNullOrWhiteSpace(apiToken))
80+
{
81+
builder.UseDaprApiToken(apiToken);
6382
}
83+
84+
return builder;
6485
}
6586
}

0 commit comments

Comments
 (0)