MCPcopy Create free account
hub / github.com/prometheus/common / Validate

Method Validate

config/http_config.go:389–462  ·  view source on GitHub ↗

Validate validates the HTTPClientConfig to check only one of BearerToken, BasicAuth and BearerTokenFile is configured. It also validates that ProxyURL is set if ProxyConnectHeader is set.

()

Source from the content-addressed store, hash-verified

387// BasicAuth and BearerTokenFile is configured. It also validates that ProxyURL
388// is set if ProxyConnectHeader is set.
389func (c *HTTPClientConfig) Validate() error {
390 // Backwards compatibility with the bearer_token field.
391 if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
392 return errors.New("at most one of bearer_token & bearer_token_file must be configured")
393 }
394 if (c.BasicAuth != nil || c.OAuth2 != nil) && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
395 return errors.New("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
396 }
397 if c.BasicAuth != nil && nonZeroCount(c.BasicAuth.Username != "", c.BasicAuth.UsernameFile != "", c.BasicAuth.UsernameRef != "") > 1 {
398 return errors.New("at most one of basic_auth username, username_file & username_ref must be configured")
399 }
400 if c.BasicAuth != nil && nonZeroCount(string(c.BasicAuth.Password) != "", c.BasicAuth.PasswordFile != "", c.BasicAuth.PasswordRef != "") > 1 {
401 return errors.New("at most one of basic_auth password, password_file & password_ref must be configured")
402 }
403 if c.Authorization != nil {
404 if len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0 {
405 return errors.New("authorization is not compatible with bearer_token & bearer_token_file")
406 }
407 if nonZeroCount(string(c.Authorization.Credentials) != "", c.Authorization.CredentialsFile != "", c.Authorization.CredentialsRef != "") > 1 {
408 return errors.New("at most one of authorization credentials & credentials_file must be configured")
409 }
410 c.Authorization.Type = strings.TrimSpace(c.Authorization.Type)
411 if len(c.Authorization.Type) == 0 {
412 c.Authorization.Type = "Bearer"
413 }
414 if strings.ToLower(c.Authorization.Type) == "basic" {
415 return errors.New(`authorization type cannot be set to "basic", use "basic_auth" instead`)
416 }
417 if c.BasicAuth != nil || c.OAuth2 != nil {
418 return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
419 }
420 } else {
421 if len(c.BearerToken) > 0 {
422 c.Authorization = &Authorization{Credentials: c.BearerToken}
423 c.Authorization.Type = "Bearer"
424 c.BearerToken = ""
425 }
426 if len(c.BearerTokenFile) > 0 {
427 c.Authorization = &Authorization{CredentialsFile: c.BearerTokenFile}
428 c.Authorization.Type = "Bearer"
429 c.BearerTokenFile = ""
430 }
431 }
432 if c.OAuth2 != nil {
433 if c.BasicAuth != nil {
434 return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
435 }
436 if len(c.OAuth2.ClientID) == 0 {
437 return errors.New("oauth2 client_id must be configured")
438 }
439 if len(c.OAuth2.TokenURL) == 0 {
440 return errors.New("oauth2 token_url must be configured")
441 }
442 if c.OAuth2.GrantType == grantTypeJWTBearer {
443 if nonZeroCount(len(c.OAuth2.ClientCertificateKey) > 0, len(c.OAuth2.ClientCertificateKeyFile) > 0, len(c.OAuth2.ClientCertificateKeyRef) > 0) > 1 {
444 return errors.New("at most one of oauth2 client_certificate_key, client_certificate_key_file & client_certificate_key_ref must be configured using grant-type=urn:ietf:params:oauth:grant-type:jwt-bearer")
445 }
446 if c.OAuth2.SignatureAlgorithm != "" && !slices.Contains(validSignatureAlgorithm, c.OAuth2.SignatureAlgorithm) {

Callers 2

UnmarshalYAMLMethod · 0.95
UnmarshalJSONMethod · 0.95

Calls 2

nonZeroCountFunction · 0.85
ValidateMethod · 0.45

Tested by

no test coverage detected