forked from adiom-data/grpcmcp
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add --forward-header for general trusted-proxy identity headers #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielPBak
wants to merge
1
commit into
danielbak/e2e-smoke-test
Choose a base branch
from
danielbak/forward-header
base: danielbak/e2e-smoke-test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os/exec" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // waitForExit runs cmd and waits up to timeout for it to exit on its own, | ||
| // rather than blocking forever if it doesn't -- which would hang the test | ||
| // suite instead of failing it when a regression makes grpcmcp start serving | ||
| // where it used to reject the flags and exit. | ||
| func waitForExit(t *testing.T, cmd *exec.Cmd, timeout time.Duration) (exited bool, err error) { | ||
| t.Helper() | ||
| if err := cmd.Start(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer cmd.Process.Kill() | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { done <- cmd.Wait() }() | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| return true, err | ||
| case <-time.After(timeout): | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| // TestForwardHeaderRequiresHostport checks that -forward-header (and | ||
| // -forward-operator-identity) are rejected without -hostport, rather than | ||
| // silently doing nothing: stdio has no inbound HTTP headers to forward, so | ||
| // an operator relying on either flag for identity attribution would | ||
| // otherwise get no error and no forwarded header. | ||
| func TestForwardHeaderRequiresHostport(t *testing.T) { | ||
| bin := buildGrpcmcp(t) | ||
| descFile := emptyDescriptorFile(t) | ||
|
|
||
| for _, flagName := range []string{"--forward-header=X-Forwarded-User", "--forward-operator-identity"} { | ||
| t.Run(flagName, func(t *testing.T) { | ||
| cmd := exec.Command(bin, flagName, "--descriptors="+descFile) | ||
| var stderr bytes.Buffer | ||
| cmd.Stderr = &stderr | ||
|
|
||
| exited, err := waitForExit(t, cmd, 2*time.Second) | ||
| if !exited { | ||
| t.Fatalf("expected the process to exit without -hostport; it is still running instead of rejecting the flag") | ||
| } | ||
| if err == nil { | ||
| t.Fatalf("expected a non-zero exit without -hostport, got success") | ||
| } | ||
| if !bytes.Contains(stderr.Bytes(), []byte("need -hostport")) { | ||
| t.Errorf("stderr = %q, want a message naming the -hostport requirement", stderr.String()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestForwardHeaderCollisionRejected checks that -forward-header naming a | ||
| // header already set via -header (most dangerously Authorization) is | ||
| // rejected at startup, rather than letting an inbound MCP client silently | ||
| // override grpcmcp's own trusted backend credential. | ||
| func TestForwardHeaderCollisionRejected(t *testing.T) { | ||
| bin := buildGrpcmcp(t) | ||
| descFile := emptyDescriptorFile(t) | ||
|
|
||
| cmd := exec.Command(bin, | ||
| "--hostport=localhost:0", | ||
| "--header=Authorization: Bearer backend-secret", | ||
| "--forward-header=Authorization", | ||
| "--descriptors="+descFile, | ||
| ) | ||
| var stderr bytes.Buffer | ||
| cmd.Stderr = &stderr | ||
|
|
||
| exited, err := waitForExit(t, cmd, 2*time.Second) | ||
| if !exited { | ||
| t.Fatalf("expected the process to reject a colliding -forward-header; it is still running instead") | ||
| } | ||
| if err == nil { | ||
| t.Fatalf("expected a non-zero exit for a colliding -forward-header, got success") | ||
| } | ||
| if !bytes.Contains(stderr.Bytes(), []byte("collides")) { | ||
| t.Errorf("stderr = %q, want a message naming the collision", stderr.String()) | ||
| } | ||
| } | ||
|
|
||
| // TestForwardOperatorIdentityCollisionRejected mirrors | ||
| // TestForwardHeaderCollisionRejected for -forward-operator-identity, whose | ||
| // forwarded header name (X-Operator-Identity) is fixed rather than | ||
| // configurable. | ||
| func TestForwardOperatorIdentityCollisionRejected(t *testing.T) { | ||
| bin := buildGrpcmcp(t) | ||
| descFile := emptyDescriptorFile(t) | ||
|
|
||
| cmd := exec.Command(bin, | ||
| "--hostport=localhost:0", | ||
| "--header=X-Operator-Identity: someone-else", | ||
| "--forward-operator-identity", | ||
| "--descriptors="+descFile, | ||
| ) | ||
| var stderr bytes.Buffer | ||
| cmd.Stderr = &stderr | ||
|
|
||
| exited, err := waitForExit(t, cmd, 2*time.Second) | ||
| if !exited { | ||
| t.Fatalf("expected the process to reject a colliding -forward-operator-identity; it is still running instead") | ||
| } | ||
| if err == nil { | ||
| t.Fatalf("expected a non-zero exit for a colliding -forward-operator-identity, got success") | ||
| } | ||
| if !bytes.Contains(stderr.Bytes(), []byte("collides")) { | ||
| t.Errorf("stderr = %q, want a message naming the collision", stderr.String()) | ||
| } | ||
| } | ||
|
|
||
| // TestForwardHeaderNoCollisionStarts checks that the collision guard does not | ||
| // reject a -forward-header configuration with no actual collision. | ||
| func TestForwardHeaderNoCollisionStarts(t *testing.T) { | ||
| bin := buildGrpcmcp(t) | ||
| descFile := emptyDescriptorFile(t) | ||
|
|
||
| cmd := exec.Command(bin, | ||
| "--hostport=localhost:0", | ||
| "--header=Authorization: Bearer backend-secret", | ||
| "--forward-header=X-Forwarded-User", | ||
| "--descriptors="+descFile, | ||
| ) | ||
| var stderr bytes.Buffer | ||
| cmd.Stderr = &stderr | ||
|
|
||
| exited, err := waitForExit(t, cmd, 1*time.Second) | ||
| if exited { | ||
| t.Fatalf("process exited, expected it to keep serving: %v, stderr: %s", err, stderr.String()) | ||
| } | ||
| // Still running after the timeout, as expected: no collision, nothing to reject. | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/Basic-Capital/grpcmcp/grpcmcp" | ||
| "github.com/mark3labs/mcp-go/mcp" | ||
| ) | ||
|
|
||
| func TestForwardHeadersCopiesNamedHeaders(t *testing.T) { | ||
| static := make(http.Header) | ||
| static.Set("Authorization", "Bearer token") | ||
| provider := forwardHeaders(grpcmcp.StaticHeaders(static), []string{"X-Forwarded-User", "X-Forwarded-Access-Token"}) | ||
|
|
||
| request := mcp.CallToolRequest{} | ||
| request.Header = http.Header{} | ||
| request.Header.Set("X-Forwarded-User", "alice@basiccapital.com") | ||
| request.Header.Set("X-Forwarded-Access-Token", "opaque-token") | ||
| request.Header.Set("X-Forwarded-Email", "alice@basiccapital.com") // not in the forward list | ||
|
|
||
| h, err := provider(context.Background(), request) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := h.Get("X-Forwarded-User"); got != "alice@basiccapital.com" { | ||
| t.Errorf("X-Forwarded-User = %q, want alice@basiccapital.com", got) | ||
| } | ||
| if got := h.Get("X-Forwarded-Access-Token"); got != "opaque-token" { | ||
| t.Errorf("X-Forwarded-Access-Token = %q, want opaque-token", got) | ||
| } | ||
| if got := h.Get("X-Forwarded-Email"); got != "" { | ||
| t.Errorf("X-Forwarded-Email = %q, want empty: not in the configured forward list", got) | ||
| } | ||
| if got := h.Get("Authorization"); got != "Bearer token" { | ||
| t.Errorf("Authorization = %q, want static headers preserved", got) | ||
| } | ||
|
|
||
| // Without any inbound headers, nothing is added. | ||
| h, err = provider(context.Background(), mcp.CallToolRequest{}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := h.Get("X-Forwarded-User"); got != "" { | ||
| t.Errorf("X-Forwarded-User = %q, want empty", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.