Fail fast on missing Keycloak config; remove UseHttpsRedirection (SOR.Admin)
- Validate Keycloak:Authority and Keycloak:ClientId at startup with a clear InvalidOperationException. Previously a missing env var produced the cryptic "Cannot redirect to the authorization endpoint" error on every request. - Remove app.UseHttpsRedirection(). The pod runs plain HTTP on :8080; TLS is terminated at the ingress. The middleware had no HTTPS port to redirect to, generating a warning on every request and potentially interfering with the OIDC flow. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -19,6 +19,13 @@ using System.Text.Json;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Fail fast with a clear message if Keycloak config is absent — prevents the
|
||||
// cryptic "Cannot redirect to authorization endpoint" runtime error.
|
||||
var keycloakAuthority = builder.Configuration["Keycloak:Authority"]
|
||||
?? throw new InvalidOperationException("Keycloak:Authority is required. Set via Keycloak__Authority env var.");
|
||||
var keycloakClientId = builder.Configuration["Keycloak:ClientId"]
|
||||
?? throw new InvalidOperationException("Keycloak:ClientId is required. Set via Keycloak__ClientId env var.");
|
||||
|
||||
// ── Blazor Server ──────────────────────────────────────────────────────────
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
@@ -51,8 +58,8 @@ builder.Services
|
||||
})
|
||||
.AddOpenIdConnect("Keycloak", options =>
|
||||
{
|
||||
options.Authority = builder.Configuration["Keycloak:Authority"];
|
||||
options.ClientId = builder.Configuration["Keycloak:ClientId"];
|
||||
options.Authority = keycloakAuthority;
|
||||
options.ClientId = keycloakClientId;
|
||||
options.ClientSecret = builder.Configuration["Keycloak:ClientSecret"];
|
||||
options.ResponseType = "code";
|
||||
options.SaveTokens = true;
|
||||
@@ -164,10 +171,11 @@ if (!app.Environment.IsDevelopment())
|
||||
ctx.Response.ContentType = "text/plain";
|
||||
await ctx.Response.WriteAsync("An internal server error occurred.");
|
||||
}));
|
||||
// HSTS only — no UseHttpsRedirection. The pod runs plain HTTP; TLS is
|
||||
// terminated at the ingress. Redirecting HTTP→HTTPS inside the pod would
|
||||
// loop forever because the pod never receives HTTPS requests.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
|
||||
Reference in New Issue
Block a user