如何导出具有基本身份验证详细信息的 Grafana 数据源并再次导入?

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

我无法使用基本身份验证详细信息导出 Grafana 中的数据源。

当我在互联网上搜索时,人们正在通过 URL <grafana>/api/datasources 下载数据源 JSON 并以相同的方式上传另一个 API。

我试过了。一切正常,但对于基本身份验证信息,只有 basicAuth: true 从上面的 rest 调用下载为 JSON 而不是密码信息。因此,导入 JSON 不会在 Grafana 中创建正确的数据源。

有没有办法可以顺利导出再导入JSON到Grafana做数据源?(专门针对Prometheus数据源)

提前致谢。

在这里更新我的场景:

要求是什么: 我希望启用基本身份验证的 Grafana 数据源能够导出和导入到客户端站点:

为什么需要: 它是必需的,因为在我的一个用例中,我使用基本身份验证配置了 Prometheus 数据源,并尝试导出为 json 并推送到 GIT。在 Jenkins (CI/CD) 的帮助下,我需要他们在 UAT 部署中 created/updated,然后用相同的方法提供给客户。

一切正常,直到我没有在 Prometheus 中启用基本身份验证。有一次,我启用了这种持续集成,因为 UAT 中的 Grafana 和客户端由于 Auth 问题无法使用 Prometheus(因为基本身份验证配置未在 JSON 中导出):

谁能帮我解决这个问题?

没有。凭据存储在 secureJsonFields 中,这些字段无法通过 API 获得。这是一个功能而不是错误 - https://github.com/grafana/grafana/issues/20274.

为了满足我的情况, 我确实找到了如何在 Grafana 中导入数据源时传递基本身份验证的密码 JSON。

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/

所以我手动将密码放在从 Grafana 导出的 JSON 中,如下所示,然后推入 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}
}

因此,每当 CI/CD 运行时,数据源都会使用正确的凭据正确填充到 Grafana 中。