2025-03-26 18:07:45 +01:00
|
|
|
package vmalertutil
|
2021-09-15 01:53:31 +03:00
|
|
|
|
|
|
|
|
import (
|
2022-03-10 13:09:12 +02:00
|
|
|
"strings"
|
|
|
|
|
|
2021-09-15 01:53:31 +03:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-10 13:09:12 +02:00
|
|
|
// AuthConfigOptions options which helps build promauth.Config
|
|
|
|
|
type AuthConfigOptions func(config *promauth.HTTPClientConfig)
|
|
|
|
|
|
2021-09-15 01:53:31 +03:00
|
|
|
// AuthConfig returns promauth.Config based on the given params
|
2022-03-10 13:09:12 +02:00
|
|
|
func AuthConfig(filterOptions ...AuthConfigOptions) (*promauth.Config, error) {
|
|
|
|
|
authCfg := &promauth.HTTPClientConfig{}
|
|
|
|
|
for _, option := range filterOptions {
|
|
|
|
|
option(authCfg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authCfg.NewConfig(".")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithBasicAuth returns AuthConfigOptions and initialized promauth.BasicAuthConfig based on given params
|
2026-05-12 13:50:44 -07:00
|
|
|
func WithBasicAuth(username, usernameFile, password, passwordFile string) AuthConfigOptions {
|
2022-03-10 13:09:12 +02:00
|
|
|
return func(config *promauth.HTTPClientConfig) {
|
2026-05-12 13:50:44 -07:00
|
|
|
if username != "" || usernameFile != "" || password != "" || passwordFile != "" {
|
2022-03-10 13:09:12 +02:00
|
|
|
config.BasicAuth = &promauth.BasicAuthConfig{
|
|
|
|
|
Username: username,
|
2026-05-12 13:50:44 -07:00
|
|
|
UsernameFile: usernameFile,
|
2022-03-10 13:09:12 +02:00
|
|
|
Password: promauth.NewSecret(password),
|
|
|
|
|
PasswordFile: passwordFile,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithBearer returns AuthConfigOptions and set BearerToken or BearerTokenFile based on given params
|
|
|
|
|
func WithBearer(token, tokenFile string) AuthConfigOptions {
|
|
|
|
|
return func(config *promauth.HTTPClientConfig) {
|
|
|
|
|
if token != "" {
|
|
|
|
|
config.BearerToken = promauth.NewSecret(token)
|
|
|
|
|
}
|
|
|
|
|
if tokenFile != "" {
|
|
|
|
|
config.BearerTokenFile = tokenFile
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithOAuth returns AuthConfigOptions and set OAuth params based on given params
|
2023-12-20 21:35:16 +02:00
|
|
|
func WithOAuth(clientID, clientSecret, clientSecretFile, tokenURL, scopes string, endpointParams map[string]string) AuthConfigOptions {
|
2022-03-10 13:09:12 +02:00
|
|
|
return func(config *promauth.HTTPClientConfig) {
|
|
|
|
|
if clientSecretFile != "" || clientSecret != "" {
|
|
|
|
|
config.OAuth2 = &promauth.OAuth2Config{
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
ClientSecret: promauth.NewSecret(clientSecret),
|
|
|
|
|
ClientSecretFile: clientSecretFile,
|
2023-12-20 21:35:16 +02:00
|
|
|
EndpointParams: endpointParams,
|
2022-03-10 13:09:12 +02:00
|
|
|
TokenURL: tokenURL,
|
|
|
|
|
Scopes: strings.Split(scopes, ";"),
|
|
|
|
|
}
|
2021-09-15 01:53:31 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-21 13:57:53 +02:00
|
|
|
|
2022-07-21 20:39:52 +03:00
|
|
|
// WithHeaders returns AuthConfigOptions and set Headers based on the given params
|
2022-07-21 13:57:53 +02:00
|
|
|
func WithHeaders(headers string) AuthConfigOptions {
|
|
|
|
|
return func(config *promauth.HTTPClientConfig) {
|
|
|
|
|
if headers != "" {
|
|
|
|
|
config.Headers = strings.Split(headers, "^^")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|