diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 0affde8..b1d4098 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -34,7 +34,14 @@ "Read(//tmp/**)", "Bash(tail:*)", "Bash(mkdir:*)", - "Bash(python3:*)" + "Bash(python3:*)", + "Bash(npx tsc:*)", + "Bash(ls node_modules/.bin/tsc* 2>/dev/null || echo \"no local tsc\"; ls node_modules 2>/dev/null | head -3)", + "Bash(npm ci:*)", + "Bash(./node_modules/.bin/prisma generate:*)", + "Bash(npm run:*)", + "Bash(cd \"/c/Users/tom_chien/Desktop/it_cost_dashboad.git_V02\" && sed -i 's/\\\\${JFROG_REGISTRY}/\\\\${REGISTRY}/g' .gitea/workflows/cicd.yaml && grep -n \"JFROG\\\\|REGISTRY\" .gitea/workflows/cicd.yaml)", + "Bash(cd \"/c/Users/tom_chien/Desktop/it_cost_dashboad.git_V02\" && grep -n \"REGISTRY\\\\|harbor\\\\|Harbor\" .gitea/workflows/cicd.yaml && helm lint ./helm -f helm/values-dev.yaml --set server.image.repository=x --set server.image.tag=t --set db.image.repository=y --set db.image.tag=t --set secrets.authSecret=a --set secrets.dbRootPassword=b --set secrets.localAdminPassword=c 2>&1 | tail -5)" ] } } diff --git a/.dockerignore b/.dockerignore index 387291a..746447b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,7 @@ # Version control .git .gitignore -.github +.gitea # Dependencies / build output (rebuilt inside the image) node_modules @@ -19,9 +19,9 @@ prisma/dev.db prisma/dev.db-journal prisma/*.db -# Secrets: .env holds file:./dev.db, and .env.local / .env.production hold REAL -# Entra secrets and the RDS password. Baking these into a layer would push them -# to JFrog. All runtime env comes from the k8s Secret instead. +# Secrets: .env holds file:./dev.db, and .env.local / .env.production hold the +# real admin password and the RDS password. Baking these into a layer would push +# them to Harbor. All runtime env comes from the k8s Secret instead. .env .env.* !.env.production.example diff --git a/.env b/.env index e9494fc..d8997bb 100644 --- a/.env +++ b/.env @@ -3,13 +3,12 @@ DATABASE_URL="file:./dev.db" DATABASE_PROVIDER="sqlite" -# SSO secrets are NOT stored here (this file is committed). Dev auth vars live in -# .env.local (gitignored); prod auth vars in .env.production — see .env.production.example. +# Auth secrets are NOT stored here (this file is committed). Dev auth vars live +# in .env.local (gitignored); prod auth vars in .env.production. -# Local break-glass admin login (username/password, in addition to SSO). The -# password is intentionally EMPTY here so local login is DISABLED by default; -# set a real value in the gitignored .env.local (dev) / .env.production (prod). -# Role model: config/users.json maps SSO emails -> "admin" | "user"; unlisted -# SSO emails cannot sign in. The local admin always has the admin role. +# Local admin login (username/password) — the only sign-in path. The password is +# intentionally EMPTY here so login is DISABLED by default; set a real value in +# the gitignored .env.local (dev) / .env.production (prod). The local admin +# always has the admin role. LOCAL_ADMIN_USERNAME="admin" LOCAL_ADMIN_PASSWORD="" diff --git a/.gitea/workflows/cicd.yaml b/.gitea/workflows/cicd.yaml new file mode 100644 index 0000000..76af792 --- /dev/null +++ b/.gitea/workflows/cicd.yaml @@ -0,0 +1,252 @@ +name: CI/CD (dev on branch, prod on tag) + +on: + push: + branches: + - dev # build & deploy for dev only + tags: + - "*" # build & deploy for prod only when a tag is pushed + +jobs: + build-and-deploy: + # Gitea Actions: runners are selected by label only (no runner groups). + runs-on: ubuntu-latest + + # Per-environment group: a dev push must never cancel an in-flight prod + # deploy. NOTE: act_runner does not support dynamic matrix (fromJSON of a + # job output expands every matrix.* to ""), so per-env config is computed + # in the "Resolve environment config" bash step below instead. + concurrency: + group: deploy-${{ github.ref_type == 'tag' && 'prod' || 'dev' }} + cancel-in-progress: true + + env: + # Harbor on the Mac mini host. The runner pod mounts the Docker Desktop + # daemon socket, so from the daemon's point of view the registry is + # reachable as host.docker.internal:8080 (plain HTTP — the daemon must + # list it under "insecure-registries"). Images live in Harbor's + # "library" project. + REGISTRY: host.docker.internal:8080/library + DOCKER_BUILDKIT: "1" + COMPOSE_DOCKER_CLI_BUILD: "1" + + steps: + - name: Checkout (with tags) + uses: actions/checkout@v4 + with: + fetch-depth: 0 # IMPORTANT to access tags + + # Replaces the former setup job + dynamic matrix (unsupported on + # act_runner). Everything derived from the ref type lands in GITHUB_ENV + # so later steps can use plain $VARS. + - name: Resolve environment config + shell: bash + run: | + if [[ "${{ github.ref_type }}" == "tag" ]]; then + DEPLOY_ENV=prod + RELEASE_TAG="${GITHUB_REF#refs/tags/}" + else + DEPLOY_ENV=dev + RELEASE_TAG="" + fi + { + echo "DEPLOY_ENV=${DEPLOY_ENV}" + echo "RELEASE_TAG=${RELEASE_TAG}" + echo "IMAGE_SERVER=${REGISTRY}/it-cost-dashboard-server-${DEPLOY_ENV}" + echo "IMAGE_DB=${REGISTRY}/it-cost-dashboard-db-${DEPLOY_ENV}" + echo "LATEST_TAG=latest-${DEPLOY_ENV}" + echo "RELEASE=it-cost-dashboard-${DEPLOY_ENV}" + echo "NAMESPACE=it-cost-dashboard-${DEPLOY_ENV}" + echo "CHART=./helm" + echo "VALUES_FILE=./helm/values-${DEPLOY_ENV}.yaml" + } >> "$GITHUB_ENV" + + echo "Resolved environment: ${DEPLOY_ENV} (release tag: '${RELEASE_TAG}')" + + - name: Verify tag is on prod branch (main) + if: github.ref_type == 'tag' + shell: bash + run: | + git fetch origin main --quiet + if git merge-base --is-ancestor "${{ github.sha }}" origin/main; then + echo "Tag commit is on main. Proceeding with prod deploy." + else + echo "::error::Tag ${GITHUB_REF#refs/tags/} points at a commit not on main. Refusing prod deploy." + exit 1 + fi + + # The runner's job image ships docker but not kubectl/helm, so install + # them at job start (static binaries, linux/amd64). + - name: Install kubectl & helm + run: | + if ! command -v kubectl >/dev/null 2>&1; then + KVER=$(curl -sfL https://dl.k8s.io/release/stable.txt) + curl -sfL -o /usr/local/bin/kubectl "https://dl.k8s.io/release/${KVER}/bin/linux/amd64/kubectl" + chmod +x /usr/local/bin/kubectl + fi + if ! command -v helm >/dev/null 2>&1; then + curl -sfL https://get.helm.sh/helm-v3.16.4-linux-amd64.tar.gz | tar xz -C /tmp + mv /tmp/linux-amd64/helm /usr/local/bin/helm + fi + kubectl version --client + helm version + + - name: Set up kubeconfig + run: | + echo "KUBECONFIG=${RUNNER_TEMP}/kubeconfig" >> "$GITHUB_ENV" + echo "${{ secrets.KUBE_CONFIG_DATA }}" | base64 -d > "${RUNNER_TEMP}/kubeconfig" + chmod 600 "${RUNNER_TEMP}/kubeconfig" + + - name: Login to Harbor + run: | + # REGISTRY is host/project but `docker login` takes only the host. + REGISTRY_HOST=$(echo "${REGISTRY}" | cut -d'/' -f1) + echo "Logging in to ${REGISTRY_HOST}" + echo "${{ secrets.HARBOR_PASSWORD }}" | docker login "${REGISTRY_HOST}" \ + -u "${{ secrets.HARBOR_USERNAME }}" \ + --password-stdin + + - name: Pre-build diagnostics + run: | + echo "Ref: ${GITHUB_REF}" + echo "Environment: ${DEPLOY_ENV}" + echo "Release tag: ${RELEASE_TAG}" + echo "Server image: ${IMAGE_SERVER}" + echo "DB image: ${IMAGE_DB}" + docker version + helm version + kubectl version --client + + - name: Build & Push server image + run: | + BUILD_TAG_ARGS=( + -t "${IMAGE_SERVER}:${LATEST_TAG}" + -t "${IMAGE_SERVER}:${{ github.sha }}" + ) + if [[ -n "${RELEASE_TAG}" ]]; then + BUILD_TAG_ARGS+=( -t "${IMAGE_SERVER}:${RELEASE_TAG}" ) + fi + + # AUTH_SECRET is build-time only (Auth.js v5 initializes NextAuth() at + # module load during `next build`). The real secret comes from k8s. + docker build \ + -f Dockerfile \ + --build-arg AUTH_SECRET=build-placeholder-not-used-at-runtime \ + "${BUILD_TAG_ARGS[@]}" \ + . + + docker push "${IMAGE_SERVER}:${LATEST_TAG}" + docker push "${IMAGE_SERVER}:${{ github.sha }}" + if [[ -n "${RELEASE_TAG}" ]]; then + docker push "${IMAGE_SERVER}:${RELEASE_TAG}" + fi + + - name: Build & Push db image + run: | + BUILD_TAG_ARGS=( + -t "${IMAGE_DB}:${LATEST_TAG}" + -t "${IMAGE_DB}:${{ github.sha }}" + ) + if [[ -n "${RELEASE_TAG}" ]]; then + BUILD_TAG_ARGS+=( -t "${IMAGE_DB}:${RELEASE_TAG}" ) + fi + + docker build \ + -f Dockerfile.db \ + "${BUILD_TAG_ARGS[@]}" \ + . + + docker push "${IMAGE_DB}:${LATEST_TAG}" + docker push "${IMAGE_DB}:${{ github.sha }}" + if [[ -n "${RELEASE_TAG}" ]]; then + docker push "${IMAGE_DB}:${RELEASE_TAG}" + fi + + - name: Ensure namespace exists + run: | + # `helm --atomic` against a missing namespace fails outright. + kubectl create namespace "${NAMESPACE}" \ + --dry-run=client -o yaml | kubectl apply -f - + + # The kubelet pulls through the same Docker Desktop daemon that built the + # image, so the pull server must match the push host exactly + # (host.docker.internal:8080). + - name: Ensure Harbor pull secret exists + run: | + kubectl create secret docker-registry harbor-pull-secret \ + --docker-server="$(echo "${REGISTRY}" | cut -d'/' -f1)" \ + --docker-username="${{ secrets.HARBOR_USERNAME }}" \ + --docker-password="${{ secrets.HARBOR_PASSWORD }}" \ + --namespace "${NAMESPACE}" \ + --dry-run=client -o yaml | kubectl apply -f - + + # ---- DEV DEPLOY (branch dev only) ---- + # Deployed by commit SHA, not latest-dev: an immutable tag sidesteps the + # moving-tag + IfNotPresent stale-cache trap and makes `kubectl describe + # pod` show exactly which commit is live. + - name: Deploy DEV with Helm + if: github.ref_type == 'branch' && github.ref == 'refs/heads/dev' + run: | + kubectl get nodes + kubectl get ns + + helm upgrade --install "${RELEASE}" "${CHART}" \ + -f "${VALUES_FILE}" \ + --namespace "${NAMESPACE}" \ + --set server.image.repository="${IMAGE_SERVER}" \ + --set server.image.tag="${{ github.sha }}" \ + --set db.image.repository="${IMAGE_DB}" \ + --set db.image.tag="${{ github.sha }}" \ + --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 }}" \ + --wait --timeout 10m --atomic --debug + + # ---- PROD DEPLOY (tag push only) ---- + - name: Deploy PROD with Helm + if: github.ref_type == 'tag' + run: | + kubectl get nodes + kubectl get ns + + if [[ -z "${RELEASE_TAG}" ]]; then + echo "No tag detected; aborting prod deploy." + exit 1 + fi + + helm upgrade --install "${RELEASE}" "${CHART}" \ + -f "${VALUES_FILE}" \ + --namespace "${NAMESPACE}" \ + --set server.image.repository="${IMAGE_SERVER}" \ + --set server.image.tag="${RELEASE_TAG}" \ + --set db.image.repository="${IMAGE_DB}" \ + --set db.image.tag="${RELEASE_TAG}" \ + --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 }}" \ + --wait --timeout 10m --atomic --debug + + # Strictly redundant with immutable image tags: Helm already rolled the + # pods and --atomic already verified health. Retained as belt-and-braces. + # Scoped to component=server so it does not bounce MySQL every push. + - name: Rollout restart (server only) + if: success() + run: | + kubectl rollout restart deployment \ + -l "app.kubernetes.io/instance=${RELEASE},app.kubernetes.io/component=server" \ + -n "${NAMESPACE}" + + - name: Post-deploy diagnostics (if failure) + if: failure() + run: | + echo "----- Diagnostics -----" + kubectl get deploy,sts,po,svc,ing -n "${NAMESPACE}" -o wide || true + echo "----- PVCs (a Pending PVC = missing/wrong StorageClass; invisible in pod logs) -----" + kubectl get pvc -n "${NAMESPACE}" -o wide || true + echo "----- Events -----" + kubectl get events -n "${NAMESPACE}" --sort-by=.lastTimestamp || true + for p in $(kubectl get po -n "${NAMESPACE}" -o name); do + echo "### $p" + kubectl describe "$p" -n "${NAMESPACE}" || true + kubectl logs "$p" -n "${NAMESPACE}" --all-containers --tail=200 || true + done diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml deleted file mode 100644 index a7193a1..0000000 --- a/.github/workflows/cicd.yaml +++ /dev/null @@ -1,324 +0,0 @@ -name: CI/CD (dev on branch, prod on tag) - -on: - push: - branches: - - dev # build & deploy for dev only - tags: - - "*" # build & deploy for prod only when a tag is pushed - -jobs: - setup: - runs-on: - group: trend-emu-intranet - labels: - - "ubuntu-latest-intranet" - outputs: - matrix: ${{ steps.pick.outputs.matrix }} - steps: - - name: Pick matrix config from ref type - id: pick - shell: bash - run: | - if [[ "${{ github.ref_type }}" == "tag" ]]; then - echo 'matrix={"include":[{"env":"prod","image_name_server":"it-cost-dashboard-server-prod","image_name_db":"it-cost-dashboard-db-prod","latest_tag":"latest-prod","release":"it-cost-dashboard-prod","namespace":"it-cost-dashboard-prod","chart":"./helm","values_file":"./helm/values-prod.yaml"}]}' >> "$GITHUB_OUTPUT" - else - echo 'matrix={"include":[{"env":"dev","image_name_server":"it-cost-dashboard-server-dev","image_name_db":"it-cost-dashboard-db-dev","latest_tag":"latest-dev","release":"it-cost-dashboard-dev","namespace":"it-cost-dashboard-dev","chart":"./helm","values_file":"./helm/values-dev.yaml"}]}' >> "$GITHUB_OUTPUT" - fi - - build-and-deploy: - needs: setup - runs-on: - group: trend-emu-intranet - labels: - - "ubuntu-latest-intranet" - strategy: - matrix: ${{ fromJSON(needs.setup.outputs.matrix) }} - - # Per-environment group: a dev push must never cancel an in-flight prod - # deploy (the original used a hardcoded "prod" group for both refs). - concurrency: - group: deploy-${{ matrix.env }} - cancel-in-progress: true - - env: - # host/repo, e.g. jfrog.trendmicro.com/ameait-it-cost-dashboard - JFROG_REGISTRY: ${{ secrets.JFROG_REGISTRY }} - DOCKER_BUILDKIT: "1" - COMPOSE_DOCKER_CLI_BUILD: "1" - - steps: - - name: Checkout (with tags) - uses: actions/checkout@v4 - with: - fetch-depth: 0 # IMPORTANT to access tags - - - name: Verify tag is on prod branch (main) - id: guard - if: github.ref_type == 'tag' - shell: bash - run: | - git fetch origin main --quiet - if git merge-base --is-ancestor "${{ github.sha }}" origin/main; then - echo "Tag commit is on main. Proceeding with prod deploy." - else - echo "::error::Tag ${GITHUB_REF#refs/tags/} points at a commit not on main. Refusing prod deploy." - exit 1 - fi - - - name: Set up kubeconfig - run: | - echo "KUBECONFIG=${RUNNER_TEMP}/kubeconfig" >> "$GITHUB_ENV" - echo "${{ secrets.KUBE_CONFIG_DATA }}" | base64 -d > "${RUNNER_TEMP}/kubeconfig" - chmod 600 "${RUNNER_TEMP}/kubeconfig" - - # Temporary debug step: we cannot shell into this self-hosted runner - # directly, so this proves (or disproves) network reachability to the - # k8s API from inside the runner itself, ahead of the image build/push - # so we fail fast instead of waiting ~10 min for the helm timeout. - # Remove once the ACL/connectivity issue is confirmed fixed. - - name: "[DEBUG] Network path to k8s API (6443)" - run: | - set +e - echo "===== Runner identity / network info =====" - hostname -f 2>/dev/null || hostname - echo "--- Interfaces ---" - ip -brief addr show 2>/dev/null || ifconfig - echo "--- Default route ---" - ip route show 2>/dev/null || route -n - - echo - echo "===== K8s API endpoint (parsed from kubeconfig) =====" - KUBE_HOST=$(grep -m1 'server:' "${KUBECONFIG}" | sed -E 's#.*https?://([^:/]+).*#\1#') - KUBE_PORT=$(grep -m1 'server:' "${KUBECONFIG}" | sed -E 's#.*:([0-9]+)/?.*#\1#') - KUBE_PORT="${KUBE_PORT:-6443}" - echo "Host: ${KUBE_HOST}" - echo "Port: ${KUBE_PORT}" - - echo - echo "===== DNS resolution =====" - getent hosts "${KUBE_HOST}" || nslookup "${KUBE_HOST}" || dig +short "${KUBE_HOST}" - - RESOLVED_IPS=$(getent hosts "${KUBE_HOST}" | awk '{print $1}') - - echo - echo "===== Route to each resolved IP =====" - for ip in ${RESOLVED_IPS}; do - echo "-- ip route get ${ip} --" - ip route get "${ip}" 2>/dev/null || echo "(ip route not available)" - done - - echo - echo "===== TCP connect test per resolved IP (port ${KUBE_PORT}) =====" - for ip in ${RESOLVED_IPS}; do - if timeout 5 bash -c "cat < /dev/null > /dev/tcp/${ip}/${KUBE_PORT}" 2>/dev/null; then - echo "OK ${ip}:${KUBE_PORT} - TCP connect succeeded" - else - echo "FAIL ${ip}:${KUBE_PORT} - TCP connect timed out or refused" - fi - done - - echo - echo "===== TCP connect test via hostname (through NLB, port ${KUBE_PORT}) =====" - if timeout 5 bash -c "cat < /dev/null > /dev/tcp/${KUBE_HOST}/${KUBE_PORT}" 2>/dev/null; then - echo "OK ${KUBE_HOST}:${KUBE_PORT} - TCP connect succeeded" - else - echo "FAIL ${KUBE_HOST}:${KUBE_PORT} - TCP connect timed out or refused" - fi - - echo - echo "===== curl -v (expect HTTP 401/403 = reachable; a hang/timeout = blocked) =====" - curl -v -k --max-time 8 "https://${KUBE_HOST}:${KUBE_PORT}/version" - - echo - echo "===== traceroute (best effort, may be blocked itself) =====" - if command -v traceroute >/dev/null 2>&1; then - traceroute -T -p "${KUBE_PORT}" -w 2 -m 15 "${KUBE_HOST}" || true - else - echo "traceroute not installed on runner, skipping" - fi - set -e - - - name: Login to JFrog - run: | - # JFROG_REGISTRY is host/repo but `docker login` takes only the host. - JFROG_HOST=$(echo "${JFROG_REGISTRY}" | cut -d'/' -f1) - echo "Logging in to ${JFROG_HOST}" - echo "${{ secrets.JFROG_IDENTITY_TOKEN }}" | docker login "${JFROG_HOST}" \ - -u "${{ secrets.JFROG_USERNAME }}" \ - --password-stdin - - - name: Determine ref type and release tag - id: refinfo - shell: bash - run: | - echo "GITHUB_REF=${GITHUB_REF}" - echo "GITHUB_REF_TYPE=${GITHUB_REF_TYPE:-${{ github.ref_type }}}" - if [[ "${{ github.ref_type }}" == "tag" ]]; then - echo "ref_type=tag" >> $GITHUB_OUTPUT - echo "release_tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - elif [[ "${{ github.ref_type }}" == "branch" ]]; then - echo "ref_type=branch" >> $GITHUB_OUTPUT - echo "release_tag=" >> $GITHUB_OUTPUT - else - echo "ref_type=unknown" >> $GITHUB_OUTPUT - echo "release_tag=" >> $GITHUB_OUTPUT - fi - - - name: Pre-build diagnostics - run: | - echo "Ref: ${GITHUB_REF}" - echo "Ref type: ${{ steps.refinfo.outputs.ref_type }}" - echo "Release tag: ${{ steps.refinfo.outputs.release_tag }}" - echo "Environment: ${{ matrix.env }}" - echo "Server image: ${JFROG_REGISTRY}/${{ matrix.image_name_server }}" - echo "DB image: ${JFROG_REGISTRY}/${{ matrix.image_name_db }}" - docker version - helm version - kubectl version --client - - - name: Build & Push server image - run: | - REF_TYPE="${{ steps.refinfo.outputs.ref_type }}" - RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}" - IMAGE="${JFROG_REGISTRY}/${{ matrix.image_name_server }}" - - BUILD_TAG_ARGS=( - -t "${IMAGE}:${{ matrix.latest_tag }}" - -t "${IMAGE}:${{ github.sha }}" - ) - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" ) - fi - - # AUTH_SECRET is build-time only (Auth.js v5 initializes NextAuth() at - # module load during `next build`). The real secret comes from k8s. - docker build \ - -f Dockerfile \ - --build-arg AUTH_SECRET=build-placeholder-not-used-at-runtime \ - "${BUILD_TAG_ARGS[@]}" \ - . - - docker push "${IMAGE}:${{ matrix.latest_tag }}" - docker push "${IMAGE}:${{ github.sha }}" - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - docker push "${IMAGE}:${RELEASE_TAG}" - fi - - - name: Build & Push db image - run: | - REF_TYPE="${{ steps.refinfo.outputs.ref_type }}" - RELEASE_TAG="${{ steps.refinfo.outputs.release_tag }}" - IMAGE="${JFROG_REGISTRY}/${{ matrix.image_name_db }}" - - BUILD_TAG_ARGS=( - -t "${IMAGE}:${{ matrix.latest_tag }}" - -t "${IMAGE}:${{ github.sha }}" - ) - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - BUILD_TAG_ARGS+=( -t "${IMAGE}:${RELEASE_TAG}" ) - fi - - docker build \ - -f Dockerfile.db \ - "${BUILD_TAG_ARGS[@]}" \ - . - - docker push "${IMAGE}:${{ matrix.latest_tag }}" - docker push "${IMAGE}:${{ github.sha }}" - if [[ "${REF_TYPE}" == "tag" && -n "${RELEASE_TAG}" ]]; then - docker push "${IMAGE}:${RELEASE_TAG}" - fi - - - name: Ensure namespace exists - run: | - # `helm --atomic` against a missing namespace fails outright. - kubectl create namespace "${{ matrix.namespace }}" \ - --dry-run=client -o yaml | kubectl apply -f - - - - name: Ensure JFrog pull secret exists - run: | - kubectl create secret docker-registry jfrog-registry-creds \ - --docker-server="$(echo "${JFROG_REGISTRY}" | cut -d'/' -f1)" \ - --docker-username="${{ secrets.JFROG_USERNAME }}" \ - --docker-password="${{ secrets.JFROG_IDENTITY_TOKEN }}" \ - --namespace "${{ matrix.namespace }}" \ - --dry-run=client -o yaml | kubectl apply -f - - - # ---- DEV DEPLOY (branch dev only) ---- - # Deployed by commit SHA, not latest-dev: an immutable tag sidesteps the - # moving-tag + IfNotPresent stale-cache trap and makes `kubectl describe - # pod` show exactly which commit is live. - - name: Deploy DEV with Helm - if: github.ref_type == 'branch' && github.ref == 'refs/heads/dev' - run: | - kubectl get nodes - kubectl get ns - - helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \ - -f "${{ matrix.values_file }}" \ - --namespace "${{ matrix.namespace }}" \ - --set server.image.repository="${JFROG_REGISTRY}/${{ matrix.image_name_server }}" \ - --set server.image.tag="${{ github.sha }}" \ - --set db.image.repository="${JFROG_REGISTRY}/${{ matrix.image_name_db }}" \ - --set db.image.tag="${{ github.sha }}" \ - --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) ---- - - name: Deploy PROD with Helm - if: github.ref_type == 'tag' - run: | - kubectl get nodes - kubectl get ns - - TAG="${GITHUB_REF#refs/tags/}" - if [[ -z "${TAG}" ]]; then - echo "No tag detected; aborting prod deploy." - exit 1 - fi - - helm upgrade --install "${{ matrix.release }}" "${{ matrix.chart }}" \ - -f "${{ matrix.values_file }}" \ - --namespace "${{ matrix.namespace }}" \ - --set server.image.repository="${JFROG_REGISTRY}/${{ matrix.image_name_server }}" \ - --set server.image.tag="${TAG}" \ - --set db.image.repository="${JFROG_REGISTRY}/${{ matrix.image_name_db }}" \ - --set db.image.tag="${TAG}" \ - --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 - # pods and --atomic already verified health. Retained as belt-and-braces. - # Scoped to component=server so it does not bounce MySQL every push. - - name: Rollout restart (server only) - if: success() && (github.ref_type == 'branch' || github.ref_type == 'tag') - run: | - kubectl rollout restart deployment \ - -l "app.kubernetes.io/instance=${{ matrix.release }},app.kubernetes.io/component=server" \ - -n "${{ matrix.namespace }}" - - - name: Post-deploy diagnostics (if failure) - if: failure() - run: | - echo "----- Diagnostics -----" - kubectl get deploy,sts,po,svc,ing -n "${{ matrix.namespace }}" -o wide || true - echo "----- PVCs (a Pending PVC = missing gp3 StorageClass; invisible in pod logs) -----" - kubectl get pvc -n "${{ matrix.namespace }}" -o wide || true - echo "----- Events -----" - kubectl get events -n "${{ matrix.namespace }}" --sort-by=.lastTimestamp || true - for p in $(kubectl get po -n "${{ matrix.namespace }}" -o name); do - echo "### $p" - kubectl describe "$p" -n "${{ matrix.namespace }}" || true - kubectl logs "$p" -n "${{ matrix.namespace }}" --all-containers --tail=200 || true - done diff --git a/DEPLOY.md b/DEPLOY.md index 5c4e2d0..cea1004 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -94,38 +94,18 @@ Restart=always ## Authentication & authorization -Sign-in uses Auth.js / NextAuth v5. There are **two** ways in, and access is -**role-based** (`admin` = full access; `user` = read-only dashboard/trend): +Sign-in uses Auth.js / NextAuth v5 with a single sign-in path: -1. **Microsoft Entra ID SSO** — allowed only if the user's email is in the - allowlist (see below). Unlisted emails cannot enter the app (fail closed). -2. **Local username/password** (break-glass admin) — a form on `/login`, - controlled by `LOCAL_ADMIN_USERNAME` / `LOCAL_ADMIN_PASSWORD`. This account is - always `admin` and bypasses the allowlist, so you can always get in to fix a - broken/empty `config/users.json`. +- **Local username/password** — a form on `/login`, controlled by + `LOCAL_ADMIN_USERNAME` / `LOCAL_ADMIN_PASSWORD`. This account always has the + `admin` role. An empty `LOCAL_ADMIN_PASSWORD` disables login entirely (fail + closed). Middleware redirects any unauthenticated request to `/login` and enforces roles -**server-side**: normal users are redirected away from admin pages +**server-side**: non-admin users are redirected away from admin pages (`/cost-registration`, `/cost-item-creation`, `/year-creation`) and any write API call (non-`GET` `/api/*`) returns **403**. Read APIs (`GET /api/*`) stay open to -any signed-in user. The sidebar "Admin" group is also hidden for normal users. - -### Role allowlist — `config/users.json` - -Committed (NOT secret), hand-edited map of **email → role**: - -```json -{ - "Sunny_Ho@trendmicro.com": "admin", - "tom_chien@trendmicro.com": "user" -} -``` - -- Emails are matched **case-insensitively**. -- An email that is absent (or an unreadable/invalid file) ⇒ SSO sign-in denied. -- The role is baked into the session JWT at sign-in, so after editing this file - users must **re-login** (or restart the app) for changes to apply: - `pm2 restart it_cost_dashboard`. +any signed-in user. The sidebar "Admin" group is also hidden for non-admins. ### Required env vars (`.env.production`) @@ -133,32 +113,22 @@ Next.js does **not** load `.env.local` in production, so these must live in `.env.production` on the EC2 host: ```env -AUTH_MICROSOFT_ENTRA_ID_ID="" -AUTH_MICROSOFT_ENTRA_ID_SECRET="" -AUTH_MICROSOFT_ENTRA_ID_ISSUER="https://login.microsoftonline.com//v2.0" AUTH_SECRET="<32-byte random string, e.g. `npx auth secret`>" AUTH_URL="https://" # or set AUTH_TRUST_HOST=true behind a TLS proxy/ALB -# Local break-glass admin. Non-empty password ENABLES the /login form; empty disables it. +# Local admin login. Non-empty password ENABLES the /login form; empty disables it. LOCAL_ADMIN_USERNAME="admin" LOCAL_ADMIN_PASSWORD="" ``` -After changing `config/users.json` or the auth env vars, rebuild and restart: +After changing the auth env vars, rebuild and restart: ```bash rm -rf .next && npm run build:prod && pm2 restart it_cost_dashboard --update-env ``` -In the Entra app registration, add the prod **redirect URI**: - -``` -https:///api/auth/callback/microsoft-entra-id -``` - -(Dev uses `http://localhost:3000/api/auth/callback/microsoft-entra-id`, configured -in the gitignored `.env.local`.) The `AUTH_*` vars are read at both build and -runtime, so they must be present before `npm run build:prod` and at `pm2` start. +The `AUTH_*` vars are read at both build and runtime, so they must be present +before `npm run build:prod` and at `pm2` start. ## Notes diff --git a/Dockerfile b/Dockerfile index 652e555..1023030 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,12 +66,10 @@ RUN groupadd --system --gid 1001 nodejs \ # .next/standalone ships its own traced node_modules — do NOT copy the # builder's. .next/static is NOT part of standalone and must come separately or -# every CSS/JS chunk 404s. config/ is read at runtime by lookupRole() -# (src/lib/authz.ts) via process.cwd() once SSO is re-enabled. +# every CSS/JS chunk 404s. # NOTE: this repo has no public/ directory; adding a COPY for it would fail. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -COPY --from=builder --chown=nextjs:nodejs /app/config ./config USER nextjs diff --git a/config/users.json b/config/users.json deleted file mode 100644 index 2380bce..0000000 --- a/config/users.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "Sunny_Ho@trendmicro.com": "admin", - "tom_chien@trendmicro.com": "user" -} diff --git a/helm/templates/server-ingress.yaml b/helm/templates/server-ingress.yaml index 4908f73..c497839 100644 --- a/helm/templates/server-ingress.yaml +++ b/helm/templates/server-ingress.yaml @@ -10,13 +10,10 @@ metadata: # Headroom for large request headers (chunked Auth.js session cookies). nginx.ingress.kubernetes.io/proxy-buffer-size: "16k" nginx.ingress.kubernetes.io/proxy-buffers-number: "4" - cert-manager.io/cluster-issuer: sectigo-acme + # POC runs plain HTTP — no TLS, no redirect to https. + nginx.ingress.kubernetes.io/ssl-redirect: "false" spec: ingressClassName: {{ .Values.ingress.className }} - tls: - - hosts: - - {{ .Values.ingress.host | quote }} - secretName: {{ include "it-cost-dashboard.fullname" . }}-server-tls rules: - host: {{ required "ingress.host is required" .Values.ingress.host | quote }} http: diff --git a/helm/templates/server-secret.yaml b/helm/templates/server-secret.yaml index d57d4ef..d25aca9 100644 --- a/helm/templates/server-secret.yaml +++ b/helm/templates/server-secret.yaml @@ -11,20 +11,14 @@ stringData: # URL-reserved characters (@ : / # ? % + & and space) — keep it [A-Za-z0-9-]. DATABASE_URL: "mysql://root:{{ required "secrets.dbRootPassword is required" .Values.secrets.dbRootPassword }}@{{ include "it-cost-dashboard.dbServiceName" . }}:{{ .Values.db.port }}/{{ .Values.db.database }}" - # Auth.js JWT signing key. Required at runtime even with SSO commented out. + # Auth.js JWT signing key. AUTH_SECRET: {{ required "secrets.authSecret is required" .Values.secrets.authSecret | quote }} AUTH_TRUST_HOST: "true" - # Break-glass local login. Empty disables it entirely (src/auth.ts fails closed). - LOCAL_ADMIN_PASSWORD: {{ .Values.secrets.localAdminPassword | default "" | quote }} + # Local admin login. Empty disables login entirely (src/auth.ts fails + # closed); required loudly here since it is now the only sign-in path. + LOCAL_ADMIN_PASSWORD: {{ required "secrets.localAdminPassword is required" .Values.secrets.localAdminPassword | quote }} - # 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 }} + # Derived from the ingress host so Auth.js builds correct absolute redirect + # URLs. POC UI is plain HTTP only (no TLS anywhere). + AUTH_URL: {{ printf "http://%s" (required "ingress.host is required" .Values.ingress.host) | quote }} diff --git a/helm/values-dev.yaml b/helm/values-dev.yaml index 6fd2abb..1497657 100644 --- a/helm/values-dev.yaml +++ b/helm/values-dev.yaml @@ -4,7 +4,7 @@ replicaCount: 1 server: image: - repository: jfrog.trendmicro.com/ameait-it-cost-dashboard/it-cost-dashboard-server-dev + repository: host.docker.internal:8080/library/it-cost-dashboard-server-dev resources: requests: cpu: 100m @@ -15,10 +15,10 @@ server: db: image: - repository: jfrog.trendmicro.com/ameait-it-cost-dashboard/it-cost-dashboard-db-dev + repository: host.docker.internal:8080/library/it-cost-dashboard-db-dev storage: size: 5Gi - storageClassName: gp3 + storageClassName: hostpath resources: requests: cpu: 100m @@ -30,5 +30,6 @@ db: ingress: enabled: true className: nginx - # TODO: point this at a hostname resolving to the ingress-nginx external IP. - host: it-cost-dashboard-dev.trendmicro.com + # Local ingress hostname — map it to the Mac mini's IP (192.168.0.31) in + # /etc/hosts or local DNS. + host: it-cost-dev.tomchien.com diff --git a/helm/values-prod.yaml b/helm/values-prod.yaml index 4db4e94..b892c92 100644 --- a/helm/values-prod.yaml +++ b/helm/values-prod.yaml @@ -4,7 +4,7 @@ replicaCount: 2 server: image: - repository: jfrog.trendmicro.com/ameait-it-cost-dashboard/it-cost-dashboard-server-prod + repository: host.docker.internal:8080/library/it-cost-dashboard-server-prod resources: requests: cpu: 250m @@ -15,10 +15,10 @@ server: db: image: - repository: jfrog.trendmicro.com/ameait-it-cost-dashboard/it-cost-dashboard-db-prod + repository: host.docker.internal:8080/library/it-cost-dashboard-db-prod storage: size: 20Gi - storageClassName: gp3 + storageClassName: hostpath resources: requests: cpu: 250m @@ -30,5 +30,9 @@ db: ingress: enabled: true className: nginx - # TODO: point this at a hostname resolving to the ingress-nginx external IP. - host: it-cost-dashboard-prod.trendmicro.com + # Local ingress hostname — map it to the Mac mini's IP (192.168.0.31) in + # /etc/hosts or local DNS. + host: tomchien.com + + path: /it-cost-prod + pathType: Prefix diff --git a/helm/values.yaml b/helm/values.yaml index 6897857..01e893a 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -29,7 +29,8 @@ db: database: it_cost_dashboard storage: size: 5Gi - storageClassName: gp3 + # Docker Desktop's built-in StorageClass. + storageClassName: hostpath resources: requests: cpu: 100m @@ -44,23 +45,19 @@ ingress: host: "" # required per-env secrets: - authSecret: "" # from GitHub secret AUTH_SECRET_ + authSecret: "" # from Gitea secret AUTH_SECRET_ # MySQL root password. MUST be [A-Za-z0-9-] only — it is interpolated into # DATABASE_URL and Helm has no URL-encode function. - dbRootPassword: "" # from GitHub secret DB_ROOT_PASSWORD_ - # Optional; empty disables local login entirely (src/auth.ts fails closed). + dbRootPassword: "" # from Gitea secret DB_ROOT_PASSWORD_ + # Local admin login password. Empty disables login entirely (src/auth.ts + # fails closed), so this is effectively required now that it is the only + # sign-in path. localAdminPassword: "" - # 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. +# Created/refreshed by the CI workflow in each namespace ("Ensure Harbor pull +# secret exists" step). Not templated: the kubelet needs it independently of Helm. imagePullSecrets: - - name: jfrog-registry-creds + - name: harbor-pull-secret nodeSelector: {} tolerations: [] diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index be388e2..e6e522a 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -7,19 +7,11 @@ function errorMessage(code: string | undefined): string | null { switch (code) { case "CredentialsSignin": return "Invalid username or password"; - case "AccessDenied": - return "Your account is not authorized — contact the administrator"; default: return code ? "Sign-in failed — please try again" : null; } } -// Server action: kick off the Microsoft SSO flow. -async function microsoftLogin() { - "use server"; - await signIn("microsoft-entra-id", { redirectTo: "/" }); -} - // Server action: local username/password login. async function credentialsLogin(formData: FormData) { "use server"; @@ -63,21 +55,6 @@ export default async function LoginPage({ )} -
- -
- -
- - OR - -
-