Skip to content
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

Add Support for Reject Handler with SmokescreenContext #232

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/smokescreen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type Config struct {
// Custom handler to allow clients to modify reject responses
RejectResponseHandler func(*http.Response)

// Custom handler to allow clients to modify reject responses
RejectResponseHandlerWithCtx func(*SmokescreenContext, *http.Response)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we guard against configuring both this handler and the legacy one? If not, we'd need to document ordering of handler execution et all for consumers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense to only let users configure one of these handler since new one is the superset of legacy one.
I can mark legacy one as depreciated.


// Custom handler to allow clients to modify successful CONNECT responses
AcceptResponseHandler func(*SmokescreenContext, *http.Response) error

Expand Down
3 changes: 3 additions & 0 deletions pkg/smokescreen/smokescreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ func rejectResponse(pctx *goproxy.ProxyCtx, err error) *http.Response {
if sctx.cfg.RejectResponseHandler != nil {
sctx.cfg.RejectResponseHandler(resp)
}
if sctx.cfg.RejectResponseHandlerWithCtx != nil {
sctx.cfg.RejectResponseHandlerWithCtx(sctx, resp)
}
return resp
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/smokescreen/smokescreen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,47 @@ func TestRejectResponseHandler(t *testing.T) {
})
}

func TestRejectResponseHandlerWithCtx(t *testing.T) {
r := require.New(t)
testHeader := "TestRejectResponseHandlerWithCtxHeader"
t.Run("Testing custom reject response handler", func(t *testing.T) {
cfg, err := testConfig("test-local-srv")

// set a custom RejectResponseHandler that will set a header on every reject response
cfg.RejectResponseHandlerWithCtx = func(_ *SmokescreenContext, resp *http.Response) {
resp.Header.Set(testHeader, "This header is added by the RejectResponseHandlerWithCtx")
}
r.NoError(err)

proxySrv := proxyServer(cfg)
r.NoError(err)
defer proxySrv.Close()

// Create a http.Client that uses our proxy
client, err := proxyClient(proxySrv.URL)
r.NoError(err)

// Send a request that should be blocked
resp, err := client.Get("http://127.0.0.1")
r.NoError(err)

// The RejectResponseHandler should set our custom header
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be RejectResponseHandlerWithCtx? (Same in all places in this test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will update this comment.

h := resp.Header.Get(testHeader)
if h == "" {
t.Errorf("Expecting header %s to be set by RejectResponseHandler", testHeader)
}
// Send a request that should be allowed
resp, err = client.Get("http://example.com")
r.NoError(err)

// The header set by our custom reject response handler should not be set
h = resp.Header.Get(testHeader)
if h != "" {
t.Errorf("Expecting header %s to not be set by RejectResponseHandler", testHeader)
}
})
}

// Test that Smokescreen calls the custom accept response handler (if defined in the Config struct)
// after every accepted request
func TestAcceptResponseHandler(t *testing.T) {
Expand Down