Skip to content

Commit feaf58b

Browse files
Allow authentication with bearer token or JWT token (#233)
* Enable auth token support for cortex-gateway * Return error when both basic auth and auth token configured * Update pkg/client/client.go Co-authored-by: Goutham Veeramachaneni <[email protected]> * Fix log message * Fix auth header Co-authored-by: Goutham Veeramachaneni <[email protected]>
1 parent 3048e55 commit feaf58b

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

pkg/client/client.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ type Config struct {
3333
Address string `yaml:"address"`
3434
ID string `yaml:"id"`
3535
TLS tls.ClientConfig
36-
UseLegacyRoutes bool `yaml:"use_legacy_routes"`
36+
UseLegacyRoutes bool `yaml:"use_legacy_routes"`
37+
AuthToken string `yaml:"auth_token"`
3738
}
3839

3940
// CortexClient is used to get and load rules into a cortex ruler
4041
type CortexClient struct {
41-
user string
42-
key string
43-
id string
44-
endpoint *url.URL
45-
Client http.Client
46-
apiPath string
42+
user string
43+
key string
44+
id string
45+
endpoint *url.URL
46+
Client http.Client
47+
apiPath string
48+
authToken string
4749
}
4850

4951
// New returns a new Client
@@ -85,12 +87,13 @@ func New(cfg Config) (*CortexClient, error) {
8587
}
8688

8789
return &CortexClient{
88-
user: cfg.User,
89-
key: cfg.Key,
90-
id: cfg.ID,
91-
endpoint: endpoint,
92-
Client: client,
93-
apiPath: path,
90+
user: cfg.User,
91+
key: cfg.Key,
92+
id: cfg.ID,
93+
endpoint: endpoint,
94+
Client: client,
95+
apiPath: path,
96+
authToken: cfg.AuthToken,
9497
}, nil
9598
}
9699

@@ -114,12 +117,26 @@ func (r *CortexClient) doRequest(path, method string, payload []byte) (*http.Res
114117
return nil, err
115118
}
116119

120+
if (r.user != "" || r.key != "") && r.authToken != "" {
121+
err := errors.New("atmost one of basic auth or auth token should be configured")
122+
log.WithFields(log.Fields{
123+
"url": req.URL.String(),
124+
"method": req.Method,
125+
"error": err,
126+
}).Errorln("error during request to cortex api")
127+
return nil, err
128+
}
129+
117130
if r.user != "" {
118131
req.SetBasicAuth(r.user, r.key)
119132
} else if r.key != "" {
120133
req.SetBasicAuth(r.id, r.key)
121134
}
122135

136+
if r.authToken != "" {
137+
req.Header.Add("Authorization", "Bearer "+r.authToken)
138+
}
139+
123140
req.Header.Add("X-Scope-OrgID", r.id)
124141

125142
log.WithFields(log.Fields{

pkg/commands/rules.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type RuleCommand struct {
9090
// Register rule related commands and flags with the kingpin application
9191
func (r *RuleCommand) Register(app *kingpin.Application) {
9292
rulesCmd := app.Command("rules", "View & edit rules stored in cortex.").PreAction(r.setup)
93+
rulesCmd.Flag("authToken", "Authentication token for bearer token or JWT auth, alternatively set CORTEX_AUTH_TOKEN.").Default("").Envar("CORTEX_AUTH_TOKEN").StringVar(&r.ClientConfig.AuthToken)
9394
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)
9495
rulesCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
9596
rulesCmd.Flag("backend", "Backend type to interact with: <cortex|loki>").Default("cortex").EnumVar(&r.Backend, backends...)

0 commit comments

Comments
 (0)