Files
traces/modules/frontend/pipeline/async_tenant_validator_middleware.go
Carles GarciaandGitHub 25606a1054 frontend, distributor: Add tenant ID validation (#5786)
* frontend, distributor: Add tenant ID validation

Use the same validation than in Mimir for consistency.
In addition, reject empty IDs ("").

* Validate tenantID everywhere except in frontend handlers

We can't do it in frontend handlers because we need to wait for the multitenant middleware,
but we rely on the validation middleware.

Validating everywhere follows the same pattern as Mimir and ensures we don't miss any edge case.

* Check status codes

* Add test for user overrides API

* add comment
2025-11-11 14:51:26 +01:00

28 lines
1015 B
Go

package pipeline
import (
"github.com/grafana/tempo/modules/frontend/combiner"
"github.com/grafana/tempo/pkg/validation"
)
type tenantValidatorRoundTripper struct {
next AsyncRoundTripper[combiner.PipelineResponse]
}
// NewTenantValidatorMiddleware returns a middleware that validates the tenant ID in requests.
// It assumes there's only one tenant ID per request, otherwise it returns an error.
// For this reason it must run after the multiTenantMiddleware.
func NewTenantValidatorMiddleware() AsyncMiddleware[combiner.PipelineResponse] {
return AsyncMiddlewareFunc[combiner.PipelineResponse](func(next AsyncRoundTripper[combiner.PipelineResponse]) AsyncRoundTripper[combiner.PipelineResponse] {
return &tenantValidatorRoundTripper{next: next}
})
}
func (t *tenantValidatorRoundTripper) RoundTrip(req Request) (Responses[combiner.PipelineResponse], error) {
_, err := validation.ExtractValidTenantID(req.Context())
if err != nil {
return NewBadRequest(err), nil
}
return t.next.RoundTrip(req)
}