diff --git a/src/Dapr.Actors.AspNetCore/ActorsEndpointRouteBuilderExtensions.cs b/src/Dapr.Actors.AspNetCore/ActorsEndpointRouteBuilderExtensions.cs index 13a77ec74..2fbbddddc 100644 --- a/src/Dapr.Actors.AspNetCore/ActorsEndpointRouteBuilderExtensions.cs +++ b/src/Dapr.Actors.AspNetCore/ActorsEndpointRouteBuilderExtensions.cs @@ -16,7 +16,7 @@ using Dapr.Actors; using Dapr.Actors.Communication; using Dapr.Actors.Runtime; -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; @@ -50,7 +50,7 @@ public static IEndpointConventionBuilder MapActorsHandlers(this IEndpointRouteBu MapActorMethodEndpoint(endpoints), MapReminderEndpoint(endpoints), MapTimerEndpoint(endpoints), - MapActorHealthChecks(endpoints), + MapActorHealthChecks(endpoints) }; return new CompositeEndpointConventionBuilder(builders); @@ -171,7 +171,7 @@ private static IEndpointConventionBuilder MapTimerEndpoint(this IEndpointRouteBu private static IEndpointConventionBuilder MapActorHealthChecks(this IEndpointRouteBuilder endpoints) { - var builder = endpoints.MapHealthChecks("/healthz"); + var builder = endpoints.MapHealthChecks("/healthz").WithMetadata(new AllowAnonymousAttribute()); builder.Add(b => { // Sets the route order so that this is matched with lower priority than an endpoint diff --git a/test/Dapr.Actors.AspNetCore.IntegrationTest/Dapr.Actors.AspNetCore.IntegrationTest.csproj b/test/Dapr.Actors.AspNetCore.IntegrationTest/Dapr.Actors.AspNetCore.IntegrationTest.csproj index 9582522cd..147481a80 100644 --- a/test/Dapr.Actors.AspNetCore.IntegrationTest/Dapr.Actors.AspNetCore.IntegrationTest.csproj +++ b/test/Dapr.Actors.AspNetCore.IntegrationTest/Dapr.Actors.AspNetCore.IntegrationTest.csproj @@ -19,6 +19,7 @@ + diff --git a/test/Dapr.Actors.AspNetCore.IntegrationTest/HostingTests.cs b/test/Dapr.Actors.AspNetCore.IntegrationTest/HostingTests.cs index 077ff3b39..508176faa 100644 --- a/test/Dapr.Actors.AspNetCore.IntegrationTest/HostingTests.cs +++ b/test/Dapr.Actors.AspNetCore.IntegrationTest/HostingTests.cs @@ -24,6 +24,8 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Xunit; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; using Xunit.Sdk; namespace Dapr.Actors.AspNetCore.IntegrationTest @@ -59,6 +61,17 @@ public async Task MapActorsHandlers_IncludesHealthChecks() await Assert2XXStatusAsync(response); } + [Fact] + public async Task ActorsHealthz_ShouldNotRequireAuthorization() + { + using var host = CreateHost(); + var server = host.GetTestServer(); + + var httpClient = server.CreateClient(); + var response = await httpClient.GetAsync("/healthz"); + await Assert2XXStatusAsync(response); + } + // We add our own health check on /healthz with worse priority than one // that would be added by a user. Make sure this works and the if the user // adds their own health check it will win. @@ -135,6 +148,28 @@ public void Configure(IApplicationBuilder app) } } + private class AuthorizedRoutesStartup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddActors(default); + services.AddAuthentication().AddDapr(options => options.Token = "abcdefg"); + + services.AddAuthorization(o => o.AddDapr()); + } + + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => + { + endpoints.MapActorsHandlers().RequireAuthorization("Dapr"); + }); + } + } + private class FallbackRouteStartup { public void ConfigureServices(IServiceCollection services)