From bea1de3eb8e451359760d59d96505fd88241abdd Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 4 Apr 2026 11:32:13 -0400 Subject: [PATCH] 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 Co-Authored-By: Happy --- Server/src/SOR.Admin/Program.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Server/src/SOR.Admin/Program.cs b/Server/src/SOR.Admin/Program.cs index 14d353f..e4d863c 100644 --- a/Server/src/SOR.Admin/Program.cs +++ b/Server/src/SOR.Admin/Program.cs @@ -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();