|
| 1 | +package extproc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 8 | + extprocv3 "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 9 | + typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3" |
| 10 | + |
| 11 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" |
| 12 | +) |
| 13 | + |
| 14 | +// pctxWithMCP builds the minimum pctx needed to trigger the JSON-RPC |
| 15 | +// rendering path on the extproc listener. |
| 16 | +func pctxWithMCP(id any) *pipeline.Context { |
| 17 | + return &pipeline.Context{ |
| 18 | + Extensions: pipeline.Extensions{ |
| 19 | + MCP: &pipeline.MCPExtension{ |
| 20 | + Method: "tools/call", |
| 21 | + RPCID: id, |
| 22 | + }, |
| 23 | + }, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func ibacBlocked() pipeline.Action { |
| 28 | + return pipeline.DenyStatus(403, "ibac.blocked", "intent does not align") |
| 29 | +} |
| 30 | + |
| 31 | +// immediateBody fishes the ImmediateResponse body out of a |
| 32 | +// ProcessingResponse — saves boilerplate in every assertion below. |
| 33 | +func immediateBody(t *testing.T, resp *extprocv3.ProcessingResponse) (typev3.StatusCode, []byte, []*corev3.HeaderValueOption) { |
| 34 | + t.Helper() |
| 35 | + imm, ok := resp.GetResponse().(*extprocv3.ProcessingResponse_ImmediateResponse) |
| 36 | + if !ok { |
| 37 | + t.Fatalf("response is not ImmediateResponse: %T", resp.GetResponse()) |
| 38 | + } |
| 39 | + var hs []*corev3.HeaderValueOption |
| 40 | + if imm.ImmediateResponse.Headers != nil { |
| 41 | + hs = imm.ImmediateResponse.Headers.SetHeaders |
| 42 | + } |
| 43 | + return imm.ImmediateResponse.Status.Code, imm.ImmediateResponse.Body, hs |
| 44 | +} |
| 45 | + |
| 46 | +// TestRejectFromActionForRequest_MCPRequest verifies the extproc |
| 47 | +// listener emits an HTTP 200 ImmediateResponse with a JSON-RPC error |
| 48 | +// body when the rejected request was MCP — the envoy-sidecar twin of |
| 49 | +// the forwardproxy fix. |
| 50 | +func TestRejectFromActionForRequest_MCPRequest(t *testing.T) { |
| 51 | + resp := rejectFromActionForRequest(ibacBlocked(), pctxWithMCP(float64(7))) |
| 52 | + status, body, headers := immediateBody(t, resp) |
| 53 | + |
| 54 | + if status != typev3.StatusCode_OK { |
| 55 | + t.Fatalf("status = %v, want OK (200) — JSON-RPC errors travel over a 200 transport", status) |
| 56 | + } |
| 57 | + gotCT := false |
| 58 | + for _, h := range headers { |
| 59 | + if h.Header.Key == "content-type" && string(h.Header.RawValue) == "application/json" { |
| 60 | + gotCT = true |
| 61 | + } |
| 62 | + } |
| 63 | + if !gotCT { |
| 64 | + t.Errorf("content-type header missing or wrong: %v", headers) |
| 65 | + } |
| 66 | + var parsed map[string]any |
| 67 | + if err := json.Unmarshal(body, &parsed); err != nil { |
| 68 | + t.Fatalf("body is not JSON: %v\n%s", err, body) |
| 69 | + } |
| 70 | + if parsed["jsonrpc"] != "2.0" { |
| 71 | + t.Errorf("jsonrpc = %v, want 2.0", parsed["jsonrpc"]) |
| 72 | + } |
| 73 | + if id, _ := parsed["id"].(float64); id != 7 { |
| 74 | + t.Errorf("id = %v, want 7", parsed["id"]) |
| 75 | + } |
| 76 | + errObj, _ := parsed["error"].(map[string]any) |
| 77 | + if errObj["code"] != float64(-32000) { |
| 78 | + t.Errorf("error.code = %v, want -32000", errObj["code"]) |
| 79 | + } |
| 80 | + if errObj["message"] != "intent does not align" { |
| 81 | + t.Errorf("error.message = %v, want IBAC reason", errObj["message"]) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestRejectFromActionForRequest_NonMCPFallsBack: non-MCP outbound |
| 86 | +// denials keep today's transport-level error shape. Asserts the |
| 87 | +// status mirrors the violation, not 200. |
| 88 | +func TestRejectFromActionForRequest_NonMCPFallsBack(t *testing.T) { |
| 89 | + resp := rejectFromActionForRequest(ibacBlocked(), &pipeline.Context{}) |
| 90 | + status, _, _ := immediateBody(t, resp) |
| 91 | + if status != typev3.StatusCode_Forbidden { |
| 92 | + t.Fatalf("status = %v, want Forbidden (403) — non-MCP request should keep HTTP-level shape", status) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestRejectFromActionForRequest_NotificationFallsBack: JSON-RPC |
| 97 | +// notifications (id == nil) get no JSON-RPC response by spec, so we |
| 98 | +// fall through to the HTTP-level shape rather than echoing a null id. |
| 99 | +func TestRejectFromActionForRequest_NotificationFallsBack(t *testing.T) { |
| 100 | + pctx := &pipeline.Context{ |
| 101 | + Extensions: pipeline.Extensions{ |
| 102 | + MCP: &pipeline.MCPExtension{Method: "tools/call", RPCID: nil}, |
| 103 | + }, |
| 104 | + } |
| 105 | + resp := rejectFromActionForRequest(ibacBlocked(), pctx) |
| 106 | + status, _, _ := immediateBody(t, resp) |
| 107 | + if status != typev3.StatusCode_Forbidden { |
| 108 | + t.Fatalf("status = %v, want Forbidden (403) — notification should keep HTTP-level shape", status) |
| 109 | + } |
| 110 | +} |
0 commit comments