作为 Terraform 部署的一部分,我想部署 Azure Managed Grafana 和一些 Grafana 资源(使用 this 提供程序)。托管 Grafana 与 AD 集成,可以使用 AAD 令牌调用 Grafanas HTTP API(az cli 正在这样做),只是不知道如何生成它以及如何向 Terraform 提供程序进行身份验证。
编辑: 所以我的临时解决方案是在运行 terraform plan / apply end 之前生成令牌,将其设置为环境变量 - 不适合开发。
export GRAFANA_AUTH=$(az account get-access-token --resource="ce34e7e5-485f-4d76-964f-b3d2b16d1e4f" --query accessToken --output tsv)
# or
export GRAFANA_AUTH="$(curl -X POST \
-d "client_id=${ARM_CLIENT_ID}" \
-d "client_secret=${ARM_CLIENT_SECRET}" \
-d "grant_type=client_credentials" \
-d "resource=ce34e7e5-485f-4d76-964f-b3d2b16d1e4f" \
"https://login.microsoftonline.com/${ARM_TENANT_ID}/oauth2/token" \
| jq -r .access_token)"
我今天刚刚找到解决方案!我们还一直致力于自动化 Azure 托管 Grafana 的部署,但尚未找到以“好方法”获取 API 密钥的方法。这是解决方案,基于官方Azuredocs:
data "http" "api_key" {
url = "https://login.microsoftonline.com/${var.azure_tenant_id}/oauth2/token"
method = "POST"
request_headers = {
Content-Type = "application/x-www-form-urlencoded"
}
request_body = "grant_type=client_credentials&client_id=${var.azure_client_id}&client_secret=${var.azure_client_secret}&resource=ce34e7e5-485f-4d76-964f-b3d2b16d1e4f"
}
然后,您可以通过执行以下操作来访问返回的 API 密钥或更正确地访问访问令牌:
output "managed_grafana_api_key" {
value = jsondecode(data.http.api_key.response_body).access_token
description = "Generated API key for managing Azure managed Grafana"
}
最后,无需任何进一步的操作,您就可以在 Grafana 提供程序中将其用作身份验证字符串:
provider "grafana" {
url = data.azurerm_dashboard_grafana.grafana.endpoint
auth = var.managed_grafana_api_key
}