Skip to content

Commit

Permalink
Add MIMIR_AUTH_TOKEN for self-hosted Mimir (#505)
Browse files Browse the repository at this point in the history
* Add MIMIR_AUTH_TOKEN for self-hosted Mimir

* Fix up syntax error and lint issues
  • Loading branch information
mtweten authored Oct 16, 2024
1 parent b398147 commit 9fdcaa3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
11 changes: 6 additions & 5 deletions docs/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 5 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 5 additions & 4 deletions pkg/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/mimir/client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 9fdcaa3

Please sign in to comment.