Skip to content

Conversation

gw0
Copy link
Contributor

@gw0 gw0 commented Nov 2, 2020

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:

provider "grafana" {
  url  = "http://172.17.0.1:3000"
  auth = "admin:admin"
  headers = {
    "Cookies" = "foo=bar"
  }
}
$ GRAFANA_HTTP_HEADERS='{"Cookies":"foo=bar"}' terraform plan

This PR is based on #119 and depends on PR grafana/grafana-api-golang-client#16.

Copy link
Contributor

@inkel inkel left a 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.

@@ -28,6 +30,14 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_AUTH", nil),
Description: "Credentials for accessing the Grafana API.",
},
"headers": {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

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)()
Copy link
Contributor

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.

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, returning the error in this case.

@@ -58,10 +68,46 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
} else {
cfg.APIKey = auth[0]
}

headersObj := d.Get("headers").(map[string]interface{})
if headersObj != nil && len(headersObj) == 0 {
Copy link
Contributor

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:

Suggested change
if headersObj != nil && len(headersObj) == 0 {
if len(headersObj) == 0 {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified.

Copy link
Contributor Author

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 {}

Comment on lines 82 to 297
switch v := v.(type) {
case string:
headers[k] = v
}
Copy link
Contributor

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:

Suggested change
switch v := v.(type) {
case string:
headers[k] = v
}
if v, ok := v.(string); ok {
headers[k] = v
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified.

@gw0 gw0 force-pushed the gw0/add-http-headers-2 branch from 326804a to 3f91970 Compare March 1, 2022 11:01
@gw0 gw0 requested a review from a team as a code owner March 1, 2022 11:01
@gw0
Copy link
Contributor Author

gw0 commented Mar 1, 2022

@inkel Thank you for your feedback. I rebased on top of master and included your suggested changes.

@gw0 gw0 force-pushed the gw0/add-http-headers-2 branch 3 times, most recently from 84d3c60 to 3bbfcae Compare March 1, 2022 11:10
@gw0 gw0 force-pushed the gw0/add-http-headers-2 branch from 3bbfcae to aa9f394 Compare March 1, 2022 11:19
@gw0
Copy link
Contributor Author

gw0 commented Mar 1, 2022

Fixed issues discovered by CI.

Copy link
Contributor

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

LGTM!

@julienduchesne julienduchesne merged commit a3b4ed2 into grafana:master Mar 1, 2022
julienduchesne added a commit that referenced this pull request Mar 1, 2022
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
@inkel
Copy link
Contributor

inkel commented Mar 3, 2022

Thank you @gw0 for the changes!

julienduchesne added a commit that referenced this pull request Mar 3, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants