Skip to content

Commit f2fb4f8

Browse files
add the ability to specify the basic auth user (#187)
* add the ability to specify the basic auth user * also add user to alertmanager command * Update README.md Co-authored-by: gotjosh <[email protected]> * Update pkg/alerting/runner.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/alerts.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/alerts.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/alerts.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/rules.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/rules.go Co-authored-by: gotjosh <[email protected]> * Update pkg/commands/alerts.go Co-authored-by: gotjosh <[email protected]> * add changelog entry Co-authored-by: gotjosh <[email protected]>
1 parent 6acf363 commit f2fb4f8

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
44

55
## unreleased/master
66

7+
* [ENHANCEMENT] Added the ability to set an explicit user when Cortex is behind basic auth. #187
8+
79
## v0.10.1
810

911
* [ENHANCEMENT] `cortextool analyse prometheus` now records cardinality by metric and job labels. #178

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Config commands interact with the Cortex api and read/create/update/delete user
3939
| Env Variables | Flag | Description |
4040
| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------- |
4141
| CORTEX_ADDRESS | `address` | Address of the API of the desired Cortex cluster. |
42-
| CORTEX_API_KEY | `key` | In cases where the Cortex API is set behind a basic auth gateway, an key can be set as a basic auth password. |
42+
| CORTEX_API_USER | `user` | In cases where the Cortex API is set behind a basic auth gateway, a user can be set as a basic auth user. If empty and CORTEX_API_KEY is set, CORTEX_TENANT_ID will be used instead. |
43+
| CORTEX_API_KEY | `key` | In cases where the Cortex API is set behind a basic auth gateway, a key can be set as a basic auth password. |
4344
| CORTEX_TENANT_ID | `id` | The tenant ID of the Cortex instance to interact with. |
4445

4546
#### Alertmanager

pkg/alerting/runner.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type RunnerConfig struct {
4646
AlertmanagerID string
4747
RulerURL string
4848
RulerID string
49+
User string
4950
Key string
5051

5152
RulesConfigFile string
@@ -62,6 +63,7 @@ func (cfg *RunnerConfig) RegisterFlags(f *flag.FlagSet) {
6263
f.StringVar(&cfg.RulerURL, "configs.ruler-url", "", "The URL under the Ruler is reachable")
6364
f.StringVar(&cfg.RulerID, "configs.ruler-id", "", "The user ID of the Ruler tenant")
6465

66+
f.StringVar(&cfg.User, "configs.user", "", "The API user to use for syncing configuration. The same user is used for both the alertmanager and ruler. If empty, configs.ruler-id is used instead.")
6567
f.StringVar(&cfg.Key, "configs.key", "", "The API key to use for syncing configuration. The same key is used for both the alertmanager and ruler.")
6668
f.DurationVar(&cfg.ConfigSyncInterval, "configs.sync-interval", 30*time.Minute, "How often should we sync the configuration with the ruler and alertmanager")
6769
}
@@ -88,6 +90,7 @@ func NewRunner(cfg RunnerConfig, logger log.Logger) (*Runner, error) {
8890
amClient, err := client.New(client.Config{
8991
Address: cfg.AlertmanagerURL,
9092
ID: cfg.AlertmanagerID,
93+
User: cfg.User,
9194
Key: cfg.Key,
9295
})
9396
if err != nil {
@@ -98,6 +101,7 @@ func NewRunner(cfg RunnerConfig, logger log.Logger) (*Runner, error) {
98101
rulerClient, err := client.New(client.Config{
99102
Address: cfg.RulerURL,
100103
ID: cfg.RulerID,
104+
User: cfg.User,
101105
Key: cfg.Key,
102106
})
103107
if err != nil {

pkg/client/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828

2929
// Config is used to configure a Ruler Client
3030
type Config struct {
31+
User string `yaml:"user"`
3132
Key string `yaml:"key"`
3233
Address string `yaml:"address"`
3334
ID string `yaml:"id"`
@@ -37,6 +38,7 @@ type Config struct {
3738

3839
// CortexClient is used to get and load rules into a cortex ruler
3940
type CortexClient struct {
41+
user string
4042
key string
4143
id string
4244
endpoint *url.URL
@@ -80,6 +82,7 @@ func New(cfg Config) (*CortexClient, error) {
8082
}
8183

8284
return &CortexClient{
85+
user: cfg.User,
8386
key: cfg.Key,
8487
id: cfg.ID,
8588
endpoint: endpoint,
@@ -108,7 +111,9 @@ func (r *CortexClient) doRequest(path, method string, payload []byte) (*http.Res
108111
return nil, err
109112
}
110113

111-
if r.key != "" {
114+
if r.user != "" {
115+
req.SetBasicAuth(r.user, r.key)
116+
} else if r.key != "" {
112117
req.SetBasicAuth(r.id, r.key)
113118
}
114119

pkg/commands/alerts.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (a *AlertmanagerCommand) Register(app *kingpin.Application) {
6464
alertCmd := app.Command("alertmanager", "View & edit alertmanager configs stored in cortex.").PreAction(a.setup)
6565
alertCmd.Flag("address", "Address of the cortex cluster, alternatively set CORTEX_ADDRESS.").Envar("CORTEX_ADDRESS").Required().StringVar(&a.ClientConfig.Address)
6666
alertCmd.Flag("id", "Cortex tenant id, alternatively set CORTEX_TENANT_ID.").Envar("CORTEX_TENANT_ID").Required().StringVar(&a.ClientConfig.ID)
67-
alertCmd.Flag("key", "Api key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&a.ClientConfig.Key)
67+
alertCmd.Flag("user", "API user to use when contacting cortex, alternatively set CORTEX_API_USER. If empty, CORTEX_TENANT_ID will be used instead.").Default("").Envar("CORTEX_API_USER").StringVar(&a.ClientConfig.User)
68+
alertCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&a.ClientConfig.Key)
6869
alertCmd.Flag("tls-ca-path", "TLS CA certificate to verify cortex API as part of mTLS, alternatively set CORTEX_TLS_CA_PATH.").Default("").Envar("CORTEX_TLS_CA_PATH").StringVar(&a.ClientConfig.TLS.CAPath)
6970
alertCmd.Flag("tls-cert-path", "TLS client certificate to authenticate with cortex API as part of mTLS, alternatively set CORTEX_TLS_CERT_PATH.").Default("").Envar("CORTEX_TLS_CERT_PATH").StringVar(&a.ClientConfig.TLS.CertPath)
7071
alertCmd.Flag("tls-key-path", "TLS client certificate private key to authenticate with cortex API as part of mTLS, alternatively set CORTEX_TLS_KEY_PATH.").Default("").Envar("CORTEX_TLS_KEY_PATH").StringVar(&a.ClientConfig.TLS.KeyPath)
@@ -141,7 +142,8 @@ func (a *AlertCommand) Register(app *kingpin.Application) {
141142
alertCmd := app.Command("alerts", "View active alerts in alertmanager.").PreAction(a.setup)
142143
alertCmd.Flag("address", "Address of the cortex cluster, alternatively set CORTEX_ADDRESS.").Envar("CORTEX_ADDRESS").Required().StringVar(&a.ClientConfig.Address)
143144
alertCmd.Flag("id", "Cortex tenant id, alternatively set CORTEX_TENANT_ID.").Envar("CORTEX_TENANT_ID").Required().StringVar(&a.ClientConfig.ID)
144-
alertCmd.Flag("key", "Api key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&a.ClientConfig.Key)
145+
alertCmd.Flag("user", "API user to use when contacting cortex, alternatively set CORTEX_API_USER. If empty, CORTEX_TENANT_ID will be used instead.").Default("").Envar("CORTEX_API_USER").StringVar(&a.ClientConfig.User)
146+
alertCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&a.ClientConfig.Key)
145147

146148
verifyAlertsCmd := alertCmd.Command("verify", "Verifies alerts in an alertmanager cluster are deduplicated; useful for verifying correct configuration when transferring from Prometheus to Cortex alert evaluation.").Action(a.verifyConfig)
147149
verifyAlertsCmd.Flag("ignore-alerts", "A comma separated list of Alert names to ignore in deduplication checks.").StringVar(&a.IgnoreString)

pkg/commands/rules.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ type RuleCommand struct {
8787
// Register rule related commands and flags with the kingpin application
8888
func (r *RuleCommand) Register(app *kingpin.Application) {
8989
rulesCmd := app.Command("rules", "View & edit rules stored in cortex.").PreAction(r.setup)
90-
rulesCmd.Flag("key", "Api key to use when contacting cortex, alternatively set $CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
90+
rulesCmd.Flag("user", "API user to use when contacting cortex, alternatively set CORTEX_API_USER. If empty, CORTEX_TENANT_ID will be used instead.").Default("").Envar("CORTEX_API_USER").StringVar(&r.ClientConfig.User)
91+
rulesCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
9192
rulesCmd.Flag("backend", "Backend type to interact with: <cortex|loki>").Default("cortex").EnumVar(&r.Backend, backends...)
9293

9394
// Register rule commands

0 commit comments

Comments
 (0)