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

feat(httpclient): support appending the same header multiple times #830

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

sathieu
Copy link
Contributor

@sathieu sathieu commented Dec 5, 2023

What this PR does / why we need it:

See #829

Which issue(s) this PR fixes:

Fixes #829

Special notes for your reviewer:

@sathieu sathieu requested a review from a team as a code owner December 5, 2023 10:28
@sathieu sathieu requested review from wbrowne, marefr and xnyo and removed request for a team December 5, 2023 10:28
@CLAassistant
Copy link

CLAassistant commented Dec 5, 2023

CLA assistant check
All committers have signed the CLA.

@marefr
Copy link
Contributor

marefr commented Dec 8, 2023

Please sync with main and resolve CI failure and we'll review this. Thanks

mage -v lint
go: downloading github.com/magefile/mage v1.15.0
Running target: Lint
exec: golangci-lint "run" "./..."
experimental/authclient/authclient_test.go:1: : # github.com/grafana/grafana-plugin-sdk-go/experimental/authclient_test [github.com/grafana/grafana-plugin-sdk-go/experimental/authclient.test]
experimental/authclient/authclient_test.go:34:15: cannot use map[string]string{…} (value of type map[string]string) as map[string][]string value in struct literal
experimental/authclient/authclient_test.go:57:17: cannot use map[string]string{…} (value of type map[string]string) as map[string][]string value in struct literal
experimental/authclient/authclient_test.go:84:15: cannot use map[string]string{…} (value of type map[string]string) as map[string][]string value in struct literal
experimental/authclient/authclient_test.go:106:15: cannot use map[string]string{…} (value of type map[string]string) as map[string][]string value in struct literal (typecheck)
package authclient_test
Error: running "golangci-lint run ./..." failed with exit code 1

@sathieu sathieu force-pushed the multiple-same-headers branch from 0314907 to f8fe86a Compare December 11, 2023 10:02
@sathieu
Copy link
Contributor Author

sathieu commented Dec 11, 2023

@marefr I've fixed the test. back to you 🏸 !

@sathieu sathieu force-pushed the multiple-same-headers branch from f8fe86a to c6c0ded Compare December 11, 2023 10:11
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

I think that in general, it makes sense to use the same type than the upstream HTTP header: https://pkg.go.dev/net/http#Header but I am worried about the breaking change. If we do this, we should prepare at least an adapting PR for grafana/grafana.

Regarding the original issue described in #829, I wonder if a possible workaround would be to define the multiple headers as a comma-separated list. @sathieu would that work?

@@ -16,7 +16,7 @@ type HTTPSettings struct {
BasicAuthEnabled bool
BasicAuthUser string
BasicAuthPassword string
Headers map[string]string
Headers map[string][]string
Copy link
Contributor

Choose a reason for hiding this comment

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

I would just go ahead and define this as a http.Header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea @andresmgot.

I have done that, and renamed from Headers to Header, as I broke API anyway (and this is now same as net/http.

@sathieu
Copy link
Contributor Author

sathieu commented Dec 14, 2023

Thanks for your review @andresmgot.

I think that in general, it makes sense to use the same type than the upstream HTTP header: https://pkg.go.dev/net/http#Header but I am worried about the breaking change. If we do this, we should prepare at least an adapting PR for grafana/grafana.

I prepared a PR in grafana at grafana/grafana#79543.

Regarding the original issue described in #829, I wonder if a possible workaround would be to define the multiple headers as a comma-separated list. @sathieu would that work?

Unfortunately, comma-separated header doesn't work for prom-label-proxy (see code). So this PR is needed.

Back to you!

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

Thanks, one minor comment here

backend/http_settings.go Outdated Show resolved Hide resolved
@sathieu sathieu force-pushed the multiple-same-headers branch from bbc71f5 to 3dee90d Compare December 18, 2023 20:26
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

LGTM, any other thought @marefr ?

@academo
Copy link
Member

academo commented Feb 28, 2024

@andresmgot @marefr is there anything blocking this PR from merging?

Copy link
Contributor

@marefr marefr left a comment

Choose a reason for hiding this comment

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

LGTM, but a question before merging

@@ -273,7 +274,7 @@ func parseHTTPSettings(jsonData json.RawMessage, secureJSONData map[string]strin

if key, exists := dat[headerNameSuffix]; exists {
if value, exists := secureJSONData[headerValueSuffix]; exists {
s.Headers[key.(string)] = value
s.Header.Add(key.(string), value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't understand why key needs to be cast to a string - isn't it already a string since dat is map[string]interface{}?

Copy link
Contributor Author

@sathieu sathieu Mar 8, 2024

Choose a reason for hiding this comment

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

dat is map[string]interface{}, so key := dat[headerNameSuffix] is interface{}.

On the other side secureJSONData is map[string]string, so value doesn't need cast.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yeah I see that now 👍

@marefr
Copy link
Contributor

marefr commented Mar 6, 2024

@sathieu please sync with main branch to resolve conflicts and see my comment. Thanks

@sathieu sathieu force-pushed the multiple-same-headers branch from 3dee90d to 5a057a4 Compare March 8, 2024 14:19
@sathieu
Copy link
Contributor Author

sathieu commented Mar 8, 2024

@marefr I've rebased and resolved conflicts. Casting is needed.

Back to you 🏓

@andresmgot
Copy link
Contributor

hi @sathieu, there are some other files that need an update: https://drone.grafana.net/grafana/grafana-plugin-sdk-go/1981/1/3

Also, can you re-open / refresh grafana/grafana#79543? (sorry, it got closed due to inactivity)

@marefr
Copy link
Contributor

marefr commented Mar 11, 2024

@sathieu back to you. Not sure whether we really should remove Headers and add Header. I would suggest to keep Headers. WDYT? Also, lint problems

mage -v lint
go: downloading github.com/magefile/mage v1.15.0
Running target: Lint
exec: golangci-lint "run" "./..."
backend/httpclient/basic_auth_middleware.go:1: : # github.com/grafana/grafana-plugin-sdk-go/backend/httpclient [github.com/grafana/grafana-plugin-sdk-go/backend/httpclient.test]
backend/httpclient/custom_headers_middleware_test.go:67:48: unknown field Headers in struct literal of type Options (typecheck)
package httpclient
backend/httpclient/contextual_middleware_example_test.go:8:2: could not import github.com/grafana/grafana-plugin-sdk-go/backend/httpclient (-: # github.com/grafana/grafana-plugin-sdk-go/backend/httpclient [github.com/grafana/grafana-plugin-sdk-go/backend/httpclient.test]
backend/httpclient/custom_headers_middleware_test.go:67:48: unknown field Headers in struct literal of type Options) (typecheck)
	"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
	^
Error: running "golangci-lint run ./..." failed with exit code 1
``

@sathieu
Copy link
Contributor Author

sathieu commented Mar 11, 2024

@andresmgot :

hi @sathieu, there are some other files that need an update: https://drone.grafana.net/grafana/grafana-plugin-sdk-go/1981/1/3

Fixed, rebased and pushed.

Also, can you re-open / refresh grafana/grafana#79543? (sorry, it got closed due to inactivity)

I can't reopen. I've created a new PR: grafana/grafana#84193

@sathieu
Copy link
Contributor Author

sathieu commented Mar 11, 2024

@marefr

I think Header is more appropriate, like http.Request, but I can change to Headers if you want.

Copy link
Contributor

@marefr marefr left a comment

Choose a reason for hiding this comment

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

LGTM sure let's use Header

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

Successfully merging this pull request may close these issues.

Allow to set the same header multiple time in Prometheus Datascources
5 participants