Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit e94f4e7

Browse files
committed
Return 500 error from incorrectly configured yarp endpoints
1 parent 486a01f commit e94f4e7

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Duende.Bff.Yarp/AccessTokenTransformProvider.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
8+
using System.Threading.Tasks;
89
using Duende.AccessTokenManagement;
10+
using Duende.Bff.Logging;
911
using Microsoft.Extensions.Logging;
1012
using Microsoft.Extensions.Options;
1113
using Yarp.ReverseProxy.Transforms;
@@ -19,18 +21,21 @@ namespace Duende.Bff.Yarp;
1921
public class AccessTokenTransformProvider : ITransformProvider
2022
{
2123
private readonly BffOptions _options;
24+
private readonly ILogger<AccessTokenTransformProvider> _logger;
2225
private readonly ILoggerFactory _loggerFactory;
2326
private readonly IDPoPProofService _dPoPProofService;
2427

2528
/// <summary>
2629
/// ctor
2730
/// </summary>
2831
/// <param name="options"></param>
32+
/// <param name="logger"></param>
2933
/// <param name="loggerFactory"></param>
3034
/// <param name="dPoPProofService"></param>
31-
public AccessTokenTransformProvider(IOptions<BffOptions> options, ILoggerFactory loggerFactory, IDPoPProofService dPoPProofService)
35+
public AccessTokenTransformProvider(IOptions<BffOptions> options, ILogger<AccessTokenTransformProvider> logger, ILoggerFactory loggerFactory, IDPoPProofService dPoPProofService)
3236
{
3337
_options = options.Value;
38+
_logger = logger;
3439
_loggerFactory = loggerFactory;
3540
_dPoPProofService = dPoPProofService;
3641
}
@@ -78,10 +83,19 @@ public void Apply(TransformBuilderContext transformBuildContext)
7883
bool optional;
7984
if(GetMetadataValue(transformBuildContext, Constants.Yarp.OptionalUserTokenMetadata, out var optionalTokenMetadata))
8085
{
86+
if (GetMetadataValue(transformBuildContext, Constants.Yarp.TokenTypeMetadata, out var tokenTypeMetadata))
87+
{
88+
transformBuildContext.AddRequestTransform(ctx =>
89+
{
90+
ctx.HttpContext.Response.StatusCode = 500;
91+
_logger.InvalidRouteConfiguration(transformBuildContext.Route.ClusterId, transformBuildContext.Route.RouteId);
92+
93+
return ValueTask.CompletedTask;
94+
});
95+
return;
96+
}
8197
optional = true;
8298
tokenType = TokenType.User;
83-
// TODO - is it an error to set both OptionalUserToken and a token type? I think yes, because setting a token type means
84-
// setting a *required* token type.
8599
}
86100
else if (GetMetadataValue(transformBuildContext, Constants.Yarp.TokenTypeMetadata, out var tokenTypeMetadata))
87101
{

src/Duende.Bff/General/Log.cs

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal static class EventIds
1818
public static readonly EventId BackChannelLogout = new (2, "BackChannelLogout");
1919
public static readonly EventId BackChannelLogoutError = new (3, "BackChannelLogoutError");
2020
public static readonly EventId AccessTokenMissing = new (4, "AccessTokenMissing");
21+
public static readonly EventId InvalidRouteConfiguration = new (5, "InvalidRouteConfiguration");
2122
}
2223

2324
internal static class Log
@@ -42,6 +43,10 @@ internal static class Log
4243
EventIds.AccessTokenMissing,
4344
"Access token is missing. token type: '{tokenType}', local path: '{localpath}', detail: '{detail}'");
4445

46+
private static readonly Action<ILogger, string, string, Exception?> _invalidRouteConfiguration = LoggerMessage.Define<string, string>(
47+
LogLevel.Warning,
48+
EventIds.InvalidRouteConfiguration,
49+
"Invalid route configuration. Cannot combine a required access token (a call to WithAccessToken) and an optional access token (a call to WithOptionalUserAccessToken). clusterId: '{clusterId}', routeId: '{routeId}'");
4550

4651
public static void AntiForgeryValidationFailed(this ILogger logger, string localPath)
4752
{
@@ -62,4 +67,9 @@ public static void AccessTokenMissing(this ILogger logger, string tokenType, str
6267
{
6368
_accessTokenMissing(logger, tokenType, localPath, detail, null);
6469
}
70+
71+
public static void InvalidRouteConfiguration(this ILogger logger, string? clusterId, string routeId)
72+
{
73+
_invalidRouteConfiguration(logger, clusterId ?? "no cluster id", routeId, null);
74+
}
6575
}

0 commit comments

Comments
 (0)