Grafana Datasource Backend Proxy

Trying to do a use case for the following:

  1. Submit request to URL foo with payload from basicAuthUser and basicAuthPassword
  2. URL foo returns payload with Bearer token
  3. Use bearer token to log into and auth to instanceSettings.url for the session
  4. Do all of the above through the “backend proxy”

Does anyone know how to interact directly with the backend proxy with multiple URL’s?

Can you explain a bit more - are you developing a plugin or connecting to an existing data source?

1 Like

Developing a new datasource. Our new datasource is integrated with a third party oauth provider so we have to make 2 calls initially one to the oauth provider and then one to the “datasource” endpoint. Both of these are “backend” proxy calls. If we try to just submit through the backendSrv it turns it into a direct call vs proxy which then gives us CORS errors. How could we make 2 backendSrv calls to 2 different URL’s?

You can specify multiple routes in your plugin.json file:

Then when you build your url in your datasource class, the url should start with the text specified in the path field for a route. The proxy will strip out the path text and replace it with the value in the url field.

For example, if my code makes a call to url azuremonitor/foo/bar with this code:

this.backendSrv
      .datasourceRequest({
        url: url,
        method: 'GET',
      })

then the Grafana proxy will transform it into “https://management.azure.com/foo/bar” and add CORS headers.

In your case, it sounds like you only need one route but with a token auth section:

"tokenAuth": {
        "url": "https://login.microsoftonline.com/{{.JsonData.tenantId}}/oauth2/token",
        "params": {
          "grant_type":  "client_credentials",
          "client_id": "{{.JsonData.clientId}}",
          "client_secret": "{{.SecureJsonData.clientSecret}}",
          "resource": "https://management.azure.com/"
        }
      }

This interpolates in data from jsonData and secureJsonData. These values are saved in the data source config page.

This is extremely helpful thank you very much!!!