-
Notifications
You must be signed in to change notification settings - Fork 272
Add support for providing additional HTTP headers #134
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
Conversation
da7d369 to
02098da
Compare
inkel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gw0 for your contribution! Overall I like the idea, although I've left some comments.
grafana/provider.go
Outdated
| DefaultFunc: schema.EnvDefaultFunc("GRAFANA_AUTH", nil), | ||
| Description: "Credentials for accessing the Grafana API.", | ||
| }, | ||
| "headers": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
headers is too generic, can you rename it to http_headers? It also maps better to the environment variable name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
grafana/provider.go
Outdated
| headersObj := d.Get("headers").(map[string]interface{}) | ||
| if headersObj != nil && len(headersObj) == 0 { | ||
| // Workaround for a bug when DefaultFunc returns a TypeMap | ||
| headersObjAbs, _ := JsonEnvDefaultFunc("GRAFANA_HTTP_HEADERS", nil)() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you deciding to ignore the error here? If GRAFANA_HTTP_HEADERS contains invalid JSON no error will be reported to the user but the provider will continue to work, so the end user won't know that their configuration was ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, returning the error in this case.
grafana/provider.go
Outdated
| } | ||
|
|
||
| headersObj := d.Get("headers").(map[string]interface{}) | ||
| if headersObj != nil && len(headersObj) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified, as a nil map will always have a len of 0:
| if headersObj != nil && len(headersObj) == 0 { | |
| if len(headersObj) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted, because it results in:
panic: interface conversion: interface {} is nil, not map[string]interface {} [recovered]
panic: interface conversion: interface {} is nil, not map[string]interface {}
grafana/provider.go
Outdated
| switch v := v.(type) { | ||
| case string: | ||
| headers[k] = v | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a single case type conversion, it's better to use a type assertion instead:
| switch v := v.(type) { | |
| case string: | |
| headers[k] = v | |
| } | |
| if v, ok := v.(string); ok { | |
| headers[k] = v | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified.
326804a to
3f91970
Compare
|
@inkel Thank you for your feedback. I rebased on top of master and included your suggested changes. |
84d3c60 to
3bbfcae
Compare
3bbfcae to
aa9f394
Compare
|
Fixed issues discovered by CI. |
julienduchesne
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Following #134 I had a bit of trouble making sure that the changes were working correctly so I decided to add a unit test for the provider configuration Changes to PR #134: - Add sub-elem string type - Remove http_headers DefaultFunc, it isn't being called - Add provider configure tests to test that configuring headers works both from env and explicitely Other changes: - Add unit test pipeline to CI
|
Thank you @gw0 for the changes! |
* Add provider configure unit test Following #134 I had a bit of trouble making sure that the changes were working correctly so I decided to add a unit test for the provider configuration Changes to PR #134: - Add sub-elem string type - Remove http_headers DefaultFunc, it isn't being called - Add provider configure tests to test that configuring headers works both from env and explicitely Other changes: - Add unit test pipeline to CI * lint
Sometimes the Grafana server is behind a proxy or application firewall and it is necessary to provide additional HTTP headers to establish a connection, such as authentication tokens or cookies. In general sense these are represented as key-value pairs of strings. To also add support to provide these using an environment variable
GRAFANA_HTTP_HEADERS, this PR assumes it contains a JSON-encoded map.Usage:
$ GRAFANA_HTTP_HEADERS='{"Cookies":"foo=bar"}' terraform planThis PR is based on #119 and depends on PR grafana/grafana-api-golang-client#16.