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

Datasources: Prevent headers from being set in json_data #1447

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading