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

Method Validate

config/http_config.go:391–464  ·  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

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