GNMON-001 M6 abgenommen: Dashboard oeffentlich via mon.ghostnet.selfhost.eu (Keycloak-OIDC)

Kette: Browser -> https://mon.ghostnet.selfhost.eu (NPM CT-103 ID 14, LE-TLS,
Force SSL, WS-Support, X-Forwarded-Proto/Host + 32k-Puffer) -> 10.0.0.90:8080
(nftables gnmon_firewall: nur von 10.0.0.10) -> Blazor v0.3.2 -> Keycloak
Realm Ghost.net, Client gnai-dashboard, Rollen gnai-dashboard-user/-admin.

Neu/geaendert:
- Rolle gnmon_firewall (nftables auf ghostnet-ki, Policy ACCEPT, 8080 nur
  von NPM, Monitoring-Ports non-loopback-drop), Tag firewall in monitor.yml
- Dashboard-Bind 10.0.0.90:8080 (Assertion: LAN-Bind nur mit Firewall-Tag);
  Image v0.3.2; Playbook-Verifikation akzeptiert 200/302 mit Forwarded-Headern
- App: ClientId gnai-dashboard, Auth:AllowedRoles konfigurierbar,
  KnownNetworks/KnownProxies per .Clear() (leere Initializer = Loopback-Default),
  GetClaimsFromUserInfoEndpoint=true (realm-roles-Mapper-Flags im Realm unset),
  SaveTokens=true (Logout: id_token_hint), Cookie AccessDeniedPath
  /access-denied + Page (sonst Redirect-Loop), csproj PrunePackageData=false +
  Containerfile ohne --no-restore (sonst fehlt blazor.web.js im Image ->
  Blazor-Circuit startet nicht)
- KeycloakRealmRolesClaimTransformation auf ClaimTypes.Role + [gnmon-auth]-Log
- CT-100: Adoption 09.11. (pct exec), Split-DNS mon -> 10.0.0.10 live;
  Befunde gefixt: dns_pihole_hosts_records in allen 3 Inventaren gespiegelt,
  FTLCONF_dns_hosts als eine ';'-gejointe Environment-Zeile (systemd: letzter
  Key gewinnt); gnmon_alloy doppelte Key korrigiert
- Doku: GNMON-000 M6 abgenommen (alle Befunde), GNMON-001 v2.2, GNAI-000
  Monitoring/DNS-Zeilen, GNAI-006 v3.1 (Proxy-Host 14), GNAI-012 Stand

