From 9fdcaa3024cb69f5c986b06a4ca892e60d1564d8 Mon Sep 17 00:00:00 2001 From: Michael Tweten Date: Wed, 16 Oct 2024 03:53:26 -0500 Subject: [PATCH] Add MIMIR_AUTH_TOKEN for self-hosted Mimir (#505) * Add MIMIR_AUTH_TOKEN for self-hosted Mimir * Fix up syntax error and lint issues --- docs/content/configuration.md | 11 ++++++----- pkg/config/config.go | 8 +++++--- pkg/config/model.go | 9 +++++---- pkg/mimir/client/http_client.go | 7 +++++-- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/docs/content/configuration.md b/docs/content/configuration.md index 7b87360d..9aaf045f 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -147,11 +147,12 @@ docs](https://grafana.com/docs/grafana/latest/http_api/auth/) for more info. ## Grafana Cloud Prometheus To interact with Grafana Cloud Prometheus, you must have these environment variables set: -| Name | Description | Required | -|-------------------|-----------------------------------------------------|----------| -| `MIMIR_ADDRESS` | URL for Grafana Cloud Prometheus instance | true | -| `MIMIR_TENANT_ID` | Tenant ID for your Grafana Cloud Prometheus account | true | -| `MIMIR_API_KEY` | Authentication token/api key | false | +| Name | Description | Required | +|--------------------|-----------------------------------------------------|----------| +| `MIMIR_ADDRESS` | URL for Grafana Cloud Prometheus instance | true | +| `MIMIR_TENANT_ID` | Tenant ID for your Grafana Cloud Prometheus account | true | +| `MIMIR_API_KEY` | Authentication token/api key | false | +| `MIMIR_AUTH_TOKEN` | Authorization Bearer Token | false | Note, this will also work with other Mimir installations, alongside Grafana Cloud Prometheus. diff --git a/pkg/config/config.go b/pkg/config/config.go index ff25f8f0..9d4dceb3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -40,9 +40,10 @@ func override(v *viper.Viper) { "synthetic-monitoring.metrics-id": "GRAFANA_SM_METRICS_ID", "synthetic-monitoring.url": "GRAFANA_SM_URL", - "mimir.address": "MIMIR_ADDRESS", - "mimir.tenant-id": "MIMIR_TENANT_ID", - "mimir.api-key": "MIMIR_API_KEY", + "mimir.address": "MIMIR_ADDRESS", + "mimir.tenant-id": "MIMIR_TENANT_ID", + "mimir.api-key": "MIMIR_API_KEY", + "mimir.auth-token": "MIMIR_AUTH_TOKEN", } // To keep retro compatibility @@ -170,6 +171,7 @@ var acceptableKeys = map[string]string{ "mimir.address": "string", "mimir.tenant-id": "string", "mimir.api-key": "string", + "mimir.auth-token": "string", "synthetic-monitoring.access-token": "string", "synthetic-monitoring.token": "string", "synthetic-monitoring.stack-id": "int", diff --git a/pkg/config/model.go b/pkg/config/model.go index 9fc17077..16df58f4 100644 --- a/pkg/config/model.go +++ b/pkg/config/model.go @@ -9,10 +9,11 @@ type GrafanaConfig struct { } type MimirConfig struct { - Address string `yaml:"address" mapstructure:"address"` - TenantID string `yaml:"tenant-id" mapstructure:"tenant-id"` - APIKey string `yaml:"api-key" mapstructure:"api-key"` - TLS MimirTLSConfig `yaml:"tls" mapstructure:"tls"` + Address string `yaml:"address" mapstructure:"address"` + TenantID string `yaml:"tenant-id" mapstructure:"tenant-id"` + APIKey string `yaml:"api-key" mapstructure:"api-key"` + TLS MimirTLSConfig `yaml:"tls" mapstructure:"tls"` + AuthToken string `yaml:"auth-token" mapstructure:"auth-token"` } type MimirTLSConfig struct { diff --git a/pkg/mimir/client/http_client.go b/pkg/mimir/client/http_client.go index 34d0728d..46ce8939 100644 --- a/pkg/mimir/client/http_client.go +++ b/pkg/mimir/client/http_client.go @@ -90,9 +90,12 @@ func (c *Client) doRequest(method string, url string, body []byte) ([]byte, erro } req.Header.Set("Content-Type", "application/yaml") - if c.config.APIKey != "" { + switch { + case c.config.APIKey != "": req.SetBasicAuth(c.config.TenantID, c.config.APIKey) - } else { + case c.config.AuthToken != "": + req.Header.Set("Authorization", "Bearer "+c.config.AuthToken) + default: req.Header.Set("X-Scope-OrgID", c.config.TenantID) }