From 63f25a3764bdd7f607661d0f3e164ef0929b848e Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 4 Apr 2026 12:28:05 -0400 Subject: [PATCH] Log actual OIDC discovery failure at startup (SOR.Admin) Eagerly fetch the Keycloak discovery document after app.Build() so any network/DNS/SSL failure appears in startup logs with the real exception and the exact URL being fetched, rather than surfacing later as the cryptic "Cannot redirect to the authorization endpoint" message. 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 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Server/src/SOR.Admin/Program.cs b/Server/src/SOR.Admin/Program.cs index de4f3e9..88de330 100644 --- a/Server/src/SOR.Admin/Program.cs +++ b/Server/src/SOR.Admin/Program.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -223,6 +224,12 @@ app.MapRazorComponents() // ── Apply pending migrations ─────────────────────────────────────────────── await MigrateAllAsync(app); +// ── Eager OIDC discovery ─────────────────────────────────────────────────── +// Fetch the Keycloak discovery document at startup so any network/DNS failure +// surfaces as a clear log error rather than the cryptic "Cannot redirect to +// the authorization endpoint" message on the first browser request. +await CheckOidcDiscoveryAsync(app); + app.Run(); // ── Helpers ──────────────────────────────────────────────────────────────── @@ -240,6 +247,27 @@ static async Task MigrateAllAsync(WebApplication app) await sp.GetRequiredService().Database.MigrateAsync(); } +static async Task CheckOidcDiscoveryAsync(WebApplication app) +{ + var opts = app.Services + .GetRequiredService>() + .Get("Keycloak"); + var url = opts.MetadataAddress + ?? $"{opts.Authority}/.well-known/openid-configuration"; + try + { + await opts.ConfigurationManager!.GetConfigurationAsync(CancellationToken.None); + app.Logger.LogInformation("Keycloak OIDC discovery OK: {Url}", url); + } + catch (Exception ex) + { + app.Logger.LogError(ex, + "Keycloak OIDC discovery FAILED from {Url}. " + + "If the pod cannot reach the public auth domain, set " + + "Keycloak__MetadataAddress to the internal k8s service URL.", url); + } +} + // Decodes the Keycloak access token and adds realm_access.roles and groups // as ClaimTypes.Role claims. Called from OnTicketReceived where SaveTokens // has already run, so the token is guaranteed present in auth properties.