E2E 09.11.2026: unauth -> 302 mit PAR request_uri; Direktzugriff 10.0.0.90:8080
gedroppt; blazor.web.js 200 text/javascript; Login ghost -> Dashboard 200 mit
Live-Daten, Unterseiten + WebSocket ok; testuser ohne Rollen -> saubere 403;
/logout ok; monitor.yml gesamt ok=84 changed=0. Server-Image-Store aufgeraeumt
(v0.3.2 + Rollback v0.2.0/v0.1.0).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-11-09 19:34:50 +01:00
parent 148104de4e
commit fd8e1a5559
25 changed files with 383 additions and 90 deletions
@@ -0,0 +1,9 @@
@page "/access-denied"
@attribute [AllowAnonymous]
@layout MainLayout
<h3>Zugriff verweigert (403)</h3>
<p>Dein Keycloak-Account (Realm <code>Ghost.net</code>) hat keine der Realm-Rollen
<code>gnai-dashboard-user</code> oder <code>gnai-dashboard-admin</code>.</p>
<p>Bitte wende dich an den Betreiber, um eine der Rollen zugewiesen zu bekommen,
oder melde dich mit einem anderen Account an: <a href="/login">Login</a></p>
@@ -5,8 +5,13 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
<!-- SDK 10 prune package data is unavailable for the system-installed
runtime pack; irrelevant for framework-dependent builds. -->
<!-- .NET 10 package pruning dropped wwwroot/_framework/blazor.web.js from
the published output (it lives in the prunable Internal.Assets
package), so the container served 404 for blazor.web.js and the
Blazor circuit never started (Befund 09.11.2026). Disable pruning;
AllowMissingPrunePackageData is required because the system-installed
runtime pack ships no prune data (NETSDK1226 otherwise). -->
<PrunePackageData>false</PrunePackageData>
<AllowMissingPrunePackageData>true</AllowMissingPrunePackageData>
</PropertyGroup>
@@ -36,8 +36,14 @@ builder.Services.AddHttpClient<AlertmanagerClient>(client =>
// --- OIDC (GNMON-001 section 6, M6) ------------------------------------
// Auth is purely configuration-driven: disabled (workstation/local
// deployments) the DashboardAccess policy authorizes everyone; enabled
// the app requires a Keycloak login with the realm role dashboard-admin.
// the app requires a Keycloak login with a realm role ai-dashboard or
// ai-dashboard-admin (Betreiber-Namenskonvention im Realm Ghost.net).
var authEnabled = builder.Configuration.GetValue<bool>("Auth:Enabled");
// Realm roles that grant dashboard access (comma-separated, configurable
// so the Keycloak naming does not require an app rebuild).
var allowedRoles = (builder.Configuration["Auth:AllowedRoles"]
?? "ai-dashboard,ai-dashboard-admin")
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (authEnabled)
{
var authority = builder.Configuration["Auth:Authority"]
@@ -49,13 +55,16 @@ if (authEnabled)
var clientSecret = builder.Configuration["Auth:ClientSecret"]
?? throw new InvalidOperationException(
"Auth:Enabled requires the client secret via Auth:ClientSecret (environment)");
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
// Forbid after endpoint authorization lands on the COOKIE scheme
// (DefaultForbidScheme), so its AccessDeniedPath must point at the page —
// the OIDC default /Account/AccessDenied does not exist in this app and
// produced a redirect loop (Befund 09.11.2026).
.AddCookie(cookie => cookie.AccessDeniedPath = "/access-denied")
.AddOpenIdConnect(options =>
{
options.Authority = authority;
@@ -63,14 +72,31 @@ if (authEnabled)
options.ClientSecret = clientSecret;
options.ResponseType = "code"; // authorization code flow + PKCE
options.UsePkce = true;
options.SaveTokens = false;
options.GetClaimsFromUserInfoEndpoint = false;
// Keycloak realm roles live in realm_access.roles; map them onto
// role claims in addition to the claims transformation (the
// transformation also covers identities without a role claim).
options.TokenValidationParameters.RoleClaimType = "role";
// The redirect URI is derived from the request host; behind NPM the
// X-Forwarded-Host/X-Forwarded-Proto headers (see UseForwardedHeaders
// below) restore the external name mon.ghostnet.selfhost.eu, so the
// computed https://mon.ghostnet.selfhost.eu/signin-oidc matches the
// URI registered in Keycloak. .NET 10 has no RedirectUri property.
// SaveTokens is required so the OIDC sign-out redirect carries
// id_token_hint (Keycloak end_session rejects logout without it:
// "Missing parameters: id_token_hint", Befund 09.11.2026).
options.SaveTokens = true;
// Pull the full claim set (incl. realm_access.roles) from the
// userinfo endpoint: the realm-roles mapper's "Add to ID token" flag
// is not set in this realm, so the ID token alone carried no
// realm_access and authorization denied everyone (Befund 09.11.2026).
options.GetClaimsFromUserInfoEndpoint = true;
// Keycloak realm roles live in the realm_access.roles JSON claim;
// KeycloakRealmRolesClaimTransformation maps them onto role claims
// (ClaimTypes.Role, the default RoleClaimType — overriding it to
// "role" broke IsInRole because inbound claim mapping renames "role"
// to ClaimTypes.Role, Befund 09.11.2026).
options.Scope.Add("openid");
options.Scope.Add("profile");
// Clean 403 page instead of the default /Account/AccessDenied, which
// does not exist in this app and produced a redirect loop in the
// browser (Befund 09.11.2026).
options.AccessDeniedPath = "/access-denied";
});
builder.Services.AddSingleton<IClaimsTransformation, KeycloakRealmRolesClaimTransformation>();
@@ -82,8 +108,9 @@ builder.Services.AddAuthorization(options =>
{
if (authEnabled)
{
// Realm role from Ghost.net (see KeycloakRealmRolesClaimTransformation).
policy.RequireRole("dashboard-admin");
// Realm roles from Ghost.net (see KeycloakRealmRolesClaimTransformation),
// configurable via Auth:AllowedRoles.
policy.RequireRole(allowedRoles);
}
else
{
@@ -97,12 +124,21 @@ builder.Services.AddCascadingAuthenticationState();
var app = builder.Build();
// TLS terminates at NPM (CT-103); trust its forwarded headers so the
// OIDC redirect URIs and HTTPS checks see the external scheme/host.
app.UseForwardedHeaders(new ForwardedHeadersOptions
// TLS terminates at NPM (CT-103); trust its forwarded headers so the OIDC
// redirect URIs and HTTPS checks see the external scheme/host. Port 8080 is
// reachable only from 10.0.0.10 anyway (gnmon_firewall), so clearing the
// known-proxy lists is safe here. NOTE: KnownNetworks/KnownProxies must be
// .Clear()ed — a `{ }` collection initializer keeps the default loopback
// entry, non-loopback proxies are then rejected and the redirect_uri is
// computed from the internal 10.0.0.90:8080 host (PAR: "Invalid parameter:
// redirect_uri", Befund 09.11.2026).
var forwardedHeaders = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
});
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
};
forwardedHeaders.KnownNetworks.Clear();
forwardedHeaders.KnownProxies.Clear();
app.UseForwardedHeaders(forwardedHeaders);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
@@ -6,8 +6,10 @@ namespace Ghostnet.Dashboard.Services;
/// <summary>
/// Maps Keycloak realm roles (the realm_access.roles JSON claim) onto
/// role claims so [Authorize]/RequireRole work with the standard OIDC
/// handler (GNMON-001 section 6: realm role dashboard-admin).
/// ClaimTypes.Role claims so [Authorize]/RequireRole work with the standard
/// OIDC handler (GNMON-001 section 6: realm roles gnai-dashboard-user /
/// gnai-dashboard-admin in realm Ghost.net). Fail closed: a malformed
/// realm_access claim adds no roles and authorization denies access.
/// </summary>
public sealed class KeycloakRealmRolesClaimTransformation : IClaimsTransformation
{
@@ -16,14 +18,12 @@ public sealed class KeycloakRealmRolesClaimTransformation : IClaimsTransformatio
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
if (principal.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
{
return Task.FromResult(principal);
}
// M6 diagnostic: always log invocation + claim inventory (claim types
// only, no values) so a missing realm_access becomes visible.
var types = principal.Claims.Select(c => c.Type).Distinct().OrderBy(t => t).ToList();
Console.WriteLine($"[gnmon-auth] transform called; authenticated={principal.Identity?.IsAuthenticated}; claim_types={string.Join(",", types)}");
// Skip when the identity already carries a usable role claim.
if (identity.HasClaim(claim =>
claim.Type == identity.RoleClaimType && !string.IsNullOrEmpty(claim.Value)))
if (principal.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
{
return Task.FromResult(principal);
}
@@ -31,9 +31,11 @@ public sealed class KeycloakRealmRolesClaimTransformation : IClaimsTransformatio
var realmAccess = principal.FindFirst(RealmAccessClaim)?.Value;
if (string.IsNullOrWhiteSpace(realmAccess))
{
Console.WriteLine("[gnmon-auth] realm_access claim MISSING");
return Task.FromResult(principal);
}
var mapped = new List<string>();
try
{
using var document = JsonDocument.Parse(realmAccess);
@@ -44,17 +46,23 @@ public sealed class KeycloakRealmRolesClaimTransformation : IClaimsTransformatio
var value = role.GetString();
if (!string.IsNullOrEmpty(value))
{
identity.AddClaim(new Claim(identity.RoleClaimType, value));
// Always ClaimTypes.Role: that is the identity's
// RoleClaimType under the default token validation
// parameters, so IsInRole finds these claims.
identity.AddClaim(new Claim(ClaimTypes.Role, value));
mapped.Add(value);
}
}
}
}
catch (JsonException)
{
// Malformed realm_access: fall through without roles; the
// authorization policy denies access (fail closed).
// Malformed realm_access: no roles added; the authorization
// policy denies access (fail closed).
}
// Role names are not secret; log them for M6 acceptance evidence.
Console.WriteLine($"[gnmon-auth] user={identity.Name} realm_roles={string.Join(",", mapped)}");
return Task.FromResult(principal);
}
}
@@ -9,7 +9,7 @@
"Auth": {
"Enabled": false,
"Authority": "https://auth.ghostnet.selfhost.eu/realms/Ghost.net",
"ClientId": "gn-dashboard"
"ClientId": "gnai-dashboard"
},
"Monitoring": {
"VictoriaMetrics": "http://127.0.0.1:8428",