mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
One path: /v1/*. We do not use /api/ — that's Casdoor's path. The
BASE_API_PREFIX env knob is gone; the prefix is hard-coded in
apis/base.go and apis/serve.go banner.
Server-side mounts:
- apis: bindHealthApi, bindCollectionApi, bindRecordCrudApi, etc. all
attach under r.Group("/v1") (was r.Group(BASE_API_PREFIX, default /api))
- platform plugin: /api/platform/* -> /v1/platform/* (including auth and
org sub-groups)
- kms plugin: /api/kms/* -> /v1/kms/*
- functions plugin: /api/functions/* -> /v1/functions/*
- cloudsql plugin: /api/cloud-sql/*, /api/meta/* -> /v1/*
- superuser OIDC: /_/api/oidc/config -> /_/v1/oidc/config
- core settings: default RateLimitRule labels /api/batch, /api/ ->
/v1/batch, /v1/
- apis/batch.go: ValidBatchActions regexes match /v1/collections/*;
internal rewrite targets /v1/collections/* (was /v1/base/collections/*,
a stale artifact)
Clients:
- sdk/base-js (BaseClient.collections, files, realtime, CRDT WS,
signIn/refresh/health) targets /v1/*
- sdk/base-react (use-crdt WS path) targets /v1/crdt
- ui-react admin (lib/api.ts, settings.application copy) targets /v1/*
- cmd/cli (NewClient.APIPrefix) defaults to /v1; client_test.go and
cmd/cli_test.go stubServer use /v1/*
- cmd/typegen calls /v1/collections
Upstream paths untouched: Casdoor IAM (/api/login, /api/userinfo,
/api/signup, /api/send-verification-code, /api/get-user), KMS Infisical
(/api/v1/auth/universal-auth/login, /api/v2/workspace/environments),
Kubernetes tasks (/api/v1/namespaces/...), IPFS (/api/v0/...), Gitea/
GitLab/Discord/Strava/Patreon/Box/Wakatime upstream APIs. Those are
their contracts, not ours.
Tests: all 437 prior /api/ test URLs across apis/*_test.go and
plugins/platform/auth_proxy_test.go updated to /v1/. Build + go vet
clean. ./apis ./core ./plugins/platform ./plugins/jsvm ./forms all
green. Pre-existing red probes (network/TestRedProbe_*,
migratecmd/TestAutomigrateCollection*) unchanged.
256 lines
6.3 KiB
Go
256 lines
6.3 KiB
Go
// Copyright (C) 2020-2026, Hanzo AI Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package kms
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/router"
|
|
)
|
|
|
|
// registerRoutes registers the KMS REST API routes.
|
|
func (p *plugin) registerRoutes(r *router.Router[*core.RequestEvent]) {
|
|
api := r.Group("/v1/kms")
|
|
|
|
api.POST("/secrets", p.handleCreateSecret)
|
|
api.GET("/secrets/{key}", p.handleGetSecret)
|
|
api.DELETE("/secrets/{key}", p.handleDeleteSecret)
|
|
api.GET("/secrets", p.handleListSecrets)
|
|
api.POST("/unlock", p.handleUnlock)
|
|
api.POST("/lock", p.handleLock)
|
|
api.POST("/invite", p.handleInvite)
|
|
api.POST("/sync", p.handleSync)
|
|
api.GET("/status", p.handleStatus)
|
|
}
|
|
|
|
// requireSuperuser checks that the request has superuser authentication.
|
|
func (p *plugin) requireSuperuser(e *core.RequestEvent) error {
|
|
if !e.HasSuperuserAuth() {
|
|
return e.UnauthorizedError("superuser auth required", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// --- Secret CRUD ---
|
|
|
|
type createSecretRequest struct {
|
|
Key string `json:"key"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
// handleCreateSecret encrypts and stores a secret via the MPC cluster.
|
|
// POST /v1/kms/secrets
|
|
func (p *plugin) handleCreateSecret(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
var req createSecretRequest
|
|
if err := e.BindBody(&req); err != nil {
|
|
return e.BadRequestError("invalid request body", err)
|
|
}
|
|
if req.Key == "" || req.Value == "" {
|
|
return e.BadRequestError("key and value are required", nil)
|
|
}
|
|
|
|
if err := p.client.Set(req.Key, []byte(req.Value)); err != nil {
|
|
p.logger.Error("kms: set secret failed", "key", req.Key, "error", err)
|
|
return e.BadRequestError("failed to store secret", err)
|
|
}
|
|
|
|
p.logger.Info("kms: secret created", "key", req.Key)
|
|
return e.JSON(http.StatusCreated, map[string]string{
|
|
"key": req.Key,
|
|
"status": "created",
|
|
})
|
|
}
|
|
|
|
// handleGetSecret retrieves and decrypts a secret from the MPC cluster.
|
|
// GET /v1/kms/secrets/{key}
|
|
func (p *plugin) handleGetSecret(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
key := e.Request.PathValue("key")
|
|
if key == "" {
|
|
return e.BadRequestError("key is required", nil)
|
|
}
|
|
|
|
value, err := p.client.Get(key)
|
|
if err != nil {
|
|
return e.NotFoundError("secret not found or decrypt failed", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]string{
|
|
"key": key,
|
|
"value": string(value),
|
|
})
|
|
}
|
|
|
|
// handleDeleteSecret removes a secret from all MPC nodes.
|
|
// DELETE /v1/kms/secrets/{key}
|
|
func (p *plugin) handleDeleteSecret(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
key := e.Request.PathValue("key")
|
|
if key == "" {
|
|
return e.BadRequestError("key is required", nil)
|
|
}
|
|
|
|
if err := p.client.Delete(key); err != nil {
|
|
p.logger.Error("kms: delete secret failed", "key", key, "error", err)
|
|
return e.BadRequestError("failed to delete secret", err)
|
|
}
|
|
|
|
p.logger.Info("kms: secret deleted", "key", key)
|
|
return e.JSON(http.StatusOK, map[string]string{
|
|
"key": key,
|
|
"status": "deleted",
|
|
})
|
|
}
|
|
|
|
// handleListSecrets returns the decrypted names of all secrets.
|
|
// GET /v1/kms/secrets
|
|
func (p *plugin) handleListSecrets(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
names, err := p.client.List()
|
|
if err != nil {
|
|
p.logger.Error("kms: list secrets failed", "error", err)
|
|
return e.BadRequestError("failed to list secrets", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"secrets": names,
|
|
"total": len(names),
|
|
})
|
|
}
|
|
|
|
// --- Lock/Unlock ---
|
|
|
|
type unlockRequest struct {
|
|
Passphrase string `json:"passphrase"`
|
|
}
|
|
|
|
// handleUnlock derives the CEK from a passphrase and stores it in memory.
|
|
// POST /v1/kms/unlock
|
|
func (p *plugin) handleUnlock(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
var req unlockRequest
|
|
if err := e.BindBody(&req); err != nil {
|
|
return e.BadRequestError("invalid request body", err)
|
|
}
|
|
if req.Passphrase == "" {
|
|
return e.BadRequestError("passphrase is required", nil)
|
|
}
|
|
|
|
if err := p.client.Unlock(req.Passphrase); err != nil {
|
|
return e.BadRequestError("unlock failed", err)
|
|
}
|
|
|
|
p.logger.Info("kms: unlocked")
|
|
return e.JSON(http.StatusOK, map[string]string{"status": "unlocked"})
|
|
}
|
|
|
|
// handleLock zeros the CEK from memory.
|
|
// POST /v1/kms/lock
|
|
func (p *plugin) handleLock(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
p.client.Lock()
|
|
|
|
p.logger.Info("kms: locked")
|
|
return e.JSON(http.StatusOK, map[string]string{"status": "locked"})
|
|
}
|
|
|
|
// --- Member Management ---
|
|
|
|
type inviteRequest struct {
|
|
MemberPubKey []byte `json:"member_pub_key"`
|
|
}
|
|
|
|
// handleInvite wraps the CEK for a new member's HPKE public key.
|
|
// POST /v1/kms/invite
|
|
func (p *plugin) handleInvite(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
var req inviteRequest
|
|
if err := e.BindBody(&req); err != nil {
|
|
return e.BadRequestError("invalid request body", err)
|
|
}
|
|
if len(req.MemberPubKey) == 0 {
|
|
return e.BadRequestError("member_pub_key is required", nil)
|
|
}
|
|
|
|
wrapped, err := p.client.InviteMember(req.MemberPubKey)
|
|
if err != nil {
|
|
return e.BadRequestError("invite failed", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"status": "invited",
|
|
"wrapped_cek": json.RawMessage(wrapped),
|
|
})
|
|
}
|
|
|
|
// --- Sync ---
|
|
|
|
// handleSync triggers CRDT sync across all MPC nodes.
|
|
// POST /v1/kms/sync
|
|
func (p *plugin) handleSync(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := p.client.Sync(); err != nil {
|
|
return e.BadRequestError("sync failed", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]string{"status": "synced"})
|
|
}
|
|
|
|
// --- Status ---
|
|
|
|
// handleStatus returns the health status of all MPC nodes.
|
|
// GET /v1/kms/status
|
|
func (p *plugin) handleStatus(e *core.RequestEvent) error {
|
|
if err := p.requireSuperuser(e); err != nil {
|
|
return err
|
|
}
|
|
|
|
statuses, err := p.client.Status()
|
|
if err != nil {
|
|
return e.BadRequestError("status check failed", err)
|
|
}
|
|
|
|
healthy := 0
|
|
for _, s := range statuses {
|
|
if s.Healthy {
|
|
healthy++
|
|
}
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"nodes": statuses,
|
|
"healthy": healthy,
|
|
"total": len(statuses),
|
|
"threshold": p.config.Threshold,
|
|
"unlocked": p.client.IsUnlocked(),
|
|
})
|
|
}
|