The storage and wire contract already key resources by (org, kind), but no caller sent an org, so every request acted in the default org. The org now travels on the request context: webui's authorize middleware attaches the verified principal's org, and one request-constructor per wire shape (controlplanerpc's Query/History/Watch/CommandRequest) stamps it back, so a call site cannot send the wrong org and a new one cannot forget it. No behaviour change today: the shared token's principal is the default org's owner, no production path creates another org, and a context with no principal yields DefaultOrgID (what the server already derived from an empty org_id).
165 lines
5.1 KiB
Go
165 lines
5.1 KiB
Go
package webui
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
)
|
|
|
|
type controlPlaneCommandRequest struct {
|
|
Value json.RawMessage `json:"value"`
|
|
ExpectedVersion int64 `json:"expected_version"`
|
|
}
|
|
|
|
type controlPlaneResource struct {
|
|
Kind string `json:"kind"`
|
|
Version int64 `json:"version"`
|
|
Value json.RawMessage `json:"value"`
|
|
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
|
}
|
|
|
|
type controlPlaneResourceResponse struct {
|
|
Resource controlPlaneResource `json:"resource"`
|
|
}
|
|
|
|
// controlPlaneRevision is one entry of a resource's audit trail. Value is the
|
|
// payload a restore sends back through the replace command, so it stays raw
|
|
// JSON rather than the base64 a proto bytes field would otherwise render as.
|
|
type controlPlaneRevision struct {
|
|
Version int64 `json:"version"`
|
|
Value json.RawMessage `json:"value"`
|
|
Actor string `json:"actor"`
|
|
Source string `json:"source"`
|
|
RequestID string `json:"request_id"`
|
|
At *time.Time `json:"at,omitempty"`
|
|
}
|
|
|
|
type controlPlaneHistoryResponse struct {
|
|
Revisions []controlPlaneRevision `json:"revisions"`
|
|
}
|
|
|
|
func (s *Server) handleControlPlaneCatalog(w http.ResponseWriter, r *http.Request) {
|
|
if s.ControlPlane == nil {
|
|
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
response, err := s.ControlPlane.Catalog(r.Context(), &controlpb.CatalogRequest{})
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, response)
|
|
}
|
|
|
|
func (s *Server) handleControlPlaneQuery(w http.ResponseWriter, r *http.Request) {
|
|
if s.ControlPlane == nil {
|
|
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
response, err := s.ControlPlane.Query(r.Context(), controlplanerpc.QueryRequest(r.Context(), r.PathValue("kind")))
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, controlPlaneResourceResponse{Resource: controlPlaneResourceView(response.Resource)})
|
|
}
|
|
|
|
func (s *Server) handleControlPlaneHistory(w http.ResponseWriter, r *http.Request) {
|
|
if s.ControlPlane == nil {
|
|
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
response, err := s.ControlPlane.History(r.Context(), controlplanerpc.HistoryRequest(r.Context(), r.PathValue("kind")))
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
revisions := make([]controlPlaneRevision, 0, len(response.Revisions))
|
|
for _, revision := range response.Revisions {
|
|
view := controlPlaneRevision{
|
|
Version: revision.Version, Value: revision.ValueJson, Actor: revision.Actor,
|
|
Source: revision.Source, RequestID: revision.RequestId,
|
|
}
|
|
if revision.At != nil {
|
|
at := revision.At.AsTime()
|
|
view.At = &at
|
|
}
|
|
revisions = append(revisions, view)
|
|
}
|
|
writeJSON(w, controlPlaneHistoryResponse{Revisions: revisions})
|
|
}
|
|
|
|
func (s *Server) handleControlPlaneCommand(w http.ResponseWriter, r *http.Request) {
|
|
if s.ControlPlane == nil {
|
|
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
var request controlPlaneCommandRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil || len(request.Value) == 0 {
|
|
http.Error(w, "invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
audit, err := webAudit()
|
|
if err != nil {
|
|
http.Error(w, "cannot create request ID", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
response, err := s.ControlPlane.Command(r.Context(), controlplanerpc.CommandRequest(r.Context(), &controlpb.CommandRequest{
|
|
Kind: r.PathValue("kind"), Command: r.PathValue("command"), ValueJson: request.Value,
|
|
ExpectedVersion: request.ExpectedVersion, Actor: string(audit.ActorID),
|
|
Source: audit.Source, RequestId: audit.RequestID,
|
|
}))
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, controlPlaneResourceResponse{Resource: controlPlaneResourceView(response.Resource)})
|
|
}
|
|
|
|
func controlPlaneResourceView(resource *controlpb.Resource) controlPlaneResource {
|
|
if resource == nil {
|
|
return controlPlaneResource{}
|
|
}
|
|
view := controlPlaneResource{Kind: resource.Kind, Version: resource.Version, Value: resource.ValueJson}
|
|
if resource.UpdatedAt != nil {
|
|
updatedAt := resource.UpdatedAt.AsTime()
|
|
view.UpdatedAt = &updatedAt
|
|
}
|
|
return view
|
|
}
|
|
|
|
func newControlPlaneRequestID() (string, error) {
|
|
var value [16]byte
|
|
if _, err := rand.Read(value[:]); err != nil {
|
|
return "", err
|
|
}
|
|
return "ui-" + hex.EncodeToString(value[:]), nil
|
|
}
|
|
|
|
func writeControlPlaneError(w http.ResponseWriter, err error) {
|
|
code := http.StatusInternalServerError
|
|
switch status.Code(err) {
|
|
case codes.InvalidArgument:
|
|
code = http.StatusBadRequest
|
|
case codes.NotFound:
|
|
code = http.StatusNotFound
|
|
case codes.Aborted:
|
|
code = http.StatusConflict
|
|
case codes.Unavailable, codes.DeadlineExceeded:
|
|
code = http.StatusServiceUnavailable
|
|
}
|
|
message := status.Convert(err).Message()
|
|
if message == "" {
|
|
message = http.StatusText(code)
|
|
}
|
|
http.Error(w, message, code)
|
|
}
|