Skip to content

Commit

Permalink
Datasources: Prevent headers from being set in json_data
Browse files Browse the repository at this point in the history
Closes #1369
There is a dedicated field for those
  • Loading branch information
julienduchesne committed Mar 27, 2024
1 parent 9c3bb51 commit 8cd4ebb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
33 changes: 24 additions & 9 deletions internal/resources/grafana/resource_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafana
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -111,10 +112,17 @@ source selected (via the 'type' argument).
Description: "(Required by some data source types) The username to use to authenticate to the data source.",
},
"json_data_encoded": {
Type: schema.TypeString,
Optional: true,
Description: "Serialized JSON string containing the json data. This attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.",
ValidateFunc: validation.StringIsJSON,
Type: schema.TypeString,
Optional: true,
Description: "Serialized JSON string containing the json data. This attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.",
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
if strings.Contains(i.(string), "httpHeaderName") {
return nil, []error{
errors.New("httpHeaderName{num} is a reserved key and cannot be used in JSON data. Use the http_headers attribute instead"),
}
}
return validation.StringIsJSON(i, s)
},
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
Expand All @@ -127,11 +135,18 @@ source selected (via the 'type' argument).
},
},
"secure_json_data_encoded": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Serialized JSON string containing the secure json data. This attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.",
ValidateFunc: validation.StringIsJSON,
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Serialized JSON string containing the secure json data. This attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.",
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
if strings.Contains(i.(string), "httpHeaderValue") {
return nil, []error{
errors.New("httpHeaderValue{num} is a reserved key and cannot be used in JSON data. Use the http_headers attribute instead"),
}
}
return validation.StringIsJSON(i, s)
},
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
Expand Down
30 changes: 30 additions & 0 deletions internal/resources/grafana/resource_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,36 @@ func TestAccDatasource_inOrg(t *testing.T) {
})
}

func TestAccDataSource_ValidateHttpHeaders(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "grafana_data_source" "influx" {
type = "influxdb"
name = "anything"
url = "http://acc-test.invalid/"
json_data_encoded = jsonencode({
httpHeaderName1 = "Authorization"
defaultBucket = "telegraf"
organization = "organization"
tlsAuth = false
tlsAuthWithCACert = false
version = "Flux"
})
secure_json_data_encoded = jsonencode({
httpHeaderValue1 = "Token sdkfjsdjflkdsjflksjdklfjslkdfjdksljfldksjsflkj"
})
}`,
ExpectError: regexp.MustCompile(`httpHeaderName{num} is a reserved key and cannot be used in JSON data. Use the http_headers attribute instead`),
},
},
})
}

func testAccDatasourceInOrganization(orgName string) string {
return fmt.Sprintf(`
resource "grafana_organization" "test" {
Expand Down

0 comments on commit 8cd4ebb

Please sign in to comment.