fix(webui): instance health, logs, config and identities are system-org only

This commit is contained in:
sam committed 2026-10-10 20:20:07 +11:00
1 parent cef34ec438
commit a74b5955de
4 files changed
+93 -14

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
package webui
import (
"net/http"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// instanceViewer reports whether r may see instance-wide detail: process
// health, service logs, the loaded config and every identity on the instance.
// Only the system org operates the instance; a request with no principal is
// an install without the access chain, whose single operator is that org.
func instanceViewer(r *http.Request) bool {
p, ok := access.PrincipalFromContext(r.Context())
return !ok || p.Org == org.DefaultOrgID
}
// instanceOnly serves h to instance viewers and answers not found to anyone
// else, so another org learns nothing about the instance.
func instanceOnly(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !instanceViewer(r) {
http.NotFound(w, r)
return
}
h(w, r)
}
}
+38
View File
@@ -0,0 +1,38 @@
package webui
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// Instance-wide detail reaches the system org and an install without the
// access chain, and reads as absent to every other org.
func TestInstanceOnlyHidesTheInstanceFromOtherOrgs(t *testing.T) {
served := instanceOnly(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })
for _, tt := range []struct {
name string
principal *access.Principal
want int
}{
{"the system org", &access.Principal{Org: org.DefaultOrgID}, http.StatusOK},
{"no access chain", nil, http.StatusOK},
{"another org", &access.Principal{Org: "acme"}, http.StatusNotFound},
} {
t.Run(tt.name, func(t *testing.T) {
ctx := t.Context()
if tt.principal != nil {
ctx = access.WithPrincipal(ctx, *tt.principal)
}
r := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/logs", nil)
w := httptest.NewRecorder()
served(w, r)
if w.Code != tt.want {
t.Fatalf("status %d, want %d", w.Code, tt.want)
}
})
}
}
+11 -11
View File
@@ -240,10 +240,10 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
mux.HandleFunc("PUT /api/workflows/{id}/enabled", s.handleWorkflowEnabled)
mux.HandleFunc("POST /api/work-requests", s.handleWorkRequest)
mux.HandleFunc("GET /api/skills", s.handleSkills)
mux.HandleFunc("GET /api/curators", s.handleCurators)
mux.HandleFunc("GET /api/curators", instanceOnly(s.handleCurators))
mux.HandleFunc("GET /api/captures", s.handleCaptures)
mux.HandleFunc("GET /api/channels", s.handleChannels)
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
mux.HandleFunc("GET /api/channels", instanceOnly(s.handleChannels))
mux.HandleFunc("POST /api/channels/{id}/reload", instanceOnly(s.handleChannelReload))
mux.HandleFunc("GET /api/version", s.handleVersion)
mux.HandleFunc("GET /api/capabilities", s.handleCapabilities)
}
@@ -313,8 +313,8 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
mux.HandleFunc("PUT /api/extensions/{name}/enabled", s.handleExtensionEnabled)
mux.HandleFunc("DELETE /api/extensions/{name}", s.handleExtensionRemove)
mux.HandleFunc("GET /api/control-plane/catalog", s.handleControlPlaneCatalog)
mux.HandleFunc("GET /api/control-plane/apply-status", s.handleApplyStatus)
mux.HandleFunc("GET /api/services", s.handleServices)
mux.HandleFunc("GET /api/control-plane/apply-status", instanceOnly(s.handleApplyStatus))
mux.HandleFunc("GET /api/services", instanceOnly(s.handleServices))
mux.HandleFunc("GET /api/access/policies", s.handleListPolicies)
mux.HandleFunc("PUT /api/access/policies", s.handlePutPolicy)
mux.HandleFunc("DELETE /api/access/policies", s.handleDeletePolicy)
@@ -322,7 +322,7 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory)
mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand)
mux.HandleFunc("GET /api/control-plane/audit", s.handleAudit)
mux.HandleFunc("GET /api/identities", s.handleIdentitiesList)
mux.HandleFunc("GET /api/identities", instanceOnly(s.handleIdentitiesList))
mux.HandleFunc("GET /api/orgs", s.handleOrgsList)
mux.HandleFunc("POST /api/orgs", s.handleOrgCreate)
mux.HandleFunc("GET /api/orgs/{id}", s.handleOrgGet)
@@ -335,10 +335,10 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/tokens", s.handlePersonalTokensList)
mux.HandleFunc("POST /api/tokens", s.handlePersonalTokenCreate)
mux.HandleFunc("DELETE /api/tokens/{id}", s.handlePersonalTokenRevoke)
mux.HandleFunc("POST /api/identities", s.handleIdentityCreate)
mux.HandleFunc("POST /api/identities/{id}/{command}", s.handleIdentityCommand)
mux.HandleFunc("GET /api/config", s.handleConfig)
mux.HandleFunc("GET /api/logs", s.handleLogs)
mux.HandleFunc("POST /api/identities", instanceOnly(s.handleIdentityCreate))
mux.HandleFunc("POST /api/identities/{id}/{command}", instanceOnly(s.handleIdentityCommand))
mux.HandleFunc("GET /api/config", instanceOnly(s.handleConfig))
mux.HandleFunc("GET /api/logs", instanceOnly(s.handleLogs))
}
func (s *Server) registerChatRoutes(mux *http.ServeMux) {
@@ -366,7 +366,7 @@ func (s *Server) Handler() http.Handler {
s.registerConfigAndLogRoutes(mux)
s.registerChatRoutes(mux)
mux.HandleFunc("GET /health/detailed", s.handleHealthDetailed)
mux.HandleFunc("GET /health/detailed", instanceOnly(s.handleHealthDetailed))
mux.HandleFunc("GET /api/stream", s.handleSSE)
mux.Handle("GET /", s.assets())
+15 -3
View File
@@ -36,13 +36,15 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
return
}
stream.logsSince = cursors.Logs
stream.instance = instanceViewer(r)
// Register before reading the backlog. An event published between the
// backlog read and subscription would otherwise be lost permanently.
conn, snapshot, stale, unregister := s.registerSSEConn()
defer unregister()
var logs <-chan logging.Entry
if r.URL.Query().Get("topics") == "logs" && s.LogFeed != nil {
wantLogs := stream.instance && r.URL.Query().Get("topics") == "logs"
if wantLogs && s.LogFeed != nil {
logs = s.LogFeed.Subscribe(r.Context())
}
// Headers make an idle stream live before any resource changes.
@@ -57,7 +59,7 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
return
}
}
if r.URL.Query().Get("topics") == "logs" && !stream.sendLogSnapshot(s.LogFeed) {
if wantLogs && !stream.sendLogSnapshot(s.LogFeed) {
return
}
stream.drain(r.Context(), conn, stale, logs)
@@ -106,6 +108,9 @@ type sseStream struct {
fl http.Flusher
since string
logsSince int64
// instance is whether the viewer may see the instance-wide topics: the
// system org's resources, identities, apply status and service logs.
instance bool
}
// newSSEStream builds a stream over w, reporting false if w cannot be
@@ -149,7 +154,14 @@ func (s *sseStream) sendLog(entry logging.Entry) bool {
return true
}
func (s *sseStream) sendLive(update liveUpdate) bool { return s.writeLive(update, "") }
// sendLive sends a state topic. Every state topic the hub publishes is
// instance-wide, so a viewer from another org receives none of them.
func (s *sseStream) sendLive(update liveUpdate) bool {
if !s.instance && update.topic != "logs-status" {
return true
}
return s.writeLive(update, "")
}
func (s *sseStream) writeLive(update liveUpdate, id string) bool {
body, err := marshalLiveFrame(update)