diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index c91e4aa..176430d 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -122,7 +122,13 @@ jobs: - name: Build + push run: | set -eu + # GET /version: the latest release tag (the clone is shallow, so + # fetch the history and tags first) and the commit. + git fetch --quiet --unshallow --tags origin || true + VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo dev) docker build \ + --build-arg VERSION="$VERSION" \ + --build-arg COMMIT="${GITHUB_SHA}" \ -t repo.breakpilot.com/platform/tenant-registry:latest \ -t repo.breakpilot.com/platform/tenant-registry:sha-${GITHUB_SHA} \ . diff --git a/Dockerfile b/Dockerfile index d495253..3f4c9c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,10 @@ WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/tenant-registry ./cmd/server && \ +# Build version and commit for GET /version (CI passes both; portal#64). +ARG VERSION=dev +ARG COMMIT= +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X git.breakpilot.com/platform/tenant-registry/internal/buildinfo.Version=${VERSION} -X git.breakpilot.com/platform/tenant-registry/internal/buildinfo.Commit=${COMMIT}" -o /out/tenant-registry ./cmd/server && \ CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/migrate ./cmd/migrate && \ CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/erpmock ./cmd/erpmock && \ CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/erpsetup ./cmd/erpsetup diff --git a/internal/buildinfo/buildinfo.go b/internal/buildinfo/buildinfo.go new file mode 100644 index 0000000..5949fa8 --- /dev/null +++ b/internal/buildinfo/buildinfo.go @@ -0,0 +1,11 @@ +// Package buildinfo carries the version and commit the binary was built from. +// The image build sets both with -ldflags -X (Dockerfile build args VERSION and +// COMMIT, filled by CI); a local `go build` keeps the defaults. +package buildinfo + +var ( + // Version is the release tag the build descends from, e.g. "v0.1.4". + Version = "dev" + // Commit is the full git commit hash; empty for a local build. + Commit = "" +) diff --git a/internal/server/openapi_test.go b/internal/server/openapi_test.go index 3956048..a24a7b2 100644 --- a/internal/server/openapi_test.go +++ b/internal/server/openapi_test.go @@ -39,6 +39,7 @@ func TestOpenAPISpec_loadsAndIsConsistent(t *testing.T) { }{ {"GET", "/healthz"}, {"GET", "/readyz"}, + {"GET", "/version"}, {"GET", "/v1/tenants/by-slug/acme"}, {"GET", "/v1/entitlements?tenant_id=00000000-0000-0000-0000-000000000001"}, {"GET", "/v1/api-keys?tenant_id=00000000-0000-0000-0000-000000000001"}, diff --git a/internal/server/server.go b/internal/server/server.go index 42b130e..f2740ee 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -13,6 +13,7 @@ import ( "time" "git.breakpilot.com/platform/tenant-registry/internal/authn" + "git.breakpilot.com/platform/tenant-registry/internal/buildinfo" "git.breakpilot.com/platform/tenant-registry/internal/config" "git.breakpilot.com/platform/tenant-registry/internal/erp" "git.breakpilot.com/platform/tenant-registry/internal/keycloak" @@ -80,6 +81,9 @@ func NewRouter(s *Server) http.Handler { // PUBLIC_EXPLICIT: health + status probes. root.HandleFunc("GET /healthz", s.healthz) root.HandleFunc("GET /readyz", s.readyz) + // PUBLIC_EXPLICIT: build version and commit (portal#64), no tenant data; + // the staff dashboard and feedback reports show which build runs. + root.HandleFunc("GET /version", s.version) // PUBLIC_EXPLICIT: ERPNext webhooks carry no Keycloak token — the body // is HMAC-signed with ERP_WEBHOOK_SECRET (receiver refuses without it). root.HandleFunc("POST /v1/internal/erp/webhook", s.erpWebhook) @@ -228,6 +232,11 @@ func (s *Server) healthz(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) } +// version reports the build (internal/buildinfo, set at image build time). +func (s *Server) version(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, map[string]string{"version": buildinfo.Version, "commit": buildinfo.Commit}) +} + func (s *Server) readyz(w http.ResponseWriter, r *http.Request) { if err := s.Store.Ping(r.Context()); err != nil { writeError(w, http.StatusServiceUnavailable, "store_unavailable", err.Error()) diff --git a/internal/server/tenants_test.go b/internal/server/tenants_test.go index 6bd1395..1398785 100644 --- a/internal/server/tenants_test.go +++ b/internal/server/tenants_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "git.breakpilot.com/platform/tenant-registry/internal/buildinfo" "git.breakpilot.com/platform/tenant-registry/internal/store" ) @@ -18,6 +19,18 @@ func TestHealthz(t *testing.T) { }) } +func TestVersion(t *testing.T) { + defer func(v, c string) { buildinfo.Version, buildinfo.Commit = v, c }(buildinfo.Version, buildinfo.Commit) + buildinfo.Version, buildinfo.Commit = "v9.9.9", "0123456789abcdef0123456789abcdef01234567" + eachStore(t, func(t *testing.T, h *testHarness) { + resp, raw := h.do("GET", "/version", nil) + got := decode[map[string]string](t, raw) + if resp.StatusCode != 200 || got["version"] != "v9.9.9" || got["commit"] != "0123456789abcdef0123456789abcdef01234567" || len(got) != 2 { + t.Fatalf("version: %d %s", resp.StatusCode, raw) + } + }) +} + func TestCreateTenant(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { resp, body := h.do("POST", "/v1/tenants", map[string]any{ diff --git a/openapi.yaml b/openapi.yaml index f6b1521..4067eee 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -9,7 +9,7 @@ info: `PRODUCT_INTEGRATION_SPEC.md §8.4` for the audit shape. Auth (RBAC Phase 1): with AUTH_ENABLED=true every route except - /healthz and /readyz requires a Keycloak-issued bearer token whose + /healthz, /readyz and /version requires a Keycloak-issued bearer token whose audience contains AUTH_EXPECTED_AUDIENCE (INTERNAL_SERVICE_ONLY posture). With AUTH_ENABLED=false (dev default) the API is open. Per-client authorization (M-MIG-5): AUTHZ_RULES maps each client @@ -45,6 +45,21 @@ paths: type: object properties: { status: { type: string, example: ok } } + /version: + get: + summary: Build version and commit (public, no auth). + responses: + "200": + description: The build the registry runs; "dev" and "" for a local build. + content: + application/json: + schema: + type: object + required: [version, commit] + properties: + version: { type: string, example: v0.1.4 } + commit: { type: string, description: full git commit hash } + /readyz: get: summary: Readiness probe — pings the store.