Skip to content

Commit

Permalink
Proxy legacy api requests in api
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippluca committed Dec 7, 2023
1 parent b591d12 commit 17e6663
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ services:
ASPNETCORE_HTTP_PORTS: 5000
DOTNET_USE_POLLING_FILE_WATCHER: 1
CONNECTIONSTRINGS__BdmsContext: Host=db;Username=SPAWNPLOW;Password=YELLOWSPATULA;Database=bdms;CommandTimeout=300
ReverseProxy__Clusters__pythonApi__Destinations__legacyApi__Address: "http://api-legacy:8888/"
S3__ENDPOINT: http://minio:9000
S3__SECURE: 1
33 changes: 33 additions & 0 deletions src/api/Authentication/LegacyApiAuthenticationMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Security.Claims;

namespace BDMS.Authentication;

public class LegacyApiAuthenticationMiddleware : IMiddleware
{
private ILogger<LegacyApiAuthenticationMiddleware> logger;

public LegacyApiAuthenticationMiddleware(ILogger<LegacyApiAuthenticationMiddleware> logger)
{
this.logger = logger;
}

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
Claim? userName;
if (context.Request.Path.StartsWithSegments(new PathString("/api/v1"), StringComparison.OrdinalIgnoreCase))
{
userName = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name);
if (userName is not null)
{
context.Request.Headers.Authorization = userName.Value;

await next.Invoke(context).ConfigureAwait(false);

logger.LogInformation("Authorized user <{UserName}> for legacy api accessing route <{Route}>", userName.Value, context.Request.Path);
return;

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 27 in src/api/Authentication/LegacyApiAuthenticationMiddleware.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

}
}

await next.Invoke(context).ConfigureAwait(false);
}
}
1 change: 1 addition & 0 deletions src/api/BDMS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Test</_Parameter1>
</AssemblyAttribute>
Expand Down
7 changes: 7 additions & 0 deletions src/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
return new AmazonS3Client(accessKey, secretKey, clientConfig);
});

builder.Services.AddScoped<LegacyApiAuthenticationMiddleware>();
builder.Services
.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

// Migrate db changes on startup
Expand All @@ -140,7 +145,9 @@

app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<LegacyApiAuthenticationMiddleware>();

app.MapControllers();
app.MapReverseProxy();

app.Run();
11 changes: 11 additions & 0 deletions src/api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@
"ENDPOINT": "http://localhost:9000",
"SECRET_KEY": "YELLOWMONKEY",
"SECURE": "0"
},
"ReverseProxy": {
"Clusters": {
"pythonApi": {
"Destinations": {
"legacyApi": {
"Address": "http://localhost:8888/"
}
}
}
}
}
}
13 changes: 12 additions & 1 deletion src/api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"pythonApi": {
"ClusterId": "pythonApi",
"AuthorizationPolicy" : "anonymous",
"Match": {
"Path": "/api/v1/{**catch-all}"
}
}
}
}
}

0 comments on commit 17e6663

Please sign in to comment.