mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
Rename / namespace: - Finish github.com/pocketbase/* → github.com/hanzoai/base (+ /dbx) sweep across all Go sources; restore the renamed root launcher (Base, base.go). - Drop the upstream-only surfaces ours doesn't ship (admin ui/, the password/OTP/oauth2/impersonate auth files, extensions) re-added by the upstream merge. - Decomplect the on-disk layout to one base_/ namespace: data/, migrations/, hooks/, types.d.ts are siblings under base_/ (default data dir base_/data). Drop the hz_ prefix everywhere; collapse migratecmd's identical JS/Go branches to one path. - Remove the accidentally-committed hz_data/ dev dir (db + iam.key) and gitignore the runtime namespace dirs. Deps / build: - kmsclient now imported from the light github.com/hanzoai/kms/sdk/go module (the root kms module zip exceeds Go's download limit); cloud/pkg/base Mount removed (lives in the fused daemon). - replicate v0.4.0 → v0.8.0; metric.Counter.Get() (no prometheus dto); network/metrics.go import fix; auth gitlab/github tidy. Tests / behavior: - Wire registerNotifyWatcherHooks; debounce notify files past the kqueue rescan window so cross-instance settings/collections reload fires. - migratecmd: exclude createdAt/updatedAt (not created/updated); regenerate Go+JS automigration goldens from canonical output. - Convert the two stale archive red-probes to assert the landed defenses (backlog cap bounds memory; forged-segment signature is rejected). IAM-native (Base = pure IAM client): - Rip the ~1k-line home-grown embedded IAM (iam_embedded*.go) and the iam-user CLI. Base no longer reinvents identity as _iam_users/_superusers rows. IAM_ENDPOINT (hanzo.id, or an in-process iam.Embed in the fused daemon) is required; Base validates IAM JWTs via JWKS only — superuser is the IAM admin claim, resolved to an ephemeral unpersisted record. - Extract the IDV reverse-proxy (/v1/idv/*) into idv_proxy.go — it was mis-colocated in the embedded onboarding file and is independent of IAM. - Bind the SQL console (/v1/sql) and test it through real IAM JWTs against a JWKS stub (no hardcoded record tokens, no persisted rows).
125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
// Package platform — /v1/iam/* proxy onto the configured IAM_ENDPOINT.
|
|
//
|
|
// The admin UI (apps/admin-base) and any first-party Base client
|
|
// targets a same-origin "/v1/iam" endpoint. This transparent reverse
|
|
// proxy forwards it to the configured IAM_ENDPOINT — Hanzo's hanzo.id,
|
|
// an enterprise Hanzo IAM, or an in-process iam.Embed() served by the
|
|
// fused daemon. Base is a pure IAM client; whichever IAM answers is
|
|
// opaque to the client. We do NOT use /api/* — that's Casdoor's path.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/router"
|
|
)
|
|
|
|
// registerIAMProxy mounts /v1/iam/{path...} forwarding to IAM_ENDPOINT.
|
|
// The proxy is opaque — every method, body, query param, and header
|
|
// (except hop-by-hop) passes through. SSE / streaming responses are
|
|
// flushed as bytes arrive.
|
|
func (p *plugin) registerIAMProxy(r *router.Router[*core.RequestEvent]) {
|
|
endpoint := strings.TrimRight(p.config.IAMEndpoint, "/")
|
|
if endpoint == "" {
|
|
return // platform.Register already errored on this
|
|
}
|
|
upstreamBase, err := url.Parse(endpoint)
|
|
if err != nil {
|
|
p.app.Logger().Error("platform: invalid IAMEndpoint for /v1/iam proxy",
|
|
"endpoint", endpoint, "err", err)
|
|
return
|
|
}
|
|
|
|
// Streaming-friendly client: no global timeout; the request's
|
|
// context cancellation handles abandonment.
|
|
client := &http.Client{Timeout: 0}
|
|
|
|
handler := func(e *core.RequestEvent) error {
|
|
// Map /v1/iam/<rest> → ${IAMEndpoint}/<rest>.
|
|
rest := strings.TrimPrefix(e.Request.URL.Path, "/v1/iam")
|
|
if rest == "" {
|
|
rest = "/"
|
|
}
|
|
upstream := *upstreamBase
|
|
upstream.Path = strings.TrimRight(upstreamBase.Path, "/") + rest
|
|
upstream.RawQuery = e.Request.URL.RawQuery
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
e.Request.Context(), e.Request.Method, upstream.String(), e.Request.Body)
|
|
if err != nil {
|
|
return e.InternalServerError("iam proxy build failed", err)
|
|
}
|
|
// Forward all headers except hop-by-hop. Strip client-supplied
|
|
// identity headers — those are minted from validated auth, not
|
|
// from request input.
|
|
for k, v := range e.Request.Header {
|
|
switch k {
|
|
case "Connection", "Keep-Alive", "Proxy-Authenticate",
|
|
"Proxy-Authorization", "Te", "Trailer", "Transfer-Encoding",
|
|
"Upgrade", "Host",
|
|
"X-User-Id", "X-Org-Id", "X-User-Email":
|
|
continue
|
|
}
|
|
req.Header[k] = v
|
|
}
|
|
// Ensure Host matches upstream (some IdP impls validate it).
|
|
req.Host = upstreamBase.Host
|
|
// Disable proxy buffering for SSE-style endpoints.
|
|
req.Header.Set("X-Accel-Buffering", "no")
|
|
|
|
// SSE / streaming heuristic: use a long-lived client.
|
|
c := client
|
|
if !isLikelyStreaming(rest) {
|
|
c = &http.Client{Timeout: 30 * time.Second}
|
|
}
|
|
resp, err := c.Do(req)
|
|
if err != nil {
|
|
return e.InternalServerError("iam unreachable", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
for k, v := range resp.Header {
|
|
e.Response.Header()[k] = v
|
|
}
|
|
e.Response.WriteHeader(resp.StatusCode)
|
|
|
|
flusher, _ := e.Response.(http.Flusher)
|
|
buf := make([]byte, 32*1024)
|
|
for {
|
|
n, rerr := resp.Body.Read(buf)
|
|
if n > 0 {
|
|
if _, werr := e.Response.Write(buf[:n]); werr != nil {
|
|
return nil
|
|
}
|
|
if flusher != nil {
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
if rerr != nil {
|
|
if rerr == io.EOF {
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
r.GET("/v1/iam/{path...}", handler)
|
|
r.POST("/v1/iam/{path...}", handler)
|
|
r.PUT("/v1/iam/{path...}", handler)
|
|
r.PATCH("/v1/iam/{path...}", handler)
|
|
r.DELETE("/v1/iam/{path...}", handler)
|
|
}
|
|
|
|
func isLikelyStreaming(path string) bool {
|
|
return strings.Contains(path, "/stream") ||
|
|
strings.Contains(path, "/sse") ||
|
|
strings.Contains(path, "/events")
|
|
}
|