Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow authentication with bearer token or JWT token #233

Merged
merged 6 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ type Config struct {
Address string `yaml:"address"`
ID string `yaml:"id"`
TLS tls.ClientConfig
UseLegacyRoutes bool `yaml:"use_legacy_routes"`
UseLegacyRoutes bool `yaml:"use_legacy_routes"`
AuthToken string `yaml:"auth_token"`
}

// CortexClient is used to get and load rules into a cortex ruler
type CortexClient struct {
user string
key string
id string
endpoint *url.URL
Client http.Client
apiPath string
user string
key string
id string
endpoint *url.URL
Client http.Client
apiPath string
authToken string
}

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

return &CortexClient{
user: cfg.User,
key: cfg.Key,
id: cfg.ID,
endpoint: endpoint,
Client: client,
apiPath: path,
user: cfg.User,
key: cfg.Key,
id: cfg.ID,
endpoint: endpoint,
Client: client,
apiPath: path,
authToken: cfg.AuthToken,
}, nil
}

Expand Down Expand Up @@ -120,6 +123,10 @@ func (r *CortexClient) doRequest(path, method string, payload []byte) (*http.Res
req.SetBasicAuth(r.id, r.key)
}

if r.authToken != "" {
req.Header.Add("Authorization", r.authToken)
}

gouthamve marked this conversation as resolved.
Show resolved Hide resolved
req.Header.Add("X-Scope-OrgID", r.id)

log.WithFields(log.Fields{
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type RuleCommand struct {
// Register rule related commands and flags with the kingpin application
func (r *RuleCommand) Register(app *kingpin.Application) {
rulesCmd := app.Command("rules", "View & edit rules stored in cortex.").PreAction(r.setup)
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)
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)
rulesCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
rulesCmd.Flag("backend", "Backend type to interact with: <cortex|loki>").Default("cortex").EnumVar(&r.Backend, backends...)
Expand Down