diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 8edf04b..c146b54 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -31,7 +31,8 @@ "Bash(git:*)", "Bash(/tmp/windows-amd64/helm.exe template:*)", "Bash(echo \"exit=$?\")", - "Read(//tmp/**)" + "Read(//tmp/**)", + "Bash(tail:*)" ] } } diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml index 2251bb4..a7193a1 100644 --- a/.github/workflows/cicd.yaml +++ b/.github/workflows/cicd.yaml @@ -265,6 +265,9 @@ jobs: --set-string secrets.authSecret="${{ secrets.AUTH_SECRET_DEV }}" \ --set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_DEV }}" \ --set-string secrets.localAdminPassword="${{ secrets.LOCAL_ADMIN_PASSWORD_DEV }}" \ + --set-string secrets.entraClientId="${{ secrets.ENTRA_CLIENT_ID_DEV }}" \ + --set-string secrets.entraClientSecret="${{ secrets.ENTRA_CLIENT_SECRET_DEV }}" \ + --set-string secrets.entraIssuer="${{ secrets.ENTRA_ISSUER_DEV }}" \ --wait --timeout 10m --atomic --debug # ---- PROD DEPLOY (tag push only) ---- @@ -290,6 +293,9 @@ jobs: --set-string secrets.authSecret="${{ secrets.AUTH_SECRET_PROD }}" \ --set-string secrets.dbRootPassword="${{ secrets.DB_PASSWORD_PROD }}" \ --set-string secrets.localAdminPassword="${{ secrets.LOCAL_ADMIN_PASSWORD_PROD }}" \ + --set-string secrets.entraClientId="${{ secrets.ENTRA_CLIENT_ID_PROD }}" \ + --set-string secrets.entraClientSecret="${{ secrets.ENTRA_CLIENT_SECRET_PROD }}" \ + --set-string secrets.entraIssuer="${{ secrets.ENTRA_ISSUER_PROD }}" \ --wait --timeout 10m --atomic --debug # Strictly redundant with immutable image tags: Helm already rolled the diff --git a/helm/templates/server-deployment.yaml b/helm/templates/server-deployment.yaml index 85b11f4..d3447f5 100644 --- a/helm/templates/server-deployment.yaml +++ b/helm/templates/server-deployment.yaml @@ -50,13 +50,13 @@ spec: - secretRef: name: {{ include "it-cost-dashboard.fullname" . }}-server-secret # Readiness hits a DB-backed route on purpose: a pod only takes - # traffic once MySQL is reachable AND the schema is right. - # NOTE (SSO restore): /api/years will 307 to /login once the - # middleware is back on, which is non-2xx — add a public /api/health - # route and probe that instead. + # traffic once MySQL is reachable. /api/health is allow-listed in + # src/auth.config.ts, so the middleware lets it through unauthenticated + # instead of 307-ing it to /login (which would be non-2xx and would + # keep the pod permanently unready). readinessProbe: httpGet: - path: /api/years + path: /api/health port: http initialDelaySeconds: 10 periodSeconds: 5 diff --git a/helm/templates/server-secret.yaml b/helm/templates/server-secret.yaml index 49fb01e..d57d4ef 100644 --- a/helm/templates/server-secret.yaml +++ b/helm/templates/server-secret.yaml @@ -18,11 +18,13 @@ stringData: # Break-glass local login. Empty disables it entirely (src/auth.ts fails closed). LOCAL_ADMIN_PASSWORD: {{ .Values.secrets.localAdminPassword | default "" | quote }} - # --- SSO DISABLED FOR CICD TEST --- - # Kept present but optional. To restore SSO: supply these values from the - # per-env GitHub secrets and swap `| default ""` for `| required "..."`. - AUTH_MICROSOFT_ENTRA_ID_ID: {{ .Values.secrets.entraClientId | default "" | quote }} - AUTH_MICROSOFT_ENTRA_ID_SECRET: {{ .Values.secrets.entraClientSecret | default "" | quote }} - AUTH_MICROSOFT_ENTRA_ID_ISSUER: {{ .Values.secrets.entraIssuer | default "" | quote }} - AUTH_URL: {{ .Values.secrets.authUrl | default "" | quote }} - # --- END SSO DISABLED --- + # Microsoft Entra ID SSO. Required: a deploy fails loudly rather than shipping + # a build whose sign-in button 500s. + AUTH_MICROSOFT_ENTRA_ID_ID: {{ required "secrets.entraClientId is required" .Values.secrets.entraClientId | quote }} + AUTH_MICROSOFT_ENTRA_ID_SECRET: {{ required "secrets.entraClientSecret is required" .Values.secrets.entraClientSecret | quote }} + AUTH_MICROSOFT_ENTRA_ID_ISSUER: {{ required "secrets.entraIssuer is required" .Values.secrets.entraIssuer | quote }} + # Derived from the ingress host so the OAuth callback URL always matches what + # is registered in Entra. AUTH_TRUST_HOST alone would leave Auth.js depending + # on X-Forwarded-Proto; if that is ever wrong it builds an http:// callback and + # Entra rejects it with AADSTS50011 (http is invalid for non-localhost). + AUTH_URL: {{ printf "https://%s" (required "ingress.host is required" .Values.ingress.host) | quote }} diff --git a/helm/values-prod.yaml b/helm/values-prod.yaml index 976cde9..4db4e94 100644 --- a/helm/values-prod.yaml +++ b/helm/values-prod.yaml @@ -31,4 +31,4 @@ ingress: enabled: true className: nginx # TODO: point this at a hostname resolving to the ingress-nginx external IP. - host: it-cost-dashboard.trendmicro.com + host: it-cost-dashboard-prod.trendmicro.com diff --git a/helm/values.yaml b/helm/values.yaml index 097252b..6897857 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -50,12 +50,12 @@ secrets: dbRootPassword: "" # from GitHub secret DB_ROOT_PASSWORD_ # Optional; empty disables local login entirely (src/auth.ts fails closed). localAdminPassword: "" - # --- SSO restore: set these (and swap `default ""` for `required` in - # templates/server-secret.yaml) when Entra ID is re-enabled --- - # entraClientId: "" - # entraClientSecret: "" - # entraIssuer: "" - # authUrl: "" + # Microsoft Entra ID SSO. Supplied per-env by the CI workflow from GitHub + # secrets; server-secret.yaml marks them required, so a deploy fails loudly + # rather than shipping a build whose sign-in button 500s. + entraClientId: "" + entraClientSecret: "" + entraIssuer: "" # Pre-created manually, once per namespace (see the deploy notes). Not templated: # the kubelet needs it independently of Helm. diff --git a/src/app/api/health/route.ts b/src/app/api/health/route.ts new file mode 100644 index 0000000..5287f1c --- /dev/null +++ b/src/app/api/health/route.ts @@ -0,0 +1,18 @@ +import { NextResponse } from "next/server"; +import { prisma } from "@/lib/prisma"; + +// Public readiness endpoint (allow-listed in src/auth.config.ts). DB-backed on +// purpose: a pod should only take traffic once MySQL is actually reachable, +// which is what the old /api/years probe gave us before auth was restored. +// force-dynamic keeps Prisma from being invoked during `next build`, which runs +// without a DATABASE_URL in the Dockerfile builder stage. +export const dynamic = "force-dynamic"; + +export async function GET() { + try { + await prisma.$queryRaw`SELECT 1`; + return NextResponse.json({ status: "ok" }); + } catch { + return NextResponse.json({ status: "unavailable" }, { status: 503 }); + } +} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 01a2108..9f4a200 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -2,9 +2,7 @@ import type { Metadata } from "next"; import "./globals.css"; import TopNav from "@/components/TopNav"; import Sidebar from "@/components/Sidebar"; -// --- SSO DISABLED FOR CICD TEST --- -// import { auth } from "@/auth"; -// --- END SSO DISABLED --- +import { auth } from "@/auth"; export const metadata: Metadata = { title: "Global IT Cost Dashboard", @@ -16,25 +14,19 @@ export default async function RootLayout({ }: { children: React.ReactNode; }) { - // --- SSO DISABLED FOR CICD TEST --- - // const session = await auth(); - // - // // Unauthenticated (e.g. the /login page) -> render a bare shell with no - // // sidebar/topnav so the sign-in screen shows on its own. - // if (!session?.user) { - // return ( - // - // {children} - // - // ); - // } - // - // const isAdmin = session.user.role === "admin"; + const session = await auth(); - // Hardcoded so the sidebar/topnav still render — auth() would return null - // with SSO off and the whole UI chrome would vanish. - const isAdmin = true; - // --- END SSO DISABLED --- + // Unauthenticated (e.g. the /login page) -> render a bare shell with no + // sidebar/topnav so the sign-in screen shows on its own. + if (!session?.user) { + return ( + + {children} + + ); + } + + const isAdmin = session.user.role === "admin"; return ( diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 445c6e7..be388e2 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -14,13 +14,11 @@ function errorMessage(code: string | undefined): string | null { } } -// --- SSO DISABLED FOR CICD TEST --- // Server action: kick off the Microsoft SSO flow. -// async function microsoftLogin() { -// "use server"; -// await signIn("microsoft-entra-id", { redirectTo: "/" }); -// } -// --- END SSO DISABLED --- +async function microsoftLogin() { + "use server"; + await signIn("microsoft-entra-id", { redirectTo: "/" }); +} // Server action: local username/password login. async function credentialsLogin(formData: FormData) { @@ -65,7 +63,6 @@ export default async function LoginPage({ )} - {/* --- SSO DISABLED FOR CICD TEST ---
-
- - + {user ? ( +
+ {user.name ?? user.email}
{ "use server"; - await signIn("microsoft-entra-id"); + await signOut({ redirectTo: "/" }); }} >
- --- END SSO DISABLED --- */} -
- {user.name} -
+
+ ) : ( +
{ + "use server"; + await signIn("microsoft-entra-id"); + }} + > + +
+ )} ); } diff --git a/src/middleware.ts b/src/middleware.ts index aeb0757..1a06aa8 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,18 +1,10 @@ -// --- SSO DISABLED FOR CICD TEST --- -// Original: protect the whole app with role-aware auth in the edge runtime. -// import NextAuth from "next-auth"; -// import { authConfig } from "@/auth.config"; -// -// export const { auth: middleware } = NextAuth(authConfig); +import NextAuth from "next-auth"; +import { authConfig } from "@/auth.config"; -// No-op replacement. The EMPTY MATCHER below is the actual kill switch — this -// function is never invoked, and every route (including the three admin pages) -// is open. -export function middleware() {} +// Edge-runtime gate for the whole app. Uses the edge-safe config only — +// src/auth.ts pulls in node:fs via lookupRole and must never be imported here. +export const { auth: middleware } = NextAuth(authConfig); export const config = { - // Original matcher (restore with the lines above): - // matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.).*)"], - matcher: [], + matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.).*)"], }; -// --- END SSO DISABLED ---