merge: t2db.40 sources and signing into pg/foundation
Resolves against t2db.36: captures carry both event_type and unsigned (proto field 10), the State Store serves sources and event types from the Postgres EDA store, the capture row shows event type and signature together, and the staterpc sentinels map both ways from one table.
This commit is contained in:
63 files changed
+3839
-1753
No files matched your search
@@ -21,9 +21,15 @@ type bindingRequest struct {
|
||||
// for multi-repo deployments. Both empty is valid (falls back to the
|
||||
// daemon's single-configured-repo behaviour); binding.Validate
|
||||
// rejects setting only one.
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Secret string `json:"secret"`
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
}
|
||||
|
||||
// bindingView is a listed binding with its source's signing state, so the
|
||||
// list can flag a binding that dispatches unsigned events.
|
||||
type bindingView struct {
|
||||
binding.Binding
|
||||
Unsigned bool `json:"unsigned"`
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingsList(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -37,8 +43,17 @@ func (s *Server) handleBindingsList(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "list bindings failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
stripBindingSecrets(bindings)
|
||||
writeJSON(w, map[string]any{"bindings": bindings})
|
||||
unsigned, err := s.unsignedSources(r.Context())
|
||||
if err != nil {
|
||||
s.Log.Error("list sources", "err", err)
|
||||
http.Error(w, "list bindings failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
views := make([]bindingView, 0, len(bindings))
|
||||
for _, b := range bindings {
|
||||
views = append(views, bindingView{Binding: b, Unsigned: unsigned[b.Matcher.Source]})
|
||||
}
|
||||
writeJSON(w, map[string]any{"bindings": views})
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -70,7 +85,6 @@ func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
Workflow: req.Workflow,
|
||||
Owner: req.Owner,
|
||||
Repo: req.Repo,
|
||||
Secret: req.Secret,
|
||||
Status: binding.StatusPendingApproval,
|
||||
}
|
||||
if err := b.Validate(); err != nil {
|
||||
@@ -93,7 +107,6 @@ func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "create binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
created.Secret = ""
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, created)
|
||||
}
|
||||
@@ -118,7 +131,6 @@ func (s *Server) handleBindingGet(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "binding not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
b.Secret = ""
|
||||
writeJSON(w, b)
|
||||
}
|
||||
|
||||
@@ -157,9 +169,8 @@ func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
Workflow: req.Workflow,
|
||||
Owner: req.Owner,
|
||||
Repo: req.Repo,
|
||||
Secret: req.Secret,
|
||||
}
|
||||
if err := b.ValidateForUpdate(); err != nil {
|
||||
if err := b.Validate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -181,7 +192,6 @@ func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "update binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
updated.Secret = ""
|
||||
writeJSON(w, updated)
|
||||
}
|
||||
|
||||
@@ -243,15 +253,5 @@ func (s *Server) handleBindingApprove(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "approve binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
updated.Secret = ""
|
||||
writeJSON(w, updated)
|
||||
}
|
||||
|
||||
// stripBindingSecrets zeroes the Secret field on every binding in the slice
|
||||
// so the list handler never leaks a shared HMAC secret through /api/bindings.
|
||||
// Single-binding reads do the same inline; this covers the list path.
|
||||
func stripBindingSecrets(bs []binding.Binding) {
|
||||
for i := range bs {
|
||||
bs[i].Secret = ""
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
@@ -52,16 +51,13 @@ func seedMapping(t *testing.T, srv *Server, name string) string {
|
||||
}
|
||||
|
||||
// validBindingRequest returns a populated bindingRequest that passes
|
||||
// binding.Validate and server-side workflow validation. The Secret is 32
|
||||
// bytes (above the 16-byte floor) so length never accidentally trips
|
||||
// validation in a test that isn't about secret length.
|
||||
// binding.Validate and server-side workflow validation.
|
||||
func validBindingRequest(suffix, source, mappingID string) map[string]any {
|
||||
return map[string]any{
|
||||
"name": "binding " + suffix,
|
||||
"matcher": map[string]any{"source": source},
|
||||
"mapping_id": mappingID,
|
||||
"workflow": "implement",
|
||||
"secret": "0123456789abcdef0123456789abcdef",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,9 +79,6 @@ func TestHandleBindingCreateAndGet(t *testing.T) {
|
||||
if created.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("created.Status = %q, want %q", created.Status, binding.StatusPendingApproval)
|
||||
}
|
||||
if created.Secret != "" {
|
||||
t.Fatalf("created.Secret = %q, want \"\" (response must strip secret)", created.Secret)
|
||||
}
|
||||
|
||||
w = doJSON(t, srv, http.MethodGet, "/api/bindings/"+created.ID, nil)
|
||||
if w.Code != http.StatusOK {
|
||||
@@ -98,15 +91,12 @@ func TestHandleBindingCreateAndGet(t *testing.T) {
|
||||
if got.ID != created.ID {
|
||||
t.Fatalf("got.ID = %q, want %q", got.ID, created.ID)
|
||||
}
|
||||
if got.Secret != "" {
|
||||
t.Fatalf("get.Secret = %q, want \"\"", got.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleBindingCreateAndUpdateRoundTripsOwnerRepo covers the
|
||||
// multi-repo fix at the API layer: a create/update request carrying
|
||||
// owner/repo persists them, and they're visible in the response
|
||||
// (they're not secret, unlike Secret).
|
||||
// (they're not secret).
|
||||
func TestHandleBindingCreateAndUpdateRoundTripsOwnerRepo(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
@@ -159,34 +149,6 @@ func TestHandleBindingCreateRejectsPartialOwnerRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingsListStripsSecrets(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
|
||||
w := doJSON(t, srv, http.MethodGet, "/api/bindings", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("list status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Bindings []binding.Binding `json:"bindings"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(body.Bindings) != 1 {
|
||||
t.Fatalf("bindings = %d, want 1", len(body.Bindings))
|
||||
}
|
||||
if body.Bindings[0].Secret != "" {
|
||||
t.Fatalf("listed Secret = %q, want \"\"", body.Bindings[0].Secret)
|
||||
}
|
||||
// Defence against accidental echo in the raw JSON: the "secret" key
|
||||
// must not appear at all (the binding.Binding tag is omitempty).
|
||||
if strings.Contains(w.Body.String(), `"secret"`) {
|
||||
t.Fatalf("list response leaked secret key: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateRejectsOverlap(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
@@ -220,10 +182,8 @@ func TestHandleBindingCreateRejectsValidationFailure(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
// Short secret trips binding.Validate's length floor regardless of
|
||||
// any store-side check.
|
||||
req := validBindingRequest("a", "sentry", mappingID)
|
||||
req["secret"] = "short"
|
||||
req["name"] = " "
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusBadRequest, w.Body.String())
|
||||
@@ -277,35 +237,6 @@ func TestHandleBindingUpdatePreservesOtherFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleBindingUpdateWithEmptySecretPreservesExisting is the API-layer
|
||||
// half of UpdateBinding's own documented contract ("Secret is preserved
|
||||
// when the caller passes an empty string ... so a partial update cannot
|
||||
// erase the HMAC secret"): an edit that never touches the secret field
|
||||
// must not be rejected by Validate's create-time secret-length floor,
|
||||
// since nothing about the existing secret is changing.
|
||||
func TestHandleBindingUpdateWithEmptySecretPreservesExisting(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
update := validBindingRequest("renamed", "sentry", mappingID)
|
||||
update["secret"] = ""
|
||||
w = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+created.ID, update)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch status = %d, want %d (empty secret must mean 'keep existing', not fail validation); body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
|
||||
stored, err := srv.Bindings.GetBinding(t.Context(), created.ID)
|
||||
if err != nil || stored == nil {
|
||||
t.Fatalf("GetBinding: %+v, %v", stored, err)
|
||||
}
|
||||
if stored.Secret != "0123456789abcdef0123456789abcdef" {
|
||||
t.Fatalf("stored secret = %q, want the original secret preserved", stored.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingApproveArmedRejected(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// Capture sources and their signing setting (docs/prds/event-automation.md
|
||||
// "Sources"). A secret is returned only by the two writes that generate it,
|
||||
// so the operator can copy it into the sender; every other read withholds it.
|
||||
// Turning signing off is a request plus an approval, both behind the same
|
||||
// mutation gate that arms a binding.
|
||||
|
||||
func (s *Server) handleSourcesList(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Sources == nil {
|
||||
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
list, err := s.Sources.ListSources(r.Context())
|
||||
if err != nil {
|
||||
s.Log.Error("list sources", "err", err)
|
||||
http.Error(w, "list sources failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for i := range list {
|
||||
list[i].Secret = ""
|
||||
}
|
||||
writeJSON(w, map[string]any{"sources": list})
|
||||
}
|
||||
|
||||
func (s *Server) handleSourceCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
}
|
||||
if s.Sources == nil {
|
||||
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
src, err := source.New(req.Path)
|
||||
if errors.Is(err, source.ErrInvalidPath) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
err = s.Sources.InsertSource(r.Context(), src)
|
||||
}
|
||||
if err != nil {
|
||||
s.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 (s *Server) handleSourceSigning(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Signed bool `json:"signed"`
|
||||
}
|
||||
if !s.authorizeTaskMutation(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
|
||||
}
|
||||
s.transitionSource(w, r, transition)
|
||||
}
|
||||
|
||||
func (s *Server) handleSourceApproveUnsigned(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
}
|
||||
s.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 (s *Server) transitionSource(w http.ResponseWriter, r *http.Request, transition func(*source.Source) error) {
|
||||
src, ok := s.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 := s.Sources.SetSourceSigning(r.Context(), src.Path, from, src.Signing); err != nil {
|
||||
s.sourceError(w, "set source signing", err)
|
||||
return
|
||||
}
|
||||
src.Secret = ""
|
||||
writeJSON(w, src)
|
||||
}
|
||||
|
||||
// handleSourceSecret generates a new secret and returns it once.
|
||||
func (s *Server) handleSourceSecret(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
}
|
||||
src, ok := s.loadSource(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
secret, err := source.NewSecret()
|
||||
if err == nil {
|
||||
err = s.Sources.SetSourceSecret(r.Context(), src.Path, secret)
|
||||
}
|
||||
if err != nil {
|
||||
s.sourceError(w, "set source secret", err)
|
||||
return
|
||||
}
|
||||
src.Secret = secret
|
||||
writeJSON(w, src)
|
||||
}
|
||||
|
||||
func (s *Server) loadSource(w http.ResponseWriter, r *http.Request) (*source.Source, bool) {
|
||||
if s.Sources == nil {
|
||||
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
||||
return nil, false
|
||||
}
|
||||
src, err := s.Sources.GetSource(r.Context(), r.PathValue("path"))
|
||||
if err != nil {
|
||||
s.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 (s *Server) 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)
|
||||
default:
|
||||
s.Log.Error(what, "err", err)
|
||||
http.Error(w, what+" failed", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// unsignedSources is the set of source paths approved to take unsigned
|
||||
// events. With no source store nothing is unsigned.
|
||||
func (s *Server) unsignedSources(ctx context.Context) (map[string]bool, error) {
|
||||
out := map[string]bool{}
|
||||
if s.Sources == nil {
|
||||
return out, nil
|
||||
}
|
||||
list, err := s.Sources.ListSources(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, src := range list {
|
||||
if src.Unsigned() {
|
||||
out[src.Path] = true
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// memSources is an in-memory SourceStore with the store's error contract.
|
||||
type memSources map[string]source.Source
|
||||
|
||||
func (m memSources) InsertSource(_ context.Context, s source.Source) error {
|
||||
if _, ok := m[s.Path]; ok {
|
||||
return storecontract.ErrSourcePathTaken
|
||||
}
|
||||
m[s.Path] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m memSources) GetSource(_ context.Context, path string) (*source.Source, error) {
|
||||
s, ok := m[path]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func (m memSources) ListSources(context.Context) ([]source.Source, error) {
|
||||
out := make([]source.Source, 0, len(m))
|
||||
for _, s := range m {
|
||||
out = append(out, s)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m memSources) SetSourceSigning(_ context.Context, path string, from, to source.Signing) error {
|
||||
s, ok := m[path]
|
||||
switch {
|
||||
case !ok:
|
||||
return storecontract.ErrSourceNotFound
|
||||
case s.Signing != from:
|
||||
return storecontract.ErrSourceSigningStale
|
||||
}
|
||||
s.Signing = to
|
||||
m[path] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m memSources) SetSourceSecret(_ context.Context, path, secret string) error {
|
||||
s, ok := m[path]
|
||||
if !ok {
|
||||
return storecontract.ErrSourceNotFound
|
||||
}
|
||||
s.Secret = secret
|
||||
m[path] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func sourceTestServer(t *testing.T) (*Server, memSources) {
|
||||
t.Helper()
|
||||
srv := bindingTestServer(t)
|
||||
sources := memSources{}
|
||||
srv.Sources = sources
|
||||
return srv, sources
|
||||
}
|
||||
|
||||
func decodeSource(t *testing.T, body []byte) source.Source {
|
||||
t.Helper()
|
||||
var s source.Source
|
||||
if err := json.Unmarshal(body, &s); err != nil {
|
||||
t.Fatalf("unmarshal source: %v; body = %s", err, body)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestSourceCreateDefaultsToSignedUUIDv7AndShowsSecretOnce(t *testing.T) {
|
||||
srv, _ := sourceTestServer(t)
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/sources", map[string]any{})
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("create status = %d; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
created := decodeSource(t, w.Body.Bytes())
|
||||
if id, err := uuid.Parse(created.Path); err != nil || id.Version() != 7 {
|
||||
t.Fatalf("path %q is not a UUIDv7 (%v)", created.Path, err)
|
||||
}
|
||||
if created.Signing != source.SigningSigned || len(created.Secret) != source.MinSecretLen {
|
||||
t.Fatalf("created = %+v, want signed with a generated secret shown once", created)
|
||||
}
|
||||
|
||||
w = doJSON(t, srv, http.MethodGet, "/api/sources", nil)
|
||||
if w.Code != http.StatusOK || strings.Contains(w.Body.String(), created.Secret) || strings.Contains(w.Body.String(), `"secret"`) {
|
||||
t.Fatalf("list = %d %s; want the secret withheld", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceCreateRefusesTakenOrUnsafePath(t *testing.T) {
|
||||
srv, _ := sourceTestServer(t)
|
||||
if w := doJSON(t, srv, http.MethodPost, "/api/sources", map[string]any{"path": "sentry"}); w.Code != http.StatusCreated {
|
||||
t.Fatalf("first create = %d; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
tests := []struct {
|
||||
path string
|
||||
want int
|
||||
}{
|
||||
{"sentry", http.StatusConflict},
|
||||
{"a/b", http.StatusBadRequest},
|
||||
{"has space", http.StatusBadRequest},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if w := doJSON(t, srv, http.MethodPost, "/api/sources", map[string]any{"path": tt.path}); w.Code != tt.want {
|
||||
t.Errorf("create %q = %d, want %d; body = %s", tt.path, w.Code, tt.want, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceUnsignedNeedsApproval(t *testing.T) {
|
||||
srv, sources := sourceTestServer(t)
|
||||
sources["fw"] = source.Source{Path: "fw", Signing: source.SigningSigned, Secret: "k"}
|
||||
steps := []struct {
|
||||
name, path string
|
||||
body any
|
||||
code int
|
||||
want source.Signing
|
||||
}{
|
||||
{"approve with no request", "/api/sources/fw/approve-unsigned", nil, http.StatusConflict, source.SigningSigned},
|
||||
{"request unsigned", "/api/sources/fw/signing", map[string]any{"signed": false}, http.StatusOK, source.SigningUnsignedPending},
|
||||
{"approve", "/api/sources/fw/approve-unsigned", nil, http.StatusOK, source.SigningUnsigned},
|
||||
{"approve twice", "/api/sources/fw/approve-unsigned", nil, http.StatusConflict, source.SigningUnsigned},
|
||||
{"sign again", "/api/sources/fw/signing", map[string]any{"signed": true}, http.StatusOK, source.SigningSigned},
|
||||
{"unknown source", "/api/sources/nope/signing", map[string]any{"signed": false}, http.StatusNotFound, source.SigningSigned},
|
||||
}
|
||||
for _, st := range steps {
|
||||
w := doJSON(t, srv, http.MethodPost, st.path, st.body)
|
||||
if w.Code != st.code {
|
||||
t.Fatalf("%s: status = %d, want %d; body = %s", st.name, w.Code, st.code, w.Body.String())
|
||||
}
|
||||
if got := sources["fw"].Signing; got != st.want {
|
||||
t.Fatalf("%s: signing = %q, want %q", st.name, got, st.want)
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `"secret"`) {
|
||||
t.Fatalf("%s: response leaked the secret: %s", st.name, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceRegenerateSecret(t *testing.T) {
|
||||
srv, sources := sourceTestServer(t)
|
||||
sources["fw"] = source.Source{Path: "fw", Signing: source.SigningSigned}
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/sources/fw/secret", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
got := decodeSource(t, w.Body.Bytes())
|
||||
if len(got.Secret) != source.MinSecretLen || sources["fw"].Secret != got.Secret {
|
||||
t.Fatalf("secret = %q (stored %q), want a new stored secret shown once", got.Secret, sources["fw"].Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceMutationsRequireCSRFHeader(t *testing.T) {
|
||||
srv, sources := sourceTestServer(t)
|
||||
sources["fw"] = source.Source{Path: "fw", Signing: source.SigningUnsignedPending}
|
||||
for _, path := range []string{"/api/sources", "/api/sources/fw/signing", "/api/sources/fw/approve-unsigned", "/api/sources/fw/secret"} {
|
||||
if w := doJSONNoCSRF(t, srv, http.MethodPost, path, map[string]any{}); w.Code != http.StatusForbidden {
|
||||
t.Errorf("%s without CSRF = %d, want %d", path, w.Code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingListMarksUnsignedSources(t *testing.T) {
|
||||
srv, sources := sourceTestServer(t)
|
||||
sources["fw"] = source.Source{Path: "fw", Signing: source.SigningUnsigned}
|
||||
sources["sentry"] = source.Source{Path: "sentry", Signing: source.SigningSigned}
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
for _, src := range []string{"fw", "sentry"} {
|
||||
if w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest(src, src, mappingID)); w.Code != http.StatusCreated {
|
||||
t.Fatalf("create binding = %d; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
w := doJSON(t, srv, http.MethodGet, "/api/bindings", nil)
|
||||
var body struct {
|
||||
Bindings []struct {
|
||||
Matcher struct{ Source string } `json:"matcher"`
|
||||
Unsigned bool `json:"unsigned"`
|
||||
} `json:"bindings"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || len(body.Bindings) != 2 {
|
||||
t.Fatalf("list = %s (%v)", w.Body.String(), err)
|
||||
}
|
||||
for _, b := range body.Bindings {
|
||||
if want := b.Matcher.Source == "fw"; b.Unsigned != want {
|
||||
t.Errorf("binding on %q unsigned = %v, want %v", b.Matcher.Source, b.Unsigned, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,11 @@ type Server struct {
|
||||
// /api/bindings route answer 503 rather than the dashboard failing to start.
|
||||
Bindings storecontract.BindingStore
|
||||
|
||||
// Sources persists capture sources and their signing setting
|
||||
// (docs/prds/event-automation.md "Sources"). Optional: nil makes every
|
||||
// /api/sources route answer 503 and marks no binding unsigned.
|
||||
Sources storecontract.SourceStore
|
||||
|
||||
// TelegramUpdateReportPath and TelegramUpdateChatID let a dashboard-
|
||||
// initiated update use the same post-restart notification route as a
|
||||
// Telegram-initiated update. The web UI has no durable chat identity, so
|
||||
@@ -274,6 +279,11 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("PATCH /api/bindings/{id}", s.handleBindingUpdate)
|
||||
mux.HandleFunc("DELETE /api/bindings/{id}", s.handleBindingDelete)
|
||||
mux.HandleFunc("POST /api/bindings/{id}/approve", s.handleBindingApprove)
|
||||
mux.HandleFunc("GET /api/sources", s.handleSourcesList)
|
||||
mux.HandleFunc("POST /api/sources", s.handleSourceCreate)
|
||||
mux.HandleFunc("POST /api/sources/{path}/signing", s.handleSourceSigning)
|
||||
mux.HandleFunc("POST /api/sources/{path}/approve-unsigned", s.handleSourceApproveUnsigned)
|
||||
mux.HandleFunc("POST /api/sources/{path}/secret", s.handleSourceSecret)
|
||||
}
|
||||
|
||||
func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
|
||||
|
||||
Reference in new issue
Block a user