Skip to content

Commit

Permalink
Add custom request transformation in AppGateway that blocks all publi…
Browse files Browse the repository at this point in the history
…c traffic to */internal-api/*
  • Loading branch information
tjementum committed Jul 1, 2024
1 parent 9e463f1 commit cd5799a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions application/AppGateway/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
);
}

builder.Services.AddSingleton<BlockInternalApiTransform>();
reverseProxyBuilder.AddTransforms(context =>
context.RequestTransforms.Add(context.Services.GetRequiredService<BlockInternalApiTransform>())
);

builder.Services.AddNamedBlobStorages(builder, ("avatars-storage", "AVATARS_STORAGE_URL"));

builder.WebHost.UseKestrel(option => option.AddServerHeader = false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Yarp.ReverseProxy.Transforms;

namespace PlatformPlatform.AppGateway.Transformations;

public class BlockInternalApiTransform : RequestTransform
{
public override async ValueTask ApplyAsync(RequestTransformContext context)
{
if (context.HttpContext.Request.Path.Value?.Contains("/internal-api/", StringComparison.OrdinalIgnoreCase) == true)
{
context.HttpContext.Response.StatusCode = StatusCodes.Status403Forbidden;
context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync("Access to internal API is forbidden.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public class ManagedIdentityTransform(TokenCredential credential)
{
protected override string? GetValue(RequestTransformContext context)
{
if (!context.HttpContext.Request.Path.StartsWithSegments("/avatars")) return null;
if (!context.HttpContext.Request.Path.StartsWithSegments("/avatars", StringComparison.OrdinalIgnoreCase))
{
return null;
}

var tokenRequestContext = new TokenRequestContext(["https://storage.azure.com/.default"]);
var token = credential.GetToken(tokenRequestContext, context.HttpContext.RequestAborted);
Expand Down

0 comments on commit cd5799a

Please sign in to comment.