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

added secure form data #889

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .changeset/clean-penguins-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'grafana-infinity-datasource': minor
---

Added support for configuring secure HTTP POST form data
4 changes: 4 additions & 0 deletions docs/sources/references/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ You can configure the headers required for the URL in the datasource config and

Note: We suggest adding secure headers only via configuration and not in query.

## Sending secure params as HTTP POST Form data

When you are using HTTP POST method and one of the HTTP Form body types (**Form Data**/**X-WWW-FORM-URLENCODED**), you can pass additional secure form data via config option. The parameters set in config will be set in addition to the form parameters set in the query. You can find the settings under **HTTP Options** in the datasource config

## Allowed Hosts

Leaving blank will allow all the hosts. This is by default.
Expand Down
10 changes: 8 additions & 2 deletions pkg/infinity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (client *Client) GetResults(ctx context.Context, query models.Query, reques
}
switch strings.ToUpper(query.URLOptions.Method) {
case http.MethodPost:
body := GetQueryBody(ctx, query)
body := GetQueryBody(ctx,query, client.Settings)
return client.req(ctx, query.URL, body, client.Settings, query, requestHeaders)
default:
return client.req(ctx, query.URL, nil, client.Settings, query, requestHeaders)
Expand Down Expand Up @@ -311,7 +311,7 @@ func CanAllowURL(url string, allowedHosts []string) bool {
return allow
}

func GetQueryBody(ctx context.Context, query models.Query) io.Reader {
func GetQueryBody(ctx context.Context, query models.Query, settings models.InfinitySettings) io.Reader {
logger := backend.Logger.FromContext(ctx)
var body io.Reader
if strings.EqualFold(query.URLOptions.Method, http.MethodPost) {
Expand All @@ -324,6 +324,9 @@ func GetQueryBody(ctx context.Context, query models.Query) io.Reader {
for _, f := range query.URLOptions.BodyForm {
_ = writer.WriteField(f.Key, f.Value)
}
for k, v := range settings.FormPostItems {
_ = writer.WriteField(k, v)
}
if err := writer.Close(); err != nil {
logger.Error("error closing the query body reader")
return nil
Expand All @@ -334,6 +337,9 @@ func GetQueryBody(ctx context.Context, query models.Query) io.Reader {
for _, f := range query.URLOptions.BodyForm {
form.Set(f.Key, f.Value)
}
for k, v := range settings.FormPostItems {
form.Set(k, v)
}
body = strings.NewReader(form.Encode())
case "graphql":
var variables map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/infinity/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NormalizeURL(u string) string {
func (client *Client) GetExecutedURL(ctx context.Context, query models.Query) string {
out := []string{}
if query.Source != "inline" && query.Source != "azure-blob" {
req, err := GetRequest(ctx, client.Settings, GetQueryBody(ctx, query), query, map[string]string{}, false)
req, err := GetRequest(ctx, client.Settings, GetQueryBody(query, models.InfinitySettings{}), query, map[string]string{}, false)
if err != nil {
return fmt.Sprintf("error retrieving full url. %s", query.URL)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/models/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type InfinitySettings struct {
ForwardOauthIdentity bool
CustomHeaders map[string]string
SecureQueryFields map[string]string
FormPostItems map[string]string
InsecureSkipVerify bool
ServerName string
TimeoutInSeconds int64
Expand Down Expand Up @@ -171,6 +172,9 @@ func (s *InfinitySettings) HaveSecureHeaders() bool {
}
return false
}
if len(s.FormPostItems) > 0 {
return true
}
return false
}

Expand Down Expand Up @@ -294,6 +298,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
}
settings.CustomHeaders = GetSecrets(config, "httpHeaderName", "httpHeaderValue")
settings.SecureQueryFields = GetSecrets(config, "secureQueryName", "secureQueryValue")
settings.FormPostItems = GetSecrets(config, "formPostItemName", "formPostItemValue")
settings.OAuth2Settings.EndpointParams = GetSecrets(config, "oauth2EndPointParamsName", "oauth2EndPointParamsValue")
if settings.AuthenticationMethod == "" {
settings.AuthenticationMethod = AuthenticationMethodNone
Expand Down
18 changes: 15 additions & 3 deletions pkg/models/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestLoadSettings(t *testing.T) {
OAuth2Settings: models.OAuth2Settings{
EndpointParams: map[string]string{},
},
FormPostItems: map[string]string{},
CustomHeaders: map[string]string{},
SecureQueryFields: map[string]string{},
},
Expand All @@ -45,12 +46,14 @@ func TestLoadSettings(t *testing.T) {
JSONData: []byte(`{
"datasource_mode" : "advanced",
"secureQueryName1" : "foo",
"formPostItemName1" : "hello",
"httpHeaderName1" : "header1"
}`),
DecryptedSecureJSONData: map[string]string{
"basicAuthPassword": "password",
"secureQueryValue1": "bar",
"httpHeaderValue1": "headervalue1",
"basicAuthPassword": "password",
"secureQueryValue1": "bar",
"formPostItemValue1": "world",
"httpHeaderValue1": "headervalue1",
},
},
wantSettings: models.InfinitySettings{
Expand All @@ -66,6 +69,9 @@ func TestLoadSettings(t *testing.T) {
OAuth2Settings: models.OAuth2Settings{
EndpointParams: map[string]string{},
},
FormPostItems: map[string]string{
"hello": "world",
},
CustomHeaders: map[string]string{
"header1": "headervalue1",
},
Expand Down Expand Up @@ -105,6 +111,7 @@ func TestLoadSettings(t *testing.T) {
OAuth2Settings: models.OAuth2Settings{
EndpointParams: map[string]string{},
},
FormPostItems: map[string]string{},
CustomHeaders: map[string]string{
"header1": "headervalue1",
},
Expand Down Expand Up @@ -144,6 +151,7 @@ func TestAllSettingsAgainstFrontEnd(t *testing.T) {
"apiKeyType" : "query",
"datasource_mode" : "advanced",
"secureQueryName1" : "foo",
"formPostItemName1": "hello",
"httpHeaderName1" : "header1",
"timeoutInSeconds" : 30,
"tlsAuth" : true,
Expand Down Expand Up @@ -178,6 +186,7 @@ func TestAllSettingsAgainstFrontEnd(t *testing.T) {
"tlsClientKey": "myTlsClientKey",
"basicAuthPassword": "password",
"secureQueryValue1": "bar",
"formPostItemValue1": "world",
"httpHeaderValue1": "headervalue1",
"apiKeyValue": "earth",
"bearerToken": "myBearerToken",
Expand Down Expand Up @@ -244,6 +253,9 @@ func TestAllSettingsAgainstFrontEnd(t *testing.T) {
CustomHealthCheckEnabled: true,
CustomHealthCheckUrl: "https://foo-check/",
UnsecuredQueryHandling: models.UnsecuredQueryHandlingDeny,
FormPostItems: map[string]string{
"hello": "world",
},
CustomHeaders: map[string]string{
"header1": "headervalue1",
},
Expand Down
14 changes: 12 additions & 2 deletions src/editors/config.editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ export const HeadersEditor = (props: DataSourcePluginOptionsEditorProps<Infinity
<Collapse isOpen={true} collapsible={true} label="URL Query Param">
<SecureFieldsEditor dataSourceConfig={options} onChange={onOptionsChange} title="URL Query Param" secureFieldName="secureQueryName" secureFieldValue="secureQueryValue" hideTile={true} />
</Collapse>
<Collapse isOpen={true} collapsible={true} label="HTTP POST form data parameter">
<SecureFieldsEditor
dataSourceConfig={options}
onChange={onOptionsChange}
title="HTTP POST form data parameter"
secureFieldName="formPostItemName"
secureFieldValue="formPostItemValue"
hideTile={true}
/>
</Collapse>
<Collapse isOpen={true} collapsible={true} label="Query Param Encoding (EXPERIMENTAL)">
<QueryParamEditor options={options} onOptionsChange={onOptionsChange} />
</Collapse>
Expand Down Expand Up @@ -138,7 +148,7 @@ export const MiscEditor = (props: DataSourcePluginOptionsEditorProps<InfinityOpt
const config_sections: Array<{ value: string; label: string }> = [
{ value: 'main', label: 'Main' },
{ value: 'auth', label: 'Authentication' },
{ value: 'headers_and_params', label: 'Headers & URL params' },
{ value: 'http_options', label: 'HTTP Options' },
{ value: 'network', label: 'Network' },
{ value: 'security', label: 'Security' },
{ value: 'health_check', label: 'Health check' },
Expand Down Expand Up @@ -214,7 +224,7 @@ export const InfinityConfigEditor = (props: DataSourcePluginOptionsEditorProps<I
<MainEditor options={options} onOptionsChange={onOptionsChange} setActiveTab={setActiveTab} />
) : activeTab === 'auth' ? (
<AuthEditor options={options} onOptionsChange={onOptionsChange} />
) : activeTab === 'headers_and_params' ? (
) : activeTab === 'http_options' ? (
<HeadersEditor options={options} onOptionsChange={onOptionsChange} />
) : activeTab === 'network' ? (
<NetworkEditor options={options} onOptionsChange={onOptionsChange} />
Expand Down
Loading