Same pattern as the bindings pilot: eventTypesAPI (registers unconditionally, list answers enabled:false without a store), mappingsAPI and sourcesAPI (constructed only when their store is configured, 503 stub otherwise). Shared helpers unsignedSources/recentRefusals/listedIn stay on Server; the dispatch-ledger handler stays shared for captures/tasks. task check green.
290 lines
8.9 KiB
Go
290 lines
8.9 KiB
Go
package webui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/source"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// sourcesAPI owns the /api/sources routes. Constructed only when the source
|
|
// store is configured; Server mounts a 503 stub otherwise.
|
|
type sourcesAPI struct {
|
|
sources storecontract.SourceStore
|
|
log *slog.Logger
|
|
authorize func(http.ResponseWriter, *http.Request) bool
|
|
authorizeCreateIn func(http.ResponseWriter, *http.Request, access.ResourceKind, org.WorkspaceID) bool
|
|
recentRefusals func(*http.Request) map[string]storecontract.CaptureRefusals
|
|
}
|
|
|
|
func newSourcesAPI(s *Server) *sourcesAPI {
|
|
return &sourcesAPI{
|
|
sources: s.Sources,
|
|
log: s.Log,
|
|
authorize: s.authorizeTaskMutation,
|
|
authorizeCreateIn: s.authorizeCreateIn,
|
|
recentRefusals: s.recentRefusals,
|
|
}
|
|
}
|
|
|
|
// sourcesRoutes lists the method+path patterns the sources module owns, so
|
|
// the 503 stub mounts exactly the same routes.
|
|
var sourcesRoutes = []string{
|
|
"GET /api/sources",
|
|
"POST /api/sources",
|
|
"POST /api/sources/{path}/signing",
|
|
"POST /api/sources/{path}/approve-unsigned",
|
|
"POST /api/sources/{path}/secret",
|
|
"PUT /api/sources/{path}/name",
|
|
"PUT /api/sources/{path}/delivery-header",
|
|
"DELETE /api/sources/{path}",
|
|
}
|
|
|
|
func (a *sourcesAPI) register(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/sources", a.handleSourcesList)
|
|
mux.HandleFunc("POST /api/sources", a.handleSourceCreate)
|
|
mux.HandleFunc("POST /api/sources/{path}/signing", a.handleSourceSigning)
|
|
mux.HandleFunc("POST /api/sources/{path}/approve-unsigned", a.handleSourceApproveUnsigned)
|
|
mux.HandleFunc("POST /api/sources/{path}/secret", a.handleSourceSecret)
|
|
mux.HandleFunc("PUT /api/sources/{path}/name", a.handleSourceName)
|
|
mux.HandleFunc("PUT /api/sources/{path}/delivery-header", a.handleSourceDeliveryHeader)
|
|
mux.HandleFunc("DELETE /api/sources/{path}", a.handleSourceDelete)
|
|
}
|
|
|
|
func (a *sourcesAPI) handleSourcesList(w http.ResponseWriter, r *http.Request) {
|
|
if a.sources == nil {
|
|
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
list, err := a.sources.ListSources(r.Context())
|
|
if err != nil {
|
|
a.log.ErrorContext(r.Context(), "list sources", "err", err)
|
|
http.Error(w, "list sources failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
list = slices.DeleteFunc(list, func(src source.Source) bool { return !listedIn(r, src.WorkspaceID) })
|
|
for i := range list {
|
|
list[i].Secret = ""
|
|
}
|
|
writeJSON(w, map[string]any{"sources": list, "refusals": a.recentRefusals(r)})
|
|
}
|
|
|
|
func (a *sourcesAPI) handleSourceCreate(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
if a.sources == nil {
|
|
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
var req struct {
|
|
Path string `json:"path"`
|
|
// Workspace is the caller's org workspace to create it in; empty is
|
|
// the default one.
|
|
Workspace org.WorkspaceID `json:"workspace"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if !a.authorizeCreateIn(w, r, access.KindSource, req.Workspace) {
|
|
return
|
|
}
|
|
src, err := source.New(req.Path)
|
|
if errors.Is(err, source.ErrInvalidPath) || errors.Is(err, source.ErrReservedPath) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err == nil {
|
|
src.WorkspaceID = req.Workspace
|
|
err = a.sources.InsertSource(r.Context(), src)
|
|
}
|
|
if err != nil {
|
|
a.sourceError(w, "create source", err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusCreated)
|
|
writeJSON(w, src)
|
|
}
|
|
|
|
// handleSourceSigning turns signing on at once, or requests turning it off.
|
|
func (a *sourcesAPI) handleSourceSigning(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Signed bool `json:"signed"`
|
|
}
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
transition := (*source.Source).RequestUnsigned
|
|
if req.Signed {
|
|
transition = (*source.Source).RequireSigning
|
|
}
|
|
a.transitionSource(w, r, transition)
|
|
}
|
|
|
|
func (a *sourcesAPI) handleSourceApproveUnsigned(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
a.transitionSource(w, r, (*source.Source).ApproveUnsigned)
|
|
}
|
|
|
|
// transitionSource applies one domain signing transition and writes it with
|
|
// the state it was decided from, so a concurrent change is refused.
|
|
func (a *sourcesAPI) transitionSource(w http.ResponseWriter, r *http.Request, transition func(*source.Source) error) {
|
|
src, ok := a.loadSource(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
from := src.Signing
|
|
if err := transition(src); err != nil {
|
|
http.Error(w, err.Error(), http.StatusConflict)
|
|
return
|
|
}
|
|
if err := a.sources.SetSourceSigning(r.Context(), src.Path, from, src.Signing); err != nil {
|
|
a.sourceError(w, "set source signing", err)
|
|
return
|
|
}
|
|
src.Secret = ""
|
|
writeJSON(w, src)
|
|
}
|
|
|
|
// handleSourceSecret generates a new secret and returns it once.
|
|
func (a *sourcesAPI) handleSourceSecret(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
src, ok := a.loadSource(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
secret, err := source.NewSecret()
|
|
if err == nil {
|
|
err = a.sources.SetSourceSecret(r.Context(), src.Path, secret)
|
|
}
|
|
if err != nil {
|
|
a.sourceError(w, "set source secret", err)
|
|
return
|
|
}
|
|
src.Secret = secret
|
|
writeJSON(w, src)
|
|
}
|
|
|
|
// handleSourceName relabels a source; its path does not change.
|
|
func (a *sourcesAPI) handleSourceName(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
src, ok := a.loadSource(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var request struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&request); err != nil {
|
|
http.Error(w, "invalid source name", http.StatusBadRequest)
|
|
return
|
|
}
|
|
src.Name = strings.TrimSpace(request.Name)
|
|
if err := a.sources.SetSourceName(r.Context(), src.Path, src.Name); err != nil {
|
|
a.sourceError(w, "set source name", err)
|
|
return
|
|
}
|
|
src.Secret = ""
|
|
writeJSON(w, src)
|
|
}
|
|
|
|
// handleSourceDeliveryHeader sets the header that names a source's
|
|
// deliveries, so a sender's retry dispatches no second run.
|
|
func (a *sourcesAPI) handleSourceDeliveryHeader(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
src, ok := a.loadSource(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var request struct {
|
|
Header string `json:"header"`
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&request); err != nil {
|
|
http.Error(w, "invalid delivery header", http.StatusBadRequest)
|
|
return
|
|
}
|
|
header, err := source.DeliveryHeader(request.Header)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
src.DeliveryHeader = header
|
|
if err := a.sources.SetSourceDeliveryHeader(r.Context(), src.Path, header); err != nil {
|
|
a.sourceError(w, "set source delivery header", err)
|
|
return
|
|
}
|
|
src.Secret = ""
|
|
writeJSON(w, src)
|
|
}
|
|
|
|
// handleSourceDelete deletes a source no armed binding fires on.
|
|
func (a *sourcesAPI) handleSourceDelete(w http.ResponseWriter, r *http.Request) {
|
|
if !a.authorize(w, r) {
|
|
return
|
|
}
|
|
if a.sources == nil {
|
|
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
if err := a.sources.DeleteSource(r.Context(), r.PathValue("path")); err != nil {
|
|
a.sourceError(w, "delete source", err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (a *sourcesAPI) loadSource(w http.ResponseWriter, r *http.Request) (*source.Source, bool) {
|
|
if a.sources == nil {
|
|
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
|
return nil, false
|
|
}
|
|
src, err := a.sources.GetSource(r.Context(), r.PathValue("path"))
|
|
if err != nil {
|
|
a.sourceError(w, "get source", err)
|
|
return nil, false
|
|
}
|
|
if src == nil {
|
|
http.Error(w, "source not found", http.StatusNotFound)
|
|
return nil, false
|
|
}
|
|
return src, true
|
|
}
|
|
|
|
func (a *sourcesAPI) sourceError(w http.ResponseWriter, what string, err error) {
|
|
switch {
|
|
case errors.Is(err, storecontract.ErrSourcePathTaken):
|
|
http.Error(w, "source path already taken", http.StatusConflict)
|
|
case errors.Is(err, storecontract.ErrSourceSigningStale):
|
|
http.Error(w, "source signing changed; reload and retry", http.StatusConflict)
|
|
case errors.Is(err, storecontract.ErrSourceNotFound):
|
|
http.Error(w, "source not found", http.StatusNotFound)
|
|
case errors.Is(err, org.ErrInvalidWorkspace):
|
|
http.Error(w, "that workspace is not one of this org's", http.StatusBadRequest)
|
|
case errors.Is(err, storecontract.ErrSourceInUse):
|
|
http.Error(w, "an armed binding fires on this source; pause or delete it first", http.StatusConflict)
|
|
default:
|
|
a.log.Error(what, "err", err)
|
|
http.Error(w, what+" failed", http.StatusInternalServerError)
|
|
}
|
|
}
|