Files
nexarch/connect/internal/statusapi/statusapi.go
T
rootandClaude Sonnet 5 e70c87332c
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
RBAC-12: echte rechte katalog fuer mail workflow ai connect core admin
Neue Migration 0018_module_policy_rules: 28 echte, granulare Rechte fuer
Mail/Workflow/AI/Connect/Core-Admin (bisher nur DMS befuellt). Echte
Durchsetzung verdrahtet fuer Mail (GET /messages, hat Rolle+Mandant aus
Modul-Token) und Connect (GET /connectors/status, Mandant aus Header,
Rolle mangels eigenem Rollenkonzept fest user). Workflow/AI/Core-Admin
erhalten nur den Rechte-Katalog-Eintrag (AK1/2) -- echte Durchsetzung
dort wuerde deren Identitaetsschicht um Rolle/Mandant erweitern muessen,
das ist eine groessere, separate Aenderung je Modul, nicht Teil dieser
Kachel (siehe Bericht).

Migration real geprueft: 28 Zeilen nach Erstlauf, idempotent (0 neue
beim Zweitlauf), sauberer Rollback (0 nach DELETE), dms.*-Rechte
unveraendert (38 vorher/nachher).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-26 14:22:04 +00:00

122 lines
4.2 KiB
Go

// Package statusapi is the HTTP binding for the connector status overview
// (API-02). User identity comes, transitionally until a central Core-JWT
// wiring exists for Connect (pattern from workflow/internal/approvalapi),
// from the X-User-Id header; the tenant comes from X-Tenant-Slug, set by
// the deployment's auth layer, never from an unauthenticated body/query
// value.
package statusapi
import (
"encoding/json"
"net/http"
"time"
"gitea.perlbach24.de/scripte/nexarch/connect/internal/policyclient"
"gitea.perlbach24.de/scripte/nexarch/connect/internal/syncjob"
)
type Server struct {
jobs *syncjob.Store
policy *policyclient.Client
}
// WithPolicy aktiviert RBAC-12: echte Rechtepruefung gegen Core (POST
// /authorize), zusaetzlich zur bestehenden X-User-Id/X-Tenant-Slug-Pruefung.
// Rolle ist mangels eigenem Rollenkonzept in Connect fest "user" (Uebergang,
// wie beim restlichen Header-basierten Identitaetsmodell dieses Pakets).
func (s *Server) WithPolicy(client *policyclient.Client) *Server {
s.policy = client
return s
}
func NewServer(jobs *syncjob.Store) *Server {
return &Server{jobs: jobs}
}
func (s *Server) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /connectors/status", s.requireIdentity(s.requirePermission("connect.status.read", s.handleList)))
}
func (s *Server) requirePermission(permission string, next func(w http.ResponseWriter, r *http.Request, tenantSlug string)) func(w http.ResponseWriter, r *http.Request, tenantSlug string) {
return func(w http.ResponseWriter, r *http.Request, tenantSlug string) {
if s.policy != nil {
allowed, err := s.policy.Allowed(r.Context(), tenantSlug, "user", permission)
if err != nil {
writeError(w, http.StatusServiceUnavailable, "berechtigungsdienst nicht erreichbar")
return
}
if !allowed {
writeError(w, http.StatusForbidden, "keine berechtigung")
return
}
}
next(w, r, tenantSlug)
}
}
func (s *Server) requireIdentity(next func(w http.ResponseWriter, r *http.Request, tenantSlug string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-User-Id") == "" {
writeError(w, http.StatusUnauthorized, "X-User-Id fehlt")
return
}
tenant := r.Header.Get("X-Tenant-Slug")
if tenant == "" {
writeError(w, http.StatusUnauthorized, "X-Tenant-Slug fehlt")
return
}
next(w, r, tenant)
}
}
type connectorStatus struct {
JobID string `json:"job_id"`
ConnectorName string `json:"connector_name"`
ConnectorInst string `json:"connector_instance_id"`
IntervalSeconds int `json:"interval_seconds"`
NextRunAt time.Time `json:"next_run_at"`
LastRunID *string `json:"last_run_id,omitempty"`
LastRunStatus *string `json:"last_run_status,omitempty"`
LastRunTrigger *string `json:"last_run_trigger,omitempty"`
LastRunStarted *time.Time `json:"last_run_started_at,omitempty"`
LastRunFinished *time.Time `json:"last_run_finished_at,omitempty"`
LastError *string `json:"last_error,omitempty"`
}
func (s *Server) handleList(w http.ResponseWriter, r *http.Request, tenantSlug string) {
statuses, err := s.jobs.ListForTenant(r.Context(), tenantSlug)
if err != nil {
writeError(w, http.StatusInternalServerError, "verbindungsstatus konnte nicht gelesen werden")
return
}
out := make([]connectorStatus, 0, len(statuses))
for _, js := range statuses {
cs := connectorStatus{
JobID: js.Job.ID,
ConnectorName: js.Job.Ref.ConnectorName,
ConnectorInst: js.Job.Ref.ConnectorInstanceID,
IntervalSeconds: js.Job.IntervalSeconds,
NextRunAt: js.Job.NextRunAt,
}
if js.LastRun != nil {
id, status, trig := js.LastRun.ID, string(js.LastRun.Status), string(js.LastRun.Trigger)
cs.LastRunID, cs.LastRunStatus, cs.LastRunTrigger = &id, &status, &trig
cs.LastRunStarted = &js.LastRun.StartedAt
cs.LastRunFinished = js.LastRun.FinishedAt
cs.LastError = js.LastRun.LastError
}
out = append(out, cs)
}
writeJSON(w, http.StatusOK, out)
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func writeError(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, map[string]string{"error": msg})
}