Search code examples
prometheusgrafana

How to export Grafana datasource with basic auth details and import it again?


I am not able to export data sources in Grafana with basic auth details.

As I searched on internet, people are downloading datasource JSON by URL <grafana>/api/datasources and uploading same way with another API.

I tried doing that. Everything works fine but for basic auth info, only basicAuth: true is downloaded from above rest call as JSON but not password info. So, importing that JSON does not create proper datasource in Grafana.

Is there a way to smoothly export then import JSON to Grafana for data sources?( Specially for Prometheus data sources)

Thanks in advance.

Updating my scenario here:

What is the requirement: I want my Grafana data sources which have basic auth enabled to get exported and imported to client site:

Why it is needed: It is needed because in my one use case, I configured Prometheus datasource with basic auth and tried exported as json and pushed to GIT. And with help of Jenkins (CI/CD), I needed them to be created/updated in UAT deployment and then to customers with same approach.

Everything was working fine until I did not enable basic auth in Prometheus. Once, I enabled, This continuous integration is breaking because Grafana in UAT and Clients are not working with Prometheus due to Auth issue (because basic auth configs are not exported in JSON):

Can anyone help me resolving this scenario?


Solution

  • To cater my scenario, I did found how to pass the password for the basic auth while importing datasource JSON in Grafana.

    Note: By defining password and basicAuthPassword under secureJsonData Grafana encrypts them securely as an encrypted blob in the database. The response then lists the encrypted fields under secureJsonFields.

    https://grafana.com/docs/grafana/latest/http_api/data_source/

    so I put my password manually in the JSON exported from Grafana like below and then pushed into GIT.

    {
      "name": "ds_prometheus",
      "type": "prometheus",
      "url": "http://my-prometheus:8080",
      "access": "proxy",
      "basicAuth": true,
      "basicAuthUser": "user",
      "secureJsonData": {
        "basicAuthPassword": "password"
      },
      "isDefault": true,
      "jsonData":{"httpMethod":"POST","keepCookies":[],"timeInterval":"30s","queryTimeout":"120s","tlsSkipVerify":true}
    }
    

    So, whenever CI/CD runs, data sources are correctly populated in Grafana with correct credentials.