turn on SSO

This commit is contained in:
s871077
2026-09-21 18:42:32 +08:00
parent 137a4a2a39
commit 167eb0807d
12 changed files with 97 additions and 102 deletions
+2 -1
View File
@@ -31,7 +31,8 @@
"Bash(git:*)",
"Bash(/tmp/windows-amd64/helm.exe template:*)",
"Bash(echo \"exit=$?\")",
"Read(//tmp/**)"
"Read(//tmp/**)",
"Bash(tail:*)"
]
}
}
+6
View File
@@ -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
+5 -5
View File
@@ -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
+10 -8
View File
@@ -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 }}
+1 -1
View File
@@ -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
+6 -6
View File
@@ -50,12 +50,12 @@ secrets:
dbRootPassword: "" # from GitHub secret DB_ROOT_PASSWORD_<ENV>
# 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.
+18
View File
@@ -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 });
}
}
+13 -21
View File
@@ -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 (
// <html lang="en">
// <body>{children}</body>
// </html>
// );
// }
//
// 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 (
<html lang="en">
<body>{children}</body>
</html>
);
}
const isAdmin = session.user.role === "admin";
return (
<html lang="en">
+4 -8
View File
@@ -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({
</div>
)}
{/* --- SSO DISABLED FOR CICD TEST ---
<form action={microsoftLogin} className="mt-6">
<button
type="submit"
@@ -80,7 +77,6 @@ export default async function LoginPage({
OR
<span className="h-px flex-1 bg-[#E5E7EB]" />
</div>
--- END SSO DISABLED --- */}
<form action={credentialsLogin} className="mt-6 flex flex-col gap-3">
<label className="flex flex-col gap-1 text-sm text-[#374151]">
+2 -2
View File
@@ -35,8 +35,8 @@ export const authConfig = {
authorized({ auth, request }) {
const { pathname } = request.nextUrl;
// Always let the sign-in page through, even when unauthenticated.
if (pathname === "/login") return true;
// Public: the sign-in page and the k8s readiness probe.
if (pathname === "/login" || pathname === "/api/health") return true;
// Not signed in -> Auth.js redirects to the sign-in page.
if (!auth?.user) return false;
+6 -18
View File
@@ -1,22 +1,13 @@
// --- SSO DISABLED FOR CICD TEST ---
// import { auth, signIn, signOut } from "@/auth";
// --- END SSO DISABLED ---
import { auth, signIn, signOut } from "@/auth";
export default async function TopNav() {
// --- SSO DISABLED FOR CICD TEST ---
// const session = await auth();
// const user = session?.user;
const user = { name: "CI Test User" };
// --- END SSO DISABLED ---
const session = await auth();
const user = session?.user;
return (
<header className="flex h-14 items-center justify-between bg-[#111827] px-6 text-white">
<span className="text-lg font-semibold">Global IT Cost Dashboard</span>
{/* --- SSO DISABLED FOR CICD TEST ---
Original: `{user ? (<logged-in block/>) : (<login form/>)}`. Both forms
are server actions calling signOut/signIn, which would throw without
the Entra env vars, so only the name is rendered below.
{user ? (
<div className="flex items-center gap-3">
<span className="text-sm text-white/80">{user.name ?? user.email}</span>
<form
@@ -33,7 +24,7 @@ export default async function TopNav() {
</button>
</form>
</div>
) : (
<form
action={async () => {
"use server";
@@ -47,10 +38,7 @@ export default async function TopNav() {
Login
</button>
</form>
--- END SSO DISABLED --- */}
<div className="flex items-center gap-3">
<span className="text-sm text-white/80">{user.name}</span>
</div>
)}
</header>
);
}
+6 -14
View File
@@ -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 ---