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

Fixing ResponseWriter to assert to http.Flusher in /sys/monitor endpoint #13200

Merged
merged 5 commits into from
Nov 23, 2021

Conversation

hghaf099
Copy link
Contributor

@hghaf099 hghaf099 commented Nov 18, 2021

There are a number of interfaces that implement http.ResponseWriter. For that, wrapping http.ResponseWriter needs a lot of care.
StatusHeaderResponseWriter is instantiated in the http layer which wraps http.ResponseWriter. Then, HTTPResponseWriter wraps the already wrapped ResponseWriter. This causes issues as for example in the sys/monitor, the http.Flusher cannot be asserted and calling the endpoint returns stream not supported

@hghaf099 hghaf099 changed the title Unify NewHTTPResponseWriter ant NewStatusHeaderResponseWriter to fix … Unify HTTPResponseWriter and StatusHeaderResponseWriter Nov 18, 2021
@vercel vercel bot temporarily deployed to Preview – vault November 18, 2021 07:57 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook November 18, 2021 07:57 Inactive
http/handler.go Outdated
statusCode: 200,
headers: listenerCustomHeaders.StatusCodeHeaderMap,
}
nw.SetHeaders(listenerCustomHeaders.StatusCodeHeaderMap)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why introduce a method for this instead of just making it an argument to the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new responseWriter nw needs to be instantiated even if the props.ListenerConfig is nil.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I'm not following. What would break if we got rid of SetHeaders and instead assigned listenerCustomHeaders.StatusCodeHeaderMap to a local var here? Then passed that var as a 2nd arg to logical.NewStatusHeaderResponseWriter?

My primary goal here is to minimize mutable state in the type.

http/logical.go Outdated
@@ -199,7 +199,7 @@ func buildLogicalRequestNoAuth(perfStandby bool, w http.ResponseWriter, r *http.
req.HTTPRequest = r
}
if responseWriter != nil {
req.ResponseWriter = logical.NewHTTPResponseWriter(responseWriter)
req.ResponseWriter = logical.NewStatusHeaderResponseWriter(responseWriter)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to re-wrap? Why can't we just assign responseWriter directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, responseWriter is of type http.ResponseWriter. if I assign that directly, then, where ever we would need extra functionality like ResponseWriter.Written(), we would need to assert the WrappingResponseWriter interface first. Although there are not many places right now for such functionality, it is easy to miss fixing those. It is not just the Written() function as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it though? I thought everything was wrapped by wrapGenericHandler, and it's always passing down a response writer of the new type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, I think so? The function signature of this function defines w http.ResponseWriter which is assigned to responseWriter. But, another thing is that of which type should the req.ResponseWriter be defined? should it be http.ResponseWriter, or logical.StatusHeaderResponseWriter?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wasn't speaking in terms of function signature, I'm saying that since wrapGenericHandler is wrapping all request handlers, we shouldn't need to wrap the response handler again. And if we don't need to handle re-wrapping I think some of the code will be simplified.

I think we should probably stick with http.ResponseWriter, but that's mostly because I expect trying to switch it would cause problems. If it's possible to use StatusHeaderResponseWriter without it being really disruptive, that could make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! I will stick with http.ResponseWriter.

Wrapped() http.ResponseWriter
SetHeaders(h map[string][]*CustomHeader)
GetHeaders() map[string][]*CustomHeader
StatusCode() int
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 actually used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found the issue in this PR while I was working at the logging requests feature. So, this is not needed now, but it is needed then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, this is not needed. Fixing it in both PRs. Thanks!

@vercel vercel bot temporarily deployed to Preview – vault November 19, 2021 16:03 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook November 19, 2021 16:03 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook November 22, 2021 22:30 Inactive
@vercel vercel bot temporarily deployed to Preview – vault November 22, 2021 22:30 Inactive
Copy link
Collaborator

@ncabatoff ncabatoff left a comment

Choose a reason for hiding this comment

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

Looking very close!

@@ -203,7 +203,7 @@ type Request struct {

// ResponseWriter if set can be used to stream a response value to the http
// request that generated this logical.Request object.
ResponseWriter *HTTPResponseWriter `json:"-" sentinel:""`
ResponseWriter *http.ResponseWriter `json:"-" sentinel:""`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd rather use the explicit type here than the interface, since there aren't any cases where we'll be using anything other than that type.

@@ -127,8 +128,9 @@ func TestCustomResponseHeadersConfigInteractUiConfig(t *testing.T) {
t.Fatalf("custom header config should be configured in core")
}

w := httptest.NewRecorder()
hw := logical.NewHTTPResponseWriter(w)
hw := func(w http.ResponseWriter) *http.ResponseWriter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a rather complicated way to avoid defining a variable.

http/handler.go Outdated
statusCode: 200,
headers: listenerCustomHeaders.StatusCodeHeaderMap,
}
nw.SetHeaders(listenerCustomHeaders.StatusCodeHeaderMap)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I'm not following. What would break if we got rid of SetHeaders and instead assigned listenerCustomHeaders.StatusCodeHeaderMap to a local var here? Then passed that var as a 2nd arg to logical.NewStatusHeaderResponseWriter?

My primary goal here is to minimize mutable state in the type.

written *uint32
}

func NewStatusHeaderResponseWriter (w http.ResponseWriter) *StatusHeaderResponseWriter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func NewStatusHeaderResponseWriter (w http.ResponseWriter) *StatusHeaderResponseWriter {
func NewStatusHeaderResponseWriter(w http.ResponseWriter) *StatusHeaderResponseWriter {

@vercel vercel bot temporarily deployed to Preview – vault November 23, 2021 18:30 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook November 23, 2021 18:30 Inactive
@hghaf099 hghaf099 marked this pull request as ready for review November 23, 2021 19:18
@hghaf099 hghaf099 merged commit 258193c into main Nov 23, 2021
hghaf099 added a commit that referenced this pull request Nov 23, 2021
* Unify NewHTTPResponseWriter ant NewStatusHeaderResponseWriter to fix ResponseWriter issues

* adding changelog

* removing unnecessary function from the WrappingResponseWriter interface

* changing logical requests responseWriter type

* reverting change to HTTPResponseWriter
@hghaf099 hghaf099 added this to the 1.9.1 milestone Nov 23, 2021
@hghaf099 hghaf099 changed the title Unify HTTPResponseWriter and StatusHeaderResponseWriter Fixing ResponseWriter to assert to http.Flusher in /sys/monitor endpoint Nov 23, 2021
hghaf099 added a commit that referenced this pull request Nov 24, 2021
…ys/monitor endpoint (#13200) (#13260)

* Unify HTTPResponseWriter and StatusHeaderResponseWriter (#13200)

* Unify NewHTTPResponseWriter ant NewStatusHeaderResponseWriter to fix ResponseWriter issues

* adding changelog

* removing unnecessary function from the WrappingResponseWriter interface

* changing logical requests responseWriter type

* reverting change to HTTPResponseWriter

* Update changelog/13200.txt

* Update changelog/13200.txt
qk4l pushed a commit to qk4l/vault that referenced this pull request Feb 4, 2022
)

* Unify NewHTTPResponseWriter ant NewStatusHeaderResponseWriter to fix ResponseWriter issues

* adding changelog

* removing unnecessary function from the WrappingResponseWriter interface

* changing logical requests responseWriter type

* reverting change to HTTPResponseWriter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants