Skip to content

Commit

Permalink
allow meta variables in query params
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Nov 28, 2024
1 parent 9a11121 commit 27a37ce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/sources/references/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ 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.

### Forwarding Grafana Headers via settings
## Forwarding Grafana meta data as headers / query params

From Infinity plugin version 2.12.0, You will be able to forward grafana headers to the outgoing requests via **Custom HTTP Headers** from the datasource settings page. In the datasource custom headers settings section, you can add any number of custom headers with their own values. The values can include following macros which will be interpolated into actual value from the request context.
From Infinity plugin version 2.12.0, You will be able to forward grafana meta data such as user id, datasource uid to the outgoing requests via **Custom HTTP Headers** / **URL Query parameters\*** from the datasource settings page. In the datasource **URL** section, you can add any number of custom headers / query parameters with their own values. The values can include following macros which will be interpolated into actual value from the request context.

| Macro name | Description |
| --------------------- | ------------------------------------------------------------------- |
Expand Down
17 changes: 1 addition & 16 deletions pkg/infinity/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,7 @@ func ApplyHeadersFromSettings(pCtx *backend.PluginContext, requestHeaders map[st
if includeSect {
headerValue = value
}
if pCtx != nil {
headerValue = strings.ReplaceAll(headerValue, "${__org.id}", fmt.Sprintf("%d", pCtx.OrgID))
headerValue = strings.ReplaceAll(headerValue, "${__plugin.id}", pCtx.PluginID)
headerValue = strings.ReplaceAll(headerValue, "${__plugin.version}", pCtx.PluginVersion)
if pCtx.DataSourceInstanceSettings != nil {
headerValue = strings.ReplaceAll(headerValue, "${__ds.uid}", pCtx.DataSourceInstanceSettings.UID)
headerValue = strings.ReplaceAll(headerValue, "${__ds.name}", pCtx.DataSourceInstanceSettings.Name)
headerValue = strings.ReplaceAll(headerValue, "${__ds.id}", fmt.Sprintf("%d", pCtx.DataSourceInstanceSettings.ID))
}
if pCtx.User != nil {
headerValue = strings.ReplaceAll(headerValue, "${__user.login}", pCtx.User.Login)
headerValue = strings.ReplaceAll(headerValue, "${__user.email}", pCtx.User.Email)
headerValue = strings.ReplaceAll(headerValue, "${__user.name}", pCtx.User.Name)
// headerValue = strings.ReplaceAll(headerValue, "${__user.role}", pCtx.User.Role)
}
}
headerValue = interpolateGrafanaMetaDataMacros(headerValue, pCtx)
if key != "" {
req.Header.Set(key, headerValue)
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/infinity/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func GetRequest(ctx context.Context, pCtx *backend.PluginContext, settings models.InfinitySettings, body io.Reader, query models.Query, requestHeaders map[string]string, includeSect bool) (req *http.Request, err error) {
ctx, span := tracing.DefaultTracer().Start(ctx, "GetRequest")
defer span.End()
url, err := GetQueryURL(ctx, settings, query, includeSect)
url, err := GetQueryURL(ctx, pCtx, settings, query, includeSect)
if err != nil {
return nil, err
}
Expand All @@ -38,7 +38,7 @@ func GetRequest(ctx context.Context, pCtx *backend.PluginContext, settings model
return req, err
}

func GetQueryURL(ctx context.Context, settings models.InfinitySettings, query models.Query, includeSect bool) (string, error) {
func GetQueryURL(ctx context.Context, pCtx *backend.PluginContext, settings models.InfinitySettings, query models.Query, includeSect bool) (string, error) {
_, span := tracing.DefaultTracer().Start(ctx, "GetQueryURL")
defer span.End()
urlString := query.URL
Expand All @@ -60,6 +60,7 @@ func GetQueryURL(ctx context.Context, settings models.InfinitySettings, query mo
if includeSect {
val = value
}
val = interpolateGrafanaMetaDataMacros(val, pCtx)
q.Set(key, val)
}
if settings.AuthenticationMethod == models.AuthenticationMethodApiKey && settings.ApiKeyType == models.ApiKeyTypeQuery {
Expand Down Expand Up @@ -124,3 +125,22 @@ func (client *Client) GetExecutedURL(ctx context.Context, query models.Query) st
}
return strings.Join(out, "\n")
}

func interpolateGrafanaMetaDataMacros(value string, pCtx *backend.PluginContext) string {
if pCtx != nil {
value = strings.ReplaceAll(value, "${__org.id}", fmt.Sprintf("%d", pCtx.OrgID))
value = strings.ReplaceAll(value, "${__plugin.id}", pCtx.PluginID)
value = strings.ReplaceAll(value, "${__plugin.version}", pCtx.PluginVersion)
if pCtx.DataSourceInstanceSettings != nil {
value = strings.ReplaceAll(value, "${__ds.uid}", pCtx.DataSourceInstanceSettings.UID)
value = strings.ReplaceAll(value, "${__ds.name}", pCtx.DataSourceInstanceSettings.Name)
value = strings.ReplaceAll(value, "${__ds.id}", fmt.Sprintf("%d", pCtx.DataSourceInstanceSettings.ID))
}
if pCtx.User != nil {
value = strings.ReplaceAll(value, "${__user.login}", pCtx.User.Login)
value = strings.ReplaceAll(value, "${__user.email}", pCtx.User.Email)
value = strings.ReplaceAll(value, "${__user.name}", pCtx.User.Name)
}
}
return value
}
15 changes: 13 additions & 2 deletions pkg/infinity/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (
"github.com/stretchr/testify/require"
)

func Test_getQueryURL(t *testing.T) {
func TestGetQueryURL(t *testing.T) {
tests := []struct {
name string
settings models.InfinitySettings
query models.Query
pCtx *backend.PluginContext
excludeSecret bool
want string
}{
Expand Down Expand Up @@ -187,10 +188,20 @@ func Test_getQueryURL(t *testing.T) {
},
want: "https://foo.com/hello?foo=bar&key=val10&key=val11%20val%2B12&key2=value2%2Bvalue3",
},
{
name: "should respect override from settings",
pCtx: &backend.PluginContext{User: &backend.User{Login: "testuser"}},
settings: models.InfinitySettings{SecureQueryFields: map[string]string{"X-Grafana-User": "${__user.login}"}},
query: models.Query{
URL: "http://foo.com",
URLOptions: models.URLOptions{Params: []models.URLOptionKeyValuePair{{Key: "X-Grafana-User", Value: "fake-user"}}},
},
want: "http://foo.com?X-Grafana-User=testuser",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := infinity.GetQueryURL(context.TODO(), tt.settings, tt.query, !tt.excludeSecret)
u, err := infinity.GetQueryURL(context.TODO(), tt.pCtx, tt.settings, tt.query, !tt.excludeSecret)
assert.Equal(t, err, nil)
assert.Equal(t, tt.want, u)
})
Expand Down

0 comments on commit 27a37ce

Please sign in to comment.