internal/webui hardcoded the control-plane actor as "operator:web", so every audit row a dashboard edit wrote to resource_history named a constant rather than an identity the repository can resolve. The identity API next door already had the answer: the dashboard authenticates with one shared token and carries no per-user session, so the actor is the System identity. Both dashboard paths now derive attribution from one webAudit helper, which also stops swallowing the request ID error that would otherwise write an empty idempotency key. Messaging said the same thing but only checked the sender string was non-empty before passing it through as the actor. Its set and replace paths now resolve the sender to an identity and record its ID, on the same resolver the identity subcommand uses, so a sender that does not resolve cannot change settings.
167 lines
4.8 KiB
Go
167 lines
4.8 KiB
Go
package webui
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
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(), &controlpb.QueryRequest{Kind: r.PathValue("kind")})
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, controlPlaneResourceResponse{Resource: controlPlaneResourceView(response.Resource)})
|
|
}
|
|
|
|
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(), &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 (s *Server) handleControlPlaneWatch(w http.ResponseWriter, r *http.Request) {
|
|
if s.ControlPlane == nil {
|
|
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
after, err := strconv.ParseInt(r.URL.Query().Get("after"), 10, 64)
|
|
if err != nil && r.URL.Query().Get("after") != "" {
|
|
http.Error(w, "invalid after version", http.StatusBadRequest)
|
|
return
|
|
}
|
|
stream, err := s.ControlPlane.Watch(r.Context(), &controlpb.WatchRequest{Kind: r.PathValue("kind"), AfterVersion: after})
|
|
if err != nil {
|
|
writeControlPlaneError(w, err)
|
|
return
|
|
}
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
for {
|
|
response, recvErr := stream.Recv()
|
|
if errors.Is(recvErr, io.EOF) || r.Context().Err() != nil {
|
|
return
|
|
}
|
|
if recvErr != nil {
|
|
return
|
|
}
|
|
payload, marshalErr := json.Marshal(controlPlaneResourceResponse{Resource: controlPlaneResourceView(response.Resource)})
|
|
if marshalErr != nil {
|
|
return
|
|
}
|
|
if _, err = w.Write(append(append([]byte("data: "), payload...), '\n', '\n')); err != nil {
|
|
return
|
|
}
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|