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

Introduce interface to ease forward/access HTTP headers #562

Merged
merged 8 commits into from Dec 15, 2022

Conversation

marefr
Copy link
Member

@marefr marefr commented Nov 15, 2022

In grafana/grafana#58132 we're type switching on different backend request types, see code.

Introduce a new interface ForwardHTTPHeaders would allow us to typeswitch/typecast to that interface instead and simplify the code.

From plugin authors perspective we have documentation for Forward OAuth identity for the logged-in user, Forward cookies for the logged-in user and soon for Forward user header and we provides instruction for accessing these forwarded headers by access the request.Headers field. This field is different for different request types:

  • For QueryDataRequest and CheckHealthRequest there's a map[string]string (they're suppose to hold environment context/metadata
  • For CallResourceRequest there's a map[string][]string (suppose to hold HTTP headers since it's HTTP proxy over gRPC basically.

With these changes, rather than accessing request.Headers directly to access HTTP headers it's suggested that they use request.GetHTTPHeader(<key>) instead, e.g. request.GetHTTPHeader(backend.OAuthIdentityTokenHeaderName) or request.GetHTTPHeader(backend.OAuthIdentityIDTokenHeaderName), to access Forwarded OAuth Identity headers.

Using request.GetHTTPHeader(<key>) or ``request.GetHTTPHeaders()` would also automatically handle canonical/non-canonical form problems, i.e. grafana/grafana#58598

A potential alternative could be to send forwarded HTTP headers via gRPC metadata, but not sure if that can affect the gRPC connection since it uses HTTP2.

WDYT?`

@marefr marefr self-assigned this Nov 15, 2022
@marefr marefr changed the title WIP: Introduce interface to ease forwarding of HTTP headers WIP: Introduce interface to ease forward/access HTTP headers Nov 15, 2022
@marefr marefr requested review from xnyo and wbrowne November 15, 2022 12:24
backend/data.go Outdated
req.Headers = map[string]string{}
}

req.Headers[fmt.Sprintf("http_%s", key)] = value
Copy link
Member Author

@marefr marefr Nov 15, 2022

Choose a reason for hiding this comment

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

Reason to this would be to allow the environment info to hold other things than HTTP headers.

Main goal being to not always forward all environment info in outgoing HTTP headers.

Not sure it make sense 🤷

Copy link
Member

@xnyo xnyo left a comment

Choose a reason for hiding this comment

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

Awesome work, I really like this idea 👏 👏 👏 👏
I have left some comments/questions:

backend/data.go Outdated Show resolved Hide resolved
backend/data.go Outdated Show resolved Hide resolved
backend/data.go Outdated Show resolved Hide resolved
refactor a bit, adds tests, adds proper documentation of methods and fields.
@marefr marefr requested a review from xnyo December 5, 2022 21:40
@marefr marefr marked this pull request as ready for review December 6, 2022 05:05
@marefr marefr requested a review from a team as a code owner December 6, 2022 05:05
@marefr marefr changed the title WIP: Introduce interface to ease forward/access HTTP headers Introduce interface to ease forward/access HTTP headers Dec 6, 2022
Copy link
Member

@xnyo xnyo left a comment

Choose a reason for hiding this comment

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

Amazing 🥇

LGTM! 🚀 🚀 🚀

@marefr marefr merged commit 1e4af2c into main Dec 15, 2022
@marefr marefr deleted the poc_forward_headers branch December 15, 2022 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants