mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-01 20:41:28 +02:00
[management,proxy] Add agentgateway integration (#7274)
* [management] Add agentgateway provider catalog entry Allow Agent Network providers to target an operator-supplied agentgateway proxy while stamping trusted NetBird identity headers. Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> * [proxy] Allow trusted Agent Network identity headers Permit only the built-in identity injector to replace the two reserved agentgateway attribution headers while keeping them blocked for every other middleware. Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> * [management,proxy] Add multi-vendor gateway routing Let one Agent Network route declare multiple parser surfaces while preserving the existing singular vendor wire field. Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> * [management] Update router test for model policies Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> * [proxy] Cover reserved header policy Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> * [management] Add agentgateway model discovery Use agentgateway's OpenAI-compatible models endpoint and omit wildcard patterns until NetBird can authorize and price them consistently. Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io> --------- Signed-off-by: Daneyon Hansen <daneyon.hansen@solo.io>
This commit is contained in:
@@ -36,7 +36,10 @@ type ProviderRoute struct {
|
||||
// request on a same-vendor route so catch-all gateways of a different
|
||||
// vendor can't swallow it. Empty disables vendor filtering for this
|
||||
// route.
|
||||
Vendor string `json:"vendor,omitempty"`
|
||||
Vendor string `json:"vendor,omitempty"`
|
||||
// Vendors lists every parser surface a multi-surface gateway accepts.
|
||||
// Vendor remains supported for existing single-surface configurations.
|
||||
Vendors []string `json:"vendors,omitempty"`
|
||||
Models []string `json:"models"`
|
||||
UpstreamScheme string `json:"upstream_scheme"`
|
||||
UpstreamHost string `json:"upstream_host"`
|
||||
|
||||
@@ -409,7 +409,7 @@ func stripBedrockNamespace(out *middleware.Output) {
|
||||
// peer, return matchOutcomeUnauthorised so the caller can emit
|
||||
// the dedicated no_authorised_provider deny code.
|
||||
// 3. Vendor precedence: when the request carries a detected vendor
|
||||
// (llm.provider) and at least one candidate is the same vendor,
|
||||
// (llm.provider) and at least one candidate declares that vendor,
|
||||
// drop the rest — a vendor-tagged request must never cross to
|
||||
// another vendor's route (e.g. an Anthropic call landing on an
|
||||
// OpenAI-compatible gateway that also claims the model).
|
||||
@@ -432,9 +432,9 @@ func (m *Middleware) matchRoute(model, vendor, reqPath string, userGroups []stri
|
||||
|
||||
// Vendor pinning runs BEFORE the group filter so a request the parser
|
||||
// tagged with a vendor can never cross to another vendor's route — not
|
||||
// even an authorised one. Narrow to same-vendor routes when any
|
||||
// model-matched route declares that vendor; setups with no vendor tag on
|
||||
// any route fall through unchanged. After narrowing, if no same-vendor
|
||||
// even an authorised one. Narrow to supporting routes when any
|
||||
// model-matched route declares that vendor; setups with no matching vendor
|
||||
// declaration fall through unchanged. After narrowing, if no supporting
|
||||
// route authorises the caller, that's matchOutcomeUnauthorised (no
|
||||
// cross-vendor fallback).
|
||||
if vendor != "" {
|
||||
@@ -805,21 +805,31 @@ func authorisingGroupsCSV(routeGroups, userGroups []string) string {
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
||||
// matchingVendor returns the subset of routes whose Vendor equals the
|
||||
// request's detected vendor. Routes with an empty Vendor never match — an
|
||||
// untagged route can't be asserted to speak the request's surface, so it
|
||||
// stays out of the vendor-filtered set (but remains eligible via the
|
||||
// fall-through when no route matches the vendor at all).
|
||||
// matchingVendor returns the routes that declare the request's detected
|
||||
// vendor through either the legacy singular field or the multi-vendor field.
|
||||
// Untagged routes remain eligible only when no route declares the vendor.
|
||||
func matchingVendor(routes []ProviderRoute, vendor string) []ProviderRoute {
|
||||
var out []ProviderRoute
|
||||
for _, r := range routes {
|
||||
if r.Vendor == vendor {
|
||||
if routeSupportsVendor(r, vendor) {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func routeSupportsVendor(route ProviderRoute, vendor string) bool {
|
||||
if route.Vendor == vendor {
|
||||
return true
|
||||
}
|
||||
for _, candidate := range route.Vendors {
|
||||
if candidate == vendor {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// explicitlyClaiming returns the subset of routes whose Models list
|
||||
// names the model exactly. Catch-all routes (empty Models) are excluded,
|
||||
// so callers can prefer a provider that genuinely declares the model over
|
||||
|
||||
@@ -412,6 +412,50 @@ func TestRouter_VendorKeepsOpenAIOffAnthropic(t *testing.T) {
|
||||
assert.Equal(t, "api.openai.com", out.Mutations.RewriteUpstream.Host, "openai vendor must pin to the openai route despite anthropic being declared first")
|
||||
}
|
||||
|
||||
func TestRouter_MultiVendorGatewayAcceptsBothSurfaces(t *testing.T) {
|
||||
gateway := ProviderRoute{
|
||||
ID: "agentgateway",
|
||||
Vendors: []string{"openai", "anthropic"},
|
||||
Models: nil,
|
||||
AllowedGroupIDs: []string{defaultTestGroup},
|
||||
UpstreamScheme: "https",
|
||||
UpstreamHost: "gateway.example.com",
|
||||
}
|
||||
other := ProviderRoute{
|
||||
ID: "other-vendor",
|
||||
Vendor: "mistral",
|
||||
Models: nil,
|
||||
AllowedGroupIDs: []string{defaultTestGroup},
|
||||
UpstreamScheme: "https",
|
||||
UpstreamHost: "mistral.example.com",
|
||||
}
|
||||
mw := New(Config{Providers: []ProviderRoute{other, gateway}})
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
vendor string
|
||||
model string
|
||||
path string
|
||||
}{
|
||||
{name: "OpenAI", vendor: "openai", model: "gpt-4o-mini", path: "/v1/chat/completions"},
|
||||
{name: "Anthropic", vendor: "anthropic", model: "claude-sonnet-4-5", path: "/v1/messages"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out, err := mw.Invoke(context.Background(), newInputVendorModelURL(tc.vendor, tc.model, tc.path))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, out)
|
||||
assert.Equal(t, middleware.DecisionAllow, out.Decision,
|
||||
"supported vendor must route through the multi-surface gateway")
|
||||
require.NotNil(t, out.Mutations)
|
||||
require.NotNil(t, out.Mutations.RewriteUpstream)
|
||||
assert.Equal(t, "gateway.example.com", out.Mutations.RewriteUpstream.Host)
|
||||
|
||||
provider, _ := metaValue(t, out.Metadata, middleware.KeyLLMResolvedProviderID)
|
||||
assert.Equal(t, "agentgateway", provider)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouter_VendorAbsentFallsBackToModelPath confirms vendor filtering is
|
||||
// inert when the request carries no detected vendor: routing then relies on
|
||||
// model/path as before.
|
||||
@@ -692,6 +736,23 @@ func TestRouter_FactoryRejectsBadJSON(t *testing.T) {
|
||||
require.Error(t, err, "malformed JSON config must be rejected at chain build time")
|
||||
}
|
||||
|
||||
func TestRouter_FactoryDecodesLegacyAndMultiVendorFields(t *testing.T) {
|
||||
raw := []byte(`{"providers":[` +
|
||||
`{"id":"legacy","vendor":"openai","models":[],"upstream_scheme":"https","upstream_host":"openai.example.com","auth_header_name":"Authorization","auth_header_value":"Bearer legacy","allowed_group_ids":["group"]},` +
|
||||
`{"id":"multi","vendors":["openai","anthropic"],"models":[],"upstream_scheme":"https","upstream_host":"gateway.example.com","auth_header_name":"Authorization","auth_header_value":"Bearer multi","allowed_group_ids":["group"]}` +
|
||||
`]}`)
|
||||
|
||||
resolved, err := Factory{}.New(raw)
|
||||
require.NoError(t, err)
|
||||
router, ok := resolved.(*Middleware)
|
||||
require.True(t, ok, "factory must return the concrete router middleware")
|
||||
require.Len(t, router.cfg.Providers, 2)
|
||||
assert.Equal(t, "openai", router.cfg.Providers[0].Vendor,
|
||||
"the legacy singular field must keep decoding")
|
||||
assert.Equal(t, []string{"openai", "anthropic"}, router.cfg.Providers[1].Vendors,
|
||||
"the multi-vendor field must decode both supported surfaces")
|
||||
}
|
||||
|
||||
func TestRouter_FactoryAcceptsEmptyShapes(t *testing.T) {
|
||||
cases := [][]byte{nil, []byte(""), []byte(" "), []byte("null"), []byte("{}"), []byte("[]")}
|
||||
for _, raw := range cases {
|
||||
|
||||
@@ -264,7 +264,7 @@ func applyMutations(ctx context.Context, d *Dispatcher, spec Spec, r *http.Reque
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
add, remove, blocked := FilterHeaderMutations(m)
|
||||
add, remove, blocked := filterHeaderMutations(m, spec.ID)
|
||||
for _, h := range blocked {
|
||||
d.metrics.IncHeaderMutationBlocked(ctx, spec.ID, h)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
@@ -278,6 +279,64 @@ func TestChain_ApplyMutations_RewriteGatedOnCanMutate(t *testing.T) {
|
||||
assert.Nil(t, rewrite, "rewrite must be filtered when CanMutate=false")
|
||||
}
|
||||
|
||||
func TestChain_IdentityInjectReplacesReservedNetBirdHeaders(t *testing.T) {
|
||||
mw := &fakeMiddleware{
|
||||
id: "llm_identity_inject",
|
||||
slot: SlotOnRequest,
|
||||
mutationsSupported: true,
|
||||
canMutate: true,
|
||||
mutations: &Mutations{
|
||||
HeadersRemove: []string{"x-netbird-user-id", "x-netbird-groups"},
|
||||
HeadersAdd: []KV{
|
||||
{Key: "x-netbird-user-id", Value: "trusted-user"},
|
||||
{Key: "x-netbird-groups", Value: "trusted-group"},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := chainFor(t, mw)
|
||||
req, err := http.NewRequest(http.MethodGet, "https://gateway.example.com/v1/models", nil)
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("x-netbird-user-id", "spoofed-user")
|
||||
req.Header.Set("x-netbird-groups", "spoofed-group")
|
||||
|
||||
denied, _, _, err := c.RunRequest(context.Background(), req, &Input{}, NewAccumulator(0))
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, denied, "identity injection must not deny the request")
|
||||
assert.Equal(t, "trusted-user", req.Header.Get("x-netbird-user-id"),
|
||||
"the built-in identity middleware must replace a spoofed user header")
|
||||
assert.Equal(t, "trusted-group", req.Header.Get("x-netbird-groups"),
|
||||
"the built-in identity middleware must replace spoofed groups")
|
||||
}
|
||||
|
||||
func TestChain_OtherMiddlewareCannotReplaceReservedNetBirdHeaders(t *testing.T) {
|
||||
mw := &fakeMiddleware{
|
||||
id: "untrusted-middleware",
|
||||
slot: SlotOnRequest,
|
||||
mutationsSupported: true,
|
||||
canMutate: true,
|
||||
mutations: &Mutations{
|
||||
HeadersRemove: []string{"x-netbird-user-id", "x-netbird-groups"},
|
||||
HeadersAdd: []KV{
|
||||
{Key: "x-netbird-user-id", Value: "replacement-user"},
|
||||
{Key: "x-netbird-groups", Value: "replacement-group"},
|
||||
},
|
||||
},
|
||||
}
|
||||
c := chainFor(t, mw)
|
||||
req, err := http.NewRequest(http.MethodGet, "https://gateway.example.com/v1/models", nil)
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("x-netbird-user-id", "original-user")
|
||||
req.Header.Set("x-netbird-groups", "original-group")
|
||||
|
||||
denied, _, _, err := c.RunRequest(context.Background(), req, &Input{}, NewAccumulator(0))
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, denied, "blocked mutations must not deny the request")
|
||||
assert.Equal(t, "original-user", req.Header.Get("x-netbird-user-id"),
|
||||
"other middleware must remain unable to mutate reserved identity headers")
|
||||
assert.Equal(t, "original-group", req.Header.Get("x-netbird-groups"),
|
||||
"other middleware must remain unable to mutate reserved identity headers")
|
||||
}
|
||||
|
||||
// TestChain_RunRequest_PropagatesUserGroups asserts the chain forwards
|
||||
// Input.UserGroups verbatim through cloneInputFor so policy-aware
|
||||
// middlewares (e.g. llm_policy_check) can authorise without an extra
|
||||
|
||||
@@ -2,6 +2,8 @@ package middleware
|
||||
|
||||
import "strings"
|
||||
|
||||
const trustedIdentityMiddlewareID = "llm_identity_inject"
|
||||
|
||||
var denyHeaders = []string{
|
||||
"Authorization",
|
||||
"Connection",
|
||||
@@ -78,18 +80,22 @@ func isHeaderFieldName(name string) bool {
|
||||
// header names so the dispatcher can increment the blocked-header
|
||||
// metric.
|
||||
func FilterHeaderMutations(m *Mutations) (filteredAdd []KV, filteredRemove []string, blocked []string) {
|
||||
return filterHeaderMutations(m, "")
|
||||
}
|
||||
|
||||
func filterHeaderMutations(m *Mutations, middlewareID string) (filteredAdd []KV, filteredRemove []string, blocked []string) {
|
||||
if m == nil {
|
||||
return nil, nil, nil
|
||||
}
|
||||
for _, kv := range m.HeadersAdd {
|
||||
if IsHeaderMutable(kv.Key) {
|
||||
if IsHeaderMutable(kv.Key) || isTrustedIdentityHeader(middlewareID, kv.Key) {
|
||||
filteredAdd = append(filteredAdd, kv)
|
||||
continue
|
||||
}
|
||||
blocked = append(blocked, kv.Key)
|
||||
}
|
||||
for _, name := range m.HeadersRemove {
|
||||
if IsHeaderMutable(name) {
|
||||
if IsHeaderMutable(name) || isTrustedIdentityHeader(middlewareID, name) {
|
||||
filteredRemove = append(filteredRemove, name)
|
||||
continue
|
||||
}
|
||||
@@ -97,3 +103,11 @@ func FilterHeaderMutations(m *Mutations) (filteredAdd []KV, filteredRemove []str
|
||||
}
|
||||
return filteredAdd, filteredRemove, blocked
|
||||
}
|
||||
|
||||
func isTrustedIdentityHeader(middlewareID, name string) bool {
|
||||
if middlewareID != trustedIdentityMiddlewareID {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(name, "x-netbird-user-id") ||
|
||||
strings.EqualFold(name, "x-netbird-groups")
|
||||
}
|
||||
|
||||
26
proxy/internal/middleware/headerpolicy_test.go
Normal file
26
proxy/internal/middleware/headerpolicy_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFilterHeaderMutationsDoesNotTrustReservedHeaders(t *testing.T) {
|
||||
mutations := &Mutations{
|
||||
HeadersAdd: []KV{
|
||||
{Key: "x-request-label", Value: "allowed"},
|
||||
{Key: "x-netbird-user-id", Value: "spoofed-user"},
|
||||
},
|
||||
HeadersRemove: []string{"x-request-label", "x-netbird-groups"},
|
||||
}
|
||||
|
||||
filteredAdd, filteredRemove, blocked := FilterHeaderMutations(mutations)
|
||||
|
||||
assert.Equal(t, []KV{{Key: "x-request-label", Value: "allowed"}}, filteredAdd,
|
||||
"the public filter should retain mutable additions")
|
||||
assert.Equal(t, []string{"x-request-label"}, filteredRemove,
|
||||
"the public filter should retain mutable removals")
|
||||
assert.ElementsMatch(t, []string{"x-netbird-user-id", "x-netbird-groups"}, blocked,
|
||||
"the public filter must not grant the identity middleware exception")
|
||||
}
|
||||
Reference in New Issue
Block a user