mirror of
https://github.com/hanzoai/o11y.git
synced 2026-11-03 02:04:48 +00:00
gorilla/mux, otelmux, gorilla/handlers and rs/cors leave go.mod. Every route the runtime serves is a zip route on a zip group: 233 from the apiserver tree, 134 from query-service, 367 total — the same 367 the mount publishes. pkg/http/routing is the one registration surface. It exists because a registration carries three facts zip has no slot for, and the tree this replaces recovered all three PER REQUEST by asking the router what it had just matched: the bound segments, the path template, and the route's declared resources. mux.CurrentRoute(req).GetHandler() was type-asserted back to handler.Handler in two separate packages to re-derive at request time what was already known once at boot. Stating them at registration deletes both lookups. The middleware chain is composed at the LEAF rather than registered ambient, because Audit keys on the route template and Resource resolves the route's declared resources — ambient middleware runs before the router has matched, so both would read an empty route. Per-leaf composition reproduces the order the old tree ran in exactly: match, then middleware, then handler. Duplicate registration now panics at boot; gorilla let the first one win. The Sentry ingest constraint becomes fiber's own guid — a UUID parse instead of a hand-written character class. Paths keep their public brace spelling in the source and in the table; the respelling happens in one function. The standalone server hands its listener to the app, so streams and the websocket upgrade run native fasthttp. Counted, not asserted: routes_test.go in the apiserver pins 233 against the real AddToRouter, routing_test.go pins the table against the router itself. This repo has already shipped 83 typed ops that reached no router while the suite stayed green — an uncalled func is legal Go, so arithmetic is the test. Co-authored-by: Hanzo Dev <[email protected]>
91 lines
3.4 KiB
Go
91 lines
3.4 KiB
Go
package o11yapiserver
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/o11y/pkg/http/routing"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hanzoai/o11y/pkg/modules/dashboard"
|
|
"github.com/hanzoai/o11y/pkg/types"
|
|
"github.com/hanzoai/o11y/pkg/types/coretypes"
|
|
"github.com/hanzoai/o11y/pkg/valuer"
|
|
)
|
|
|
|
// The two anonymous public dashboard routes have no claims to name their
|
|
// subject: the gate resolves it from the {id} PATH segment before any identity
|
|
// exists. So what has to hold is that the id the ROUTER matched is the id the
|
|
// gate asks about — read the wrong half of the URL here and the gate authorizes
|
|
// against the zero uuid.
|
|
type recordingDashboardModule struct {
|
|
dashboard.Module // only the one method the gate reaches is implemented
|
|
|
|
gotID valuer.UUID
|
|
called bool
|
|
}
|
|
|
|
func (module *recordingDashboardModule) GetPublicDashboardSelectorsAndOrg(_ context.Context, id valuer.UUID, _ []*types.Organization) ([]coretypes.Selector, valuer.UUID, error) {
|
|
module.gotID, module.called = id, true
|
|
return nil, valuer.UUID{}, nil
|
|
}
|
|
|
|
// servePublicDashboard drives one request through a REAL router registered at
|
|
// the REAL template and hands back what the gate asked the module.
|
|
func servePublicDashboard(t *testing.T, template, target string) *recordingDashboardModule {
|
|
t.Helper()
|
|
|
|
module := &recordingDashboardModule{}
|
|
prov := &provider{dashboardModule: module}
|
|
|
|
var err error
|
|
router := routing.Serve(http.MethodGet, template, http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
|
|
_, _, err = prov.publicDashboardSelectors(req, nil)
|
|
}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, http.NoBody))
|
|
require.Equal(t, http.StatusOK, rec.Code, "the route did not match — the test is not exercising the read")
|
|
require.NoError(t, err)
|
|
return module
|
|
}
|
|
|
|
func TestPublicDashboardSelectorsReadTheIDTheRouterMatched(t *testing.T) {
|
|
want := valuer.GenerateUUID()
|
|
|
|
module := servePublicDashboard(t, "/v1/o11y/public/dashboards/{id}", "/v1/o11y/public/dashboards/"+want.String())
|
|
|
|
require.True(t, module.called)
|
|
assert.Equal(t, want, module.gotID)
|
|
}
|
|
|
|
// The widget route carries a second segment, and the gate still names the
|
|
// dashboard by {id} — never by {idx}, which is a widget index.
|
|
func TestPublicDashboardSelectorsIgnoreTheWidgetSegment(t *testing.T) {
|
|
want := valuer.GenerateUUID()
|
|
|
|
module := servePublicDashboard(t, "/v1/o11y/public/dashboards/{id}/widgets/{idx}/query_range", "/v1/o11y/public/dashboards/"+want.String()+"/widgets/3/query_range")
|
|
|
|
require.True(t, module.called)
|
|
assert.Equal(t, want, module.gotID)
|
|
}
|
|
|
|
// A segment that is not a uuid is refused before the module is asked: the gate
|
|
// must never resolve selectors for the zero uuid.
|
|
func TestPublicDashboardSelectorsRefuseANonUUIDSegment(t *testing.T) {
|
|
module := &recordingDashboardModule{}
|
|
prov := &provider{dashboardModule: module}
|
|
|
|
var err error
|
|
router := routing.Serve(http.MethodGet, "/v1/o11y/public/dashboards/{id}", http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
|
|
_, _, err = prov.publicDashboardSelectors(req, nil)
|
|
}))
|
|
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/v1/o11y/public/dashboards/not-a-uuid", http.NoBody))
|
|
|
|
require.Error(t, err)
|
|
assert.False(t, module.called, "a malformed id must not reach the module")
|
|
}
|